From 3ec8651ea2bf079f8763fb389773368c4055e15b Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Fri, 21 Apr 2017 22:38:34 +0200 Subject: [PATCH] Add debouncing for touchpads --- src/keyboard.cpp | 7 +++++++ src/keyboard.h | 2 ++ src/main.cpp | 22 +++++++++++++--------- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/keyboard.cpp b/src/keyboard.cpp index 21d6e00..7c60c16 100644 --- a/src/keyboard.cpp +++ b/src/keyboard.cpp @@ -12,6 +12,8 @@ using namespace std; +void (*Keyboard::callbacks[TOUCH_PAD_NUM])(); + Keyboard::Keyboard() { } @@ -31,6 +33,7 @@ static void tp_example_rtc_intr(void *arg) { for (int i = 0; i < TOUCH_PAD_NUM; i++) { if ((pad_intr >> i) & 0x01) { s_pad_activated[i] = true; + //if (Keyboard::callbacks[i]) Keyboard::callbacks[i](); } } } @@ -44,6 +47,10 @@ bool Keyboard::getTouchDetected(uint8_t pad) { return res; } +void Keyboard::setCallback(uint8_t key_id, void(*new_function)()) { + Keyboard::callbacks[key_id] = new_function; +} + void Keyboard::init() { touch_pad_init(); diff --git a/src/keyboard.h b/src/keyboard.h index 4b13ce1..b8da103 100644 --- a/src/keyboard.h +++ b/src/keyboard.h @@ -16,6 +16,8 @@ class Keyboard { Keyboard(); void init(); bool getTouchDetected(uint8_t pad); + void setCallback(uint8_t key_id, void(*new_function)()); + static void (*callbacks[TOUCH_PAD_NUM])(); private: }; diff --git a/src/main.cpp b/src/main.cpp index f0ef103..40a5ae4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,6 +24,9 @@ static const char *soundFile[3][3] = {{"/T0.wav", "/T1.wav", "/T2.wav"}, {"/T3.wav", "/T4.wav", "/T5.wav"}, {"/T6.wav", "/T7.wav", "/T8.wav"}}; +unsigned long lastDebounceTime = 0; // the last time the output pin was toggled +unsigned long debounceDelay = 100; // the debounce time + void setup() { // initialize LED digital pin as an output. @@ -42,23 +45,24 @@ void setup() static const char *bootSound = "/boot.wav"; - //xTaskCreate(&(Sound::play_task), "play_task", 3072, (void*)bootSound, 5, &xTaskSound); + sound.play(bootSound); + +// keyboard.setCallback(0, [&soundFile]() {int i=0; sound.play(soundFile[i/3][i%3]);}); +// keyboard.setCallback(1, []() {Serial.println("interrupt 1");}); } void loop() { bool touched = false; - bool playing = false; for(int i=0; i<=9; i++) { if (keyboard.getTouchDetected(i) == true) { - touched = true; - Serial.print("touch "); - Serial.println(i, DEC); - if (playing == false) { - playing == true; - Serial.println("main loop touched==true: new task"); + if ((millis() - lastDebounceTime) > debounceDelay) { + lastDebounceTime = millis(); + Serial.println("main loop touched==true: play new sound"); sound.play(soundFile[i/3][i%3]); } + Serial.print("touch "); + Serial.println(i, DEC); } } @@ -71,6 +75,6 @@ void loop() } wifi.loop(); - // wait for a second + // wait for a moment delay(50); }