/** * State machine for menu and display */ void run_states(uint8_t state, s_time* time) { // State machine zum Ändern der Uhrzeit switch (0xf0 & state) { case STATE_MENU_SECOND: set_all_leds(1); if (b_pressed()) { add_second(time, 0); } display_seconds(*time); break; case STATE_MENU_MINUTE: set_all_leds(1); if (b_pressed()) { add_minute(time, 0); } display_minutes(*time); break; case STATE_MENU_HOUR: set_all_leds(1); if (b_pressed()) { add_hour(time); } display_hours(*time); break; case STATE_NONE: set_all_leds(0); #ifdef WITH_ENLIGHT if (enlight > 0) { #endif display_seconds(*time); display_minutes(*time); display_hours(*time); #ifdef WITH_ENLIGHT enlight--; } #endif add_second(time, 1); break; } }
/** * main function */ int main(int argc, char **argv) { // initialisiere alle Werte state = STATE_NONE; enlight = 10; time.hour = 1; time.minute = 0; time.second = 0; // Ausgänge für Stundenzeiger DDR_HOUR |= PORTMASK_HOUR; // Ausgänge für Minutenzeiger DDR_MINUTE |= PORTMASK_MINUTE; // Ausgänge für Sekundenzeiger DDR_SECOND |= PORTMASK_SECOND; // Eingänge für Menütaster, aktiviere Pullups DDRD &= ~(1 << PORTD2 | 1 << PORTD3); PORTD |= (1 << PORTD2 | 1 << PORTD3); // timer initialisieren init_timer(); while (1) { // State machine zum Ändern der Uhrzeit run_states(state, &time); wait_a_second(); // Prüfe ob Menuetaste gedrückt wurde und ändere Status if (a_pressed()) { state += 0x10; if ((state & 0xf0) == STATE_MENU_END) { state = STATE_NONE; } } #ifdef WITH_ENLIGHT // schalte die Uhrzeitanzeige an. if (b_pressed()) { enlight = 10; } #endif } }
/* Returns 1 if any of the 8 GameBoy keys are being held down, * 0 otherwise */ int key_pressed() { return down_pressed() || up_pressed() || left_pressed() || right_pressed() || a_pressed() || b_pressed() || start_pressed() || select_pressed(); }