From 673e4900e93767b8bb3241b89c13424ff6fbf547 Mon Sep 17 00:00:00 2001 From: Vasil Zlatanov Date: Mon, 8 May 2017 00:40:40 +0100 Subject: Implement 2048 game prototype --- src/main.cpp | 54 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 17 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 19f280d..229ef25 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,5 @@ #include "mbed.h" +#include "2048.h" #include "spi_init.h" #include "counter.h" #include "Adafruit_SSD1306.h" @@ -7,15 +8,14 @@ #define SW_PERIOD 20000 // 20ms void tout(void); -void pwm_invert(void); +void draw(int board[4][4]); // Onboard LED -DigitalOut out_wave(LED1); PinName switch_pin[] = { SW_PINS }; Counter *switch_position[4]; Ticker timer; -Ticker pwm; +//Ticker pwm; volatile uint16_t switch_count[4] = { 0, 0, 0, 0 }; volatile uint16_t switch_pressed[4] = { 0, 0, 0, 0 }; @@ -31,6 +31,10 @@ 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); + oled.setRotation(2); wait(0.5); @@ -43,30 +47,30 @@ int main(void) timer.attach_us(&tout, SW_PERIOD); oled.clearDisplay(); - oled.printf("%ux%u Group Ay08-04\r\n", oled.width(), oled.height()); + oled.printf("%ux%u Group Ay08-04\n", oled.width(), oled.height()); + + draw(board); for (;;) { if (update) { update = 0; + done = 0; oled.setTextCursor(0, 0); //Write the latest switch osciallor count for (int i = 3; i >= 0; --i) { - current_f[i] += (switch_pressed[i] && !last_pressed[i]); - if (current_f[i] > 9) - current_f[i] = 0; - oled.printf("\nS:%u C:%05u N:%u", switch_pressed[i], switch_count[i], current_f[i]); + if (switch_pressed[i] && !last_pressed[i]) + done = move(i, board); } - uint16_t frequency = 1000*current_f[3] + 100*current_f[2] + 10*current_f[1] + current_f[0]; - - oled.printf("\nF:%u ", frequency); + if(done){ + add2(board); + draw(board); + } - if (frequency < 25) - pwm.attach_ms(&pwm_invert, 500/frequency); - else - pwm.attach_us(&pwm_invert, 500000/frequency); + if(gameover(board)) + oled.printf("\nGame Over!"); //Copy the display buffer to the display oled.display(); @@ -95,6 +99,22 @@ void tout(void) update = 1; } -void pwm_invert(void){ - out_wave = !out_wave; +void draw(int board[SIZE][SIZE]) +{ + 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.printf("%d\t", value); + } + oled.printf("\n"); + } } -- cgit v1.2.3-54-g00ecf