int main (void) { board_init(); init_sensor_interrupts(); init_joystick(); init_timer0(); init_serial_stdio(9600,0); init_var(); struct Motor* A = malloc(sizeof(Motor)); struct Motor* B = malloc(sizeof(Motor)); A->pos = 0; B->pos = 0; A->ID = 0; B->ID = 1; sei(); printf_P(PSTR("WOOP------------------------------------------------------------------------------\n")); clear_terminal(); //////////////////////////////////////////////////////////////////////////////////////////////////////////////// // uSEFUL cODE // //////////////////////////////////////////////////////////////////////////////////////////////////////////////// //To test the sensors are working //banana(); //Seminar demo code //DEMO(A,B); //To test the motors are working //Azathoth(A,B); //To test beta pan function //qwert(A,B); //Tuesday test code // Pin C0 acts as sensor input // Pins D0-D7 run the motors // Pin C1 is the Emitter control // Pin C2 is the Magnet Control */ Nyarlathotep(A,B); return 0; }
/* * main -- Main program. */ int main(void) { uint8_t chars_into_escape_sequence = 0; int8_t moveStatus = 0; char c; /* Initialise our main clock */ init_timer(); /* Initialise serial I/O */ init_serial_stdio(19200, 0); /* Make the display_row() function be called every 2ms. ** (This function returns a timer number, but we ignore ** this since we'll never do anything with it.) */ execute_function_periodically(2, display_row); /* Register the time_increment() function to be called every 500ms. ** This function just sets a variable (timePassedFlag). */ mainTimerNum = execute_function_periodically(500, time_increment); //4209435 /* setup AVR to handle sounds*/ init_sound(); /* ** Turn on interrupts (needed for timer and serial input/output to work) */ sei(); /* ** Display splash screen */ splash_screen(); show_instruction(NEWGAME); /* ** Perform necessary initialisations for a new game. */ new_game(); /* ** Event loop - wait for a certain amount of time to pass or wait ** for a character to arrive from standard input. The time_passed_flag ** is set within the function time_increment() below - which is setup ** to be called periodically. */ for(;;) { if(timePassedFlag) { moveStatus = move_snake(); timePassedFlag = 0; } else if(input_available()) { /* Read the input from our terminal and handle it */ c = fgetc(stdin); if(chars_into_escape_sequence == 0 && c == '\x1b') { /* ** Received ESCAPE character - we're one character into ** an escape sequence */ chars_into_escape_sequence = 1; } else if(chars_into_escape_sequence == 1 && c == '[') { /* ** We're now two characters into an escape sequence */ chars_into_escape_sequence = 2; } else if (chars_into_escape_sequence == 2) { /* We're two characters into an escape sequence and ** have received another - see if it is as expected. */ if (c == 'C') { /* Cursor right key pressed - Set next direction to ** be moved to RIGHT */ set_snake_dirn(RIGHT); } if (c == 'D') { /* Cursor left key pressed - Set next direction to ** be moved to LEFT */ set_snake_dirn(LEFT); } if (c == 'A') { /* Cursor up key pressed - Set next direction to ** be moved to UP */ set_snake_dirn(UP); } if (c == 'B') { /* Cursor down key pressed - Set next direction to ** be moved to DOWN */ set_snake_dirn(DOWN); } /* else, unknown escape sequence */ /* We're no longer part way through an escape sequence */ chars_into_escape_sequence = 0; } else if(chars_into_escape_sequence != 0) { /* ** We started an escape sequence but didn't get a character ** we recognised - discard it and assume that we're not ** in an escape sequence. */ chars_into_escape_sequence = 0; } else if (c == ' ') { /* Space character received - move snake immediately */ moveStatus = move_snake(); } else { if(c == 'N' || c == 'n'){ show_instruction(NEWGAME); new_game(); } else if(c == 'P' || c == 'p'){ moveStatus = 0; pause_game(); } else if(c == 'M' || c == 'm'){ toggle_sound(); display_sound_status(); } } } switch(moveStatus){ case ATE_FOOD: if(sound_status()) play_sound(); moveStatus = MOVE_OK; break; } if(moveStatus < 0) { /* Move failed - game over */ handle_game_over(); moveStatus = 0; update_score(); } } }