diff --git a/src/keyboard.cpp b/src/keyboard.cpp index 5854155..c979099 100644 --- a/src/keyboard.cpp +++ b/src/keyboard.cpp @@ -1,3 +1,12 @@ +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_log.h" + +#include "driver/touch_pad.h" +#include "soc/rtc_cntl_reg.h" +#include "soc/sens_reg.h" + #include "Arduino.h" #include "keyboard.h" @@ -6,64 +15,56 @@ using namespace std; Keyboard::Keyboard() { } -void gotTouch0() { - touchDetected[0] = true; -} -void gotTouch1() { - touchDetected[1] = true; -} -void gotTouch2() { - touchDetected[2] = true; -} -void gotTouch3() { - touchDetected[3] = true; -} -void gotTouch4() { - touchDetected[4] = true; -} -void gotTouch5() { - touchDetected[5] = true; -} -void gotTouch6() { - touchDetected[6] = true; -} -void gotTouch7() { - touchDetected[7] = true; -} -void gotTouch8() { - touchDetected[8] = true; -} -void gotTouch9() { - touchDetected[9] = true; +/* + Handle an interrupt triggered when a pad is touched. + Recognize what pad has been touched and save it in a table. + https://github.com/espressif/esp-idf/blob/master/examples/peripherals/touch_pad_interrupt/main/tp_interrupt_main.c + */ +static void tp_example_rtc_intr(void *arg) { + uint32_t pad_intr = READ_PERI_REG(SENS_SAR_TOUCH_CTRL2_REG) & 0x3ff; + uint32_t rtc_intr = READ_PERI_REG(RTC_CNTL_INT_ST_REG); + //clear interrupt + WRITE_PERI_REG(RTC_CNTL_INT_CLR_REG, rtc_intr); + SET_PERI_REG_MASK(SENS_SAR_TOUCH_CTRL2_REG, SENS_TOUCH_MEAS_EN_CLR); + + if (rtc_intr & RTC_CNTL_TOUCH_INT_ST) { + for (int i = 0; i < TOUCH_PAD_NUM; i++) { + if ((pad_intr >> i) & 0x01) { + s_pad_activated[i] = true; + } + } + } } bool Keyboard::getTouchDetected(uint8_t pad) { - bool res = touchDetected[pad]; - touchDetected[pad] = false; + bool res = s_pad_activated[pad]; + s_pad_activated[pad] = false; return res; } void Keyboard::init() { - threshold[0] = 40; + touch_pad_init(); + + Serial.println("touchpad values:"); + for (int i=0; i