cleanup expander

This commit is contained in:
Mitchell Marino 2026-04-01 15:55:23 -05:00
parent 1b4795b4de
commit c82c9adf32
2 changed files with 92 additions and 58 deletions

View File

@ -157,8 +157,6 @@ static void expander_task(void *arg) {
// Wait for interrupt notification (signal is sent when INT falls) // Wait for interrupt notification (signal is sent when INT falls)
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
// loop continues and will process events once INT goes low
printf("WAKE\n");
} }
} }
@ -222,9 +220,7 @@ static void handle_button_switch_event(uint8_t event) {
Switch sw = static_cast<Switch>(number); Switch sw = static_cast<Switch>(number);
SwitchFlip sw_flip = SwitchFlip(sw, pressed); SwitchFlip sw_flip = SwitchFlip(sw, pressed);
if (xQueueSendToBack(expander_peripheral_singleton.switch_flip_events, &sw_flip, 0) != pdTRUE) { xQueueSendToBack(expander_peripheral_singleton.switch_flip_events, &sw_flip, 0);
ESP_LOGE(TAG, "Failed to send switch flip event!");
}
xSemaphoreTake(expander_peripheral_singleton.state_mutex, portMAX_DELAY); xSemaphoreTake(expander_peripheral_singleton.state_mutex, portMAX_DELAY);
if (pressed) { if (pressed) {
// set // set
@ -238,16 +234,12 @@ static void handle_button_switch_event(uint8_t event) {
// button // button
Button button = static_cast<Button>(number); Button button = static_cast<Button>(number);
if (pressed) { if (pressed) {
if (xQueueSendToBack(expander_peripheral_singleton.button_press_events, &button, 0) != pdTRUE) { xQueueSendToBack(expander_peripheral_singleton.button_press_events, &button, 0);
ESP_LOGE(TAG, "Failed to send button press event!");
}
xSemaphoreTake(expander_peripheral_singleton.state_mutex, portMAX_DELAY); xSemaphoreTake(expander_peripheral_singleton.state_mutex, portMAX_DELAY);
expander_peripheral_singleton.state.button_state |= 1 << number; expander_peripheral_singleton.state.button_state |= 1 << number;
xSemaphoreGive(expander_peripheral_singleton.state_mutex); xSemaphoreGive(expander_peripheral_singleton.state_mutex);
} else { } else {
if (xQueueSendToBack(expander_peripheral_singleton.button_release_events, &button, 0) != pdTRUE) { xQueueSendToBack(expander_peripheral_singleton.button_release_events, &button, 0);
ESP_LOGE(TAG, "Failed to send button release event!");
}
xSemaphoreTake(expander_peripheral_singleton.state_mutex, portMAX_DELAY); xSemaphoreTake(expander_peripheral_singleton.state_mutex, portMAX_DELAY);
expander_peripheral_singleton.state.button_state &= ~(1 << number); expander_peripheral_singleton.state.button_state &= ~(1 << number);
xSemaphoreGive(expander_peripheral_singleton.state_mutex); xSemaphoreGive(expander_peripheral_singleton.state_mutex);
@ -270,16 +262,12 @@ static void handle_keypad_event(uint8_t event) {
// } // }
if (pressed) { if (pressed) {
if (xQueueSendToBack(expander_peripheral_singleton.keypad_press_events, &key, 0) != pdTRUE) { xQueueSendToBack(expander_peripheral_singleton.keypad_press_events, &key, 0);
ESP_LOGE(TAG, "Failed to send keypad press event!");
}
xSemaphoreTake(expander_peripheral_singleton.state_mutex, portMAX_DELAY); xSemaphoreTake(expander_peripheral_singleton.state_mutex, portMAX_DELAY);
expander_peripheral_singleton.state.keypad_state |= 1 << number; expander_peripheral_singleton.state.keypad_state |= 1 << number;
xSemaphoreGive(expander_peripheral_singleton.state_mutex); xSemaphoreGive(expander_peripheral_singleton.state_mutex);
} else { } else {
if (xQueueSendToBack(expander_peripheral_singleton.keypad_release_events, &key, 0) != pdTRUE) { xQueueSendToBack(expander_peripheral_singleton.keypad_release_events, &key, 0);
ESP_LOGE(TAG, "Failed to send keypad release event!");
}
xSemaphoreTake(expander_peripheral_singleton.state_mutex, portMAX_DELAY); xSemaphoreTake(expander_peripheral_singleton.state_mutex, portMAX_DELAY);
expander_peripheral_singleton.state.keypad_state &= ~(1 << number); expander_peripheral_singleton.state.keypad_state &= ~(1 << number);
xSemaphoreGive(expander_peripheral_singleton.state_mutex); xSemaphoreGive(expander_peripheral_singleton.state_mutex);
@ -296,15 +284,11 @@ static void handle_touch_event(uint8_t event) {
if ((sensor & FINGERPRINT_BIT) != 0) { if ((sensor & FINGERPRINT_BIT) != 0) {
TouchedReleased touch_state = static_cast<TouchedReleased>(touched); TouchedReleased touch_state = static_cast<TouchedReleased>(touched);
if (xQueueSendToBack(expander_peripheral_singleton.touch_events, &touch_state, 0) != pdTRUE) { xQueueSendToBack(expander_peripheral_singleton.touch_events, &touch_state, 0);
ESP_LOGE(TAG, "Failed to send touch event!");
}
} else { } else {
Switch sw = static_cast<Switch>(sensor); Switch sw = static_cast<Switch>(sensor);
SwitchTouch sw_touch = SwitchTouch(sw, touched); SwitchTouch sw_touch = SwitchTouch(sw, touched);
if (xQueueSendToBack(expander_peripheral_singleton.switch_touch_events, &sw_touch, 0) != pdTRUE) { xQueueSendToBack(expander_peripheral_singleton.switch_touch_events, &sw_touch, 0);
ESP_LOGE(TAG, "Failed to send switch touch event!");
}
} }
xSemaphoreTake(expander_peripheral_singleton.state_mutex, portMAX_DELAY); xSemaphoreTake(expander_peripheral_singleton.state_mutex, portMAX_DELAY);
@ -339,8 +323,15 @@ void InputsController::clear_all_events() {
xQueueReset(expander_peripheral_singleton.keypad_release_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. /// Returns `true` iff there is a button press event waiting.
bool InputsController::has_button_press() const { bool InputsController::has_button_press() {
return uxQueueMessagesWaiting(expander_peripheral_singleton.button_press_events) > 0; return uxQueueMessagesWaiting(expander_peripheral_singleton.button_press_events) > 0;
} }
@ -369,7 +360,7 @@ uint8_t InputsController::button_state() {
} }
/// Returns `true` iff there is a button release event waiting. /// Returns `true` iff there is a button release event waiting.
bool InputsController::has_button_release() const { bool InputsController::has_button_release() {
return uxQueueMessagesWaiting(expander_peripheral_singleton.button_release_events) > 0; return uxQueueMessagesWaiting(expander_peripheral_singleton.button_release_events) > 0;
} }
@ -390,7 +381,7 @@ Button InputsController::wait_button_release() {
} }
/// Returns `true` iff there is a switch flip event waiting. /// Returns `true` iff there is a switch flip event waiting.
bool InputsController::has_switch_flip() const { bool InputsController::has_switch_flip() {
return uxQueueMessagesWaiting(expander_peripheral_singleton.switch_flip_events) > 0; return uxQueueMessagesWaiting(expander_peripheral_singleton.switch_flip_events) > 0;
} }
@ -419,7 +410,7 @@ uint8_t InputsController::switch_state() {
} }
/// Returns `true` iff there is a switch touch event waiting. /// Returns `true` iff there is a switch touch event waiting.
bool InputsController::has_switch_touch() const { bool InputsController::has_switch_touch() {
return uxQueueMessagesWaiting(expander_peripheral_singleton.switch_touch_events) > 0; return uxQueueMessagesWaiting(expander_peripheral_singleton.switch_touch_events) > 0;
} }
@ -448,7 +439,7 @@ uint8_t InputsController::switch_touch_state() {
} }
/// Returns `true` iff there is a keypad press event waiting. /// Returns `true` iff there is a keypad press event waiting.
bool InputsController::has_keypad_press() const { bool InputsController::has_keypad_press() {
return uxQueueMessagesWaiting(expander_peripheral_singleton.keypad_press_events) > 0; return uxQueueMessagesWaiting(expander_peripheral_singleton.keypad_press_events) > 0;
} }
@ -469,7 +460,7 @@ KeypadKey InputsController::wait_keypad_press() {
} }
/// Returns `true` iff there is a keypad release event waiting. /// Returns `true` iff there is a keypad release event waiting.
bool InputsController::has_keypad_release() const { bool InputsController::has_keypad_release() {
return uxQueueMessagesWaiting(expander_peripheral_singleton.keypad_release_events) > 0; return uxQueueMessagesWaiting(expander_peripheral_singleton.keypad_release_events) > 0;
} }

View File

@ -32,6 +32,9 @@ enum class Button: uint8_t {
BLUE = 3, 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. /// The four switches on the bottom half.
enum class Switch: uint8_t { enum class Switch: uint8_t {
S1 = 0, S1 = 0,
@ -40,11 +43,17 @@ enum class Switch: uint8_t {
S4 = 3, 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 { enum class TouchedReleased: uint8_t {
Released = 0, Released = 0,
Touched = 1, 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. /// One of the keys on the keypad.
enum class KeypadKey: uint8_t { enum class KeypadKey: uint8_t {
K0 = 0, K0 = 0,
@ -65,26 +74,44 @@ enum class KeypadKey: uint8_t {
POUND = 15, 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 { struct SwitchFlip {
private: private:
// [bit2: pressed] [bit1-0: switch] // [bit2: up] [bit1-0: switch]
uint8_t data; uint8_t data;
public: public:
// Constructor // Constructor
SwitchFlip(Switch sw, bool pressed) SwitchFlip(Switch sw, bool up)
: data((static_cast<uint8_t>(sw) & 0b11) | : data((static_cast<uint8_t>(sw) & 0b11) |
((pressed ? 1 : 0) << 2)) {} ((up ? 1 : 0) << 2)) {}
// Default constructor // Default constructor
SwitchFlip() : data(0) {} 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 // Getters
Switch get_switch() const { Switch get_switch() const {
return static_cast<Switch>(data & 0b11); return static_cast<Switch>(data & 0b11);
} }
bool is_pressed() const { bool is_up() const {
return (data >> 2) & 1; return (data >> 2) & 1;
} }
@ -93,8 +120,8 @@ public:
data = (data & ~0b11) | (static_cast<uint8_t>(sw) & 0b11); data = (data & ~0b11) | (static_cast<uint8_t>(sw) & 0b11);
} }
void set_pressed(bool pressed) { void set_up(bool up) {
data = (data & ~(1 << 2)) | ((pressed ? 1 : 0) << 2); data = (data & ~(1 << 2)) | ((up ? 1 : 0) << 2);
} }
}; };
static_assert(sizeof(SwitchFlip) == 1); static_assert(sizeof(SwitchFlip) == 1);
@ -113,6 +140,12 @@ public:
// Default constructor // Default constructor
SwitchTouch() : data(0) {} 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 // Getters
Switch get_switch() const { Switch get_switch() const {
return static_cast<Switch>(data & 0b11); return static_cast<Switch>(data & 0b11);
@ -146,6 +179,12 @@ public:
ButtonOrSwitch() : data(0) {} 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 // Getters
uint8_t number() const { uint8_t number() const {
return data & 0b11; return data & 0b11;
@ -199,35 +238,39 @@ struct ExpanderState {
class InputsController { class InputsController {
public: public:
void clear_all_events(); static void clear_all_events();
bool has_button_press() const; static ExpanderState get_input_state();
std::optional<Button> get_button_press();
Button wait_button_press(); static bool has_button_press();
uint8_t button_state(); static std::optional<Button> get_button_press();
static Button wait_button_press();
static uint8_t button_state();
bool has_button_release() const; static bool has_button_release();
std::optional<Button> get_button_release(); static std::optional<Button> get_button_release();
Button wait_button_release(); static Button wait_button_release();
bool has_switch_flip() const; static bool has_switch_flip();
std::optional<SwitchFlip> get_switch_flip(); static std::optional<SwitchFlip> get_switch_flip();
SwitchFlip wait_switch_flip(); static SwitchFlip wait_switch_flip();
uint8_t switch_state(); static uint8_t switch_state();
bool has_switch_touch() const; static bool has_switch_touch();
std::optional<SwitchTouch> get_switch_touch(); static std::optional<SwitchTouch> get_switch_touch();
SwitchTouch wait_switch_touch(); static SwitchTouch wait_switch_touch();
uint8_t switch_touch_state(); static uint8_t switch_touch_state();
bool has_keypad_press() const; static bool has_keypad_press();
std::optional<KeypadKey> get_keypad_press(); static std::optional<KeypadKey> get_keypad_press();
KeypadKey wait_keypad_press(); static KeypadKey wait_keypad_press();
bool has_keypad_release() const; static bool has_keypad_release();
std::optional<KeypadKey> get_keypad_release(); static std::optional<KeypadKey> get_keypad_release();
KeypadKey wait_keypad_release(); static KeypadKey wait_keypad_release();
uint16_t keypad_state(); static uint16_t keypad_state();
// TODO: impl and add the hal and RFID stuff
}; };
#endif // EXPANDER_H #endif // EXPANDER_H