14
14
* limitations under the License.
15
15
*/
16
16
#include <freertos/FreeRTOS.h>
17
- #include < driver/gpio.h>
17
+ #include " driver/touch_pad.h"
18
18
#include <esp_log.h>
19
19
20
20
#include "button.h"
@@ -25,24 +25,64 @@ static const char *TAG = "HW/BUTTON";
25
25
QueueHandle_t buttonQueue ;
26
26
27
27
// button pressed interrupt function
28
- static void IRAM_ATTR BUTTON_isr_handler (void * arg )
28
+ // static void IRAM_ATTR BUTTON_isr_handler(void *arg)
29
+ // {
30
+ // // pin number of the button pressed
31
+ // uint8_t pinNumber = (int)arg;
32
+ // // send button pressed event to the button queue
33
+ // xQueueSendFromISR(buttonQueue, &pinNumber, NULL);
34
+ // }
35
+
36
+ static void calibrate_touch_pad (touch_pad_t pad )
37
+ {
38
+ int avg = 0 ;
39
+ const size_t calibration_count = 128 ;
40
+
41
+ for (int i = 0 ; i < calibration_count ; ++ i )
42
+ {
43
+ uint16_t val ;
44
+ touch_pad_read (pad , & val );
45
+ avg += val ;
46
+ }
47
+ avg /= calibration_count ;
48
+ const int min_reading = 300 ;
49
+ if (avg < min_reading )
50
+ {
51
+ ESP_LOGE (TAG , "Touch pad #%d average reading is too low: %d (expecting at least %d). "
52
+ "Not using for deep sleep wakeup." ,
53
+ pad , avg , min_reading );
54
+ touch_pad_config (pad , 0 );
55
+ }
56
+ else
57
+ {
58
+ int threshold = avg - 100 ;
59
+ ESP_LOGI (TAG , "Calibrated touch pad #%d. Count average: %d, wakeup threshold set to %d." , pad , avg , threshold );
60
+ touch_pad_config (pad , threshold );
61
+ }
62
+ }
63
+
64
+ // Initialize touch pad
65
+ void BUTTON_InitTouchPad (touch_pad_t num )
29
66
{
30
- // pin number of the button pressed
31
- uint8_t pinNumber = (int )arg ;
32
- // send button pressed event to the button queue
33
- xQueueSendFromISR (buttonQueue , & pinNumber , NULL );
67
+ // Initialize touch pad peripheral.
68
+ // The default fsm mode is software trigger mode.
69
+ ESP_ERROR_CHECK (touch_pad_init ());
70
+ // If use touch pad wake up, should set touch sensor FSM mode at 'TOUCH_FSM_MODE_TIMER'.
71
+ touch_pad_set_fsm_mode (TOUCH_FSM_MODE_TIMER );
72
+
73
+ touch_pad_set_voltage (TOUCH_HVOLT_2V7 , TOUCH_LVOLT_0V5 , TOUCH_HVOLT_ATTEN_1V );
74
+ // init RTC IO and mode for touch pad.
75
+ touch_pad_config (num , 400 );
76
+ // touch_pad_config(num, TOUCH_THRESH_NO_USE);
77
+ calibrate_touch_pad (num );
34
78
}
35
79
36
80
// initialize button handling
37
81
void BUTTON_Init ()
38
82
{
39
83
buttonQueue = xQueueCreate (10 , sizeof (uint8_t ));
40
84
41
- gpio_reset_pin (BUTTON_GPIO_PIN );
42
- gpio_set_direction (BUTTON_GPIO_PIN , GPIO_MODE_INPUT );
43
- gpio_set_intr_type (BUTTON_GPIO_PIN , GPIO_INTR_NEGEDGE );
44
- gpio_install_isr_service (0 );
45
- gpio_isr_handler_add (BUTTON_GPIO_PIN , BUTTON_isr_handler , (void * )BUTTON_GPIO_PIN );
85
+ BUTTON_InitTouchPad (CONFIG_TOUCH_BUTTON_1_NUMBER );
46
86
}
47
87
48
88
// monitor button pressed queue
@@ -58,8 +98,6 @@ void BUTTON_Monitor(void *pvParameters)
58
98
59
99
ESP_LOGI (TAG , "GPIO %d was pressed." , buttonEvent .pin_number );
60
100
61
- // TODO: Disable interrupt for a while to debounce
62
-
63
101
// Handle button event
64
102
BUTTON_Handle (buttonEvent );
65
103
}
0 commit comments