Skip to content

Commit

Permalink
Enhance key function
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisxhe committed Apr 8, 2024
1 parent 3e46075 commit 490655e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/LilyGo_Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions src/LilyGo_Button.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
};
6 changes: 5 additions & 1 deletion src/LilyGo_Wristband.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 490655e

Please sign in to comment.