Hendrik Langer 7 years ago
parent
commit
b646e09b79
  1. 101
      src/keyboard.cpp
  2. 11
      src/keyboard.h
  3. 3
      src/main.cpp

101
src/keyboard.cpp

@ -1,3 +1,12 @@
#include <stdio.h>
#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<TOUCH_PAD_NUM; i++) {
uint16_t touch_value;
touch_pad_read((touch_pad_t)i, &touch_value);
Serial.println(touch_value, DEC);
}
// set thresholds // ToDo: don't hardcode thresholds
threshold[0] = 500;
threshold[1] = 0;
threshold[2] = 0;
threshold[3] = 40;
threshold[4] = 40;
threshold[5] = 40;
threshold[6] = 40;
threshold[7] = 40;
threshold[8] = 20;
threshold[9] = 20;
for (int i=0; i<=9; i++) touchDetected[i] = false;
touchAttachInterrupt(T0, gotTouch0, threshold[0]);
// touchAttachInterrupt(T1, gotTouch1, threshold[1]); // GPIO_00 not usable on dev board
// touchAttachInterrupt(T2, gotTouch2, threshold[2]); // GPIO_02 not usable on dev board
touchAttachInterrupt(T3, gotTouch3, threshold[3]);
touchAttachInterrupt(T4, gotTouch4, threshold[4]);
touchAttachInterrupt(T5, gotTouch5, threshold[5]);
touchAttachInterrupt(T6, gotTouch6, threshold[6]);
touchAttachInterrupt(T7, gotTouch7, threshold[7]);
touchAttachInterrupt(T8, gotTouch8, threshold[8]);
touchAttachInterrupt(T9, gotTouch9, threshold[9]);
threshold[3] = 666;
threshold[4] = 700;
threshold[5] = 700;
threshold[6] = 800;
threshold[7] = 800;
threshold[8] = 170;
threshold[9] = 350;
for (int i=0; i<TOUCH_PAD_NUM; i++) touch_pad_config((touch_pad_t)i, threshold[i]);
// interrupt handler
touch_pad_isr_handler_register(tp_example_rtc_intr, NULL, 0, NULL);
}

11
src/keyboard.h

@ -1,10 +1,15 @@
#ifndef _KEYBOARD_H
#define _KEYBOARD_H
#include "Arduino.h"
#include "driver/touch_pad.h"
#define TOUCH_PAD_NUM 10
static bool s_pad_activated[TOUCH_PAD_NUM];
static bool touchDetected[TOUCH_PAD_NUM];
static uint16_t threshold[TOUCH_PAD_NUM];
static bool touchDetected[10];
static uint8_t threshold[10];
class Keyboard {
public:

3
src/main.cpp

@ -18,13 +18,14 @@ void setup()
{
// initialize LED digital pin as an output.
pinMode(LED_PIN, OUTPUT);
keyboard.init();
Serial.begin(115200);
Serial.println("-------------------------------------");
Serial.print("Soundboard v.");
Serial.println(VERSION);
Serial.println("https://dev.xd0.de/hendrik/soundboard");
Serial.println("-------------------------------------");
keyboard.init();
}
void loop()

Loading…
Cancel
Save