void loop(){ /* * Check battery status */ do_battery_diagnostics(); /* * Check for danger status (draw or re-charge issues) */ if(has_recharging_error() || is_battery_in_dangerous_state()){ motors_shutdown(); if(is_battery_in_dangerous_state()) set_overload_timeout(); return; } /* * Does the battery need to be recharged? */ if(is_battery_recharing()) return; /* * Now that we pass all those checks, send off control */ switch(control_mode){ case RC: handle_rc(); break; case SERIAL: handle_serial(); break; case I2C: handle_i2c(); break; DEFAULT: motors_shutdown(); } //if(DEBUGGING_ENABLED) debugger(); //delay(loop_delay); }
int main( void ) { _delay_ms(100); // set clock speed CLKPR = _BV( CLKPCE ); // enable clock prescale change CLKPR = 0; // full speed (8MHz); #if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || \ defined(__AVR_ATtiny85__) // set up periodic timer for state machine ('script_tick') TCCR0B = _BV( CS02 ) | _BV(CS00); // start timer, prescale CLK/1024 TIFR = _BV( TOV0 ); // clear interrupt flag TIMSK = _BV( TOIE0 ); // enable overflow interrupt // set up output pins PORTB = INPI2C_MASK; // turn on pullups DDRB = LED_MASK; // set LED port pins to output #elif defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || \ defined(__AVR_ATtiny84__) // set up periodic timer for state machine ('script_tick') //TCCR0B = _BV( CS02 ) | _BV(CS00); // start timer, prescale CLK/1024 //TIFR0 = _BV( TOV0 ); // clear interrupt flag //TIMSK0 = _BV( TOIE0 ); // enable overflow interrupt // set up output pins PORTA = INPI2C_MASK; // turn on pullups DDRA = 0xFF; //LEDA_MASK; // set LED port pins to output DDRB = 0xFF; //LEDB_MASK; // set LED port pins to output #endif fanfare( 3, 300 ); #if 0 // test for ATtiny44/84 MaxM fanfare( 3, 300 ); IRsend_enableIROut(); while( 1 ) { _delay_ms(10); IRsend_iroff(); _delay_ms(10); IRsend_iron(); } /* uint8_t f = OCR1B; while( 1 ) { _delay_ms(10); f++; if( f== OCR1A ) f=0; // OCR1A == period OCR1B = f; // OCR1B == duty cycle (0-OCR1A) } */ #endif #if 0 // test timing of script_tick _delay_ms(2000); sei(); _delay_ms(500); // this should cause script_tick to equal 15 uint8_t j = script_tick; for( int i=0; i<j; i++ ) { led_flash(); _delay_ms(300); } #endif ////// begin normal startup uint8_t boot_mode = eeprom_read_byte( &ee_boot_mode ); uint8_t boot_script_id = eeprom_read_byte( &ee_boot_script_id ); uint8_t boot_reps = eeprom_read_byte( &ee_boot_reps ); //uint8_t boot_fadespeed = eeprom_read_byte( &ee_boot_fadespeed ); uint8_t boot_timeadj = eeprom_read_byte( &ee_boot_timeadj ); // initialize i2c interface uint8_t i2c_addr = eeprom_read_byte( &ee_i2c_addr ); if( i2c_addr==0 || i2c_addr>0x7f) i2c_addr = I2C_ADDR; // just in case i2c_addrs[0] = i2c_addr; for( uint8_t i = 1; i<slaveAddressesCount; i++ ) { i2c_addrs[i] = i2c_addrs[0] + i; } usiTwiSlaveInit( i2c_addrs ); timeadj = boot_timeadj; if( boot_mode == BOOT_PLAY_SCRIPT ) { play_script( boot_script_id, boot_reps, 0 ); } sei(); // enable interrupts #if 0 basic_tests(); #endif RB_Init(); // This loop runs forever. // If the TWI Transceiver is busy the execution will just // continue doing other operations. for(;;) { handle_i2c(); handle_inputs(); handle_script(); handle_ir_queue(); } } // end