Browse Source

Add debouncing for touchpads

main
Hendrik Langer 8 years ago
parent
commit
3ec8651ea2
  1. 7
      src/keyboard.cpp
  2. 2
      src/keyboard.h
  3. 22
      src/main.cpp

7
src/keyboard.cpp

@ -12,6 +12,8 @@
using namespace std; using namespace std;
void (*Keyboard::callbacks[TOUCH_PAD_NUM])();
Keyboard::Keyboard() { Keyboard::Keyboard() {
} }
@ -31,6 +33,7 @@ static void tp_example_rtc_intr(void *arg) {
for (int i = 0; i < TOUCH_PAD_NUM; i++) { for (int i = 0; i < TOUCH_PAD_NUM; i++) {
if ((pad_intr >> i) & 0x01) { if ((pad_intr >> i) & 0x01) {
s_pad_activated[i] = true; s_pad_activated[i] = true;
//if (Keyboard::callbacks[i]) Keyboard::callbacks[i]();
} }
} }
} }
@ -44,6 +47,10 @@ bool Keyboard::getTouchDetected(uint8_t pad) {
return res; return res;
} }
void Keyboard::setCallback(uint8_t key_id, void(*new_function)()) {
Keyboard::callbacks[key_id] = new_function;
}
void Keyboard::init() { void Keyboard::init() {
touch_pad_init(); touch_pad_init();

2
src/keyboard.h

@ -16,6 +16,8 @@ class Keyboard {
Keyboard(); Keyboard();
void init(); void init();
bool getTouchDetected(uint8_t pad); bool getTouchDetected(uint8_t pad);
void setCallback(uint8_t key_id, void(*new_function)());
static void (*callbacks[TOUCH_PAD_NUM])();
private: private:
}; };

22
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"}, {"/T3.wav", "/T4.wav", "/T5.wav"},
{"/T6.wav", "/T7.wav", "/T8.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() void setup()
{ {
// initialize LED digital pin as an output. // initialize LED digital pin as an output.
@ -42,23 +45,24 @@ void setup()
static const char *bootSound = "/boot.wav"; 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() void loop()
{ {
bool touched = false; bool touched = false;
bool playing = false;
for(int i=0; i<=9; i++) { for(int i=0; i<=9; i++) {
if (keyboard.getTouchDetected(i) == true) { if (keyboard.getTouchDetected(i) == true) {
touched = true; if ((millis() - lastDebounceTime) > debounceDelay) {
Serial.print("touch "); lastDebounceTime = millis();
Serial.println(i, DEC); Serial.println("main loop touched==true: play new sound");
if (playing == false) {
playing == true;
Serial.println("main loop touched==true: new task");
sound.play(soundFile[i/3][i%3]); sound.play(soundFile[i/3][i%3]);
} }
Serial.print("touch ");
Serial.println(i, DEC);
} }
} }
@ -71,6 +75,6 @@ void loop()
} }
wifi.loop(); wifi.loop();
// wait for a second // wait for a moment
delay(50); delay(50);
} }

Loading…
Cancel
Save