Skip to content

Instantly share code, notes, and snippets.

@Elijahg
Created April 2, 2019 02:41
Show Gist options
  • Save Elijahg/01099f8b9f1d695a9e0892df61e7142e to your computer and use it in GitHub Desktop.
Save Elijahg/01099f8b9f1d695a9e0892df61e7142e to your computer and use it in GitHub Desktop.
/* Touch Pad Interrupt Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#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"
static const char* TAG = "Touch pad";
#define TOUCH_THRESH_NO_USE (0)
#define TOUCHPAD_FILTER_TOUCH_PERIOD (10)
static bool s_pad_activated[TOUCH_PAD_MAX];
static uint32_t s_pad_init_val[TOUCH_PAD_MAX];
volatile uint16_t isrMask=0;
#define TOUCH_PAD_MAX 4
static void tp_example_read_task(void *pvParameter)
{
int show_message=1;
touch_trigger_mode_t triggerMode;
while (1) {
//interrupt mode, enable touch interrupt
touch_pad_intr_enable();
if (isrMask!=0){
ESP_LOGI(TAG, "Bit mask from ISR: %#x",isrMask );
}
isrMask=0;
/* for (int i = 2; i < TOUCH_PAD_MAX; i++) {
if (s_pad_activated[i] == true) {
ESP_LOGI(TAG, "T%d activated!", i);
// Wait a while for the pad being released
vTaskDelay(20 / portTICK_PERIOD_MS);
// Clear information on pad activation
s_pad_activated[i] = false;
// Reset the counter triggering a message
// that application is running
}
}
*/
vTaskDelay(10 / portTICK_PERIOD_MS);
// If no pad is touched, every couple of seconds, show a message
// that application is running
if (show_message++ % 500 == 0) {
touch_pad_get_trigger_mode(&triggerMode);
/* if (triggerMode==TOUCH_TRIGGER_BELOW){
ESP_LOGI(TAG, "Switching trigger mode to ABOVE");
touch_pad_set_trigger_mode(TOUCH_TRIGGER_ABOVE);
} else {
*/
ESP_LOGI(TAG, "Trigger mode is BELOW");
touch_pad_set_trigger_mode(TOUCH_TRIGGER_BELOW);
//}
}
}
}
/*
Handle an interrupt triggered when a pad is touched.
Recognize what pad has been touched and save it in a table.
*/
static void tp_example_rtc_intr(void * arg)
{
isrMask = touch_pad_get_status();
//clear interrupt
touch_pad_clear_status();
}
void app_main()
{
// Initialize touch pad peripheral, it will start a timer to run a filter
ESP_LOGI(TAG, "Initializing touch pad");
touch_pad_init();
// If use interrupt trigger mode, should set touch sensor FSM mode at 'TOUCH_FSM_MODE_TIMER'.
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
touch_pad_set_trigger_mode(TOUCH_TRIGGER_BELOW);
// Set reference voltage for charging/discharging
// For most usage scenarios, we recommend using the following combination:
// the high reference valtage will be 2.7V - 1V = 1.7V, The low reference voltage will be 0.5V.
touch_pad_set_voltage(TOUCH_HVOLT_2V7, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_1V);
// Init touch pad IO
for (int i = 2;i< TOUCH_PAD_MAX;i++) {
//init RTC IO and mode for touch pad.
touch_pad_config(i, TOUCH_THRESH_NO_USE);
}
// Set thresh hold
uint16_t touch_value;
for (int i = 2; i<TOUCH_PAD_MAX; i++) {
//read filtered value
touch_pad_read(i, &touch_value);
s_pad_init_val[i] = touch_value;
ESP_LOGI(TAG, "test init: touch pad [%d] val is %d", i, touch_value);
//set interrupt threshold.
ESP_ERROR_CHECK(touch_pad_set_thresh(i, touch_value * 0.95));
}
// Register touch interrupt ISR
touch_pad_isr_register(tp_example_rtc_intr, NULL);
// Start a task to show what pads have been touched
xTaskCreate(&tp_example_read_task, "touch_pad_read_task", 2048, NULL, 5, NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment