Started with game logic, implemented some draw stuff.
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@
|
|||||||
*.elf
|
*.elf
|
||||||
*.map
|
*.map
|
||||||
*.bin
|
*.bin
|
||||||
|
*.swp
|
||||||
|
|||||||
119
src/game.c
Normal file
119
src/game.c
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
#include "game.h"
|
||||||
|
#include <lcd.h>
|
||||||
|
#include <lcd_lld.h>
|
||||||
|
#include <color.h>
|
||||||
|
#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);
|
||||||
|
|
||||||
|
//uart init
|
||||||
|
|
||||||
|
//lcd init
|
||||||
|
LCD_Init();
|
||||||
|
LCD_Clear(GUI_COLOR_BLACK);
|
||||||
|
|
||||||
|
//struct init
|
||||||
|
game->state=prestart;
|
||||||
|
game->host_id = 0;
|
||||||
|
game->ticks_per_pixel = SPEED_SLOW;
|
||||||
|
game->ticks_leftover =0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void game_step(game_t* game, uint64_t deltaTime) {
|
||||||
|
static long l = 0;
|
||||||
|
switch(game->state) {
|
||||||
|
case prestart:
|
||||||
|
//Draw welcome screen
|
||||||
|
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));
|
||||||
|
|
||||||
|
//send game start request to slave
|
||||||
|
//wait on game accept response
|
||||||
|
|
||||||
|
//setup
|
||||||
|
player_init(&(game->player[game->host_id]));
|
||||||
|
|
||||||
|
|
||||||
|
//switch state
|
||||||
|
game->state = running;
|
||||||
|
LCD_Clear(GUI_COLOR_BLACK);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
19
src/game.h
19
src/game.h
@@ -1,26 +1,35 @@
|
|||||||
#ifndef PLAYER_H
|
#ifndef GAME_H
|
||||||
#define PLAYER_H
|
#define GAME_H
|
||||||
|
|
||||||
#include<stdint.h>
|
#include<stdint.h>
|
||||||
#include<stdlib.h>
|
#include<stdlib.h>
|
||||||
#include"player.h"
|
#include"player.h"
|
||||||
|
|
||||||
#define PLAYER_COUNT 2
|
#define PLAYER_COUNT 2
|
||||||
|
#define PLAYER_WIDTH 3
|
||||||
|
#define SPEED_SLOW 10
|
||||||
|
|
||||||
typedef struct game_s{
|
typedef struct game_s{
|
||||||
|
//public section
|
||||||
|
|
||||||
uint16_t time; // seconds since game start
|
uint16_t time; // seconds since game start
|
||||||
uint8_t winner_id;
|
int8_t winner_id;
|
||||||
uint8_t host_id;
|
uint8_t host_id;
|
||||||
|
uint8_t ticks_per_pixel;
|
||||||
player_t player[PLAYER_COUNT];
|
player_t player[PLAYER_COUNT];
|
||||||
|
|
||||||
enum{
|
enum{
|
||||||
|
prestart,
|
||||||
running,
|
running,
|
||||||
ended
|
ended
|
||||||
} state;
|
} state;
|
||||||
|
|
||||||
|
|
||||||
|
//private section ahead:
|
||||||
|
uint8_t ticks_leftover;
|
||||||
} game_t;
|
} game_t;
|
||||||
|
|
||||||
void game_init(game_t* game);
|
void game_init(game_t* game);
|
||||||
void game_step(game_t* game, uint16_t time_delta);
|
void game_step(game_t* game, uint64_t deltaTime);
|
||||||
|
|
||||||
#endif /* PLAYER_H */
|
#endif /* GAME_H */
|
||||||
|
|||||||
49
src/io.c
Normal file
49
src/io.c
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
#include "io.h"
|
||||||
|
|
||||||
|
|
||||||
|
void pin_create(pin_t* pin, GPIO_TypeDef* GPIO, uint8_t pinnr, bool input) {
|
||||||
|
GPIO_InitTypeDef gi;
|
||||||
|
GPIO_StructInit(&gi);
|
||||||
|
|
||||||
|
gi.GPIO_Pin = 1 << pinnr;
|
||||||
|
|
||||||
|
if(input) {
|
||||||
|
gi.GPIO_Mode = GPIO_Mode_IN;
|
||||||
|
gi.GPIO_OType = GPIO_OType_OD;
|
||||||
|
gi.GPIO_PuPd = GPIO_PuPd_UP;
|
||||||
|
} else {
|
||||||
|
gi.GPIO_Mode = GPIO_Mode_OUT;
|
||||||
|
gi.GPIO_OType = GPIO_OType_PP;
|
||||||
|
gi.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
GPIO_Init(GPIO,&gi);
|
||||||
|
|
||||||
|
pin->GPIO=GPIO;
|
||||||
|
pin->pinmask=0x01<<pinnr;
|
||||||
|
pin->input = input;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool pin_get(pin_t* pin) {
|
||||||
|
if(pin->input) {
|
||||||
|
return GPIO_ReadInputDataBit(pin->GPIO,pin->pinmask);
|
||||||
|
} else {
|
||||||
|
return GPIO_ReadOutputDataBit(pin->GPIO,pin->pinmask);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void pin_set(pin_t* pin, bool status) {
|
||||||
|
if(!pin->input) {
|
||||||
|
GPIO_WriteBit(pin->GPIO,pin->pinmask,status);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void pin_toggle(pin_t* pin) {
|
||||||
|
if(!pin->input) {
|
||||||
|
set_pin(pin,!get_pin(pin));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
17
src/io.h
Normal file
17
src/io.h
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#include <stdbool.h>
|
||||||
|
#include <stm32f4xx.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
75
src/main.c
75
src/main.c
@@ -30,71 +30,32 @@
|
|||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include "uart.h"
|
#include "uart.h"
|
||||||
|
|
||||||
#define SPEED 500000
|
|
||||||
|
|
||||||
uint16_t colors[] = {
|
game_t gameobj;
|
||||||
GUI_COLOR_BLACK,
|
|
||||||
GUI_COLOR_WHITE,
|
|
||||||
GUI_COLOR_LIGHT_GRAY,
|
|
||||||
GUI_COLOR_DARK_GREY,
|
|
||||||
GUI_COLOR_RED,
|
|
||||||
GUI_COLOR_YELLOW,
|
|
||||||
GUI_COLOR_ORANGE,
|
|
||||||
GUI_COLOR_BROWN,
|
|
||||||
GUI_COLOR_GREEN,
|
|
||||||
GUI_COLOR_CYAN,
|
|
||||||
GUI_COLOR_BLUE,
|
|
||||||
GUI_COLOR_PINK,
|
|
||||||
GUI_COLOR_MAGENTA
|
|
||||||
};
|
|
||||||
|
|
||||||
volatile unsigned char* LED = (volatile unsigned char*)0x6C000200;
|
uint64_t ticks;
|
||||||
volatile unsigned char* SWITCH = (volatile unsigned char*)0x6C000400;
|
uint64_t lastTicks;
|
||||||
GPIO_InitTypeDef g;
|
|
||||||
|
|
||||||
|
void SysTick_Handler() {
|
||||||
void init_gpio(void)
|
ticks++;
|
||||||
{
|
|
||||||
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
|
|
||||||
g.GPIO_Pin = GPIO_Pin_0;
|
|
||||||
g.GPIO_Mode = GPIO_Mode_OUT;
|
|
||||||
g.GPIO_OType = GPIO_OType_PP;
|
|
||||||
g.GPIO_Speed = GPIO_Speed_2MHz;
|
|
||||||
g.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
|
||||||
GPIO_Init(GPIOA, &g);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#define SYSCLK 168e6
|
||||||
|
#define TICKS_PER_SECOND 100
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
int i = 0, j = 0, m = 0;
|
if(SysTick_Config(SYSCLK/TICKS_PER_SECOND)) { //if systick config fails
|
||||||
|
while(1); //sleep forever
|
||||||
|
}
|
||||||
|
|
||||||
*LED = 0b00000001;
|
game_init(&gameobj);
|
||||||
|
while(1) {
|
||||||
LCD_Init();
|
uint64_t curTicks = ticks;
|
||||||
init_gpio();
|
game_step(&gameobj,curTicks-lastTicks); //calculate next game step, and pass it the delta time
|
||||||
LCD_Clear(GUI_COLOR_RED);
|
lastTicks = curTicks;
|
||||||
|
}
|
||||||
while(1){
|
|
||||||
|
|
||||||
for(m = 0; (m <= 12); m++){
|
|
||||||
|
|
||||||
LCD_Clear(colors[m]);
|
|
||||||
|
|
||||||
GPIO_WriteBit(GPIOA, GPIO_Pin_0, 1);
|
|
||||||
|
|
||||||
for(i = 0; i < 7; i++){
|
|
||||||
*LED<<=1;
|
|
||||||
for(j = 0; j < SPEED; j++);
|
|
||||||
}
|
|
||||||
|
|
||||||
GPIO_WriteBit(GPIOA, GPIO_Pin_0, 0);
|
|
||||||
|
|
||||||
for(i = 7; i > 0; i--){
|
|
||||||
*LED>>=1;
|
|
||||||
for(j = 0; j < SPEED; j++);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
21
src/player.c
Normal file
21
src/player.c
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#include "player.h"
|
||||||
|
#include <color.h>
|
||||||
|
|
||||||
|
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};
|
||||||
|
player->past_positions[0] = player->position;
|
||||||
|
player->direction = right;
|
||||||
|
player->state = alive;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void player_append_position(player_t* player, point_t point) {
|
||||||
|
if(player->num_positions < max_positions) {
|
||||||
|
player->past_positions[player->num_positions++] = point;
|
||||||
|
}
|
||||||
|
}
|
||||||
11
src/player.h
11
src/player.h
@@ -18,13 +18,20 @@ typedef struct player_s {
|
|||||||
uint16_t color;
|
uint16_t color;
|
||||||
uint16_t num_positions;
|
uint16_t num_positions;
|
||||||
|
|
||||||
|
enum {
|
||||||
|
right,
|
||||||
|
down,
|
||||||
|
left,
|
||||||
|
up
|
||||||
|
} direction;
|
||||||
|
|
||||||
enum{
|
enum{
|
||||||
dead,
|
dead,
|
||||||
alive,
|
alive,
|
||||||
}state;
|
} state;
|
||||||
|
|
||||||
point_t past_positions[max_positions];
|
point_t past_positions[max_positions];
|
||||||
point_t curr_position;
|
point_t position;
|
||||||
|
|
||||||
} player_t;
|
} player_t;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user