#include "mbed.h" #include "2048.h" #include "spi_init.h" #include "counter.h" #include "Adafruit_SSD1306.h" #define SW_PINS p24, p23, p22, p21 #define SW_PERIOD 20000 // 20ms void tout(void); void draw(int board[4][4], int score); // Onboard LED PinName switch_pin[] = { SW_PINS }; Counter *switch_position[4]; Ticker timer; //Ticker pwm; volatile uint16_t switch_count[4] = { 0, 0, 0, 0 }; volatile uint16_t switch_pressed[4] = { 0, 0, 0, 0 }; volatile uint16_t last_pressed[4] = { 0, 0, 0, 0 }; uint16_t current_f[4] = { 0, 0, 0, 0 }; volatile uint16_t update = 0; // Initialise display SPInit gSpi(D_MOSI_PIN, NC, D_CLK_PIN); Adafruit_SSD1306_Spi oled(gSpi, D_DC_PIN, D_RST_PIN, D_CS_PIN, 64, 128); int main(void) { int done = 0; int board[4][4]; init(board); //Function from 2048.h to initialise values oled.setRotation(2); wait(0.5); // Enable ISR for the switch rising edge for (int i = 0; i < 4; ++i) { switch_position[i] = new Counter(switch_pin[i]); } //Attach switch sampling timer ISR to the timer instance with the required period timer.attach_us(&tout, SW_PERIOD); //Print splash screen for 1 second oled.clearDisplay(); oled.setTextCursor(oled.width()/2-10,oled.height()/2); oled.printf("2048"); oled.setTextCursor(oled.width()/2-30,oled.height()/2+10); oled.printf("\n%ux%u Group Ay08-04\n", oled.width(), oled.height()); oled.display(); wait(1); int score = 0; bool gameOver = false; //Print initial board oled.clearDisplay(); draw(board,score); oled.display(); for (;;) { if (update) { update = 0; done = 0; oled.setTextCursor(0, 0); //Write the latest switch osciallor count. done is true if move valid if(!gameOver) { for (int i = 3; i >= 0; --i) { if (switch_pressed[i] && !last_pressed[i]) done = move(i, board); //Apply move to board } if(done){ add2(board); //Add a new 2 if there are 0's score++; oled.clearDisplay(); draw(board,score); oled.display(); } } if(gameover(board)) { oled.clearDisplay(); oled.setTextCursor(oled.width()/2-30,oled.height()/2); oled.printf("Game Over!"); oled.display(); gameOver = true; wait(3); oled.clearDisplay(); oled.setTextCursor(oled.width()/2-30,oled.height()/2); oled.printf("Again?"); oled.display(); //TODO: Put in code to set game over to false if up is pressed } //Copy the display buffer to the display } } } //Interrupt Service Routine for the switch sampling timer void tout(void) { // Query how many times switch triggered for (int i = 0; i < 4; ++i) { switch_count[i] = switch_position[i]->read(); switch_position[i]->write(0); last_pressed[i] = switch_pressed[i]; if (switch_count[i] < 600) switch_pressed[i] = 1; else if (switch_count[i] > 700) switch_pressed[i] = 0; } // Update display update = 1; } //Board stores powers of 2, so must convert and then print void draw(int board[SIZE][SIZE], int score) { oled.setTextCursor(0,0); oled.printf("Score: %d", score); int x, y; int value; for (y = 0; y < SIZE; y++) { for (x = 0; x < SIZE; x++) { if (board[x][y] != 0) { value = 2; for (int i = 1; i < board[x][y]; ++i) { value = value * 2; } } else { value = 0; } oled.setTextCursor(oled.width()/4*(x),oled.height()*(y+1)/5); oled.printf("%d", value); } } }