static inline loopSensorMode() { while(1) { if(twiDataInReceiveBuffer()) { uint8_t usiRx = twiReceiveByte(); if(TWI_GET_CAPACITANCE == usiRx) { twiTransmitByte(currCapacitance >> 8); twiTransmitByte(currCapacitance &0x00FF); currCapacitance = getCapacitance(); } else if(TWI_SET_ADDRESS == usiRx) { uint8_t newAddress = twiReceiveByte(); if(twiIsValidAddress(newAddress)) { eeprom_write_byte((uint8_t*)0x01, newAddress); } } else if(TWI_GET_ADDRESS == usiRx) { uint8_t newAddress = eeprom_read_byte((uint8_t*) 0x01); twiTransmitByte(newAddress); } else if(TWI_MEASURE_LIGHT == usiRx) { if(!lightMeasurementInProgress()) { getLight(); } } else if(TWI_GET_LIGHT == usiRx) { GIMSK &= ~_BV(PCIE0);//disable pin change interrupts TCCR1B = 0; //stop timer twiTransmitByte(lightCounter >> 8); twiTransmitByte(lightCounter & 0x00FF); GIMSK |= _BV(PCIE0); TCCR1B = _BV(CS10) | _BV(CS11); //start timer1 with prescaler clk/64 } else if(TWI_GET_TEMPERATURE == usiRx) {
/* * Load ALL range data into output FIFO */ void mod_range_finder_us_getAllRange() { twiTransmitByte( MOD_RANGE_FINDER_US_ID ); twiTransmitByte( mrfus_right ); twiTransmitByte( mrfus_center ); twiTransmitByte( mrfus_left ); }
/* * Send ALL IR Range Data */ void mrfir_SendAllIR() { // static uint8_t count = 0; // mrf_ir_range[1] = ++count; twiTransmitByte( MOD_RANGE_FINDER_IR_ID ); twiTransmitByte( mrf_ir_range[0] ); twiTransmitByte( mrf_ir_range[1] ); twiTransmitByte( mrf_ir_range[2] ); twiTransmitByte( mrf_ir_range[3] ); }
/* * Send IR Range Data * E1 ID 01 N * N: 0-3 */ void mrfir_SendIR() { uint8_t sensor; sensor = getMsgData(3); if( sensor > 3) sensor = 0; twiTransmitByte( MOD_RANGE_FINDER_IR_ID ); twiTransmitByte( mrf_ir_range[sensor] ); }
/* * Load range data into output FIFO * msgData[3] = N */ void mod_range_finder_us_getRange() { twiTransmitByte( MOD_RANGE_FINDER_US_ID ); switch( getMsgData(3) ) { case MRFUS_SEL_RIGHT: twiTransmitByte( mrfus_right ); break; case MRFUS_SEL_CENTER: twiTransmitByte( mrfus_center ); break; case MRFUS_SEL_LEFT: twiTransmitByte( mrfus_left ); break; } }
/* Set minimum range to trigger LED. */ void mod_range_finder_us_setMinimumRange() { twiTransmitByte( MOD_RANGE_FINDER_US_ID ); switch( getMsgData(3) ) { case MRFUS_SEL_RIGHT: mrfus_right_min = getMsgData(4); break; case MRFUS_SEL_CENTER: mrfus_center_min = getMsgData(4); break; case MRFUS_SEL_LEFT: mrfus_left_min = getMsgData(4); break; } }
int main(void) { uint8_t count; count = 0; twiSlaveInit( SLAVE_ADRS ); // Initialize TWI hardware for Slave operation. sei(); // Enable interrupts. twiSlaveEnable(); // Enable the TWI interface to receive data. while(1) { if( !twiDataInTransmitBuffer() ) { twiTransmitByte( count ); // stuff count into TxBuf[] ++count; // inc for next value. } } }