Broadcast button presses to CAN

This commit is contained in:
Jasper Blanckenburg 2023-04-11 21:32:14 +02:00
parent 7065cb455a
commit 30607718ac
4 changed files with 19 additions and 6 deletions

View File

@ -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;

View File

@ -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
}

View File

@ -7,6 +7,7 @@
#include "hx8357d.h"
#include "main.h"
#include "vehicle.h"
#include <stdint.h>
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);
}

View File

@ -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: