#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" using namespace std; void (*Keyboard::callbacks[TOUCH_PAD_NUM])(); Keyboard::Keyboard() { } /* 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; //if (Keyboard::callbacks[i]) Keyboard::callbacks[i](); } } } } bool Keyboard::getTouchDetected(uint8_t pad) { if (pad==8) pad=9; // fix hardware wiring if (pad==1) pad=8; bool res = s_pad_activated[pad]; s_pad_activated[pad] = false; return res; } void Keyboard::setCallback(uint8_t key_id, void(*new_function)()) { Keyboard::callbacks[key_id] = new_function; } void Keyboard::init() { touch_pad_init(); Serial.println("touchpad values:"); for (int i=0; i