expander work

This commit is contained in:
Mitchell Marino 2026-03-30 20:57:29 -05:00
parent af60710405
commit 8c0b6823fd
9 changed files with 522 additions and 8 deletions

View File

@ -1,6 +1,9 @@
idf_component_register(
SRCS "blk_box.cpp"
INCLUDE_DIRS "include"
INCLUDE_DIRS "include" "."
PRIV_REQUIRES
esp_driver_gpio
esp_driver_i2c
)
add_subdirectory(drivers)

View File

@ -1,5 +1,9 @@
#include "blk_box.h"
void init_blk_box(BlkBoxInitConfig cfg) {
#include "blk_box_drivers/i2c.h"
#include "blk_box_drivers/expander.hpp"
void init_blk_box(BlkBoxInitConfig cfg) {
init_main_i2c();
init_expander();
}

View File

@ -1,4 +1,5 @@
set(SOURCES
"expander.cpp"
"i2c.cpp"
)

246
drivers/expander.cpp Normal file
View File

@ -0,0 +1,246 @@
#include "blk_box_drivers/expander.hpp"
#include "pins.h"
#include "blk_box_drivers/i2c.h"
#include "driver/i2c_master.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "esp_err.h"
static const char *TAG = "EXPANDER";
i2c_master_dev_handle_t expander_i2c_dev_handle;
const static uint8_t REG_WHOAMI = 0x01;
const static uint8_t REG_SW_VERSION = 0x02;
const static uint8_t REG_EVENT_QUEUE_POP = 0x10;
const static uint8_t REG_EVENT_QUEUE_LEN = 0x11;
const static uint8_t REG_STATE_BUTTONS = 0x20;
const static uint8_t REG_STATE_SWITCHES = 0x21;
const static uint8_t REG_STATE_KEYPAD = 0x22;
const static uint8_t REG_STATE_TOUCH = 0x23;
const static uint8_t REG_STATE_RFID = 0x24;
const static uint8_t REG_STATE_HALL = 0x25;
const static uint8_t REG_STATE_CLOSE = 0x26;
const static uint8_t REG_RESET = 0x30;
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;
ExpanderPeripheral expander_peripheral;
// forward declarations
static void handle_event(uint8_t event);
static void handle_button_switch_event(uint8_t event);
static void handle_keypad_event(uint8_t event);
static void handle_touch_event(uint8_t event);
static void handle_rfid_event(uint8_t event);
static void handle_close_hal_event(uint8_t event);
void init_expander() {
ESP_LOGI(TAG, "Initializing expander...");
i2c_device_config_t dev_config = {
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
.device_address = EXPANDER_I2C_ADDR,
.scl_speed_hz = EXPANDER_I2C_SPEED,
.scl_wait_us = 0, // default
.flags = {
.disable_ack_check = 0,
}
};
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");
// TODO: setup interrupt on PIN_EXPANDER_INT
uint8_t read_buf[2] = {0};
ESP_ERROR_CHECK(i2c_master_transmit_receive(expander_i2c_dev_handle, &REG_WHOAMI, 1, read_buf, 1, EXPANDER_TIMEOUT_MS));
if (read_buf[0] != EXPANDER_WHOAMI_VALUE) {
ESP_LOGE(TAG, "WHOAMI mismatch, expected 0x%02X, got 0x%02X", EXPANDER_WHOAMI_VALUE, read_buf[0]);
return;
}
ESP_LOGD(TAG, "Expander WHOAMI check passed");
ESP_ERROR_CHECK(i2c_master_transmit_receive(expander_i2c_dev_handle, &REG_SW_VERSION, 1, read_buf, 2, EXPANDER_TIMEOUT_MS));
// init the peripheral struct
expander_peripheral.state_mutex = xSemaphoreCreateMutex();
expander_peripheral.button_press_events= xQueueCreate(EXPANDER_EVENT_QUEUE_SIZE, sizeof(Button));
expander_peripheral.button_release_events= xQueueCreate(EXPANDER_EVENT_QUEUE_SIZE, sizeof(Button));
expander_peripheral.switch_flip_events= xQueueCreate(EXPANDER_EVENT_QUEUE_SIZE, sizeof(SwitchFlip));
expander_peripheral.switch_touch_events= xQueueCreate(EXPANDER_EVENT_QUEUE_SIZE, sizeof(SwitchTouch));
expander_peripheral.touch_events= xQueueCreate(EXPANDER_EVENT_QUEUE_SIZE, sizeof(TouchedReleased));
expander_peripheral.keypad_press_events= xQueueCreate(EXPANDER_KEYPAD_QUEUE_SIZE, sizeof(KeypadKey));
expander_peripheral.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]);
}
void get_events() {
uint8_t recv;
do {
ESP_ERROR_CHECK(i2c_master_transmit_receive(expander_i2c_dev_handle, &REG_EVENT_QUEUE_POP, 1, &recv, 1, EXPANDER_TIMEOUT_MS));
handle_event(recv);
} while (gpio_get_level(PIN_EXPANDER_INT) == 0);
}
static void handle_event(uint8_t event) {
const uint8_t BUTTON_SWITCH = 0b000;
const uint8_t KEYPAD = 0b001;
const uint8_t TOUCH = 0b010;
const uint8_t RFID = 0b011;
ESP_LOGI(TAG, "Expander event: 0b%08b (0x%02X)", event, event);
if (event == 0) {
ESP_LOGE(TAG, "We read from event queue while it was empty!");
return;
}
uint8_t type_bits = event >> 5;
switch (type_bits) {
case BUTTON_SWITCH:
handle_button_switch_event(event);
break;
case KEYPAD:
handle_keypad_event(event);
break;
case TOUCH:
handle_touch_event(event);
break;
case RFID:
handle_rfid_event(event);
break;
default:
handle_close_hal_event(event);
break;
}
}
static void handle_button_switch_event(uint8_t event) {
const uint8_t PRESSED_NOT_RELEASED_BIT = 0b10000;
const uint8_t SWITCH_NOT_BUTTON_BIT = 0b01000;
const uint8_t SWITCH_UP_NOT_DOWN_BIT = 0b00100;
const uint8_t NUMBER_MASK = 0b00011;
bool pressed = (event & PRESSED_NOT_RELEASED_BIT) != 0;
uint8_t number = event & NUMBER_MASK;
if ((event & SWITCH_NOT_BUTTON_BIT) != 0) {
// For now, we support two position switches by only looking at the switch up events
bool switch_up = (event & SWITCH_UP_NOT_DOWN_BIT) != 0;
if (!switch_up) {
return;
}
Switch sw = static_cast<Switch>(number);
SwitchFlip sw_flip = SwitchFlip(sw, pressed);
if (xQueueSendToBack(expander_peripheral.switch_flip_events, &sw_flip, 0) != pdTRUE) {
ESP_LOGE(TAG, "Failed to send switch flip event!");
}
xSemaphoreTake(expander_peripheral.state_mutex, portMAX_DELAY);
if (pressed) {
// set
expander_peripheral.state.switch_state |= 1 << number;
} else {
// clear
expander_peripheral.state.switch_state &= ~(1 << number);
}
xSemaphoreGive(expander_peripheral.state_mutex);
} else {
// button
Button button = static_cast<Button>(number);
if (pressed) {
if (xQueueSendToBack(expander_peripheral.button_press_events, &button, 0) != pdTRUE) {
ESP_LOGE(TAG, "Failed to send button press event!");
}
xSemaphoreTake(expander_peripheral.state_mutex, portMAX_DELAY);
expander_peripheral.state.button_state |= 1 << number;
xSemaphoreGive(expander_peripheral.state_mutex);
} else {
if (xQueueSendToBack(expander_peripheral.button_release_events, &button, 0) != pdTRUE) {
ESP_LOGE(TAG, "Failed to send button release event!");
}
xSemaphoreTake(expander_peripheral.state_mutex, portMAX_DELAY);
expander_peripheral.state.button_state &= ~(1 << number);
xSemaphoreGive(expander_peripheral.state_mutex);
}
}
}
static void handle_keypad_event(uint8_t event) {
const uint8_t PRESSED_NOT_RELEASED_BIT = 0b10000;
const uint8_t KEY_MASK = 0b1111;
bool pressed = (event & PRESSED_NOT_RELEASED_BIT) != 0;
uint8_t number = event & KEY_MASK;
KeypadKey key = static_cast<KeypadKey>(number);
// starcode system gets first dibs
// TODO: do starcode inbetweener
// if starcode_handle_keypad(key, pressed).await {
// return;
// }
if (pressed) {
if (xQueueSendToBack(expander_peripheral.keypad_press_events, &key, 0) != pdTRUE) {
ESP_LOGE(TAG, "Failed to send keypad press event!");
}
xSemaphoreTake(expander_peripheral.state_mutex, portMAX_DELAY);
expander_peripheral.state.keypad_state |= 1 << number;
xSemaphoreGive(expander_peripheral.state_mutex);
} else {
if (xQueueSendToBack(expander_peripheral.keypad_release_events, &key, 0) != pdTRUE) {
ESP_LOGE(TAG, "Failed to send keypad release event!");
}
xSemaphoreTake(expander_peripheral.state_mutex, portMAX_DELAY);
expander_peripheral.state.keypad_state &= ~(1 << number);
xSemaphoreGive(expander_peripheral.state_mutex);
}
}
static void handle_touch_event(uint8_t event) {
const uint8_t TOUCHED_NOT_UNTOUCHED_BIT = 0b10000;
const uint8_t SENSOR_MASK = 0b0111;
const uint8_t FINGERPRINT_BIT = 0b0100;
bool touched = (event & TOUCHED_NOT_UNTOUCHED_BIT) != 0;
uint8_t sensor = event & SENSOR_MASK;
if ((sensor & FINGERPRINT_BIT) != 0) {
TouchedReleased touch_state = static_cast<TouchedReleased>(touched);
if (xQueueSendToBack(expander_peripheral.touch_events, &touch_state, 0) != pdTRUE) {
ESP_LOGE(TAG, "Failed to send touch event!");
}
} else {
Switch sw = static_cast<Switch>(sensor);
SwitchTouch sw_touch = SwitchTouch(sw, touched);
if (xQueueSendToBack(expander_peripheral.switch_touch_events, &sw_touch, 0) != pdTRUE) {
ESP_LOGE(TAG, "Failed to send switch touch event!");
}
}
xSemaphoreTake(expander_peripheral.state_mutex, portMAX_DELAY);
if (touched) {
expander_peripheral.state.touch_state |= 1 << sensor;
} else {
expander_peripheral.state.touch_state &= ~(1 << sensor);
}
xSemaphoreGive(expander_peripheral.state_mutex);
}
static void handle_rfid_event(uint8_t event) {
// TODO: impl
(void)event;
}
static void handle_close_hal_event(uint8_t event) {
// TODO: impl
(void)event;
}

View File

@ -1,5 +1,23 @@
#include "stdio.h"
#include "blk_box_drivers/i2c.h"
void i2c_test() {
printf("initializing i2c or something\n");
#include "pins.h"
i2c_master_bus_handle_t i2c_main_bus_handle;
void init_main_i2c() {
i2c_master_bus_config_t bus_config = {
.i2c_port = MAIN_I2C_NUM,
.sda_io_num = PIN_SDA,
.scl_io_num = PIN_SCL,
.clk_source = I2C_CLK_SRC_DEFAULT,
.glitch_ignore_cnt = 7,
.intr_priority = 0,
.trans_queue_depth = 0,
.flags = {
.enable_internal_pullup = false,
.allow_pd = false,
}
};
ESP_ERROR_CHECK(i2c_new_master_bus(&bus_config, &i2c_main_bus_handle));
}

View File

@ -1,3 +1,6 @@
#ifndef BLK_BOX_H
#define BLK_BOX_H
struct BlkBoxInitConfig {
// nothing for now...
};
@ -5,3 +8,5 @@ struct BlkBoxInitConfig {
void init_blk_box(BlkBoxInitConfig cfg);
#endif // BLK_BOX_H

View File

@ -0,0 +1,221 @@
#ifndef EXPANDER_H
#define EXPANDER_H
#include "blk_box_drivers/i2c.h"
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#define EXPANDER_I2C_ADDR (0x7E)
#define EXPANDER_I2C_SPEED (400000)
#define EXPANDER_TIMEOUT_MS (10)
#define EXPANDER_WHOAMI_VALUE (0x85)
// queue sizes
#define EXPANDER_EVENT_QUEUE_SIZE 4
#define EXPANDER_KEYPAD_QUEUE_SIZE 16
void init_expander();
/// The four buttons on the bottom half.
enum class Button: uint8_t {
B1 = 0,
B2 = 1,
B3 = 2,
B4 = 3,
GREEN = 0,
YELLOW = 1,
RED = 2,
BLUE = 3,
};
/// The four switches on the bottom half.
enum class Switch: uint8_t {
S1 = 0,
S2 = 1,
S3 = 2,
S4 = 3,
};
enum class TouchedReleased: uint8_t {
Released = 0,
Touched = 1,
};
/// One of the keys on the keypad.
enum class KeypadKey: uint8_t {
K0 = 0,
K1 = 1,
K2 = 2,
K3 = 3,
K4 = 4,
K5 = 5,
K6 = 6,
K7 = 7,
K8 = 8,
K9 = 9,
A = 10,
B = 11,
C = 12,
D = 13,
STAR = 14,
POUND = 15,
};
struct SwitchFlip {
private:
// [bit2: pressed] [bit1-0: switch]
uint8_t data;
public:
// Constructor
SwitchFlip(Switch sw, bool pressed)
: data((static_cast<uint8_t>(sw) & 0b11) |
((pressed ? 1 : 0) << 2)) {}
// Default constructor
SwitchFlip() : data(0) {}
// Getters
Switch get_switch() const {
return static_cast<Switch>(data & 0b11);
}
bool is_pressed() const {
return (data >> 2) & 1;
}
// Setters
void set_switch(Switch sw) {
data = (data & ~0b11) | (static_cast<uint8_t>(sw) & 0b11);
}
void set_pressed(bool pressed) {
data = (data & ~(1 << 2)) | ((pressed ? 1 : 0) << 2);
}
};
static_assert(sizeof(SwitchFlip) == 1);
struct SwitchTouch {
private:
// [bit2: touched] [bit1-0: switch]
uint8_t data;
public:
// Constructor
SwitchTouch(Switch sw, bool touched)
: data((static_cast<uint8_t>(sw) & 0b11) |
((touched ? 1 : 0) << 2)) {}
// Default constructor
SwitchTouch() : data(0) {}
// Getters
Switch get_switch() const {
return static_cast<Switch>(data & 0b11);
}
bool is_touched() const {
return (data >> 2) & 1;
}
// Setters
void set_switch(Switch sw) {
data = (data & ~0b11) | (static_cast<uint8_t>(sw) & 0b11);
}
void set_touched(bool touched) {
data = (data & ~(1 << 2)) | ((touched ? 1 : 0) << 2);
}
};
static_assert(sizeof(SwitchTouch) == 1);
struct ButtonOrSwitch {
private:
// [bit2: is_switch] [bit1-0: number]
uint8_t data;
public:
// Constructor
ButtonOrSwitch(uint8_t number, bool is_switch)
: data((number & 0b11) |
((is_switch ? 1 : 0) << 2)) {}
ButtonOrSwitch() : data(0) {}
// Getters
uint8_t number() const {
return data & 0b11;
}
bool is_switch() const {
return (data >> 2) & 1;
}
// Setters
void set_number(uint8_t number) {
data = (data & ~0b11) | (number & 0b11);
}
void set_is_switch(bool is_switch) {
data = (data & ~(1 << 2)) | ((is_switch ? 1 : 0) << 2);
}
};
static_assert(sizeof(ButtonOrSwitch) == 1);
/// @brief The state of the bottom half of the box.
struct ExpanderState {
/// The touch state of the switches in the lower 4 bits.
/// The touch pad state in bit 4.
uint8_t touch_state;
/// The current state of the buttons in the lower 4 bits.
uint8_t button_state;
/// The current state of the switches. Up switches are stored
/// in the lower 4 bits, switches that are down are stored in
/// the upper 4 bits. If switches are in the middle, the
/// corresponding bit will be `0` in the upper and lower 4.
uint8_t switch_state;
/// The state of the keypad.
uint16_t keypad_state;
/// The sensitivity of the `hal` value to auto update.
uint16_t hal_sense;
/// The sensitivity of the `close_hal` value to auto update.
uint16_t close_hal_sense;
/// A non-exact hal value reading.
/// This only gets updated when it changes by `hal_sense`
uint16_t hal;
/// A non-exact hal value reading.
/// This only gets updated when it changes by `close_hal_sense`
uint16_t close_hal;
/// The RFID card that was presented last.
uint32_t rfid_state;
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;
// button_press_events: Channel<CriticalSectionRawMutex, Button, EXPANDER_EVENT_QUEUE_SIZE>,
// button_release_events: Channel<CriticalSectionRawMutex, Button, EXPANDER_EVENT_QUEUE_SIZE>,
// switch_flip_events: Channel<CriticalSectionRawMutex, SwitchFlip, EXPANDER_EVENT_QUEUE_SIZE>,
// switch_touch_events: Channel<CriticalSectionRawMutex, SwitchTouch, EXPANDER_EVENT_QUEUE_SIZE>,
// touch_events: Channel<CriticalSectionRawMutex, TouchRelease, EXPANDER_EVENT_QUEUE_SIZE>,
// keypad_press_events: Channel<CriticalSectionRawMutex, KeypadKey, EXPANDER_KEYPAD_QUEUE_SIZE>,
// keypad_release_events: Channel<CriticalSectionRawMutex, KeypadKey, EXPANDER_KEYPAD_QUEUE_SIZE>,
};
#endif // EXPANDER_H

View File

@ -1 +1,12 @@
void i2c_test();
#ifndef I2C_H
#define I2C_H
#include "driver/i2c_master.h"
#define MAIN_I2C_NUM I2C_NUM_0
extern i2c_master_bus_handle_t i2c_main_bus_handle;
void init_main_i2c();
#endif // I2C_H

View File

@ -1,7 +1,10 @@
#ifndef PINS_H
#define PINS_H
#include "driver/gpio.h"
#define PIN_SDA (GPIO_NUM_7)
#define PIN_SDA (GPIO_NUM_15)
#define PIN_SCL (GPIO_NUM_15)
#define PIN_LCD_MISO (GPIO_NUM_16)
#define PIN_LCD_MOSI (GPIO_NUM_17)
@ -38,3 +41,5 @@
#define PIN_PERH2 (GPIO_NUM_4)
#define PIN_PERH3 (GPIO_NUM_2)
#define PIN_PERH4 (GPIO_NUM_1)
#endif // PINS_H