update buttons

This commit is contained in:
Leonard Gies 2025-04-19 13:38:51 +02:00
parent 0913466808
commit d4bb9ab825
Signed by: l.gies
GPG Key ID: 6F6FB9338EE44F71
3 changed files with 19 additions and 16 deletions

View File

@ -7,10 +7,12 @@
extern "C" {
#endif
#define NUM_BUTTONS 7
#define NUM_BUTTONS 9
#define NUM_ENCS 2
#define BUTTON_MIN_INTERVAL 50 // ms
#define ENC_MAX_PHASE 50 // ms
#define BUTTON_MIN_INTERVAL 50 // ms
#define ENC_MAX_PHASE 50 // ms
#define DRS_BUTTON_IDX (8)
typedef enum { UMK_BTN_PRESSED, UMK_ENC_CW, UMK_ENC_CCW } ButtonMessageKind;
@ -25,4 +27,4 @@ void ui_thread_entry(ULONG _);
}
#endif
#endif // __INC_ui_H
#endif // __INC_ui_H

View File

@ -11,16 +11,17 @@
#include "vehicle.h"
#include "vehicle_state.h"
#define DRS_BUTTON_IDX (6)
#define DRS_PRESS_WAIT_CYCLES (10)
static int drs_press_buf_cycles = 0;
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, SW_DRS_GPIO_Port};
GPIO_TypeDef *button_ports[NUM_BUTTONS] = {
BTN1_GPIO_Port, BTN2_GPIO_Port, BTN3_GPIO_Port,
BTN4_GPIO_Port, BTN5_GPIO_Port, BTN6_GPIO_Port,
BTN7_GPIO_Port, BTN8_GPIO_Port, SW_DRS_GPIO_Port};
uint16_t button_pins[NUM_BUTTONS] = {BTN1_Pin, BTN2_Pin, BTN3_Pin,
BTN4_Pin, BTN5_Pin, BTN6_Pin, SW_DRS_Pin};
BTN4_Pin, BTN5_Pin, BTN6_Pin,
BTN7_Pin, BTN8_Pin, SW_DRS_Pin};
GPIO_PinState button_states[NUM_BUTTONS] = {GPIO_PIN_RESET};
uint32_t button_press_times[NUM_BUTTONS] = {HAL_GetTick()};
@ -28,8 +29,11 @@ void ui_thread_entry(ULONG _) {
int press_event = -1;
for (int i = 0; i < NUM_BUTTONS; i++) {
GPIO_PinState state = HAL_GPIO_ReadPin(button_ports[i], button_pins[i]);
// if button state changed (newly pressed or released)
if (state != button_states[i]) {
uint32_t now = HAL_GetTick();
// if button is pressed and last press was at least BUTTON_MIN_INTERVAL
// ago
if (state == GPIO_PIN_SET &&
now - button_press_times[i] >= BUTTON_MIN_INTERVAL) {
// Button press event!
@ -41,11 +45,9 @@ void ui_thread_entry(ULONG _) {
button_states[i] = state;
}
}
// FIXME: The right button doesn't work :(
// if ((press_event == 1 || press_event == 2) && button_states[1] &&
// button_states[2]) {
if (press_event == 1 && button_states[1]) {
// if button 4 (left encoder) is pressed, change to next screen
if (press_event == 4 && button_states[4]) {
tx_event_flags_set(&gui_update_events, GUI_UPDATE_NEXT_SCREEN, TX_OR);
}

View File

@ -95,9 +95,8 @@ void vehicle_broadcast_param(ParamType param, int32_t value) {
}
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) |
(button_states[6] << 4);
uint8_t data = (button_states[DRS_BUTTON_IDX] << 0) |
(button_states[6] << 1) | (button_states[7] << 2);
ftcan_transmit(CAN_ID_STW_BUTTONS, &data, 1);
}