2023-03-07 20:13:28 +01:00
|
|
|
#include "ui.h"
|
|
|
|
|
|
|
|
#include "stm32h7a3xx.h"
|
|
|
|
#include "stm32h7xx_hal.h"
|
|
|
|
#include "stm32h7xx_hal_gpio.h"
|
|
|
|
#include "tx_api.h"
|
|
|
|
|
|
|
|
#include "hx8357d.h"
|
|
|
|
#include "main.h"
|
2023-04-11 21:32:14 +02:00
|
|
|
#include "vehicle.h"
|
2023-03-07 20:13:28 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
void ui_thread_entry(ULONG _) {
|
|
|
|
GPIO_TypeDef *button_ports[NUM_BUTTONS] = {BTN1_GPIO_Port, BTN2_GPIO_Port,
|
|
|
|
BTN3_GPIO_Port, BTN4_GPIO_Port,
|
|
|
|
BTN5_GPIO_Port, BTN6_GPIO_Port};
|
|
|
|
uint16_t button_pins[NUM_BUTTONS] = {BTN1_Pin, BTN2_Pin, BTN3_Pin,
|
|
|
|
BTN4_Pin, BTN5_Pin, BTN6_Pin};
|
|
|
|
GPIO_PinState button_states[NUM_BUTTONS] = {GPIO_PIN_RESET};
|
2023-04-11 21:32:14 +02:00
|
|
|
uint32_t button_press_times[NUM_BUTTONS] = {HAL_GetTick()};
|
2023-03-07 20:13:28 +01:00
|
|
|
|
|
|
|
while (1) {
|
2023-04-11 21:53:36 +02:00
|
|
|
int press_event = -1;
|
2023-03-07 20:13:28 +01:00
|
|
|
for (int i = 0; i < NUM_BUTTONS; i++) {
|
|
|
|
GPIO_PinState state = HAL_GPIO_ReadPin(button_ports[i], button_pins[i]);
|
|
|
|
if (state != button_states[i]) {
|
|
|
|
uint32_t now = HAL_GetTick();
|
2023-04-11 21:32:14 +02:00
|
|
|
if (state == GPIO_PIN_SET &&
|
|
|
|
now - button_press_times[i] >= BUTTON_MIN_INTERVAL) {
|
|
|
|
// Button press event!
|
2023-04-11 21:53:36 +02:00
|
|
|
press_event = i;
|
2023-04-11 21:32:14 +02:00
|
|
|
button_press_times[i] = now;
|
2023-04-11 21:53:36 +02:00
|
|
|
ButtonMessage msg = {.kind = UMK_BTN_PRESSED, .number = i};
|
2023-03-07 21:58:28 +01:00
|
|
|
tx_queue_send(&gui_button_queue, &msg, TX_NO_WAIT);
|
2023-03-07 20:13:28 +01:00
|
|
|
}
|
|
|
|
button_states[i] = state;
|
|
|
|
}
|
|
|
|
}
|
2023-04-11 21:53:36 +02:00
|
|
|
if ((press_event == 1 || press_event == 2) && button_states[1] &&
|
|
|
|
button_states[2]) {
|
|
|
|
tx_event_flags_set(&gui_update_events, GUI_UPDATE_NEXT_SCREEN, TX_OR);
|
|
|
|
}
|
2023-04-11 21:32:14 +02:00
|
|
|
vehicle_broadcast_buttons(button_states);
|
2023-03-07 20:13:28 +01:00
|
|
|
// Release so other threads can get scheduled
|
|
|
|
tx_thread_sleep(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
|
|
|
|
// This gets called when an edge on one of the encoder pins is detected.
|
|
|
|
static GPIO_PinState last_state[NUM_ENCS * 2] = {GPIO_PIN_RESET};
|
|
|
|
|
|
|
|
if (GPIO_Pin == ENC2B_Pin) {
|
|
|
|
// ENC2A and ENC2B share an interrupt line, so the HAL calls this callback
|
|
|
|
// once with each pin. Since we already handled the interrupt once for
|
|
|
|
// ENC2A, we can just ignore it now.
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t pin_a, pin_b;
|
|
|
|
int idx_a, idx_b;
|
2023-03-07 21:58:28 +01:00
|
|
|
ButtonMessage msg;
|
2023-03-07 20:13:28 +01:00
|
|
|
if (GPIO_Pin == ENC1A_Pin || GPIO_Pin == ENC1B_Pin) {
|
|
|
|
pin_a = ENC1A_Pin;
|
|
|
|
pin_b = ENC1B_Pin;
|
|
|
|
idx_a = 0;
|
|
|
|
idx_b = 1;
|
|
|
|
msg.number = 0;
|
|
|
|
} else {
|
|
|
|
pin_a = ENC2A_Pin;
|
|
|
|
pin_b = ENC2B_Pin;
|
|
|
|
idx_a = 2;
|
|
|
|
idx_b = 3;
|
|
|
|
msg.number = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All encoder pins are on port E
|
|
|
|
GPIO_PinState state_a = HAL_GPIO_ReadPin(GPIOE, pin_a);
|
|
|
|
GPIO_PinState state_b = HAL_GPIO_ReadPin(GPIOE, pin_b);
|
|
|
|
int a_changed = state_a != last_state[idx_a];
|
|
|
|
int b_changed = state_b != last_state[idx_b];
|
|
|
|
last_state[idx_a] = state_a;
|
|
|
|
last_state[idx_b] = state_b;
|
|
|
|
|
2023-03-17 14:30:56 +01:00
|
|
|
if (state_a == GPIO_PIN_SET && state_b == GPIO_PIN_SET) {
|
|
|
|
// Second rising edge, direction depends on which pin changed last
|
2023-03-07 20:13:28 +01:00
|
|
|
if (a_changed && b_changed) {
|
|
|
|
// This shouldn't happen. Ignore this event.
|
|
|
|
return;
|
|
|
|
} else if (a_changed) {
|
|
|
|
msg.kind = UMK_ENC_CCW;
|
|
|
|
} else if (b_changed) {
|
|
|
|
msg.kind = UMK_ENC_CW;
|
|
|
|
} else {
|
|
|
|
// This shouldn't happen. Ignore this event.
|
|
|
|
return;
|
|
|
|
}
|
2023-03-07 21:58:28 +01:00
|
|
|
tx_queue_send(&gui_button_queue, &msg, TX_NO_WAIT);
|
2023-03-07 20:13:28 +01:00
|
|
|
}
|
|
|
|
}
|