Support switch touching

This commit is contained in:
Mitchell Marino 2025-03-25 19:44:19 -05:00
parent 5bb6b05002
commit 3806fd7ee8
3 changed files with 73 additions and 14 deletions

View File

@ -5,16 +5,19 @@ static const char *TAG = "bottom_half";
static uint16_t keypad_state; static uint16_t keypad_state;
static uint8_t button_state; static uint8_t button_state;
static uint8_t switch_state; static uint8_t switch_state;
static uint8_t switch_touch_state;
static uint8_t touch_state; static uint8_t touch_state;
static uint16_t keypad_pressed; static uint16_t keypad_pressed;
static uint8_t button_pressed; static uint8_t button_pressed;
static uint8_t switch_flipped_up; static uint8_t switch_flipped_up;
static uint8_t switch_touch_pressed;
static uint8_t touch_pressed; static uint8_t touch_pressed;
static uint16_t keypad_released; static uint16_t keypad_released;
static uint8_t button_released; static uint8_t button_released;
static uint8_t switch_flipped_down; static uint8_t switch_flipped_down;
static uint8_t switch_touch_released;
static uint8_t touch_released; static uint8_t touch_released;
/// read buffer /// read buffer
@ -77,8 +80,9 @@ static void receive_keypad(void) {
static void receive_button_switch(void) { static void receive_button_switch(void) {
uint8_t reg = 3; 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))); 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_button_state = buf[1] & 0xF;
uint8_t new_switch_state = buf[0]; uint8_t new_switch_state = (~buf[0]) & 0xF;
uint8_t new_switch_touch_state = (buf[1] >> 4) & 0xF;
// button // button
uint8_t just_pressed = new_button_state & ~button_state; uint8_t just_pressed = new_button_state & ~button_state;
@ -90,13 +94,22 @@ static void receive_button_switch(void) {
button_state = new_button_state; button_state = new_button_state;
// switch // switch
uint8_t just_flipped_down = new_switch_state & ~switch_state; uint8_t just_flipped_up = new_switch_state & ~switch_state;
switch_flipped_down |= just_flipped_down;
uint8_t just_flipped_up = ~new_switch_state & switch_state;
switch_flipped_up |= just_flipped_up; 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_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) { static void receive_touch(void) {
@ -275,11 +288,21 @@ bool get_flipped_switch(SwitchKey* switch_) {
} }
return false; return false;
} }
uint8_t get_switch_state() { 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) { bool get_touch_state(void) {
return touch_state; return touch_state;
} }

View File

@ -73,6 +73,8 @@ bool get_released_keypad(KeypadKey* kp);
char char_of_keypad_key(KeypadKey kp); char char_of_keypad_key(KeypadKey kp);
// TODO: add a get_keypad state? // TODO: add a get_keypad state?
// TODO: rename these to get_button_pressed.
/// @brief Gets the button that was just pressed (if any) /// @brief Gets the button that was just pressed (if any)
/// @param button an OUT variable for the button that was 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 /// @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. /// @return Bitflags for s1-s4.
uint8_t get_switch_state(); 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 /// @brief Gets the state of the fingerprint sensor
/// @return true if the fingerprint sensor is touched /// @return true if the fingerprint sensor is touched
bool get_touch_state(); bool get_touch_state();

View File

@ -269,6 +269,7 @@ static void print_4bin_rev(char* out_str, uint8_t n) {
static void debug_switches(void) { static void debug_switches(void) {
clean_bomb(); clean_bomb();
uint8_t switch_state = 0xFF; uint8_t switch_state = 0xFF;
uint8_t switch_touch_state = 0xFF;
uint8_t button_state = 0xFF; uint8_t button_state = 0xFF;
char bin_buf[5] = {0}; char bin_buf[5] = {0};
@ -284,7 +285,7 @@ static void debug_switches(void) {
ESP_LOGI(TAG, "%s", buf); ESP_LOGI(TAG, "%s", buf);
} }
if (get_released_button(&button)) { if (get_released_button(&button)) {
sprintf(buf, "Button Released: %d ", button); sprintf(buf, "Button Released: %d", button);
lcd_print(0, 3, buf); lcd_print(0, 3, buf);
ESP_LOGI(TAG, "%s", buf); ESP_LOGI(TAG, "%s", buf);
} }
@ -298,13 +299,32 @@ static void debug_switches(void) {
lcd_print(0, 3, buf); lcd_print(0, 3, buf);
ESP_LOGI(TAG, "%s", buf); ESP_LOGI(TAG, "%s", buf);
} }
if (get_switch_touch_pressed(&switch_)) {
sprintf(buf, "Switch Touch: %d ", switch_);
lcd_print(0, 3, buf);
ESP_LOGI(TAG, "%s", buf);
}
if (get_switch_touch_released(&switch_)) {
sprintf(buf, "Switch Un-touch: %d", switch_);
lcd_print(0, 3, buf);
ESP_LOGI(TAG, "%s", buf);
}
uint8_t new_switch_touch_state = get_switch_touch_state();
if (new_switch_touch_state != switch_touch_state) {
switch_touch_state = new_switch_touch_state;
print_4bin_rev(bin_buf, switch_touch_state);
sprintf(buf, "t: %s", bin_buf);
lcd_print(1, 0, buf);
ESP_LOGI(TAG, "%s", buf);
}
uint8_t new_switch_state = get_switch_state(); uint8_t new_switch_state = get_switch_state();
if (new_switch_state != switch_state) { if (new_switch_state != switch_state) {
switch_state = new_switch_state; switch_state = new_switch_state;
print_4bin_rev(bin_buf, switch_state); print_4bin_rev(bin_buf, switch_state);
sprintf(buf, "s: %s", bin_buf); sprintf(buf, "s: %s", bin_buf);
lcd_print(1, 0, buf); lcd_print(1, 1, buf);
ESP_LOGI(TAG, "%s", buf); ESP_LOGI(TAG, "%s", buf);
} }
@ -313,10 +333,11 @@ static void debug_switches(void) {
button_state = new_button_state; button_state = new_button_state;
print_4bin_rev(bin_buf, button_state); print_4bin_rev(bin_buf, button_state);
sprintf(buf, "b: %s", bin_buf); sprintf(buf, "b: %s", bin_buf);
lcd_print(1, 1, buf); lcd_print(1, 2, buf);
ESP_LOGI(TAG, "%s", buf); ESP_LOGI(TAG, "%s", buf);
} }
if (get_pressed_keypad(&key) && key == KeypadKey::pound) { if (get_pressed_keypad(&key) && key == KeypadKey::pound) {
return; return;
} }