Compare commits

...

2 Commits

Author SHA1 Message Date
c82c9adf32 cleanup expander 2026-04-01 15:55:23 -05:00
1b4795b4de Add inputs controller 2026-04-01 13:29:40 -05:00
2 changed files with 283 additions and 44 deletions

View File

@ -31,6 +31,24 @@ const static uint8_t REG_HALL_SENSITIVITY = 0x31;
const static uint8_t REG_CLOSE_SENSITIVITY = 0x32;
const static uint8_t REG_SWITCH_TOUCH_EVENT = 0x33;
/// The global data for the expander peripheral.
class ExpanderPeripheral {
// TODO: change these to private
// or even make this class hidden
public:
SemaphoreHandle_t state_mutex;
ExpanderState state;
// channels
QueueHandle_t button_press_events;
QueueHandle_t button_release_events;
QueueHandle_t switch_flip_events;
QueueHandle_t switch_touch_events;
QueueHandle_t touch_events;
QueueHandle_t keypad_press_events;
QueueHandle_t keypad_release_events;
};
ExpanderPeripheral expander_peripheral_singleton;
// forward declarations
@ -139,8 +157,6 @@ static void expander_task(void *arg) {
// Wait for interrupt notification (signal is sent when INT falls)
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
// loop continues and will process events once INT goes low
printf("WAKE\n");
}
}
@ -204,9 +220,7 @@ static void handle_button_switch_event(uint8_t event) {
Switch sw = static_cast<Switch>(number);
SwitchFlip sw_flip = SwitchFlip(sw, pressed);
if (xQueueSendToBack(expander_peripheral_singleton.switch_flip_events, &sw_flip, 0) != pdTRUE) {
ESP_LOGE(TAG, "Failed to send switch flip event!");
}
xQueueSendToBack(expander_peripheral_singleton.switch_flip_events, &sw_flip, 0);
xSemaphoreTake(expander_peripheral_singleton.state_mutex, portMAX_DELAY);
if (pressed) {
// set
@ -220,16 +234,12 @@ static void handle_button_switch_event(uint8_t event) {
// button
Button button = static_cast<Button>(number);
if (pressed) {
if (xQueueSendToBack(expander_peripheral_singleton.button_press_events, &button, 0) != pdTRUE) {
ESP_LOGE(TAG, "Failed to send button press event!");
}
xQueueSendToBack(expander_peripheral_singleton.button_press_events, &button, 0);
xSemaphoreTake(expander_peripheral_singleton.state_mutex, portMAX_DELAY);
expander_peripheral_singleton.state.button_state |= 1 << number;
xSemaphoreGive(expander_peripheral_singleton.state_mutex);
} else {
if (xQueueSendToBack(expander_peripheral_singleton.button_release_events, &button, 0) != pdTRUE) {
ESP_LOGE(TAG, "Failed to send button release event!");
}
xQueueSendToBack(expander_peripheral_singleton.button_release_events, &button, 0);
xSemaphoreTake(expander_peripheral_singleton.state_mutex, portMAX_DELAY);
expander_peripheral_singleton.state.button_state &= ~(1 << number);
xSemaphoreGive(expander_peripheral_singleton.state_mutex);
@ -252,16 +262,12 @@ static void handle_keypad_event(uint8_t event) {
// }
if (pressed) {
if (xQueueSendToBack(expander_peripheral_singleton.keypad_press_events, &key, 0) != pdTRUE) {
ESP_LOGE(TAG, "Failed to send keypad press event!");
}
xQueueSendToBack(expander_peripheral_singleton.keypad_press_events, &key, 0);
xSemaphoreTake(expander_peripheral_singleton.state_mutex, portMAX_DELAY);
expander_peripheral_singleton.state.keypad_state |= 1 << number;
xSemaphoreGive(expander_peripheral_singleton.state_mutex);
} else {
if (xQueueSendToBack(expander_peripheral_singleton.keypad_release_events, &key, 0) != pdTRUE) {
ESP_LOGE(TAG, "Failed to send keypad release event!");
}
xQueueSendToBack(expander_peripheral_singleton.keypad_release_events, &key, 0);
xSemaphoreTake(expander_peripheral_singleton.state_mutex, portMAX_DELAY);
expander_peripheral_singleton.state.keypad_state &= ~(1 << number);
xSemaphoreGive(expander_peripheral_singleton.state_mutex);
@ -278,15 +284,11 @@ static void handle_touch_event(uint8_t event) {
if ((sensor & FINGERPRINT_BIT) != 0) {
TouchedReleased touch_state = static_cast<TouchedReleased>(touched);
if (xQueueSendToBack(expander_peripheral_singleton.touch_events, &touch_state, 0) != pdTRUE) {
ESP_LOGE(TAG, "Failed to send touch event!");
}
xQueueSendToBack(expander_peripheral_singleton.touch_events, &touch_state, 0);
} else {
Switch sw = static_cast<Switch>(sensor);
SwitchTouch sw_touch = SwitchTouch(sw, touched);
if (xQueueSendToBack(expander_peripheral_singleton.switch_touch_events, &sw_touch, 0) != pdTRUE) {
ESP_LOGE(TAG, "Failed to send switch touch event!");
}
xQueueSendToBack(expander_peripheral_singleton.switch_touch_events, &sw_touch, 0);
}
xSemaphoreTake(expander_peripheral_singleton.state_mutex, portMAX_DELAY);
@ -308,3 +310,181 @@ static void handle_close_hal_event(uint8_t event) {
(void)event;
}
// InputsController implementations
/// Clears all events waiting in the queues.
void InputsController::clear_all_events() {
xQueueReset(expander_peripheral_singleton.button_press_events);
xQueueReset(expander_peripheral_singleton.button_release_events);
xQueueReset(expander_peripheral_singleton.switch_flip_events);
xQueueReset(expander_peripheral_singleton.switch_touch_events);
xQueueReset(expander_peripheral_singleton.touch_events);
xQueueReset(expander_peripheral_singleton.keypad_press_events);
xQueueReset(expander_peripheral_singleton.keypad_release_events);
}
ExpanderState InputsController::get_input_state() {
xSemaphoreTake(expander_peripheral_singleton.state_mutex, portMAX_DELAY);
ExpanderState state_copy = expander_peripheral_singleton.state;
xSemaphoreGive(expander_peripheral_singleton.state_mutex);
return state_copy;
}
/// Returns `true` iff there is a button press event waiting.
bool InputsController::has_button_press() {
return uxQueueMessagesWaiting(expander_peripheral_singleton.button_press_events) > 0;
}
/// Gets the next button press event (if any).
std::optional<Button> InputsController::get_button_press() {
Button b;
if (xQueueReceive(expander_peripheral_singleton.button_press_events, &b, 0) == pdTRUE) {
return b;
}
return std::nullopt;
}
/// Gets the next button press event, waiting if neccesary.
Button InputsController::wait_button_press() {
Button b;
xQueueReceive(expander_peripheral_singleton.button_press_events, &b, portMAX_DELAY);
return b;
}
/// Gets the current state of the buttons.
uint8_t InputsController::button_state() {
xSemaphoreTake(expander_peripheral_singleton.state_mutex, portMAX_DELAY);
uint8_t value = expander_peripheral_singleton.state.button_state;
xSemaphoreGive(expander_peripheral_singleton.state_mutex);
return value;
}
/// Returns `true` iff there is a button release event waiting.
bool InputsController::has_button_release() {
return uxQueueMessagesWaiting(expander_peripheral_singleton.button_release_events) > 0;
}
/// Gets the next button release event (if any).
std::optional<Button> InputsController::get_button_release() {
Button b;
if (xQueueReceive(expander_peripheral_singleton.button_release_events, &b, 0) == pdTRUE) {
return b;
}
return std::nullopt;
}
/// Gets the next button release event, waiting if neccesary.
Button InputsController::wait_button_release() {
Button b;
xQueueReceive(expander_peripheral_singleton.button_release_events, &b, portMAX_DELAY);
return b;
}
/// Returns `true` iff there is a switch flip event waiting.
bool InputsController::has_switch_flip() {
return uxQueueMessagesWaiting(expander_peripheral_singleton.switch_flip_events) > 0;
}
/// Gets the next switch flip event (if any).
std::optional<SwitchFlip> InputsController::get_switch_flip() {
SwitchFlip s;
if (xQueueReceive(expander_peripheral_singleton.switch_flip_events, &s, 0) == pdTRUE) {
return s;
}
return std::nullopt;
}
/// Gets the next switch flip event, waiting if neccesary.
SwitchFlip InputsController::wait_switch_flip() {
SwitchFlip s;
xQueueReceive(expander_peripheral_singleton.switch_flip_events, &s, portMAX_DELAY);
return s;
}
/// Gets the current state of the switches.
uint8_t InputsController::switch_state() {
xSemaphoreTake(expander_peripheral_singleton.state_mutex, portMAX_DELAY);
uint8_t value = expander_peripheral_singleton.state.switch_state;
xSemaphoreGive(expander_peripheral_singleton.state_mutex);
return value;
}
/// Returns `true` iff there is a switch touch event waiting.
bool InputsController::has_switch_touch() {
return uxQueueMessagesWaiting(expander_peripheral_singleton.switch_touch_events) > 0;
}
/// Gets the next switch touch event (if any).
std::optional<SwitchTouch> InputsController::get_switch_touch() {
SwitchTouch s;
if (xQueueReceive(expander_peripheral_singleton.switch_touch_events, &s, 0) == pdTRUE) {
return s;
}
return std::nullopt;
}
/// Gets the next switch touch event, waiting if neccesary.
SwitchTouch InputsController::wait_switch_touch() {
SwitchTouch s;
xQueueReceive(expander_peripheral_singleton.switch_touch_events, &s, portMAX_DELAY);
return s;
}
/// Gets the current state of the touch sensors.
uint8_t InputsController::switch_touch_state() {
xSemaphoreTake(expander_peripheral_singleton.state_mutex, portMAX_DELAY);
uint8_t value = expander_peripheral_singleton.state.touch_state;
xSemaphoreGive(expander_peripheral_singleton.state_mutex);
return value;
}
/// Returns `true` iff there is a keypad press event waiting.
bool InputsController::has_keypad_press() {
return uxQueueMessagesWaiting(expander_peripheral_singleton.keypad_press_events) > 0;
}
/// Gets the next keypad press event (if any).
std::optional<KeypadKey> InputsController::get_keypad_press() {
KeypadKey k;
if (xQueueReceive(expander_peripheral_singleton.keypad_press_events, &k, 0) == pdTRUE) {
return k;
}
return std::nullopt;
}
/// Gets the next keypad press event, waiting if neccesary.
KeypadKey InputsController::wait_keypad_press() {
KeypadKey k;
xQueueReceive(expander_peripheral_singleton.keypad_press_events, &k, portMAX_DELAY);
return k;
}
/// Returns `true` iff there is a keypad release event waiting.
bool InputsController::has_keypad_release() {
return uxQueueMessagesWaiting(expander_peripheral_singleton.keypad_release_events) > 0;
}
/// Gets the next keypad release event (if any).
std::optional<KeypadKey> InputsController::get_keypad_release() {
KeypadKey k;
if (xQueueReceive(expander_peripheral_singleton.keypad_release_events, &k, 0) == pdTRUE) {
return k;
}
return std::nullopt;
}
/// Gets the next keypad release event, waiting if neccesary.
KeypadKey InputsController::wait_keypad_release() {
KeypadKey k;
xQueueReceive(expander_peripheral_singleton.keypad_release_events, &k, portMAX_DELAY);
return k;
}
/// Gets the current state of the keypad.
uint16_t InputsController::keypad_state() {
xSemaphoreTake(expander_peripheral_singleton.state_mutex, portMAX_DELAY);
uint16_t value = expander_peripheral_singleton.state.keypad_state;
xSemaphoreGive(expander_peripheral_singleton.state_mutex);
return value;
}

View File

@ -3,7 +3,9 @@
#include "blk_box_drivers/i2c.h"
#include <freertos/FreeRTOS.h>
#include <freertos/queue.h>
#include <freertos/semphr.h>
#include <optional>
#define EXPANDER_I2C_ADDR (0x7E)
#define EXPANDER_I2C_SPEED (400000)
@ -30,6 +32,9 @@ enum class Button: uint8_t {
BLUE = 3,
};
constexpr uint8_t raw_value(Button v) { return static_cast<uint8_t>(v); }
constexpr Button button_from_raw(uint8_t raw) { return static_cast<Button>(raw & 0b11); }
/// The four switches on the bottom half.
enum class Switch: uint8_t {
S1 = 0,
@ -38,11 +43,17 @@ enum class Switch: uint8_t {
S4 = 3,
};
constexpr uint8_t raw_value(Switch v) { return static_cast<uint8_t>(v); }
constexpr Switch switch_from_raw(uint8_t raw) { return static_cast<Switch>(raw & 0b11); }
enum class TouchedReleased: uint8_t {
Released = 0,
Touched = 1,
};
constexpr uint8_t raw_value(TouchedReleased v) { return static_cast<uint8_t>(v); }
constexpr TouchedReleased touched_released_from_raw(uint8_t raw) { return static_cast<TouchedReleased>(raw & 0b1); }
/// One of the keys on the keypad.
enum class KeypadKey: uint8_t {
K0 = 0,
@ -63,26 +74,44 @@ enum class KeypadKey: uint8_t {
POUND = 15,
};
constexpr uint8_t raw_value(KeypadKey v) { return static_cast<uint8_t>(v); }
constexpr KeypadKey keypad_key_from_raw(uint8_t raw) { return static_cast<KeypadKey>(raw & 0b1111); }
constexpr char keypad_key_to_char(KeypadKey key) {
static constexpr char lookup[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', '*', '#'
};
return lookup[static_cast<uint8_t>(key) & 0b1111];
}
struct SwitchFlip {
private:
// [bit2: pressed] [bit1-0: switch]
// [bit2: up] [bit1-0: switch]
uint8_t data;
public:
// Constructor
SwitchFlip(Switch sw, bool pressed)
SwitchFlip(Switch sw, bool up)
: data((static_cast<uint8_t>(sw) & 0b11) |
((pressed ? 1 : 0) << 2)) {}
((up ? 1 : 0) << 2)) {}
// Default constructor
SwitchFlip() : data(0) {}
// Raw value constructor
explicit SwitchFlip(uint8_t raw_data) : data(raw_data) {}
// Raw value getter
uint8_t raw() const { return data; }
// Getters
Switch get_switch() const {
return static_cast<Switch>(data & 0b11);
}
bool is_pressed() const {
bool is_up() const {
return (data >> 2) & 1;
}
@ -91,8 +120,8 @@ public:
data = (data & ~0b11) | (static_cast<uint8_t>(sw) & 0b11);
}
void set_pressed(bool pressed) {
data = (data & ~(1 << 2)) | ((pressed ? 1 : 0) << 2);
void set_up(bool up) {
data = (data & ~(1 << 2)) | ((up ? 1 : 0) << 2);
}
};
static_assert(sizeof(SwitchFlip) == 1);
@ -111,6 +140,12 @@ public:
// Default constructor
SwitchTouch() : data(0) {}
// Raw value constructor
explicit SwitchTouch(uint8_t raw_data) : data(raw_data) {}
// Raw value getter
uint8_t raw() const { return data; }
// Getters
Switch get_switch() const {
return static_cast<Switch>(data & 0b11);
@ -144,6 +179,12 @@ public:
ButtonOrSwitch() : data(0) {}
// Raw value constructor
explicit ButtonOrSwitch(uint8_t raw_data) : data(raw_data) {}
// Raw value getter
uint8_t raw() const { return data; }
// Getters
uint8_t number() const {
return data & 0b11;
@ -195,23 +236,41 @@ struct ExpanderState {
ExpanderState() : touch_state(0), button_state(0), switch_state(0), keypad_state(0), hal_sense(0), close_hal_sense(0), hal(0), close_hal(0), rfid_state(0) {}
};
/// The global data for the expander peripheral.
class ExpanderPeripheral {
// TODO: change these to private
public:
SemaphoreHandle_t state_mutex;
ExpanderState state;
// channels
QueueHandle_t button_press_events;
QueueHandle_t button_release_events;
QueueHandle_t switch_flip_events;
QueueHandle_t switch_touch_events;
QueueHandle_t touch_events;
QueueHandle_t keypad_press_events;
QueueHandle_t keypad_release_events;
class InputsController {
public:
static void clear_all_events();
static ExpanderState get_input_state();
static bool has_button_press();
static std::optional<Button> get_button_press();
static Button wait_button_press();
static uint8_t button_state();
static bool has_button_release();
static std::optional<Button> get_button_release();
static Button wait_button_release();
static bool has_switch_flip();
static std::optional<SwitchFlip> get_switch_flip();
static SwitchFlip wait_switch_flip();
static uint8_t switch_state();
static bool has_switch_touch();
static std::optional<SwitchTouch> get_switch_touch();
static SwitchTouch wait_switch_touch();
static uint8_t switch_touch_state();
static bool has_keypad_press();
static std::optional<KeypadKey> get_keypad_press();
static KeypadKey wait_keypad_press();
static bool has_keypad_release();
static std::optional<KeypadKey> get_keypad_release();
static KeypadKey wait_keypad_release();
static uint16_t keypad_state();
// TODO: impl and add the hal and RFID stuff
};
#endif // EXPANDER_H