Skip to content

Commit 2aef79a

Browse files
committed
touch btn 1 wakes up from deep sleep
1 parent f4d93d9 commit 2aef79a

File tree

3 files changed

+61
-13
lines changed

3 files changed

+61
-13
lines changed

main/Kconfig.projbuild

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ menu "Project configuration"
7676
help
7777
Audio OUT GPIO Pin to use.
7878

79+
config TOUCH_BUTTON_1_NUMBER
80+
int "Touch button 1 number"
81+
default 3
82+
help
83+
Touch button 1 number.
84+
7985
config BEACON_MODE
8086
int "Beacon mode"
8187
default 1

main/hardware/button.c

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616
#include <freertos/FreeRTOS.h>
17-
#include <driver/gpio.h>
17+
#include "driver/touch_pad.h"
1818
#include <esp_log.h>
1919

2020
#include "button.h"
@@ -25,24 +25,64 @@ static const char *TAG = "HW/BUTTON";
2525
QueueHandle_t buttonQueue;
2626

2727
// 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)
2966
{
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);
3478
}
3579

3680
// initialize button handling
3781
void BUTTON_Init()
3882
{
3983
buttonQueue = xQueueCreate(10, sizeof(uint8_t));
4084

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);
4686
}
4787

4888
// monitor button pressed queue
@@ -58,8 +98,6 @@ void BUTTON_Monitor(void *pvParameters)
5898

5999
ESP_LOGI(TAG, "GPIO %d was pressed.", buttonEvent.pin_number);
60100

61-
// TODO: Disable interrupt for a while to debounce
62-
63101
// Handle button event
64102
BUTTON_Handle(buttonEvent);
65103
}

main/web/handlers/api/system.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ esp_err_t API_SYSTEM_DeepSleep(httpd_req_t *req)
100100

101101
SYSTEM_Shutdown();
102102

103+
// Enable wake-up with touch button
104+
ESP_ERROR_CHECK(esp_sleep_enable_touchpad_wakeup());
105+
ESP_ERROR_CHECK(esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON));
106+
103107
esp_deep_sleep_start();
104108

105109
return ESP_OK;

0 commit comments

Comments
 (0)