Add inputs controller
This commit is contained in:
parent
837c3eacda
commit
1b4795b4de
@ -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_CLOSE_SENSITIVITY = 0x32;
|
||||||
const static uint8_t REG_SWITCH_TOUCH_EVENT = 0x33;
|
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;
|
ExpanderPeripheral expander_peripheral_singleton;
|
||||||
|
|
||||||
// forward declarations
|
// forward declarations
|
||||||
@ -308,3 +326,174 @@ static void handle_close_hal_event(uint8_t event) {
|
|||||||
(void)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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns `true` iff there is a button press event waiting.
|
||||||
|
bool InputsController::has_button_press() const {
|
||||||
|
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() const {
|
||||||
|
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() const {
|
||||||
|
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() const {
|
||||||
|
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() const {
|
||||||
|
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() const {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,9 @@
|
|||||||
|
|
||||||
#include "blk_box_drivers/i2c.h"
|
#include "blk_box_drivers/i2c.h"
|
||||||
#include <freertos/FreeRTOS.h>
|
#include <freertos/FreeRTOS.h>
|
||||||
|
#include <freertos/queue.h>
|
||||||
#include <freertos/semphr.h>
|
#include <freertos/semphr.h>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
#define EXPANDER_I2C_ADDR (0x7E)
|
#define EXPANDER_I2C_ADDR (0x7E)
|
||||||
#define EXPANDER_I2C_SPEED (400000)
|
#define EXPANDER_I2C_SPEED (400000)
|
||||||
@ -195,23 +197,37 @@ 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) {}
|
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 InputsController {
|
||||||
class ExpanderPeripheral {
|
public:
|
||||||
// TODO: change these to private
|
void clear_all_events();
|
||||||
public:
|
|
||||||
SemaphoreHandle_t state_mutex;
|
|
||||||
ExpanderState state;
|
|
||||||
|
|
||||||
// channels
|
bool has_button_press() const;
|
||||||
QueueHandle_t button_press_events;
|
std::optional<Button> get_button_press();
|
||||||
QueueHandle_t button_release_events;
|
Button wait_button_press();
|
||||||
QueueHandle_t switch_flip_events;
|
uint8_t button_state();
|
||||||
QueueHandle_t switch_touch_events;
|
|
||||||
QueueHandle_t touch_events;
|
|
||||||
QueueHandle_t keypad_press_events;
|
|
||||||
QueueHandle_t keypad_release_events;
|
|
||||||
|
|
||||||
|
bool has_button_release() const;
|
||||||
|
std::optional<Button> get_button_release();
|
||||||
|
Button wait_button_release();
|
||||||
|
|
||||||
|
bool has_switch_flip() const;
|
||||||
|
std::optional<SwitchFlip> get_switch_flip();
|
||||||
|
SwitchFlip wait_switch_flip();
|
||||||
|
uint8_t switch_state();
|
||||||
|
|
||||||
|
bool has_switch_touch() const;
|
||||||
|
std::optional<SwitchTouch> get_switch_touch();
|
||||||
|
SwitchTouch wait_switch_touch();
|
||||||
|
uint8_t switch_touch_state();
|
||||||
|
|
||||||
|
bool has_keypad_press() const;
|
||||||
|
std::optional<KeypadKey> get_keypad_press();
|
||||||
|
KeypadKey wait_keypad_press();
|
||||||
|
|
||||||
|
bool has_keypad_release() const;
|
||||||
|
std::optional<KeypadKey> get_keypad_release();
|
||||||
|
KeypadKey wait_keypad_release();
|
||||||
|
uint16_t keypad_state();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // EXPANDER_H
|
#endif // EXPANDER_H
|
||||||
Loading…
Reference in New Issue
Block a user