Skip to content

Instantly share code, notes, and snippets.

@elieDaan
Created December 20, 2019 09:59
Show Gist options
  • Save elieDaan/735146c1b686a5f320061658954c5020 to your computer and use it in GitHub Desktop.
Save elieDaan/735146c1b686a5f320061658954c5020 to your computer and use it in GitHub Desktop.
/*** INCLUDES ***/
#include "capacitive_sensor.h"
/******* Variables****/
SemaphoreHandle_t xSemaphore_door_capa=NULL;/** \brief Semaphore give when user touch the sensor*/
static bool s_pad_activated[TOUCH_PAD_MAX];
static uint32_t s_pad_init_val[TOUCH_PAD_MAX];
static const char* TAG = "Touch pad";
BaseType_t Mycapa_xHigherPrioritTaskWoken=pdFALSE;
extern SemaphoreHandle_t mutex_display_lock;
/***** Extern functions ****/
extern void launch_door();
/**
* @brief Handle an interrupt triggered when a pad is touched.
Recognize what pad has been touched and save it in a table.
*
* @param arg
*/
void handler_intr_rtc(void * arg){
uint32_t pad_intr = touch_pad_get_status();
//clear interrupt
touch_pad_clear_status();
if ((pad_intr >> CAPA_PORTE) & 0x01) {
s_pad_activated[CAPA_PORTE] = true;
}
//xSemaphoreGiveFromISR(xSemaphore_door_capa,&Mycapa_xHigherPrioritTaskWoken);
}
/**
* @brief Function for initialize a touch pad ISR with the num of touch pad, the threshold,
* if the ISR shoud be if the value is above or below the threshold, the name of the interrupt handler
*
* @param touch_num
* @param threshold
* @param mode
* @param fn_handler
*/
void init_isr_touchpad(touch_pad_t touch_num, uint16_t threshold, touch_trigger_mode_t mode,intr_handler_t fn_handler )
{
touch_pad_t mytouch_num=touch_num;
uint16_t mythresh=threshold;
touch_trigger_mode_t mymode=mode;
intr_handler_t myhandler=fn_handler ;
//Configure the ISR on the threshold
touch_pad_config(mytouch_num,mythresh);
//Define the threshold
//touch_pad_set_thresh(mytouch_num,mythresh);
touch_pad_set_trigger_mode(mymode);
touch_pad_isr_register(myhandler, NULL);
touch_pad_intr_enable();
}
/**
* @brief Init touch pad library
*
*/
void init_touch_pad(void *arg){
// 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);
// 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_0V5);
//init RTC IO and mode for touch pad.
touch_pad_config(CAPA_PORTE, TOUCH_THRESH_NO_USE);
touch_pad_filter_start(TOUCHPAD_FILTER_TOUCH_PERIOD);
// Set thresh hold
tp_set_thresholds();
// Register touch interrupt ISR
touch_pad_isr_register(handler_intr_rtc,NULL);
touch_pad_intr_enable();
printf("end set touch pad \n");
vTaskDelete(NULL);
}
/*
Read values sensed at all available touch pads.
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.
Do not touch any pads when this routine
is running (on application start).
*/
void tp_set_thresholds(void)
{
uint16_t touch_value;
//read filtered value
touch_pad_read_filtered(CAPA_PORTE, &touch_value);
s_pad_init_val[CAPA_PORTE] = touch_value;
ESP_LOGI(TAG, "test init: touch pad [%d] val is %d", CAPA_PORTE, touch_value);
//set interrupt threshold.
ESP_ERROR_CHECK(touch_pad_set_thresh(CAPA_PORTE, touch_value * TOUCH_THRESH_PERCENT / 100));
}
/*
Function to monitor the sensor
*/
void task_check_door(void* arg){
uint16_t receive_cycle_running=0;
BaseType_t status_cycle;
uint32_t io_num;
uint16_t value = 0;
printf("task check door created \n");
xSemaphore_door_capa= xSemaphoreCreateBinary(); /* Binary semaphore to be take when sensor is touched */
#if 1
//Initialisation of capacitive measure
//init_touch_pad();
//Put a ISR when someone touch the sensor
tp_set_thresholds();
touch_pad_isr_register(handler_intr_rtc, NULL);
touch_pad_intr_enable();
#endif
#if 0
init_isr_touchpad(CAPA_PORTE,THRESH_DOOR_CAPACITANCE,TOUCH_TRIGGER_ABOVE,handler_intr_rtc);
tp_set_thresholds();
touch_pad_isr_register(handler_intr_rtc, NULL);
#endif
for(;;){
//if(xSemaphoreTake( xSemaphore_door_capa, portMAX_DELAY ) == pdPASS ){
#if 0
touch_pad_read_filtered(CAPA_PORTE, &value);
if (value < s_pad_init_val[CAPA_PORTE] * TOUCH_THRESH_PERCENT / 100) {
ESP_LOGI(TAG, "T%d activated!", (CAPA_PORTE));
ESP_LOGI(TAG, "value: %d; init val: %d", value, s_pad_init_val[CAPA_PORTE]);
vTaskDelay(20 / portTICK_PERIOD_MS);
}
#endif
if (s_pad_activated[CAPA_PORTE] == true){
ESP_LOGI(TAG, "T%d activated! ", CAPA_PORTE);
}
#endif
// Wait a while for the pad being released
vTaskDelay(500 / portTICK_PERIOD_MS);
// Clear information on pad activation
s_pad_activated[CAPA_PORTE] = false;
}
//}
}
/* In your main :
xTaskCreate(init_touch_pad, "init_touch_pad", 4096, NULL, 8,NULL);
xTaskCreate(task_check_door, "task check door", 4096, NULL, 7,&xCheck_door);
*/
/*** INCLUDES ***/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "esp_log.h"
/*** DEFINES***/
#define THRESH_DOOR_CAPACITANCE 200 /** \brief Threshold for capacitance measure **/
#define TOUCHPAD_FILTER_TOUCH_PERIOD (10)
#define TOUCH_THRESH_NO_USE 0
#define TOUCH_THRESH_PERCENT (95)
/*** PROTOTYPES ****/
/**
* @brief Init touch pad library
*
*/
void init_touch_pad(void *arg);
/**
* @brief Handle an interrupt triggered when a pad is touched.
Recognize what pad has been touched and save it in a table.
*
* @param arg
*/
void handler_intr_rtc(void * arg);
/*
Function to monitor the sensor
*/
void task_check_door(void* arg);
/**
* @brief Function for initialize a touch pad ISR with the num of touch pad, the threshold,
* if the ISR shoud be if the value is above or below the threshold, the name of the interrupt handler
*
* @param touch_num
* @param threshold
* @param mode
* @param fn_handler
*/
void init_isr_touchpad(touch_pad_t touch_num, uint16_t threshold, touch_trigger_mode_t mode,intr_handler_t fn_handler );
/*
Read values sensed at all available touch pads.
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.
Do not touch any pads when this routine
is running (on application start).
*/
void tp_set_thresholds(void);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment