void loop(){ //one frame static uint8_t escape_state = 0; uint16_t receive_state = 1; //primitive somewhat messy state machine of the uart interface do{ //Always empty the receive buffer since there are _delay_xxs in the following code and thus this might not run all that often. receive_state = uart_getc(); char c = receive_state&0xFF; receive_state &= 0xFF00; //escape code format: // \\ - backslash // \n - newline // \[x] - x //eats [n] commands: 's' (0x73) led value sets led number [led] to [value] // 'b' (0x62) buffer buffer buffer buffer sets the whole frame buffer // 'a' (0x61) meter value sets analog meter number [meter] to [value] // 'r' (0x72) read the frame buffer // 'd' (0x64) display digit digit digit digit sets the 7-segment display (CAUTION: "display" currently is ignored) //this device will utter a "'c' (0x63) num state" when switch [num] changes state to [state] //commands are terminated by \n if(!receive_state){ if(!escape_state){ if(c == '\\'){ receive_state |= 0x02; escape_state = 1; }else if(c == '\n'){ receive_state |= 0x02; state = 0; } }else{ receive_state = 0; escape_state = 0; switch(c){ case '\\': break; case 'n': c = '\n'; break; } } } if(!receive_state){ switch(state){ case 0: //Do not assume anything about the variables used //command char switch(c){ #ifdef HAS_LED_SUPPORT case 's': state = 2; break; case 'b': nbuf = 0; state = 4; break; #endif//HAS_LED_SUPPORT #ifdef HAS_PWM_SUPPORT case 'a': state = 5; nbuf = 0; break; #endif//HAS_PWM_SUPPORT #ifdef HAS_NOISE_MAKER_SUPPORT case 'x': //DDRF |= 0x01; //PORTF |= 0x01; break; case 'X': //DDRF &= 0xFE; //PORTF &= 0xFE; break; #endif//HAS_NOISE_MAKER_SUPPORT #ifdef HAS_LED_SUPPORT case 'r': uart_putc('r'); for(uint8_t i=0; i<sizeof(frameBuffer); i++){ uart_puthex(frameBuffer[i]); } uart_putc('\n'); break; #endif//HAS_LED_SUPPORT #ifdef HAS_7SEG_SUPPORT case 'd': nbuf = 0; bpos = 0; state = 7; #endif//HAS_7SEG_SUPPORT } break; #ifdef HAS_LED_SUPPORT case 2: nbuf=c; state = 3; break; case 3: setLED(nbuf, c); state = 0; break; case 4: secondFrameBuffer[(uint8_t) nbuf] = c; nbuf++; if(nbuf == 7){ swapBuffers(); state = 0; } break; #endif//HAS_LED_SUPPORT #ifdef HAS_PWM_SUPPORT case 5: if(c > PWM_COUNT) c = 0; nbuf = c; if(nbuf >= PWM_COUNT) nbuf = 0; state = 6; break; case 6: pwm_val[(uint8_t) nbuf] = c; uart_puts_p(PSTR("ACK\n")); state = 0; break; #endif//HAS_PWM_SUPPORT #ifdef HAS_7SEG_SUPPORT case 7: nbuf = c; state = 8; break; case 8: l7seg_buf[(uint8_t)bpos] = c; bpos++; if(bpos == 4){ state = 0; } break; #endif//HAS_7SEG_SUPPORT } } }while(!receive_state); led_loop(); r0ketbeam_loop(); l7seg_loop(); input_loop(); pwm_loop(); config_loop(); _delay_ms(1); }
int main (int argc, char *argv[]) { if (sdl_init () != 0) return 1; if (textures_init ()) return 1; if (audio_init ()) { fprintf (stderr, "Error loading SFX: %s\n", SDL_GetError()); return 1; } if (music_init ()) { fprintf (stderr, "Error loading SFX: %s\n", SDL_GetError()); return 1; } if (config_load ()) { fprintf (stderr, "Error loading config\n"); } hiscore_init (); //gamestate = GAME_DEMO; running = 1; while (running) { sdl_read_input (); if (!paused) { SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); if (gamestate != GAME_OVER) SDL_RenderClear (renderer); switch (gamestate) { case GAME_DEMO: draw_test (); break; case GAME_RUNNING: game_loop (); break; case GAME_AMODE: default: amode_loop (); break; case GAME_OVER: gameover_loop (); break; case GAME_HSENTRY: hsentry_loop (); break; case GAME_CONFIG: case GAME_CONFIG_INPUT: config_loop (); break; case GAME_SELECT_RECORD: playback_loop (); break; } } SDL_RenderPresent (renderer); } hiscore_save (); sdl_close (); return 0; }