expander work
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user