Support switch touching

This commit is contained in:
2025-03-25 19:44:19 -05:00
parent 5bb6b05002
commit 3806fd7ee8
3 changed files with 73 additions and 14 deletions
+31 -8
View File
@@ -5,16 +5,19 @@ static const char *TAG = "bottom_half";
static uint16_t keypad_state;
static uint8_t button_state;
static uint8_t switch_state;
static uint8_t switch_touch_state;
static uint8_t touch_state;
static uint16_t keypad_pressed;
static uint8_t button_pressed;
static uint8_t switch_flipped_up;
static uint8_t switch_touch_pressed;
static uint8_t touch_pressed;
static uint16_t keypad_released;
static uint8_t button_released;
static uint8_t switch_flipped_down;
static uint8_t switch_touch_released;
static uint8_t touch_released;
/// read buffer
@@ -77,8 +80,9 @@ static void receive_keypad(void) {
static void receive_button_switch(void) {
uint8_t reg = 3;
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(BOTTOM_I2C_NUM, BOTTOM_I2C_ADDR, &reg, 1, buf, 2, (100 / portTICK_PERIOD_MS)));
uint8_t new_button_state = buf[1];
uint8_t new_switch_state = buf[0];
uint8_t new_button_state = buf[1] & 0xF;
uint8_t new_switch_state = (~buf[0]) & 0xF;
uint8_t new_switch_touch_state = (buf[1] >> 4) & 0xF;
// button
uint8_t just_pressed = new_button_state & ~button_state;
@@ -90,13 +94,22 @@ static void receive_button_switch(void) {
button_state = new_button_state;
// switch
uint8_t just_flipped_down = new_switch_state & ~switch_state;
switch_flipped_down |= just_flipped_down;
uint8_t just_flipped_up = ~new_switch_state & switch_state;
uint8_t just_flipped_up = new_switch_state & ~switch_state;
switch_flipped_up |= just_flipped_up;
uint8_t just_flipped_down = ~new_switch_state & switch_state;
switch_flipped_down |= just_flipped_down;
switch_state = new_switch_state;
// switch touch
uint8_t touch_just_pressed = new_switch_touch_state & ~switch_touch_state;
switch_touch_pressed |= touch_just_pressed;
uint8_t touch_just_released = ~new_switch_touch_state & switch_touch_state;
switch_touch_released |= touch_just_released;
switch_touch_state = new_switch_touch_state;
}
static void receive_touch(void) {
@@ -275,11 +288,21 @@ bool get_flipped_switch(SwitchKey* switch_) {
}
return false;
}
uint8_t get_switch_state() {
return (uint8_t)(~switch_state & 0xF);
return switch_state;
}
bool get_switch_touch_pressed(SwitchKey* switch_) {
return _take_switch(switch_, &switch_touch_pressed);
}
bool get_switch_touch_released(SwitchKey* switch_) {
return _take_switch(switch_, &switch_touch_released);
}
uint8_t get_switch_touch_state(){
return switch_touch_state;
}
bool get_touch_state(void) {
return touch_state;
}
+15
View File
@@ -73,6 +73,8 @@ bool get_released_keypad(KeypadKey* kp);
char char_of_keypad_key(KeypadKey kp);
// TODO: add a get_keypad state?
// TODO: rename these to get_button_pressed.
/// @brief Gets the button that was just pressed (if any)
/// @param button an OUT variable for the button that was pressed (if any)
/// @return true if there was a button that was just pressed
@@ -103,6 +105,19 @@ bool get_flipped_switch(SwitchKey* switch_);
/// @return Bitflags for s1-s4.
uint8_t get_switch_state();
/// @brief Gets the switch that was just touched (if any)
/// @param switch_ an OUT variable for the switch that was touched (if any)
/// @return true if there was a switch that was just touched
bool get_switch_touch_pressed(SwitchKey* switch_);
/// @brief Gets the switch that was just un-touched (if any)
/// @param switch_ an OUT variable for the switch that was un-touched (if any)
/// @return true if there was a switch that was just un-touched
bool get_switch_touch_released(SwitchKey* switch_);
/// @brief Gets the raw state of the touched switches s1-s4 as bitflags.
/// S1 is MSB and S4 is LSB.
/// @return Bitflags for the touched state of S1-S4.
uint8_t get_switch_touch_state();
/// @brief Gets the state of the fingerprint sensor
/// @return true if the fingerprint sensor is touched
bool get_touch_state();