Finished adc implementation. Increased game ticks.
This commit is contained in:
70
src/game.c
70
src/game.c
@@ -17,7 +17,7 @@ void game_init(game_t* game, uint16_t ticks_per_sec) {
|
|||||||
|
|
||||||
// struct init
|
// struct init
|
||||||
game->state=prestart;
|
game->state=prestart;
|
||||||
game->ticks_per_pixel = SPEED_DEFAULT;
|
game->ticks_per_pixel = SPEED_SLOW;
|
||||||
game->ticks_leftover = 0;
|
game->ticks_leftover = 0;
|
||||||
game->ticks_per_sec = ticks_per_sec;
|
game->ticks_per_sec = ticks_per_sec;
|
||||||
game->time = 0;
|
game->time = 0;
|
||||||
@@ -275,7 +275,7 @@ bool game_step_prestart(game_t* game) {
|
|||||||
//Draw bounding box of gamespeed bar
|
//Draw bounding box of gamespeed bar
|
||||||
LCD_DrawRect( TG_START_COL2_X,
|
LCD_DrawRect( TG_START_COL2_X,
|
||||||
TG_START_Y + PLAYER_COUNT*(TG_START_BLOCK_HEIGHT+TG_START_BLOCK_SPACING),
|
TG_START_Y + PLAYER_COUNT*(TG_START_BLOCK_HEIGHT+TG_START_BLOCK_SPACING),
|
||||||
TG_START_COL2_X,
|
TG_START_COL2_WIDTH,
|
||||||
TG_START_BLOCK_HEIGHT,
|
TG_START_BLOCK_HEIGHT,
|
||||||
GUI_COLOR_WHITE);
|
GUI_COLOR_WHITE);
|
||||||
|
|
||||||
@@ -291,39 +291,63 @@ bool game_step_prestart(game_t* game) {
|
|||||||
|
|
||||||
uint8_t switches_old = 0;
|
uint8_t switches_old = 0;
|
||||||
uint16_t adc_old =0;
|
uint16_t adc_old =0;
|
||||||
uint16_t player1_color=0, player2_color=0;
|
uint16_t player1_color=0, player2_color=0,game_speed=0;
|
||||||
bool first = true;
|
bool first = true;
|
||||||
|
|
||||||
while(!io_button_has_edge(BTN_START)) { //As long as nobody presses the start button
|
while(!io_button_has_edge(BTN_START)) { //As long as nobody presses the start button
|
||||||
uint8_t switches = read_switches(); //read current switches value
|
uint8_t switches = read_switches(); //read current switches value
|
||||||
uint16_t adc = read_adc(); //read current adc value
|
uint16_t adc = read_adc(); //read current adc value
|
||||||
|
if(adc>ADC_MAX) { //adc value exeeds maximum
|
||||||
if(switches!=switches_old || first) { //switch values changed or we are in the first loop
|
adc = ADC_MAX; //set to maximum
|
||||||
game_get_color(switches,&player1_color,&player2_color); //calculate colors from switch value
|
|
||||||
LCD_DrawRectF( TG_START_COL2_X,
|
|
||||||
TG_START_Y,
|
|
||||||
TG_START_COL2_X,
|
|
||||||
TG_START_BLOCK_HEIGHT,
|
|
||||||
player1_color);
|
|
||||||
LCD_DrawRectF( TG_START_COL2_X,
|
|
||||||
TG_START_Y + TG_START_BLOCK_HEIGHT + TG_START_BLOCK_SPACING,
|
|
||||||
TG_START_COL2_X,
|
|
||||||
TG_START_BLOCK_HEIGHT,
|
|
||||||
player2_color);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(adc!=adc_old || first) { //adc value changed or we are in the first loop
|
if(switches!=switches_old || first) { //switch values changed or we are in the first loop
|
||||||
uint8_t bar_width = (TG_START_COL2_X -2)*50/100; //TODO: use adc value
|
game_get_color(switches,&player1_color,&player2_color); //calculate colors from switch value
|
||||||
|
//Draw player1 color
|
||||||
|
LCD_DrawRectF( TG_START_COL2_X,
|
||||||
|
TG_START_Y,
|
||||||
|
TG_START_COL2_WIDTH,
|
||||||
|
TG_START_BLOCK_HEIGHT,
|
||||||
|
player1_color);
|
||||||
|
//Draw player 2 color
|
||||||
|
LCD_DrawRectF( TG_START_COL2_X,
|
||||||
|
TG_START_Y + TG_START_BLOCK_HEIGHT + TG_START_BLOCK_SPACING,
|
||||||
|
TG_START_COL2_WIDTH,
|
||||||
|
TG_START_BLOCK_HEIGHT,
|
||||||
|
player2_color);
|
||||||
|
switches_old = switches; //save switch state, to detect edge
|
||||||
|
}
|
||||||
|
|
||||||
|
if(abs( (int16_t) adc - adc_old ) > ADC_TOLERANCE || first) { //adc value changed quite a bit or we are in the first loop
|
||||||
|
uint8_t bar_width = (TG_START_COL2_WIDTH -2)*adc/ADC_MAX;
|
||||||
|
game_speed = (ADC_MAX-adc)*(SPEED_SLOW-SPEED_FAST)/ADC_MAX + SPEED_FAST;
|
||||||
|
|
||||||
|
|
||||||
|
//Draw background part of bar
|
||||||
|
LCD_DrawRectF(TG_START_COL2_X + 1 + bar_width,
|
||||||
|
TG_START_Y + PLAYER_COUNT*(TG_START_BLOCK_HEIGHT+TG_START_BLOCK_SPACING) + 1,
|
||||||
|
TG_START_COL2_WIDTH - 2 - bar_width,
|
||||||
|
TG_START_BLOCK_HEIGHT-2,
|
||||||
|
GUI_COLOR_BLACK);
|
||||||
|
//Draw foreground part of bar
|
||||||
LCD_DrawRectF( TG_START_COL2_X + 1,
|
LCD_DrawRectF( TG_START_COL2_X + 1,
|
||||||
TG_START_Y + PLAYER_COUNT*(TG_START_BLOCK_HEIGHT+TG_START_BLOCK_SPACING) + 1,
|
TG_START_Y + PLAYER_COUNT*(TG_START_BLOCK_HEIGHT+TG_START_BLOCK_SPACING) + 1,
|
||||||
bar_width,
|
bar_width,
|
||||||
TG_START_BLOCK_HEIGHT-2,
|
TG_START_BLOCK_HEIGHT - 2,
|
||||||
GUI_COLOR_BLUE);
|
GUI_COLOR_BLUE);
|
||||||
|
|
||||||
|
//For debug purposes
|
||||||
|
/*static char buf[16]; // Text buffer
|
||||||
|
LCD_SetTextColor(GUI_COLOR_WHITE);
|
||||||
|
sprintf(buf, "speed: %02d", game_speed);
|
||||||
|
LCD_DisplayStringXY(TG_START_COL2_X + TG_START_COL2_WIDTH + TG_START_FONT_OFFSET_Y,
|
||||||
|
TG_START_Y + TG_START_FONT_OFFSET_Y + PLAYER_COUNT*(TG_START_BLOCK_HEIGHT+TG_START_BLOCK_SPACING) + 1,
|
||||||
|
buf );*/
|
||||||
|
|
||||||
|
|
||||||
|
adc_old = adc; //save old adc value, to detect change
|
||||||
}
|
}
|
||||||
|
|
||||||
//save "old" values, to detect changes later (above^^)
|
|
||||||
adc_old = adc;
|
|
||||||
switches_old = switches;
|
|
||||||
first = false; // we're no longer in the first loop
|
first = false; // we're no longer in the first loop
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -350,7 +374,7 @@ bool game_step_prestart(game_t* game) {
|
|||||||
player2_color,
|
player2_color,
|
||||||
left);
|
left);
|
||||||
|
|
||||||
|
game->ticks_per_pixel = game_speed;
|
||||||
game->state = running; // Switch the game state to running
|
game->state = running; // Switch the game state to running
|
||||||
game->time = 0; // Reset the game time
|
game->time = 0; // Reset the game time
|
||||||
|
|
||||||
|
|||||||
11
src/game.h
11
src/game.h
@@ -11,9 +11,8 @@
|
|||||||
#define PLAYER_WIDTH 0 // Don't change
|
#define PLAYER_WIDTH 0 // Don't change
|
||||||
|
|
||||||
// Speed definitions
|
// Speed definitions
|
||||||
#define SPEED_SLOW 10
|
#define SPEED_SLOW 75
|
||||||
#define SPEED_FAST 1
|
#define SPEED_FAST 5
|
||||||
#define SPEED_DEFAULT (SPEED_FAST)
|
|
||||||
|
|
||||||
// Button definitions
|
// Button definitions
|
||||||
#define BTN_START 0
|
#define BTN_START 0
|
||||||
@@ -54,7 +53,7 @@ typedef struct game_s{
|
|||||||
uint16_t time; // Seconds since game start
|
uint16_t time; // Seconds since game start
|
||||||
uint16_t ticks_per_sec; // Number of game ticks per second
|
uint16_t ticks_per_sec; // Number of game ticks per second
|
||||||
int8_t winner_id; // Player who won the previous round
|
int8_t winner_id; // Player who won the previous round
|
||||||
uint8_t ticks_per_pixel; // Number of pixels you a player moves per tick
|
uint8_t ticks_per_pixel; // Ticks that are needed to move the player by one pixel
|
||||||
player_t player[PLAYER_COUNT];
|
player_t player[PLAYER_COUNT];
|
||||||
|
|
||||||
enum{ // Current state of the game
|
enum{ // Current state of the game
|
||||||
@@ -64,8 +63,8 @@ typedef struct game_s{
|
|||||||
} state;
|
} state;
|
||||||
|
|
||||||
//private section ahead
|
//private section ahead
|
||||||
uint8_t ticks_leftover; // Ticks left to complete a second
|
uint16_t ticks_leftover; // Ticks which were not used in the last round, to advance the pixels
|
||||||
uint8_t ticks_sum_sec; // Used to calculate the game time
|
uint16_t ticks_sum_sec; // Ticks which were not used in the last round, to advance the seconds
|
||||||
} game_t;
|
} game_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
14
src/io.c
14
src/io.c
@@ -27,6 +27,10 @@ static volatile uint8_t edg = 0;
|
|||||||
static volatile unsigned char* LED = (volatile unsigned char*)0x6C000200;
|
static volatile unsigned char* LED = (volatile unsigned char*)0x6C000200;
|
||||||
static volatile unsigned char* SWITCH = (volatile unsigned char*)0x6C000400;
|
static volatile unsigned char* SWITCH = (volatile unsigned char*)0x6C000400;
|
||||||
|
|
||||||
|
#define SQR3_SQ_SET ((uint32_t)0x0000001F)
|
||||||
|
#define SMPR1_SMP_SET ((uint32_t)0x00000007)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void pin_create(pin_t* pin, GPIO_TypeDef* GPIO, uint8_t pinnr, bool input) {
|
void pin_create(pin_t* pin, GPIO_TypeDef* GPIO, uint8_t pinnr, bool input) {
|
||||||
GPIO_InitTypeDef gi; // Create gpio init structure
|
GPIO_InitTypeDef gi; // Create gpio init structure
|
||||||
@@ -80,7 +84,7 @@ void io_init(void){
|
|||||||
pin_create(&pin_t3, GPIOI, 0, true); // create pin_t3
|
pin_create(&pin_t3, GPIOI, 0, true); // create pin_t3
|
||||||
|
|
||||||
// ADC Init
|
// ADC Init
|
||||||
/*
|
|
||||||
//Enable the peripheral clock of GPIOB
|
//Enable the peripheral clock of GPIOB
|
||||||
//This has been already done in the startup code
|
//This has been already done in the startup code
|
||||||
//Page 239/1718 of "RM0090 Reference Reference Manual (October 2014)"
|
//Page 239/1718 of "RM0090 Reference Reference Manual (October 2014)"
|
||||||
@@ -110,7 +114,7 @@ void io_init(void){
|
|||||||
// Set the ADON bit to enable the ADC
|
// Set the ADON bit to enable the ADC
|
||||||
// Page 418/1718 of "RM0090 Reference Reference Manual (October 2014)"
|
// Page 418/1718 of "RM0090 Reference Reference Manual (October 2014)"
|
||||||
ADC1->CR2 = ADC_CR2_ADON;
|
ADC1->CR2 = ADC_CR2_ADON;
|
||||||
*/
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,7 +141,7 @@ bool io_button_has_edge(uint8_t btnnumber) {
|
|||||||
|
|
||||||
uint16_t read_adc(){
|
uint16_t read_adc(){
|
||||||
uint16_t value = 0;
|
uint16_t value = 0;
|
||||||
/*
|
|
||||||
// Specify the sample time for the conversion
|
// Specify the sample time for the conversion
|
||||||
ADC1->SMPR2 &= SMPR1_SMP_SET << (3 * ADC_Channel_8);
|
ADC1->SMPR2 &= SMPR1_SMP_SET << (3 * ADC_Channel_8);
|
||||||
ADC1->SMPR2 |= ADC_SampleTime_15Cycles << (3 * ADC_Channel_8);
|
ADC1->SMPR2 |= ADC_SampleTime_15Cycles << (3 * ADC_Channel_8);
|
||||||
@@ -150,8 +154,8 @@ uint16_t read_adc(){
|
|||||||
while( (ADC1->SR & ADC_FLAG_EOC) == 0 );
|
while( (ADC1->SR & ADC_FLAG_EOC) == 0 );
|
||||||
// Read the value
|
// Read the value
|
||||||
value = ADC1->DR;
|
value = ADC1->DR;
|
||||||
value &= 0x03FF;
|
value &= ADC_MASK;
|
||||||
*/
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
5
src/io.h
5
src/io.h
@@ -34,6 +34,11 @@ bool io_button_has_edge(uint8_t btnnumber);
|
|||||||
uint16_t read_adc();
|
uint16_t read_adc();
|
||||||
|
|
||||||
|
|
||||||
|
#define ADC_MASK 0x3FF
|
||||||
|
#define ADC_MAX 0x3A0
|
||||||
|
#define ADC_TOLERANCE 0x08
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Read the values of the 8 switches
|
* @brief Read the values of the 8 switches
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ void SysTick_Handler() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#define SYSCLK 168e6
|
#define SYSCLK 168e6
|
||||||
#define TICKS_PER_SECOND 100
|
#define TICKS_PER_SECOND 1000
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user