Add score display

This commit is contained in:
Matthias Schiffer 2013-03-15 16:27:24 +01:00
parent a5ba3499fb
commit f98dd6b347
2 changed files with 117 additions and 0 deletions

View file

@ -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)

116
snake.c
View file

@ -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;