Handle buttons & encoders in View, not Model

This commit is contained in:
2023-03-07 21:58:28 +01:00
parent 38d3d765ad
commit d26339e265
19 changed files with 257 additions and 165 deletions

View File

@ -0,0 +1,24 @@
#ifndef __INC_STW_BUTTON_CONTROLLER_HPP
#define __INC_STW_BUTTON_CONTROLLER_HPP
#include "platform/driver/button/ButtonController.hpp"
#include <stdint.h>
constexpr uint8_t KEY_BTN1 = 1;
constexpr uint8_t KEY_BTN2 = 2;
constexpr uint8_t KEY_BTN3 = 3;
constexpr uint8_t KEY_BTN4 = 4;
constexpr uint8_t KEY_BTN5 = 5;
constexpr uint8_t KEY_BTN6 = 6;
constexpr uint8_t KEY_ENC1_CW = 21;
constexpr uint8_t KEY_ENC1_CCW = 22;
constexpr uint8_t KEY_ENC2_CW = 23;
constexpr uint8_t KEY_ENC2_CCW = 24;
class STWButtonController : public touchgfx::ButtonController {
public:
virtual void init();
virtual bool sample(uint8_t &key);
};
#endif // __INC_STW_BUTTON_CONTROLLER_HPP

View File

@ -42,7 +42,7 @@ extern "C" {
/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */
extern volatile int ltdc_cb_triggered;
extern TX_QUEUE ui_queue;
extern TX_QUEUE gui_button_queue;
/* USER CODE END EC */
/* Exported macro ------------------------------------------------------------*/
@ -62,16 +62,12 @@ void Error_Handler(void);
/* Private defines -----------------------------------------------------------*/
#define ENC1A_Pin GPIO_PIN_3
#define ENC1A_GPIO_Port GPIOE
#define ENC1A_EXTI_IRQn EXTI3_IRQn
#define ENC1B_Pin GPIO_PIN_4
#define ENC1B_GPIO_Port GPIOE
#define ENC1B_EXTI_IRQn EXTI4_IRQn
#define ENC2A_Pin GPIO_PIN_5
#define ENC2A_GPIO_Port GPIOE
#define ENC2A_EXTI_IRQn EXTI9_5_IRQn
#define ENC2B_Pin GPIO_PIN_6
#define ENC2B_GPIO_Port GPIOE
#define ENC2B_EXTI_IRQn EXTI9_5_IRQn
#define BTN1_Pin GPIO_PIN_0
#define BTN1_GPIO_Port GPIOF
#define BTN2_Pin GPIO_PIN_1

View File

@ -12,12 +12,12 @@ extern "C" {
#define BUTTON_MIN_PRESS_TIME 50 // ms
#define ENC_MAX_PHASE 50 // ms
typedef enum { UMK_BTN_RELEASED, UMK_ENC_CW, UMK_ENC_CCW } UIMessageKind;
typedef enum { UMK_BTN_RELEASED, UMK_ENC_CW, UMK_ENC_CCW } ButtonMessageKind;
typedef struct {
UIMessageKind kind;
ButtonMessageKind kind;
int number;
} UIMessage;
} ButtonMessage;
void ui_thread_entry(ULONG _);

View File

@ -0,0 +1,36 @@
#include "STWButtonController.hpp"
#include "main.h"
#include "tx_api.h"
#include "ui.h"
void STWButtonController::init() {}
bool STWButtonController::sample(uint8_t &key) {
ButtonMessage msg;
if (tx_queue_receive(&gui_button_queue, &msg, TX_NO_WAIT) == TX_SUCCESS) {
switch (msg.kind) {
case UMK_BTN_RELEASED:
key = KEY_BTN1 + msg.number;
break;
case UMK_ENC_CW: {
if (msg.number == 0) {
key = KEY_ENC1_CW;
} else {
key = KEY_ENC2_CW;
}
break;
}
case UMK_ENC_CCW:
if (msg.number == 0) {
key = KEY_ENC1_CCW;
} else {
key = KEY_ENC2_CCW;
}
}
HAL_GPIO_TogglePin(STATUS2_GPIO_Port, STATUS2_Pin);
return true;
}
return false;
}

View File

@ -25,8 +25,8 @@ void ui_thread_entry(ULONG _) {
uint32_t now = HAL_GetTick();
if (state == GPIO_PIN_RESET && now - button_change_times[i]) {
// Button release event!
UIMessage msg = {.kind = UMK_BTN_RELEASED, .number = i};
tx_queue_send(&ui_queue, &msg, TX_NO_WAIT);
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;
@ -52,7 +52,7 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
uint16_t pin_a, pin_b;
int idx_a, idx_b;
UIMessage msg;
ButtonMessage msg;
if (GPIO_Pin == ENC1A_Pin || GPIO_Pin == ENC1B_Pin) {
pin_a = ENC1A_Pin;
pin_b = ENC1B_Pin;
@ -92,6 +92,6 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
// This shouldn't happen. Ignore this event.
return;
}
tx_queue_send(&ui_queue, &msg, TX_NO_WAIT);
tx_queue_send(&gui_button_queue, &msg, TX_NO_WAIT);
}
}