Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c82c9adf32 | ||
|
|
1b4795b4de | ||
|
|
837c3eacda |
+300
-56
@@ -4,12 +4,16 @@
|
|||||||
#include "blk_box_drivers/i2c.h"
|
#include "blk_box_drivers/i2c.h"
|
||||||
#include "driver/i2c_master.h"
|
#include "driver/i2c_master.h"
|
||||||
#include "driver/gpio.h"
|
#include "driver/gpio.h"
|
||||||
|
#include "freertos/FreeRTOS.h"
|
||||||
|
#include "freertos/task.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "esp_err.h"
|
#include "esp_err.h"
|
||||||
|
|
||||||
static const char *TAG = "EXPANDER";
|
static const char *TAG = "EXPANDER";
|
||||||
|
|
||||||
i2c_master_dev_handle_t expander_i2c_dev_handle;
|
static TaskHandle_t expander_task_handle = NULL;
|
||||||
|
|
||||||
|
static i2c_master_dev_handle_t expander_i2c_dev_handle;
|
||||||
|
|
||||||
const static uint8_t REG_WHOAMI = 0x01;
|
const static uint8_t REG_WHOAMI = 0x01;
|
||||||
const static uint8_t REG_SW_VERSION = 0x02;
|
const static uint8_t REG_SW_VERSION = 0x02;
|
||||||
@@ -27,15 +31,48 @@ 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;
|
||||||
|
|
||||||
ExpanderPeripheral expander_peripheral;
|
/// 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
|
// forward declarations
|
||||||
|
static void get_events();
|
||||||
static void handle_event(uint8_t event);
|
static void handle_event(uint8_t event);
|
||||||
static void handle_button_switch_event(uint8_t event);
|
static void handle_button_switch_event(uint8_t event);
|
||||||
static void handle_keypad_event(uint8_t event);
|
static void handle_keypad_event(uint8_t event);
|
||||||
static void handle_touch_event(uint8_t event);
|
static void handle_touch_event(uint8_t event);
|
||||||
static void handle_rfid_event(uint8_t event);
|
static void handle_rfid_event(uint8_t event);
|
||||||
static void handle_close_hal_event(uint8_t event);
|
static void handle_close_hal_event(uint8_t event);
|
||||||
|
static void expander_task(void *arg);
|
||||||
|
|
||||||
|
// ISR handler
|
||||||
|
static void IRAM_ATTR expander_isr_handler(void *arg) {
|
||||||
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||||
|
|
||||||
|
if (expander_task_handle != NULL) {
|
||||||
|
vTaskNotifyGiveFromISR(expander_task_handle, &xHigherPriorityTaskWoken);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (xHigherPriorityTaskWoken == pdTRUE) {
|
||||||
|
portYIELD_FROM_ISR();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void init_expander() {
|
void init_expander() {
|
||||||
ESP_LOGI(TAG, "Initializing expander...");
|
ESP_LOGI(TAG, "Initializing expander...");
|
||||||
@@ -50,11 +87,28 @@ void init_expander() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// TODO: replace all these ESP_ERROR_CHECK with proper error handling that doesn't just crash the program
|
||||||
ESP_ERROR_CHECK(i2c_master_bus_add_device(i2c_main_bus_handle, &dev_config, &expander_i2c_dev_handle));
|
ESP_ERROR_CHECK(i2c_master_bus_add_device(i2c_main_bus_handle, &dev_config, &expander_i2c_dev_handle));
|
||||||
ESP_LOGD(TAG, "Expander I2C device added to bus");
|
ESP_LOGD(TAG, "Expander I2C device added to bus");
|
||||||
|
|
||||||
// TODO: setup interrupt on PIN_EXPANDER_INT
|
// setup interrupt on PIN_EXPANDER_INT
|
||||||
|
gpio_config_t io_conf = {
|
||||||
|
.pin_bit_mask = (1ULL << PIN_EXPANDER_INT),
|
||||||
|
.mode = GPIO_MODE_INPUT,
|
||||||
|
.pull_up_en = GPIO_PULLUP_ENABLE,
|
||||||
|
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||||
|
.intr_type = GPIO_INTR_NEGEDGE
|
||||||
|
};
|
||||||
|
|
||||||
|
ESP_ERROR_CHECK(gpio_config(&io_conf));
|
||||||
|
|
||||||
|
// Install ISR service (only call once in your program)
|
||||||
|
ESP_ERROR_CHECK(gpio_install_isr_service(0));
|
||||||
|
|
||||||
|
// Attach the ISR to the expander pin
|
||||||
|
ESP_ERROR_CHECK(gpio_isr_handler_add(PIN_EXPANDER_INT, expander_isr_handler, NULL));
|
||||||
|
|
||||||
|
// verify the expander connection status by reading the WHOAMI register
|
||||||
uint8_t read_buf[2] = {0};
|
uint8_t read_buf[2] = {0};
|
||||||
|
|
||||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(expander_i2c_dev_handle, ®_WHOAMI, 1, read_buf, 1, EXPANDER_TIMEOUT_MS));
|
ESP_ERROR_CHECK(i2c_master_transmit_receive(expander_i2c_dev_handle, ®_WHOAMI, 1, read_buf, 1, EXPANDER_TIMEOUT_MS));
|
||||||
@@ -69,24 +123,49 @@ void init_expander() {
|
|||||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(expander_i2c_dev_handle, ®_SW_VERSION, 1, read_buf, 2, EXPANDER_TIMEOUT_MS));
|
ESP_ERROR_CHECK(i2c_master_transmit_receive(expander_i2c_dev_handle, ®_SW_VERSION, 1, read_buf, 2, EXPANDER_TIMEOUT_MS));
|
||||||
|
|
||||||
// init the peripheral struct
|
// init the peripheral struct
|
||||||
expander_peripheral.state_mutex = xSemaphoreCreateMutex();
|
expander_peripheral_singleton.state_mutex = xSemaphoreCreateMutex();
|
||||||
expander_peripheral.button_press_events= xQueueCreate(EXPANDER_EVENT_QUEUE_SIZE, sizeof(Button));
|
expander_peripheral_singleton.button_press_events= xQueueCreate(EXPANDER_EVENT_QUEUE_SIZE, sizeof(Button));
|
||||||
expander_peripheral.button_release_events= xQueueCreate(EXPANDER_EVENT_QUEUE_SIZE, sizeof(Button));
|
expander_peripheral_singleton.button_release_events= xQueueCreate(EXPANDER_EVENT_QUEUE_SIZE, sizeof(Button));
|
||||||
expander_peripheral.switch_flip_events= xQueueCreate(EXPANDER_EVENT_QUEUE_SIZE, sizeof(SwitchFlip));
|
expander_peripheral_singleton.switch_flip_events= xQueueCreate(EXPANDER_EVENT_QUEUE_SIZE, sizeof(SwitchFlip));
|
||||||
expander_peripheral.switch_touch_events= xQueueCreate(EXPANDER_EVENT_QUEUE_SIZE, sizeof(SwitchTouch));
|
expander_peripheral_singleton.switch_touch_events= xQueueCreate(EXPANDER_EVENT_QUEUE_SIZE, sizeof(SwitchTouch));
|
||||||
expander_peripheral.touch_events= xQueueCreate(EXPANDER_EVENT_QUEUE_SIZE, sizeof(TouchedReleased));
|
expander_peripheral_singleton.touch_events= xQueueCreate(EXPANDER_EVENT_QUEUE_SIZE, sizeof(TouchedReleased));
|
||||||
expander_peripheral.keypad_press_events= xQueueCreate(EXPANDER_KEYPAD_QUEUE_SIZE, sizeof(KeypadKey));
|
expander_peripheral_singleton.keypad_press_events= xQueueCreate(EXPANDER_KEYPAD_QUEUE_SIZE, sizeof(KeypadKey));
|
||||||
expander_peripheral.keypad_release_events= xQueueCreate(EXPANDER_KEYPAD_QUEUE_SIZE, sizeof(KeypadKey));
|
expander_peripheral_singleton.keypad_release_events= xQueueCreate(EXPANDER_KEYPAD_QUEUE_SIZE, sizeof(KeypadKey));
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Expander initialized! SW version: v%d.%d", read_buf[0], read_buf[1]);
|
ESP_LOGI(TAG, "Expander initialized! SW version: v%d.%d", read_buf[0], read_buf[1]);
|
||||||
|
|
||||||
|
// Create the expander background worker task
|
||||||
|
BaseType_t task_created = xTaskCreate(
|
||||||
|
expander_task,
|
||||||
|
"expander_task",
|
||||||
|
4096,
|
||||||
|
NULL,
|
||||||
|
tskIDLE_PRIORITY + 1,
|
||||||
|
&expander_task_handle
|
||||||
|
);
|
||||||
|
|
||||||
|
if (task_created != pdPASS) {
|
||||||
|
ESP_LOGE(TAG, "Failed to create expander task");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_events() {
|
static void expander_task(void *arg) {
|
||||||
|
(void)arg;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
get_events();
|
||||||
|
|
||||||
|
// Wait for interrupt notification (signal is sent when INT falls)
|
||||||
|
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void get_events() {
|
||||||
uint8_t recv;
|
uint8_t recv;
|
||||||
do {
|
while (gpio_get_level(PIN_EXPANDER_INT) == 0) {
|
||||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(expander_i2c_dev_handle, ®_EVENT_QUEUE_POP, 1, &recv, 1, EXPANDER_TIMEOUT_MS));
|
ESP_ERROR_CHECK(i2c_master_transmit_receive(expander_i2c_dev_handle, ®_EVENT_QUEUE_POP, 1, &recv, 1, EXPANDER_TIMEOUT_MS));
|
||||||
handle_event(recv);
|
handle_event(recv);
|
||||||
} while (gpio_get_level(PIN_EXPANDER_INT) == 0);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void handle_event(uint8_t event) {
|
static void handle_event(uint8_t event) {
|
||||||
@@ -95,7 +174,7 @@ static void handle_event(uint8_t event) {
|
|||||||
const uint8_t TOUCH = 0b010;
|
const uint8_t TOUCH = 0b010;
|
||||||
const uint8_t RFID = 0b011;
|
const uint8_t RFID = 0b011;
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Expander event: 0b%08b (0x%02X)", event, event);
|
ESP_LOGD(TAG, "Expander event: 0b%08b (0x%02X)", event, event);
|
||||||
|
|
||||||
if (event == 0) {
|
if (event == 0) {
|
||||||
ESP_LOGE(TAG, "We read from event queue while it was empty!");
|
ESP_LOGE(TAG, "We read from event queue while it was empty!");
|
||||||
@@ -141,35 +220,29 @@ 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.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.state_mutex, portMAX_DELAY);
|
|
||||||
if (pressed) {
|
if (pressed) {
|
||||||
// set
|
// set
|
||||||
expander_peripheral.state.switch_state |= 1 << number;
|
expander_peripheral_singleton.state.switch_state |= 1 << number;
|
||||||
} else {
|
} else {
|
||||||
// clear
|
// clear
|
||||||
expander_peripheral.state.switch_state &= ~(1 << number);
|
expander_peripheral_singleton.state.switch_state &= ~(1 << number);
|
||||||
}
|
}
|
||||||
xSemaphoreGive(expander_peripheral.state_mutex);
|
xSemaphoreGive(expander_peripheral_singleton.state_mutex);
|
||||||
} else {
|
} else {
|
||||||
// button
|
// button
|
||||||
Button button = static_cast<Button>(number);
|
Button button = static_cast<Button>(number);
|
||||||
if (pressed) {
|
if (pressed) {
|
||||||
if (xQueueSendToBack(expander_peripheral.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);
|
||||||
}
|
expander_peripheral_singleton.state.button_state |= 1 << number;
|
||||||
xSemaphoreTake(expander_peripheral.state_mutex, portMAX_DELAY);
|
xSemaphoreGive(expander_peripheral_singleton.state_mutex);
|
||||||
expander_peripheral.state.button_state |= 1 << number;
|
|
||||||
xSemaphoreGive(expander_peripheral.state_mutex);
|
|
||||||
} else {
|
} else {
|
||||||
if (xQueueSendToBack(expander_peripheral.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);
|
||||||
}
|
expander_peripheral_singleton.state.button_state &= ~(1 << number);
|
||||||
xSemaphoreTake(expander_peripheral.state_mutex, portMAX_DELAY);
|
xSemaphoreGive(expander_peripheral_singleton.state_mutex);
|
||||||
expander_peripheral.state.button_state &= ~(1 << number);
|
|
||||||
xSemaphoreGive(expander_peripheral.state_mutex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -189,19 +262,15 @@ static void handle_keypad_event(uint8_t event) {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
if (pressed) {
|
if (pressed) {
|
||||||
if (xQueueSendToBack(expander_peripheral.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);
|
||||||
}
|
expander_peripheral_singleton.state.keypad_state |= 1 << number;
|
||||||
xSemaphoreTake(expander_peripheral.state_mutex, portMAX_DELAY);
|
xSemaphoreGive(expander_peripheral_singleton.state_mutex);
|
||||||
expander_peripheral.state.keypad_state |= 1 << number;
|
|
||||||
xSemaphoreGive(expander_peripheral.state_mutex);
|
|
||||||
} else {
|
} else {
|
||||||
if (xQueueSendToBack(expander_peripheral.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);
|
||||||
}
|
expander_peripheral_singleton.state.keypad_state &= ~(1 << number);
|
||||||
xSemaphoreTake(expander_peripheral.state_mutex, portMAX_DELAY);
|
xSemaphoreGive(expander_peripheral_singleton.state_mutex);
|
||||||
expander_peripheral.state.keypad_state &= ~(1 << number);
|
|
||||||
xSemaphoreGive(expander_peripheral.state_mutex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,24 +284,20 @@ 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.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.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.state_mutex, portMAX_DELAY);
|
xSemaphoreTake(expander_peripheral_singleton.state_mutex, portMAX_DELAY);
|
||||||
if (touched) {
|
if (touched) {
|
||||||
expander_peripheral.state.touch_state |= 1 << sensor;
|
expander_peripheral_singleton.state.touch_state |= 1 << sensor;
|
||||||
} else {
|
} else {
|
||||||
expander_peripheral.state.touch_state &= ~(1 << sensor);
|
expander_peripheral_singleton.state.touch_state &= ~(1 << sensor);
|
||||||
}
|
}
|
||||||
xSemaphoreGive(expander_peripheral.state_mutex);
|
xSemaphoreGive(expander_peripheral_singleton.state_mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void handle_rfid_event(uint8_t event) {
|
static void handle_rfid_event(uint8_t event) {
|
||||||
@@ -244,3 +309,182 @@ static void handle_close_hal_event(uint8_t event) {
|
|||||||
// TODO: impl
|
// TODO: impl
|
||||||
(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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,11 +3,14 @@
|
|||||||
|
|
||||||
#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)
|
||||||
#define EXPANDER_TIMEOUT_MS (10)
|
// the actual transaction takes ~0.3ms, but for some reason a timout of ~10 or lower causes issues.
|
||||||
|
#define EXPANDER_TIMEOUT_MS (100)
|
||||||
|
|
||||||
#define EXPANDER_WHOAMI_VALUE (0x85)
|
#define EXPANDER_WHOAMI_VALUE (0x85)
|
||||||
|
|
||||||
@@ -29,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,
|
||||||
@@ -37,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,
|
||||||
@@ -62,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,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);
|
||||||
@@ -110,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);
|
||||||
@@ -143,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;
|
||||||
@@ -194,28 +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) {}
|
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 {
|
|
||||||
// TODO: change these to private
|
|
||||||
public:
|
public:
|
||||||
SemaphoreHandle_t state_mutex;
|
static void clear_all_events();
|
||||||
ExpanderState state;
|
|
||||||
|
|
||||||
// channels
|
static ExpanderState get_input_state();
|
||||||
QueueHandle_t button_press_events;
|
|
||||||
QueueHandle_t button_release_events;
|
static bool has_button_press();
|
||||||
QueueHandle_t switch_flip_events;
|
static std::optional<Button> get_button_press();
|
||||||
QueueHandle_t switch_touch_events;
|
static Button wait_button_press();
|
||||||
QueueHandle_t touch_events;
|
static uint8_t button_state();
|
||||||
QueueHandle_t keypad_press_events;
|
|
||||||
QueueHandle_t keypad_release_events;
|
static bool has_button_release();
|
||||||
// button_press_events: Channel<CriticalSectionRawMutex, Button, EXPANDER_EVENT_QUEUE_SIZE>,
|
static std::optional<Button> get_button_release();
|
||||||
// button_release_events: Channel<CriticalSectionRawMutex, Button, EXPANDER_EVENT_QUEUE_SIZE>,
|
static Button wait_button_release();
|
||||||
// switch_flip_events: Channel<CriticalSectionRawMutex, SwitchFlip, EXPANDER_EVENT_QUEUE_SIZE>,
|
|
||||||
// switch_touch_events: Channel<CriticalSectionRawMutex, SwitchTouch, EXPANDER_EVENT_QUEUE_SIZE>,
|
static bool has_switch_flip();
|
||||||
// touch_events: Channel<CriticalSectionRawMutex, TouchRelease, EXPANDER_EVENT_QUEUE_SIZE>,
|
static std::optional<SwitchFlip> get_switch_flip();
|
||||||
// keypad_press_events: Channel<CriticalSectionRawMutex, KeypadKey, EXPANDER_KEYPAD_QUEUE_SIZE>,
|
static SwitchFlip wait_switch_flip();
|
||||||
// keypad_release_events: Channel<CriticalSectionRawMutex, KeypadKey, EXPANDER_KEYPAD_QUEUE_SIZE>,
|
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
|
#endif // EXPANDER_H
|
||||||
Reference in New Issue
Block a user