summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2013-03-15 16:27:24 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2013-03-15 16:27:24 +0100
commitf98dd6b3479d70465d87c6d794990d56ebb0e799 (patch)
tree925569abe6814d9b4b0a880da107752c382e03dc
parenta5ba3499fb1e46fcbbece4c28d6eb6aebab88239 (diff)
downloadsnake-master.tar
snake-master.zip
Add score displayHEADmaster
-rw-r--r--CMakeLists.txt1
-rw-r--r--snake.c116
2 files changed, 117 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d31ff5e..ec6a3ef 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -30,6 +30,7 @@ set_target_properties(snake.elf PROPERTIES
LINK_FLAGS "-Wall -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Os -mmcu=${BOARD}"
COMPILE_DEFINITIONS "F_CPU=${CLOCK}"
)
+target_link_libraries(snake.elf m)
add_custom_command(OUTPUT snake.hex COMMAND ${CMAKE_OBJCOPY} -O ihex -R .eeprom snake.elf snake.hex DEPENDS snake.elf)
add_custom_target(snake ALL DEPENDS snake.hex)
diff --git a/snake.c b/snake.c
index c7f3d86..1b958c3 100644
--- a/snake.c
+++ b/snake.c
@@ -7,6 +7,83 @@
#include <stdbool.h>
+#define NUMBER_WIDTH 3
+#define NUMBER_HEIGHT 7
+
+static const uint8_t NUMBERS[10][NUMBER_HEIGHT][NUMBER_WIDTH] = {
+ {
+ {1, 1, 1},
+ {1, 0, 1},
+ {1, 0, 1},
+ {1, 0, 1},
+ {1, 1, 1},
+ },
+ {
+ {0, 0, 1},
+ {0, 0, 1},
+ {0, 0, 1},
+ {0, 0, 1},
+ {0, 0, 1},
+ },
+ {
+ {1, 1, 1},
+ {0, 0, 1},
+ {1, 1, 1},
+ {1, 0, 0},
+ {1, 1, 1},
+ },
+ {
+ {1, 1, 1},
+ {0, 0, 1},
+ {1, 1, 1},
+ {0, 0, 1},
+ {1, 1, 1},
+ },
+ {
+ {1, 0, 1},
+ {1, 0, 1},
+ {1, 1, 1},
+ {0, 0, 1},
+ {0, 0, 1},
+ },
+ {
+ {1, 1, 1},
+ {1, 0, 0},
+ {1, 1, 1},
+ {0, 0, 1},
+ {1, 1, 1},
+ },
+ {
+ {1, 1, 1},
+ {1, 0, 0},
+ {1, 1, 1},
+ {1, 0, 1},
+ {1, 1, 1},
+ },
+ {
+ {1, 1, 1},
+ {0, 0, 1},
+ {0, 0, 1},
+ {0, 0, 1},
+ {0, 0, 1},
+ },
+ {
+ {1, 1, 1},
+ {1, 0, 1},
+ {1, 1, 1},
+ {1, 0, 1},
+ {1, 1, 1},
+ },
+ {
+ {1, 1, 1},
+ {1, 0, 1},
+ {1, 1, 1},
+ {0, 0, 1},
+ {1, 1, 1},
+ },
+};
+
+
typedef struct _point_t {
uint8_t x, y;
} point_t;
@@ -25,6 +102,7 @@ static volatile bool start;
static volatile uint16_t lfsr = 0xace1;
static uint8_t dir;
+static uint8_t score;
static point_t tail = {7, 4};
#define HISTORY_MAX 128
@@ -94,6 +172,7 @@ static void reset(void) {
tail = (point_t){7, 4};
history_len = 0;
history_pos = 0;
+ score = 0;
unsigned i;
for (i = 0; i < 2; i++)
@@ -104,6 +183,39 @@ static void set_led(uint8_t x, uint8_t y, uint8_t val) {
LedSignSet(13-x, 8-y, val);
}
+static void set_number(uint8_t x, uint8_t y, uint8_t val) {
+ uint8_t i, j;
+
+ for (i = 0; i < NUMBER_WIDTH; i++) {
+ for (j = 0; j < NUMBER_HEIGHT; j++) {
+ set_led(x+i, y+j, NUMBERS[val][j][i] ? 7 : 0);
+ }
+ }
+}
+
+static void show_score(uint8_t score) {
+ LedSignClear(7);
+ _delay_ms(50);
+ LedSignClear(6);
+ _delay_ms(50);
+ LedSignClear(5);
+ _delay_ms(50);
+ LedSignClear(4);
+ _delay_ms(50);
+ LedSignClear(3);
+ _delay_ms(50);
+ LedSignClear(2);
+ _delay_ms(50);
+ LedSignClear(1);
+ _delay_ms(50);
+ LedSignClear(0);
+ _delay_ms(50);
+
+ set_number(1, 2, (score/100)%10);
+ set_number(NUMBER_WIDTH+2, 2, (score/10)%10);
+ set_number(2*NUMBER_WIDTH+3, 2, score%10);
+}
+
void kbd_handle(uint16_t code, bool make) {
if (!make)
return;
@@ -187,6 +299,7 @@ static bool step(void) {
return false;
if (points_equal(&pt, &q)) {
+ score++;
rand_q();
move(d, true);
}
@@ -218,6 +331,9 @@ int main() {
while (step())
_delay_ms(100);
+
+ _delay_ms(200);
+ show_score(score);
}
return 0;