From 490655e68caf6a75eb7de0444b4db882f01f14fc Mon Sep 17 00:00:00 2001 From: lewisxhe Date: Mon, 8 Apr 2024 11:54:55 +0800 Subject: [PATCH] Enhance key function --- src/LilyGo_Button.cpp | 13 +++++++++++-- src/LilyGo_Button.h | 5 +++-- src/LilyGo_Wristband.cpp | 6 +++++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/LilyGo_Button.cpp b/src/LilyGo_Button.cpp index ee7462c..2adab8c 100644 --- a/src/LilyGo_Button.cpp +++ b/src/LilyGo_Button.cpp @@ -8,9 +8,14 @@ */ #include "LilyGo_Button.h" -void LilyGo_Button::init(uint32_t gpio, uint32_t debounceTimeout ) + +void LilyGo_Button::init(uint32_t gpio, uint32_t debounceTimeout, gpio_read_callback cb) { this->gpio = gpio; + read_pin_cb = cb; + if (!read_pin_cb) { + pinMode(this->gpio, INPUT_PULLUP); + } setDebounceTime(debounceTimeout); } @@ -46,7 +51,11 @@ void LilyGo_Button::update() { prev_state = curr_state; - curr_state = touchInterruptGetLastStatus(gpio) == 0; + if (read_pin_cb) { + curr_state = read_pin_cb(); + } else { + curr_state = digitalRead(this->gpio) == LOW; + } if (prev_state == HIGH && curr_state == LOW) { down_ms = millis(); diff --git a/src/LilyGo_Button.h b/src/LilyGo_Button.h index a263da0..a627197 100644 --- a/src/LilyGo_Button.h +++ b/src/LilyGo_Button.h @@ -34,9 +34,9 @@ enum ButtonState { class LilyGo_Button { typedef void (*event_callback) (ButtonState state); + typedef bool (*gpio_read_callback) (); public: - - void init(uint32_t gpio, uint32_t debounceTimeout = DEBOUNCE_MS); + void init(uint32_t gpio, uint32_t debounceTimeout = DEBOUNCE_MS, gpio_read_callback cb = NULL); void setDebounceTime(uint32_t ms); void setEventCallback(event_callback f); void update(); @@ -58,4 +58,5 @@ class LilyGo_Button bool longclick_detected = false; bool long_pressed_detected = false; event_callback event_cb = NULL; + gpio_read_callback read_pin_cb = NULL; }; diff --git a/src/LilyGo_Wristband.cpp b/src/LilyGo_Wristband.cpp index 66a8c05..574d64c 100644 --- a/src/LilyGo_Wristband.cpp +++ b/src/LilyGo_Wristband.cpp @@ -338,6 +338,10 @@ static esp_err_t panel_jd9613_set_rotation(esp_lcd_panel_t *panel, uint8_t r) } __END_DECLS +static bool touchPadReadFunction() +{ + return touchInterruptGetLastStatus(BOARD_TOUCH_BUTTON) == 0; +} LilyGo_Wristband::LilyGo_Wristband(): _brightness(AMOLED_DEFAULT_BRIGHTNESS), panel_handle(NULL), threshold(2000) { @@ -386,7 +390,7 @@ bool LilyGo_Wristband::begin() // Initialize touch button touchAttachInterrupt(BOARD_TOUCH_BUTTON, touchISR, threshold); - LilyGo_Button::init(BOARD_TOUCH_BUTTON); + LilyGo_Button::init(BOARD_TOUCH_BUTTON, 50, touchPadReadFunction); // Initialize vibration motor tone(BOARD_VIBRATION_PIN, 1000, 50);