diff --git a/Core/Inc/game.h b/Core/Inc/game.h new file mode 100644 index 0000000..40662bb --- /dev/null +++ b/Core/Inc/game.h @@ -0,0 +1,28 @@ +#define MAX_PLAYERS 6 +#define TIMER_MAX 180 +#define TIMER_STEP 5 + +#include +#include + +typedef struct { + bool countScores; + uint8_t activePlayers; + uint8_t currentPlayer; + uint32_t turnTime; + uint32_t timerValue; + uint32_t scores[MAX_PLAYERS]; +} GameEngine; + +extern GameEngine game; + +void InitGameEngine(); +void IncrementTurnTime(); +void DecrementTurnTime(); +void AddPlayer(); +void RemovePlayer(); +uint32_t GetCurrentPlayer(); +uint32_t GetTimerValue(); +void ChangeScore(int8_t delta); +void ResetTurnTimer(); +void NextPlayer(); \ No newline at end of file diff --git a/Core/Src/game.c b/Core/Src/game.c new file mode 100644 index 0000000..0f7b42a --- /dev/null +++ b/Core/Src/game.c @@ -0,0 +1,61 @@ +#include "game.h" + +void InitGameEngine() { + game.timerValue = game.turnTime; + game.countScores = false; + game.activePlayers = 0; + game.currentPlayer = 0; + game.turnTime = TIMER_MAX / 2; + game.timerValue = game.turnTime; + for (int i=0; i TIMER_STEP) + game.turnTime -= TIMER_STEP; + else + game.turnTime = TIMER_MAX; + game.timerValue = game.turnTime; +} + +void IncrementTurnTime() { + if (game.turnTime + TIMER_STEP <= TIMER_MAX) + game.turnTime += TIMER_STEP; + else + game.turnTime = TIMER_STEP; + game.timerValue = game.turnTime; +} + +void AddPlayer() { + if (game.activePlayers < MAX_PLAYERS) { + game.scores[game.activePlayers++] = 0; + } +} + +void RemovePlayer() { + if (game.activePlayers > 0) { + game.scores[game.activePlayers--] = -1; + } +} + +uint32_t GetCurrentPlayer() { + return game.currentPlayer; +} + +uint32_t GetTimerValue() { + return game.timerValue; +} + +void ChangeScore(int8_t delta) { + game.scores[game.currentPlayer] += delta; +} + +void ResetTurnTimer() { + game.timerValue = game.turnTime; +} + +void NextPlayer() { + game.currentPlayer = (game.currentPlayer + 1) % game.activePlayers; + ResetTurnTimer(); +} diff --git a/Core/Src/main.c b/Core/Src/main.c index 8979b20..5d50b37 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -25,6 +25,7 @@ /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "lcd.h" +#include "game.h" #include /* USER CODE END Includes */ @@ -36,6 +37,9 @@ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ +#define LCD_ADDR (0x27 << 1) +#define LCD_COLS 20 +#define LCD_ROWS 4 /* USER CODE END PD */ @@ -55,11 +59,13 @@ UART_HandleTypeDef huart1; DMA_HandleTypeDef hdma_usart1_tx; /* USER CODE BEGIN PV */ + +GameEngine game; LCD_HandleTypeDef hlcd = { &hi2c1, - (0x27 << 1), - 4, - 20 + LCD_ADDR, + LCD_ROWS, + LCD_COLS }; /* USER CODE END PV */ @@ -118,13 +124,13 @@ int main(void) MX_USB_DEVICE_Init(); /* USER CODE BEGIN 2 */ LCD_Init(&hlcd); + InitGameEngine(); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { - /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ diff --git a/Makefile b/Makefile index 1e1996a..1cc147d 100644 --- a/Makefile +++ b/Makefile @@ -38,6 +38,7 @@ BUILD_DIR = build C_SOURCES = \ Core/Src/main.c \ Core/Src/lcd.c \ +Core/Src/game.c \ Core/Src/stm32f1xx_it.c \ Core/Src/stm32f1xx_hal_msp.c \ USB_DEVICE/App/usb_device.c \