From 77e64dacf78297cb4367c407413199295d7df2b9 Mon Sep 17 00:00:00 2001 From: id101010 Date: Tue, 26 Jan 2016 12:25:24 +0100 Subject: [PATCH] Implemented two players on single host, changed io processing to interrupt. --- src/game.c | 143 ++++++++++++++++++++++++++------------------------- src/game.h | 16 ++++-- src/io.c | 54 ++++++++++++++++++- src/io.h | 18 +++---- src/main.c | 8 +-- src/player.c | 16 +++--- src/player.h | 20 +++---- src/uart.h | 24 --------- 8 files changed, 166 insertions(+), 133 deletions(-) delete mode 100644 src/uart.h diff --git a/src/game.c b/src/game.c index fdf901a..5fd0b32 100644 --- a/src/game.c +++ b/src/game.c @@ -4,17 +4,11 @@ #include #include "io.h" -static pin_t pin_left; -static pin_t pin_right; - - void game_init(game_t* game) { //Sysinit - //gpio init (move to module?) - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOC,ENABLE); - pin_create(&pin_right, GPIOC, 7, true); - pin_create(&pin_left, GPIOI, 0, true); + //gpio init + io_init(); //uart init @@ -24,8 +18,7 @@ void game_init(game_t* game) { //struct init game->state=prestart; - game->host_id = 0; - game->ticks_per_pixel = SPEED_SLOW; + game->ticks_per_pixel = SPEED_DEFAULT; game->ticks_leftover =0; } @@ -38,14 +31,15 @@ void game_step(game_t* game, uint64_t deltaTime) { LCD_DrawRectF(10,10,100,50,GUI_COLOR_BLUE); //wait on player to press start (host) - while(!pin_get(&pin_right)); - while(pin_get(&pin_right)); + while(!io_button_has_edge(BTN_START)); //send game start request to slave //wait on game accept response //setup - player_init(&(game->player[game->host_id])); + //void player_init(player_t* player, uint8_t btn_left, uint8_t btn_right, point_t pos, uint16_t color, direction_t direction); + player_init(&(game->player[0]), BTN_PLAYER_1_LEFT, BTN_PLAYER_1_RIGHT, (point_t){.x=10,.y=120}, GUI_COLOR_BLUE, right); + player_init(&(game->player[1]), BTN_PLAYER_2_LEFT, BTN_PLAYER_2_RIGHT, (point_t){.x=230,.y=120}, GUI_COLOR_RED, left); //switch state @@ -54,65 +48,74 @@ void game_step(game_t* game, uint64_t deltaTime) { break; case running: { - bool directionChange = false; - player_t* host_player = &(game->player[game->host_id]); - if(pin_get(&pin_left)) { - while(pin_get(&pin_left)); - host_player->direction= (host_player->direction + 3) % 4 ; // "decrement enum value" - directionChange = true; - } else if(pin_get(&pin_right)) { - while(pin_get(&pin_right)); - host_player->direction= (host_player->direction + 1) % 4 ; // "increment enum value" - directionChange = true; - } + uint16_t ticks; + uint16_t pixels = 0; + + if(deltaTime) { - if(directionChange) { - player_append_position(host_player,host_player->position); - } - if(deltaTime) { - uint16_t ticks = game->ticks_leftover + deltaTime; - uint16_t pixels = ticks / game->ticks_per_pixel; - game->ticks_leftover = ticks % game->ticks_per_pixel; - if(pixels) { - point_t last_point = host_player->past_positions[host_player->num_positions-1]; - switch(host_player->direction) { - case down: - host_player->position.y+=pixels; - LCD_DrawRectF( host_player->position.x, - last_point.y, - PLAYER_WIDTH, - host_player->position.y - last_point.y, - host_player->color); - break; - case left: - host_player->position.x-=pixels; - LCD_DrawRectF( host_player->position.x, - host_player->position.y, - last_point.x -host_player->position.x, - PLAYER_WIDTH, - host_player->color); - break; - case up: - host_player->position.y-=pixels; - LCD_DrawRectF( host_player->position.x, - host_player->position.y, - PLAYER_WIDTH, - last_point.y - host_player->position.y, - host_player->color); - break; - case right: - host_player->position.x+=pixels; - LCD_DrawRectF( last_point.x, - host_player->position.y, - host_player->position.x - last_point.x, - PLAYER_WIDTH, - host_player->color); - break; - } - } - } - } + ticks = game->ticks_leftover + deltaTime; + pixels = ticks / game->ticks_per_pixel; + + game->ticks_leftover = ticks % game->ticks_per_pixel; + } + + for(int i = 0; i < PLAYER_COUNT; i++) { + + bool directionChange = false; + player_t* player = &(game->player[i]); + + if(io_button_has_edge(player->btn_left)) { + player->direction= (player->direction + (4 - 1)) % 4 ; // "decrement enum value" + directionChange = true; + } else if(io_button_has_edge(player->btn_right)) { + player->direction= (player->direction + 1) % 4 ; // "increment enum value" + directionChange = true; + } + + if(directionChange) { + player_append_position(player,player->position); + } + + if(pixels) { + point_t last_point = player->past_positions[player->num_positions-1]; + switch(player->direction) { + case down: + player->position.y+=pixels; + LCD_DrawRectF( player->position.x, + last_point.y, + PLAYER_WIDTH, + player->position.y - last_point.y, + player->color); + break; + case left: + player->position.x-=pixels; + LCD_DrawRectF( player->position.x, + player->position.y, + last_point.x -player->position.x, + PLAYER_WIDTH, + player->color); + break; + case up: + player->position.y-=pixels; + LCD_DrawRectF( player->position.x, + player->position.y, + PLAYER_WIDTH, + last_point.y - player->position.y, + player->color); + break; + case right: + player->position.x+=pixels; + LCD_DrawRectF( last_point.x, + player->position.y, + player->position.x - last_point.x, + PLAYER_WIDTH, + player->color); + break; + } + } + } break; + } } } diff --git a/src/game.h b/src/game.h index 8e8fb61..a6c4916 100644 --- a/src/game.h +++ b/src/game.h @@ -5,16 +5,23 @@ #include #include"player.h" -#define PLAYER_COUNT 2 -#define PLAYER_WIDTH 3 -#define SPEED_SLOW 10 +#define PLAYER_COUNT 2 +#define PLAYER_WIDTH 0 // Don't change +#define SPEED_SLOW 10 +#define SPEED_FAST 1 +#define SPEED_DEFAULT (SPEED_SLOW) + +#define BTN_START 0 +#define BTN_PLAYER_1_LEFT 3 +#define BTN_PLAYER_1_RIGHT 2 +#define BTN_PLAYER_2_LEFT 1 +#define BTN_PLAYER_2_RIGHT 0 typedef struct game_s{ //public section uint16_t time; // seconds since game start int8_t winner_id; - uint8_t host_id; uint8_t ticks_per_pixel; player_t player[PLAYER_COUNT]; @@ -24,7 +31,6 @@ typedef struct game_s{ ended } state; - //private section ahead: uint8_t ticks_leftover; } game_t; diff --git a/src/io.c b/src/io.c index 9e674a8..f7082b5 100644 --- a/src/io.c +++ b/src/io.c @@ -1,6 +1,29 @@ #include "io.h" +//-----------Local types & functions-------------------------- +typedef struct pin_s { + GPIO_TypeDef* GPIO; + uint16_t pinmask; + bool input; +} pin_t; +static void pin_create(pin_t* pin, GPIO_TypeDef* GPIO, uint8_t pinnr, bool input); +static bool pin_get(pin_t* pin); +static void pin_set(pin_t* pin, bool status); +static void pin_toggle(pin_t* pin); + + +//-------------Local Variables------------------------- +static pin_t pin_t0; +static pin_t pin_t1; +static pin_t pin_t2; +static pin_t pin_t3; + +static uint8_t new = 0; +static uint8_t old = 0; +static volatile uint8_t edg = 0; + +//---------------Implementation -------------------------------- void pin_create(pin_t* pin, GPIO_TypeDef* GPIO, uint8_t pinnr, bool input) { GPIO_InitTypeDef gi; GPIO_StructInit(&gi); @@ -41,9 +64,38 @@ void pin_set(pin_t* pin, bool status) { void pin_toggle(pin_t* pin) { if(!pin->input) { - set_pin(pin,!get_pin(pin)); + pin_set(pin,!pin_get(pin)); } } +void io_init(void){ + //gpio init + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOC,ENABLE); + pin_create(&pin_t0, GPIOC, 7, true); + pin_create(&pin_t1, GPIOB, 15, true); + pin_create(&pin_t2, GPIOB, 14, true); + pin_create(&pin_t3, GPIOI, 0, true); +} +void io_process(void) { + new = pin_get(&pin_t0) | + pin_get(&pin_t1) << 1 | + pin_get(&pin_t2) << 2 | + pin_get(&pin_t3) << 3; + + edg |= (new ^ old) & new; // detect positive edge + old = new; +} + +bool io_button_has_edge(uint8_t btnnumber) { + + uint8_t bm = (1 << btnnumber); + bool status = ((edg & bm) > 0); + + if(status){ + edg &= ~bm; + } + + return status; +} diff --git a/src/io.h b/src/io.h index f200233..d945209 100644 --- a/src/io.h +++ b/src/io.h @@ -1,17 +1,13 @@ +#ifndef IO_H +#define IO_H + #include #include -typedef struct pin_s { - GPIO_TypeDef* GPIO; - uint16_t pinmask; - bool input; -} pin_t; - -void pin_create(pin_t* pin, GPIO_TypeDef* GPIO, uint8_t pinnr, bool input); -bool pin_get(pin_t* pin); -void pin_set(pin_t* pin, bool status); -void pin_toggle(pin_t* pin); - +void io_init(void); +void io_process(void); +bool io_button_has_edge(uint8_t btnnumber); +#endif /* IO_H */ diff --git a/src/main.c b/src/main.c index ccbe391..a3bcd90 100644 --- a/src/main.c +++ b/src/main.c @@ -27,10 +27,10 @@ #include #include #include "player.h" +#include "io.h" #include "game.h" #include "uart.h" - game_t gameobj; uint64_t ticks; @@ -38,11 +38,11 @@ uint64_t lastTicks; void SysTick_Handler() { ticks++; + io_process(); } - -#define SYSCLK 168e6 -#define TICKS_PER_SECOND 100 +#define SYSCLK 168e6 +#define TICKS_PER_SECOND 100 int main(void) { diff --git a/src/player.c b/src/player.c index f3ceb87..720999d 100644 --- a/src/player.c +++ b/src/player.c @@ -1,19 +1,19 @@ #include "player.h" #include -void player_init(player_t* player) { - player->id=0; - player->color = GUI_COLOR_BLUE; - player->num_positions=1; - player->position = (point_t){.x=10,.y=100}; +void player_init(player_t* player, uint8_t btn_left, uint8_t btn_right, + point_t pos, uint16_t color, direction_t direction) { + player->color = color; + player->num_positions = 1; + player->position = pos; player->past_positions[0] = player->position; - player->direction = right; + player->direction = direction; player->state = alive; + player->btn_left = btn_left; + player->btn_right = btn_right; } - - void player_append_position(player_t* player, point_t point) { if(player->num_positions < max_positions) { player->past_positions[player->num_positions++] = point; diff --git a/src/player.h b/src/player.h index c5577fb..b9e4bce 100644 --- a/src/player.h +++ b/src/player.h @@ -1,11 +1,16 @@ #ifndef PLAYER_H #define PLAYER_H - #include #include #define max_positions 320 +typedef enum direction_e { + right, + down, + left, + up +} direction_t; typedef struct point_s{ uint16_t x; @@ -14,16 +19,11 @@ typedef struct point_s{ typedef struct player_s { - uint8_t id; + uint8_t btn_left; + uint8_t btn_right; uint16_t color; uint16_t num_positions; - - enum { - right, - down, - left, - up - } direction; + direction_t direction; enum{ dead, @@ -35,7 +35,7 @@ typedef struct player_s { } player_t; -void player_init(player_t* player); // reset all fields +void player_init(player_t* player, uint8_t btn_left, uint8_t btn_right, point_t pos, uint16_t color, direction_t direction); // reset all fields void player_append_position(player_t* player, point_t point); // updates num_position and adds current position to the list #endif /* PLAYER_H */ diff --git a/src/uart.h b/src/uart.h deleted file mode 100644 index efc7d1f..0000000 --- a/src/uart.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef UART_H -#define UART_H - -#include -#include -#include - -typedef struct uart_message_s{ - uint16_t message_length; - uint8_t *message_data; - - // Internal, do not touch! - bool __deleted; -} uart_message_t; - -void uart_init(void); -void uart_transmit(uart_message_t *message); // transmit a message and frees it -bool uart_has_message(void); // checks if there is at least one msg available -void uart_message_free(uart_message_t *message); // frees a message - -uart_message_t *uart_message_allocate(void); // allocate storage for a new message -uart_message_t *receive_message(void); // returns a message if available else null - -#endif /* UART_H */