diff --git a/Core/Inc/ui.h b/Core/Inc/ui.h index 8caff04..8fa00e0 100644 --- a/Core/Inc/ui.h +++ b/Core/Inc/ui.h @@ -9,8 +9,8 @@ extern "C" { #define NUM_BUTTONS 6 #define NUM_ENCS 2 -#define BUTTON_MIN_PRESS_TIME 50 // ms -#define ENC_MAX_PHASE 50 // ms +#define BUTTON_MIN_INTERVAL 50 // ms +#define ENC_MAX_PHASE 50 // ms typedef enum { UMK_BTN_RELEASED, UMK_ENC_CW, UMK_ENC_CCW } ButtonMessageKind; diff --git a/Core/Inc/vehicle.h b/Core/Inc/vehicle.h index a5be70e..51aceae 100644 --- a/Core/Inc/vehicle.h +++ b/Core/Inc/vehicle.h @@ -5,6 +5,8 @@ #include "stw_defines.h" #include "tx_port.h" +#include "stm32h7xx_hal.h" + #ifdef __cplusplus extern "C" { #endif @@ -108,6 +110,7 @@ void vehicle_thread_entry(ULONG hfdcan_addr); void vehicle_select_mission(Mission mission); void vehicle_broadcast_param(ParamType param, int32_t value); +void vehicle_broadcast_buttons(GPIO_PinState *button_states); #ifdef __cplusplus } diff --git a/Core/Src/ui.c b/Core/Src/ui.c index 83a350f..068166b 100644 --- a/Core/Src/ui.c +++ b/Core/Src/ui.c @@ -7,6 +7,7 @@ #include "hx8357d.h" #include "main.h" +#include "vehicle.h" #include void ui_thread_entry(ULONG _) { @@ -16,22 +17,24 @@ void ui_thread_entry(ULONG _) { 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}; - uint32_t button_change_times[NUM_BUTTONS] = {HAL_GetTick()}; + uint32_t button_press_times[NUM_BUTTONS] = {HAL_GetTick()}; while (1) { 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(); - if (state == GPIO_PIN_RESET && now - button_change_times[i]) { - // Button release event! + if (state == GPIO_PIN_SET && + now - button_press_times[i] >= BUTTON_MIN_INTERVAL) { + // Button press event! + button_press_times[i] = now; ButtonMessage msg = {.kind = UMK_BTN_RELEASED, .number = i}; tx_queue_send(&gui_button_queue, &msg, TX_NO_WAIT); } - button_change_times[i] = now; button_states[i] = state; } } + vehicle_broadcast_buttons(button_states); // Release so other threads can get scheduled tx_thread_sleep(1); } diff --git a/Core/Src/vehicle.c b/Core/Src/vehicle.c index eded09d..a81a58d 100644 --- a/Core/Src/vehicle.c +++ b/Core/Src/vehicle.c @@ -13,6 +13,7 @@ #define CAN_ID_AMS_STATUS 0xA #define CAN_ID_MISSION_SELECTED 0x400 +#define CAN_ID_STW_BUTTONS 0x401 #define CAN_ID_STW_PARAM_SET 0x402 #define CAN_ID_AS_MISSION_FB 0x410 #define CAN_ID_STW_STATUS 0x412 @@ -51,6 +52,12 @@ void vehicle_broadcast_param(ParamType param, int32_t value) { ftcan_transmit(CAN_ID_STW_PARAM_SET, data, 5); } +void vehicle_broadcast_buttons(GPIO_PinState *button_states) { + uint8_t data = (button_states[0] << 2) | (button_states[1] << 0) | + (button_states[2] << 1) | (button_states[3] << 3); + ftcan_transmit(CAN_ID_STW_BUTTONS, &data, 1); +} + void ftcan_msg_received_cb(uint16_t id, size_t datalen, const uint8_t *data) { switch (id) { case CAN_ID_AMS_STATUS: