diff --git a/src/game.c b/src/game.c index 56d5101..537085a 100644 --- a/src/game.c +++ b/src/game.c @@ -8,14 +8,17 @@ void game_init(game_t* game, uint16_t ticks_per_sec) { - //gpio init + // gpio init io_init(); + + // adc init + init_adc(); - //lcd init + // lcd init LCD_Init(); LCD_Clear(GUI_COLOR_BLACK); - //struct init + // struct init game->state=prestart; game->ticks_per_pixel = SPEED_DEFAULT; game->ticks_leftover = 0; @@ -214,9 +217,15 @@ bool game_step(game_t* game, uint64_t delta_time) { // Calculate the next game s case prestart: // If the game is in prestart state // Draw welcome screen LCD_DrawRectF(10,10,100,50,GUI_COLOR_BLUE); + + // TODO: Read color of player 1 + uint16_t player1_color = get_player_color(&(game->player[0]), true); + + // TODO: Read color of player 2 + uint16_t player2_color = get_player_color(&(game->player[1]), false); - // Wait on player to press start - while(!io_button_has_edge(BTN_START)); + // TODO: Read potentiometer and set game speed accordingly + uint16_t game_speed = read_adc(); // Setup the two players player_init(&(game->player[0]), // Player object to fill @@ -237,7 +246,10 @@ bool game_step(game_t* game, uint64_t delta_time) { // Calculate the next game s .y=(((TFT_HEIGHT - TFT_GAME_FIELD_TOP - TFT_GAME_FIELD_BOTTOM) / 2) + TFT_GAME_FIELD_TOP) // y start coordinate }, GUI_COLOR_RED, // color - left); // default moving direction + left); // default moving direction + + // Wait on player to press start + while(!io_button_has_edge(BTN_START)); game->state = running; // Switch the game state to running game->time = 0; // Reset the game time diff --git a/src/io.c b/src/io.c index 6a8f894..2c486a7 100644 --- a/src/io.c +++ b/src/io.c @@ -1,3 +1,5 @@ +#include +#include #include "io.h" // Local functions @@ -17,6 +19,14 @@ static pin_t pin_t0; static pin_t pin_t1; static pin_t pin_t2; static pin_t pin_t3; +static pin_t pin_s0; +static pin_t pin_s1; +static pin_t pin_s2; +static pin_t pin_s3; +static pin_t pin_s4; +static pin_t pin_s5; +static pin_t pin_s6; +static pin_t pin_s7; static uint8_t new = 0; static uint8_t old = 0; static volatile uint8_t edg = 0; @@ -66,11 +76,23 @@ void pin_toggle(pin_t* pin) { void io_init(void){ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOC,ENABLE); // Enable gpio clock source + // Create player pins pin_create(&pin_t0, GPIOC, 7, true); // create pin_t0 pin_create(&pin_t1, GPIOB, 15, true); // create pin_t1 pin_create(&pin_t2, GPIOB, 14, true); // create pin_t2 pin_create(&pin_t3, GPIOI, 0, true); // create pin_t3 + // TODO: Create color choosing pins + /*pin_create(&pin_s0, GPIO, , true); // create pin_t0 + pin_create(&pin_s1, GPIO, , true); // create pin_t1 + pin_create(&pin_s2, GPIO, , true); // create pin_t2 + pin_create(&pin_s3, GPIO, , true); // create pin_t3 + pin_create(&pin_s4, GPIO, , true); // create pin_t0 + pin_create(&pin_s5, GPIO, , true); // create pin_t1 + pin_create(&pin_s6, GPIO, , true); // create pin_t2 + pin_create(&pin_s7, GPIO, , true); // create pin_t3*/ + + } void io_process(void) { @@ -93,3 +115,67 @@ bool io_button_has_edge(uint8_t btnnumber) { return status; } + +void init_adc(){ + /* + //Enable the peripheral clock of GPIOB + //This has been already done in the startup code + //Page 239/1718 of "RM0090 Reference Reference Manual (October 2014)" + RCC->AHB1ENR |= RCC_AHB1Periph_GPIOB; + //Choose the working mode of PB0 with the GPIO port mode register + //Page 279/1718 of "RM0090 Reference Reference Manual (October 2014)" + GPIOB->MODER &= ~GPIO_MODER_MODER0; + GPIOB->MODER |= GPIO_Mode_AN; + //Configure the GPIO port pull-up/pull-down register for PB0 + //Page 282/1718 of "RM0090 Reference Reference Manual (October 2014)" + GPIOB->PUPDR &= ~GPIO_PUPDR_PUPDR0; + GPIOB->PUPDR |= GPIO_PuPd_UP; + + //Initialize the ADC + //Enable the peripheral clock of the ADC + //Page 245/1718 of "RM0090 Reference Reference Manual (October 2014)" + RCC->APB2ENR |= RCC_APB2Periph_ADC; + //Configure ADC1: scan conversion mode and resolution + //Set SCAN bit according to ADC_ScanConvMode value + //Set RES bit according to ADC_Resolution value + // Page 416/1718 of "RM0090 Reference Reference Manual (October 2014)" + ADC1->CR1 = ADC_Resolution_10b; + // Configure ADC1: regular channel sequence length + // Set L bits according to ADC_NbrOfConversion value + // Page 422/1718 of "RM0090 Reference Reference Manual (October 2014)" + ADC1->SQR1 = 0; + // Set the ADON bit to enable the ADC + // Page 418/1718 of "RM0090 Reference Reference Manual (October 2014)" + ADC1->CR2 = ADC_CR2_ADON; + */ +} + +uint16_t read_adc(){ + uint16_t value = 0; + /* + // Specify the sample time for the conversion + ADC1->SMPR2 &= SMPR1_SMP_SET << (3 * ADC_Channel_8); + ADC1->SMPR2 |= ADC_SampleTime_15Cycles << (3 * ADC_Channel_8); + // Set the channel 8 as the first conversion in the ADC reg seq register + ADC1->SQR3 &= ~SQR3_SQ_SET; + ADC1->SQR3 |= ADC_Channel_8; + // Start the conversion + ADC1->CR2 |= ADC_CR2_SWSTART; + // Wait until the conversion has been done + while( (ADC1->SR & ADC_FLAG_EOC) == 0 ); + // Read the value + value = ADC1->DR; + value &= 0x03FF; + */ + return value; +} + +uint16_t get_player_color(player_t* player, bool first_player){ + if(!first_player){ + // Read bit 0-3 and calculate color + }else{ + // Read but 4-7 and calculate color + } + + return 0; +} diff --git a/src/io.h b/src/io.h index 733ddaf..19397f6 100644 --- a/src/io.h +++ b/src/io.h @@ -4,6 +4,8 @@ #include #include +#include "player.h" + /** * @brief Initialize all used GPIOs and initialize their clock source. @@ -24,4 +26,27 @@ void io_process(void); */ bool io_button_has_edge(uint8_t btnnumber); +/** + * @brief Initialize the analog/digital converter in order to read the potentiometer. + */ +void init_adc(); + +/** + * @brief Read a value from the analog/digital converter. + * + * @return ADC_value as an unsigned int16 + */ +uint16_t read_adc(); + +/** + * @brief Uses buttons s0-7 to determine the coler of the player. + * Player1 is represented by the bits s0-3. + * Player2 is represented by the bits s4-s7. + * + * @param player Playerobject + * @param first_player Specify first or second player + * @return Color value for the according player + */ +uint16_t get_player_color(player_t* player, bool first_player); + #endif /* IO_H */