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;
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();

2
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:
};

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"},
{"/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);
}

Loading…
Cancel
Save