cleanup expander

This commit is contained in:
2026-04-01 15:55:23 -05:00
parent 1b4795b4de
commit c82c9adf32
2 changed files with 92 additions and 58 deletions
+72 -29
View File
@@ -32,6 +32,9 @@ enum class Button: uint8_t {
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.
enum class Switch: uint8_t {
S1 = 0,
@@ -40,11 +43,17 @@ enum class Switch: uint8_t {
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 {
Released = 0,
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.
enum class KeypadKey: uint8_t {
K0 = 0,
@@ -65,26 +74,44 @@ enum class KeypadKey: uint8_t {
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 {
private:
// [bit2: pressed] [bit1-0: switch]
// [bit2: up] [bit1-0: switch]
uint8_t data;
public:
// Constructor
SwitchFlip(Switch sw, bool pressed)
SwitchFlip(Switch sw, bool up)
: data((static_cast<uint8_t>(sw) & 0b11) |
((pressed ? 1 : 0) << 2)) {}
((up ? 1 : 0) << 2)) {}
// Default constructor
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
Switch get_switch() const {
return static_cast<Switch>(data & 0b11);
}
bool is_pressed() const {
bool is_up() const {
return (data >> 2) & 1;
}
@@ -93,8 +120,8 @@ public:
data = (data & ~0b11) | (static_cast<uint8_t>(sw) & 0b11);
}
void set_pressed(bool pressed) {
data = (data & ~(1 << 2)) | ((pressed ? 1 : 0) << 2);
void set_up(bool up) {
data = (data & ~(1 << 2)) | ((up ? 1 : 0) << 2);
}
};
static_assert(sizeof(SwitchFlip) == 1);
@@ -113,6 +140,12 @@ public:
// Default constructor
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
Switch get_switch() const {
return static_cast<Switch>(data & 0b11);
@@ -146,6 +179,12 @@ public:
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
uint8_t number() const {
return data & 0b11;
@@ -199,35 +238,39 @@ struct ExpanderState {
class InputsController {
public:
void clear_all_events();
static void clear_all_events();
bool has_button_press() const;
std::optional<Button> get_button_press();
Button wait_button_press();
uint8_t button_state();
static ExpanderState get_input_state();
static bool has_button_press();
static std::optional<Button> get_button_press();
static Button wait_button_press();
static uint8_t button_state();
bool has_button_release() const;
std::optional<Button> get_button_release();
Button wait_button_release();
static bool has_button_release();
static std::optional<Button> get_button_release();
static Button wait_button_release();
bool has_switch_flip() const;
std::optional<SwitchFlip> get_switch_flip();
SwitchFlip wait_switch_flip();
uint8_t switch_state();
static bool has_switch_flip();
static std::optional<SwitchFlip> get_switch_flip();
static SwitchFlip wait_switch_flip();
static uint8_t switch_state();
bool has_switch_touch() const;
std::optional<SwitchTouch> get_switch_touch();
SwitchTouch wait_switch_touch();
uint8_t switch_touch_state();
static bool has_switch_touch();
static std::optional<SwitchTouch> get_switch_touch();
static SwitchTouch wait_switch_touch();
static uint8_t switch_touch_state();
bool has_keypad_press() const;
std::optional<KeypadKey> get_keypad_press();
KeypadKey wait_keypad_press();
static bool has_keypad_press();
static std::optional<KeypadKey> get_keypad_press();
static KeypadKey wait_keypad_press();
bool has_keypad_release() const;
std::optional<KeypadKey> get_keypad_release();
KeypadKey wait_keypad_release();
uint16_t keypad_state();
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