diff options
author | Vasil Zlatanov <v@skozl.com> | 2017-05-08 00:40:40 +0100 |
---|---|---|
committer | Vasil Zlatanov <v@skozl.com> | 2017-05-08 00:40:40 +0100 |
commit | 673e4900e93767b8bb3241b89c13424ff6fbf547 (patch) | |
tree | 2b5490596f90fd67f3bd0b1267315420c72baf73 /src/main.cpp | |
parent | 8bd33441df8ecb4108e69362665f22a2ccf470e8 (diff) | |
download | e2-switch-673e4900e93767b8bb3241b89c13424ff6fbf547.tar.gz e2-switch-673e4900e93767b8bb3241b89c13424ff6fbf547.tar.bz2 e2-switch-673e4900e93767b8bb3241b89c13424ff6fbf547.zip |
Implement 2048 game prototype
Diffstat (limited to 'src/main.cpp')
-rw-r--r-- | src/main.cpp | 54 |
1 files changed, 37 insertions, 17 deletions
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"); + } } |