Handle Mission Select
This commit is contained in:
64
Core/Src/events.c
Normal file
64
Core/Src/events.c
Normal file
@ -0,0 +1,64 @@
|
||||
#include "events.h"
|
||||
#include "main.h"
|
||||
#include "user_inputs.h"
|
||||
#include <stdint.h>
|
||||
|
||||
static volatile EventQueue event_queue = {.read_idx = 0, .write_idx = 0};
|
||||
|
||||
void event_push(Event *ev) {
|
||||
if (event_num_free() == 0) {
|
||||
// TODO: How do we handle this?
|
||||
return;
|
||||
}
|
||||
|
||||
event_queue.ring_buffer[event_queue.write_idx] = *ev;
|
||||
event_queue.write_idx = (event_queue.write_idx + 1) % EVENT_QUEUE_SIZE;
|
||||
}
|
||||
|
||||
Event event_pop() {
|
||||
if (event_num_pending() == 0) {
|
||||
// TODO: How do we handle this?
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
Event result = event_queue.ring_buffer[event_queue.read_idx];
|
||||
event_queue.read_idx = (event_queue.read_idx + 1) % EVENT_QUEUE_SIZE;
|
||||
return result;
|
||||
}
|
||||
|
||||
uint16_t event_num_pending() {
|
||||
// Copy the values so we can be sure they don't change while we're computing
|
||||
// this
|
||||
uint16_t read_idx = event_queue.read_idx;
|
||||
uint16_t write_idx = event_queue.write_idx;
|
||||
if (write_idx >= read_idx) {
|
||||
return write_idx - read_idx;
|
||||
} else {
|
||||
return write_idx + (EVENT_QUEUE_SIZE - read_idx);
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t event_num_free() {
|
||||
// Copy the values so we can be sure they don't change while we're computing
|
||||
// this
|
||||
uint16_t read_idx = event_queue.read_idx;
|
||||
uint16_t write_idx = event_queue.write_idx;
|
||||
if (read_idx > write_idx) {
|
||||
return read_idx - write_idx;
|
||||
} else {
|
||||
return read_idx + (EVENT_QUEUE_SIZE - write_idx);
|
||||
}
|
||||
}
|
||||
|
||||
void handle_events() {
|
||||
while (event_num_pending() > 0) {
|
||||
Event ev = event_pop();
|
||||
switch (ev.type) {
|
||||
case EV_BTN_PRESS:
|
||||
handle_button_press(&ev.btn_press);
|
||||
break;
|
||||
default:
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -21,10 +21,12 @@
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "events.h"
|
||||
#include "rpi.h"
|
||||
#include "state.h"
|
||||
|
||||
#include "stm32g4xx_hal.h"
|
||||
#include "stm32g4xx_hal_gpio.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
@ -119,13 +121,7 @@ int main(void) {
|
||||
} else {
|
||||
HAL_GPIO_WritePin(DISP_RESET_GPIO_Port, DISP_RESET_Pin, GPIO_PIN_SET);
|
||||
}
|
||||
if (rpi_num_polls > 0) {
|
||||
if (rpi_active_since == 0) {
|
||||
rpi_active_since = now;
|
||||
} else if (now - rpi_active_since > 10000) {
|
||||
stw_state.view = VIEW_AMI;
|
||||
}
|
||||
}
|
||||
handle_events();
|
||||
}
|
||||
/* USER CODE END 3 */
|
||||
}
|
||||
@ -343,13 +339,21 @@ static void MX_GPIO_Init(void) {
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : BTN0_Pin BTN1_Pin BTN2_Pin BTN3_Pin
|
||||
ENC0A_Pin ENC0B_Pin ENC1A_Pin ENC1B_Pin */
|
||||
GPIO_InitStruct.Pin = BTN0_Pin | BTN1_Pin | BTN2_Pin | BTN3_Pin | ENC0A_Pin |
|
||||
ENC0B_Pin | ENC1A_Pin | ENC1B_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
/*Configure GPIO pins : BTN0_Pin BTN1_Pin BTN2_Pin BTN3_Pin */
|
||||
GPIO_InitStruct.Pin = BTN0_Pin | BTN1_Pin | BTN2_Pin | BTN3_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : ENC0A_Pin ENC0B_Pin ENC1A_Pin ENC1B_Pin */
|
||||
GPIO_InitStruct.Pin = ENC0A_Pin | ENC0B_Pin | ENC1A_Pin | ENC1B_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
@ -29,7 +29,19 @@ void rpi_init(I2C_HandleTypeDef *handle) {
|
||||
}
|
||||
}
|
||||
|
||||
void rpi_update_tx_buffer() { i2c_tx_buf[0] = stw_state.view; }
|
||||
void rpi_update_tx_buffer() {
|
||||
i2c_tx_buf[0] = stw_state.view;
|
||||
switch (stw_state.view) {
|
||||
case VIEW_MISSION_SELECT:
|
||||
rpi_update_tx_buffer_mission_select();
|
||||
break;
|
||||
default:
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
void rpi_update_tx_buffer_mission_select() {
|
||||
i2c_tx_buf[1] = stw_state.view_state.mission_select.selection;
|
||||
}
|
||||
|
||||
void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *handle) {
|
||||
switch (rpi_i2c_state) {
|
||||
|
||||
@ -2,4 +2,7 @@
|
||||
|
||||
STWState stw_state;
|
||||
|
||||
void state_init() { stw_state.view = VIEW_MISSION_SELECT; }
|
||||
void state_init() {
|
||||
stw_state.view = VIEW_MISSION_SELECT;
|
||||
stw_state.view_state.mission_select.selection = MISSION_ACCELERATION;
|
||||
}
|
||||
@ -212,6 +212,23 @@ void I2C2_EV_IRQHandler(void)
|
||||
/* USER CODE END I2C2_EV_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles EXTI line[15:10] interrupts.
|
||||
*/
|
||||
void EXTI15_10_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN EXTI15_10_IRQn 0 */
|
||||
|
||||
/* USER CODE END EXTI15_10_IRQn 0 */
|
||||
HAL_GPIO_EXTI_IRQHandler(BTN0_Pin);
|
||||
HAL_GPIO_EXTI_IRQHandler(BTN1_Pin);
|
||||
HAL_GPIO_EXTI_IRQHandler(BTN2_Pin);
|
||||
HAL_GPIO_EXTI_IRQHandler(BTN3_Pin);
|
||||
/* USER CODE BEGIN EXTI15_10_IRQn 1 */
|
||||
|
||||
/* USER CODE END EXTI15_10_IRQn 1 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
||||
|
||||
77
Core/Src/user_inputs.c
Normal file
77
Core/Src/user_inputs.c
Normal file
@ -0,0 +1,77 @@
|
||||
#include "user_inputs.h"
|
||||
|
||||
#include "events.h"
|
||||
#include "main.h"
|
||||
#include "state.h"
|
||||
#include "stm32g4xx_hal_gpio.h"
|
||||
|
||||
void HAL_GPIO_EXTI_Callback(uint16_t pin) {
|
||||
Event ev;
|
||||
|
||||
GPIO_PinState state = HAL_GPIO_ReadPin(GPIOB, pin);
|
||||
ev.type = state;
|
||||
switch (pin) {
|
||||
case BTN0_Pin:
|
||||
ev.type = EV_BTN_PRESS;
|
||||
ev.btn_press = BTN_PRESS_PREV;
|
||||
break;
|
||||
case BTN1_Pin:
|
||||
ev.type = EV_BTN_PRESS;
|
||||
ev.btn_press = BTN_PRESS_R2D;
|
||||
break;
|
||||
case BTN2_Pin:
|
||||
ev.type = EV_BTN_PRESS;
|
||||
ev.btn_press = BTN_PRESS_OK;
|
||||
break;
|
||||
case BTN3_Pin:
|
||||
ev.type = EV_BTN_PRESS;
|
||||
ev.btn_press = BTN_PRESS_NEXT;
|
||||
break;
|
||||
default:
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
event_push(&ev);
|
||||
}
|
||||
|
||||
void handle_button_press(const ButtonPressEvent *ev) {
|
||||
switch (stw_state.view) {
|
||||
case VIEW_MISSION_SELECT:
|
||||
handle_button_press_mission_select(ev);
|
||||
break;
|
||||
case VIEW_AMI:
|
||||
break;
|
||||
default:
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
void handle_button_press_mission_select(const ButtonPressEvent *ev) {
|
||||
switch (*ev) {
|
||||
case BTN_PRESS_PREV: {
|
||||
Mission *selection = &stw_state.view_state.mission_select.selection;
|
||||
if (*selection == MISSION_ACCELERATION || *selection == MISSION_NONE) {
|
||||
*selection = MISSION_MANUAL;
|
||||
} else {
|
||||
*selection = *selection - 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BTN_PRESS_R2D:
|
||||
break;
|
||||
case BTN_PRESS_OK:
|
||||
stw_state.view_state.ami.current_mission =
|
||||
stw_state.view_state.mission_select.selection;
|
||||
stw_state.view = VIEW_AMI;
|
||||
break;
|
||||
case BTN_PRESS_NEXT: {
|
||||
Mission *selection = &stw_state.view_state.mission_select.selection;
|
||||
if (*selection == MISSION_MANUAL) {
|
||||
*selection = MISSION_ACCELERATION;
|
||||
} else {
|
||||
*selection = *selection + 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user