Merge branch 'bugfix/touch_pad_driver' into 'master'
driver(touch): fix bug and add more features See merge request !1056
This commit is contained in:
@@ -16,13 +16,16 @@
|
||||
#include "soc/sens_reg.h"
|
||||
|
||||
static const char* TAG = "Touch pad";
|
||||
#define TOUCH_THRESH_NO_USE (0)
|
||||
#define TOUCH_THRESH_PERCENT (99)
|
||||
|
||||
static bool s_pad_activated[TOUCH_PAD_MAX];
|
||||
static uint32_t s_pad_init_val[TOUCH_PAD_MAX];
|
||||
|
||||
|
||||
/*
|
||||
Read values sensed at all available touch pads.
|
||||
Use half of read value as the threshold
|
||||
Use 2 / 3 of read value as the threshold
|
||||
to trigger interrupt when the pad is touched.
|
||||
Note: this routine demonstrates a simple way
|
||||
to configure activation threshold for the touch pads.
|
||||
@@ -32,9 +35,17 @@ static bool s_pad_activated[TOUCH_PAD_MAX];
|
||||
static void tp_example_set_thresholds(void)
|
||||
{
|
||||
uint16_t touch_value;
|
||||
for (int i=0; i<TOUCH_PAD_MAX; i++) {
|
||||
ESP_ERROR_CHECK(touch_pad_read(i, &touch_value));
|
||||
ESP_ERROR_CHECK(touch_pad_config(i, touch_value/2));
|
||||
//delay some time in order to make the filter work and get a initial value
|
||||
vTaskDelay(500/portTICK_PERIOD_MS);
|
||||
|
||||
for (int i = 0; i<TOUCH_PAD_MAX; i++) {
|
||||
//read filtered value
|
||||
touch_pad_read_filtered(i, &touch_value);
|
||||
s_pad_init_val[i] = touch_value;
|
||||
ESP_LOGI(TAG, "test init touch val: %d\n", touch_value);
|
||||
//set interrupt threshold.
|
||||
ESP_ERROR_CHECK(touch_pad_set_thresh(i, touch_value * 2 / 3));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,29 +54,69 @@ static void tp_example_set_thresholds(void)
|
||||
by reading a table updated by rtc_intr()
|
||||
If so, then print it out on a serial monitor.
|
||||
Clear related entry in the table afterwards
|
||||
|
||||
In interrupt mode, the table is updated in touch ISR.
|
||||
|
||||
In filter mode, we will compare the current filtered value with the initial one.
|
||||
If the current filtered value is less than 99% of the initial value, we can
|
||||
regard it as a 'touched' event.
|
||||
When calling touch_pad_init, a timer will be started to run the filter.
|
||||
This mode is designed for the situation that the pad is covered
|
||||
by a 2-or-3-mm-thick medium, usually glass or plastic.
|
||||
The difference caused by a 'touch' action could be very small, but we can still use
|
||||
filter mode to detect a 'touch' event.
|
||||
*/
|
||||
static void tp_example_read_task(void *pvParameter)
|
||||
{
|
||||
static int show_message;
|
||||
int change_mode = 0;
|
||||
int filter_mode = 0;
|
||||
while (1) {
|
||||
for (int i=0; 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(200 / portTICK_PERIOD_MS);
|
||||
// Clear information on pad activation
|
||||
s_pad_activated[i] = false;
|
||||
// Reset the counter triggering a message
|
||||
// that application is running
|
||||
show_message = 1;
|
||||
if (filter_mode == 0) {
|
||||
//interrupt mode, enable touch interrupt
|
||||
touch_pad_intr_enable();
|
||||
for (int i = 0; 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(200 / portTICK_PERIOD_MS);
|
||||
// Clear information on pad activation
|
||||
s_pad_activated[i] = false;
|
||||
// Reset the counter triggering a message
|
||||
// that application is running
|
||||
show_message = 1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//filter mode, disable touch interrupt
|
||||
touch_pad_intr_disable();
|
||||
for (int i = 0; i < TOUCH_PAD_MAX; i++) {
|
||||
uint16_t value = 0;
|
||||
touch_pad_read_filtered(i, &value);
|
||||
if (value < s_pad_init_val[i] * TOUCH_THRESH_PERCENT / 100) {
|
||||
ESP_LOGI(TAG, "T%d activated!", i);
|
||||
ESP_LOGI(TAG, "value: %d; init val: %d\n", value, s_pad_init_val[i]);
|
||||
vTaskDelay(200 / portTICK_PERIOD_MS);
|
||||
// Reset the counter to stop changing mode.
|
||||
change_mode = 1;
|
||||
show_message = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
ESP_LOGI(TAG, "Waiting for any pad being touched...");
|
||||
}
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
// Change mode if no pad is touched for a long time.
|
||||
// We can compare the two different mode.
|
||||
if (change_mode++ % 2000 == 0) {
|
||||
filter_mode = !filter_mode;
|
||||
ESP_LOGI(TAG, "Change mode...%s\n", filter_mode == 0? "interrupt mode": "filter mode");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,29 +126,51 @@ static void tp_example_read_task(void *pvParameter)
|
||||
*/
|
||||
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);
|
||||
uint32_t pad_intr = touch_pad_get_status();
|
||||
//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_MAX; i++) {
|
||||
if ((pad_intr >> i) & 0x01) {
|
||||
s_pad_activated[i] = true;
|
||||
}
|
||||
touch_pad_clear_status();
|
||||
for (int i = 0; i < TOUCH_PAD_MAX; i++) {
|
||||
if ((pad_intr >> i) & 0x01) {
|
||||
s_pad_activated[i] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Before reading touch pad, we need to initialize the RTC IO.
|
||||
*/
|
||||
static void tp_example_touch_pad_init()
|
||||
{
|
||||
for (int i = 0;i< TOUCH_PAD_MAX;i++) {
|
||||
//init RTC IO and mode for touch pad.
|
||||
touch_pad_config(i, TOUCH_THRESH_NO_USE);
|
||||
}
|
||||
}
|
||||
|
||||
void app_main()
|
||||
{
|
||||
// Initialize touch pad peripheral
|
||||
// Initialize touch pad peripheral, it will start a timer to run a filter
|
||||
ESP_LOGI(TAG, "Initializing touch pad");
|
||||
touch_pad_init();
|
||||
|
||||
// Initialize and start a software filter to detect slight change of capacitance.
|
||||
touch_pad_filter_start(10);
|
||||
// Set measuring time and sleep time
|
||||
// In this case, measurement will sustain 0xffff / 8Mhz = 8.19ms
|
||||
// Meanwhile, sleep time between two measurement will be 0x1000 / 150Khz = 27.3 ms
|
||||
touch_pad_set_meas_time(0x1000, 0xffff);
|
||||
|
||||
//set reference voltage for charging/discharging
|
||||
// In this case, the high reference valtage will be 2.4V - 1.5V = 0.9V
|
||||
// The low reference voltage will be 0.8V, so that the procedure of charging
|
||||
// and discharging would be very fast.
|
||||
touch_pad_set_voltage(TOUCH_HVOLT_2V4, TOUCH_LVOLT_0V8, TOUCH_HVOLT_ATTEN_1V5);
|
||||
// Init touch pad IO
|
||||
tp_example_touch_pad_init();
|
||||
// Set thresh hold
|
||||
tp_example_set_thresholds();
|
||||
touch_pad_isr_handler_register(tp_example_rtc_intr, NULL, 0, NULL);
|
||||
// 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);
|
||||
|
||||
@@ -9,24 +9,78 @@
|
||||
#include <stdio.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
#include "driver/touch_pad.h"
|
||||
|
||||
|
||||
#define TOUCH_TEST_LOOP_NUM (10)
|
||||
#define TOUCH_PAD_NO_CHANGE (-1)
|
||||
#define TOUCH_THRESH_NO_USE (0)
|
||||
/*
|
||||
Read values sensed at all available touch pads.
|
||||
Print out values in a loop on a serial monitor.
|
||||
Print out values in a loop on a serial monitor.
|
||||
*/
|
||||
static void tp_example_read_task(void *pvParameter)
|
||||
{
|
||||
while (1) {
|
||||
uint16_t touch_value;
|
||||
for (int i=0; i<TOUCH_PAD_MAX; i++) {
|
||||
ESP_ERROR_CHECK(touch_pad_read(i, &touch_value));
|
||||
printf("T%d:%4d ", i, touch_value);
|
||||
//set reference voltage for charging/discharging
|
||||
// In this case, the high reference valtage will be 2.7V - 0V = 2.7V
|
||||
// The low reference voltage will be 0.5
|
||||
// So the charing/discharging time would be longer, so the counter value would be smaller.
|
||||
touch_pad_set_voltage(TOUCH_HVOLT_2V7, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_0V);
|
||||
|
||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||
printf("Case[1], set default measure time\n");
|
||||
for (int j = 0; j < TOUCH_TEST_LOOP_NUM; j++) {
|
||||
for (int i = 0; i < TOUCH_PAD_MAX; i++) {
|
||||
ESP_ERROR_CHECK(touch_pad_read(i, &touch_value));
|
||||
printf("T%d:%5d ", i, touch_value);
|
||||
}
|
||||
printf("\n");
|
||||
vTaskDelay(500 / portTICK_PERIOD_MS);
|
||||
}
|
||||
printf("\n");
|
||||
vTaskDelay(500 / portTICK_PERIOD_MS);
|
||||
|
||||
printf("Case[2], set max measure time\n");
|
||||
//set reference voltage for charging/discharging
|
||||
// In this case, the high reference valtage will be 2.4V - 1.5V = 0.9V
|
||||
// The low reference voltage will be 0.8
|
||||
// So the charing/discharging time would be shorter, so the counter value would be larger.
|
||||
touch_pad_set_voltage(TOUCH_HVOLT_2V4, TOUCH_LVOLT_0V8, TOUCH_HVOLT_ATTEN_1V5);
|
||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||
for (int j = 0; j < TOUCH_TEST_LOOP_NUM; j++) {
|
||||
for (int i = 0; i < TOUCH_PAD_MAX; i++) {
|
||||
ESP_ERROR_CHECK(touch_pad_read(i, &touch_value));
|
||||
printf("T%d:%5d ", i, touch_value);
|
||||
}
|
||||
printf("\n");
|
||||
vTaskDelay(500 / portTICK_PERIOD_MS);
|
||||
}
|
||||
|
||||
|
||||
touch_pad_set_voltage(TOUCH_HVOLT_2V7, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_0V);
|
||||
vTaskDelay(100/portTICK_PERIOD_MS);
|
||||
|
||||
printf("Case[3], set differen slope for each channel\n");
|
||||
for (int i = 0;i<TOUCH_PAD_MAX;i++) {
|
||||
touch_pad_set_cnt_mode(i, (i % TOUCH_PAD_SLOPE_7) + 1, TOUCH_PAD_TIE_OPT_HIGH);
|
||||
}
|
||||
for (int j = 0; j < TOUCH_TEST_LOOP_NUM; j++) {
|
||||
for (int i = 0; i < TOUCH_PAD_MAX; i++) {
|
||||
ESP_ERROR_CHECK(touch_pad_read(i, &touch_value));
|
||||
printf("T%d:%5d ", i, touch_value);
|
||||
}
|
||||
printf("\n");
|
||||
vTaskDelay(500 / portTICK_PERIOD_MS);
|
||||
}
|
||||
for (int i = 0;i<TOUCH_PAD_MAX;i++) {
|
||||
touch_pad_set_cnt_mode(i, TOUCH_PAD_SLOPE_7, TOUCH_PAD_TIE_OPT_HIGH);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void tp_example_touch_pad_init()
|
||||
{
|
||||
for (int i = 0;i< TOUCH_PAD_MAX;i++) {
|
||||
touch_pad_config(i, TOUCH_THRESH_NO_USE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +88,7 @@ void app_main()
|
||||
{
|
||||
// Initialize touch pad peripheral
|
||||
touch_pad_init();
|
||||
tp_example_touch_pad_init();
|
||||
|
||||
// Start task to read values sensed by pads
|
||||
xTaskCreate(&tp_example_read_task, "touch_pad_read_task", 2048, NULL, 5, NULL);
|
||||
|
||||
Reference in New Issue
Block a user