diff --git a/src/matrix/matrix.c b/src/matrix/matrix.c index 947c915..d0b3720 100644 --- a/src/matrix/matrix.c +++ b/src/matrix/matrix.c @@ -1,3 +1,4 @@ +#include #include #include @@ -5,6 +6,14 @@ typedef unsigned char byte; +static void delay_ms(unsigned int ms) +{ + systick_clear(); + for (unsigned int i=0; i> 28) & 7; - food[1] = (rng_state >> 20) & 7; - rng_state *= 0x3771cadb; -} - -static void snake_init(void) -{ - snake[0][0] = 0; - snake[0][1] = 0; - snake_dir = 3; - snake_tail = 0; - snake_head = 1; - gen_food(); -} - -static void snake_show(void) -{ - memset(display, 0, 8); - for (int i=snake_tail; i != snake_head; i = (i+1) % SNAKE_MAX) - display[snake[i][0]] |= 1 << snake[i][1]; - display[food[0]] |= 1 << food[1]; - refresh_display(); -} - -static void snake_step(void) -{ - if (buttons[0]) - snake_dir = (snake_dir+3) % 4; - else if (buttons[1]) - snake_dir = (snake_dir+1) % 4; - - int hi = (snake_head + SNAKE_MAX - 1) % SNAKE_MAX; - int x = snake[hi][0] + dirs[snake_dir][0]; - int y = snake[hi][1] + dirs[snake_dir][1]; - - if (x < 0 || x > 7 || y < 0 || y > 7) - return; - - snake[snake_head][0] = x; - snake[snake_head][1] = y; - snake_head = (snake_head + 1) % SNAKE_MAX; - - if (x == food[0] && y == food[1]) - gen_food(); - else - snake_tail = (snake_tail + 1) % SNAKE_MAX; - - snake_show(); -} - int main(void) { rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]); @@ -136,10 +78,12 @@ int main(void) gpio_set(GPIOB, GPIO13); gpio_clear(GPIOB, GPIO14); +#if 0 // PA0 = left button // PA1 = right button gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO0 | GPIO1); gpio_set(GPIOA, GPIO0 | GPIO1); +#endif refresh_display(); set_reg(9, 0); @@ -148,20 +92,10 @@ int main(void) set_reg(13, 0); // test set_reg(12, 1); // shutdown - snake_init(); - snake_show(); - for (;;) { - buttons[0] = buttons[1] = 0; - for (int i=0; i<1000000; i++) { - if (!gpio_get(GPIOA, GPIO0)) - buttons[0] = 1; - if (!gpio_get(GPIOA, GPIO1)) - buttons[1] = 1; - asm volatile (""); - } - - gpio_toggle(GPIOC, GPIO13); - snake_step(); + gpio_clear(GPIOC, GPIO13); + delay_ms(100); + gpio_set(GPIOC, GPIO13); + delay_ms(500); } }