27
27
#include "shared-bindings/alarm/touch/TouchAlarm.h"
28
28
#include "shared-bindings/microcontroller/__init__.h"
29
29
30
+ #include "esp_sleep.h"
30
31
#include "peripherals/touch.h"
32
+ #include "supervisor/esp_port.h"
31
33
32
- #include "esp_sleep.h"
34
+ static volatile bool woke_up = false;
35
+ static touch_pad_t touch_channel = TOUCH_PAD_MAX ;
33
36
34
37
void common_hal_alarm_touch_touchalarm_construct (alarm_touch_touchalarm_obj_t * self , const mcu_pin_obj_t * pin ) {
35
38
if (pin -> touch_channel == TOUCH_PAD_MAX ) {
@@ -39,24 +42,22 @@ void common_hal_alarm_touch_touchalarm_construct(alarm_touch_touchalarm_obj_t *s
39
42
self -> pin = pin ;
40
43
}
41
44
42
- mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm (size_t n_alarms , const mp_obj_t * alarms ) {
45
+ mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm (const size_t n_alarms , const mp_obj_t * alarms ) {
43
46
// First, check to see if we match any given alarms.
44
47
for (size_t i = 0 ; i < n_alarms ; i ++ ) {
45
48
if (MP_OBJ_IS_TYPE (alarms [i ], & alarm_touch_touchalarm_type )) {
46
49
return alarms [i ];
47
50
}
48
51
}
49
52
50
- gpio_num_t pin_number = esp_sleep_get_touchpad_wakeup_status ();
51
-
52
53
alarm_touch_touchalarm_obj_t * alarm = m_new_obj (alarm_touch_touchalarm_obj_t );
53
54
alarm -> base .type = & alarm_touch_touchalarm_type ;
54
55
alarm -> pin = NULL ;
55
56
56
57
// Map the pin number back to a pin object.
57
58
for (size_t i = 0 ; i < mcu_pin_globals .map .used ; i ++ ) {
58
59
const mcu_pin_obj_t * pin_obj = MP_OBJ_TO_PTR (mcu_pin_globals .map .table [i ].value );
59
- if (pin_obj -> number == pin_number ) {
60
+ if (pin_obj -> touch_channel == touch_channel ) {
60
61
alarm -> pin = mcu_pin_globals .map .table [i ].value ;
61
62
break ;
62
63
}
@@ -65,16 +66,67 @@ mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t
65
66
return alarm ;
66
67
}
67
68
68
- static touch_pad_t touch_channel = TOUCH_PAD_MAX ;
69
+ // This is used to wake the main CircuitPython task.
70
+ void touch_interrupt (void * arg ) {
71
+ (void ) arg ;
72
+ woke_up = true;
73
+ BaseType_t task_wakeup ;
74
+ vTaskNotifyGiveFromISR (circuitpython_task , & task_wakeup );
75
+ if (task_wakeup ) {
76
+ portYIELD_FROM_ISR ();
77
+ }
78
+ }
69
79
70
- void alarm_touch_touchalarm_set_alarm (alarm_touch_touchalarm_obj_t * self ) {
71
- touch_channel = (touch_pad_t )self -> pin -> number ;
72
- esp_sleep_enable_touchpad_wakeup ();
73
- esp_sleep_pd_config (ESP_PD_DOMAIN_RTC_PERIPH , ESP_PD_OPTION_ON );
80
+ void alarm_touch_touchalarm_set_alarm (const bool deep_sleep , const size_t n_alarms , const mp_obj_t * alarms ) {
81
+ bool touch_alarm_set = false;
82
+ alarm_touch_touchalarm_obj_t * touch_alarm = MP_OBJ_NULL ;
83
+
84
+ for (size_t i = 0 ; i < n_alarms ; i ++ ) {
85
+ if (MP_OBJ_IS_TYPE (alarms [i ], & alarm_touch_touchalarm_type )) {
86
+ if (!touch_alarm_set ) {
87
+ if (deep_sleep ) {
88
+ touch_alarm = MP_OBJ_TO_PTR (alarms [i ]);
89
+ touch_alarm_set = true;
90
+ } else {
91
+ mp_raise_NotImplementedError (translate ("TouchAlarm not available in light sleep" ));
92
+ }
93
+ } else {
94
+ mp_raise_ValueError (translate ("Only one alarm.touch alarm can be set." ));
95
+ }
96
+ }
97
+ }
98
+ if (!touch_alarm_set ) {
99
+ return ;
100
+ }
101
+
102
+ touch_channel = touch_alarm -> pin -> touch_channel ;
103
+
104
+ // configure interrupt for pretend to deep sleep
105
+ // this will be disabled if we actually deep sleep
106
+
107
+ // intialize touchpad
108
+ peripherals_touch_reset ();
109
+ peripherals_touch_never_reset (true);
110
+ peripherals_touch_init (touch_channel );
111
+
112
+ // wait for touch data to reset
113
+ mp_hal_delay_ms (10 );
114
+
115
+ // configure trigger threshold
116
+ uint32_t touch_value ;
117
+ touch_pad_read_benchmark (touch_channel , & touch_value );
118
+ touch_pad_set_thresh (touch_channel , touch_value * 0.1 ); //10%
119
+
120
+ // configure touch interrupt
121
+ touch_pad_timeout_set (true, SOC_TOUCH_PAD_THRESHOLD_MAX );
122
+ touch_pad_isr_register (touch_interrupt , NULL , TOUCH_PAD_INTR_MASK_ALL );
123
+ touch_pad_intr_enable (TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE | TOUCH_PAD_INTR_MASK_TIMEOUT );
74
124
}
75
125
76
126
void alarm_touch_touchalarm_prepare_for_deep_sleep (void ) {
77
127
// intialize touchpad
128
+ peripherals_touch_never_reset (false);
129
+ peripherals_touch_reset ();
78
130
peripherals_touch_init (touch_channel );
79
131
80
132
// configure touchpad for sleep
@@ -84,15 +136,22 @@ void alarm_touch_touchalarm_prepare_for_deep_sleep(void) {
84
136
// wait for touch data to reset
85
137
mp_hal_delay_ms (10 );
86
138
139
+ // configure trigger threshold
87
140
uint32_t touch_value ;
88
141
touch_pad_sleep_channel_read_smooth (touch_channel , & touch_value );
89
142
touch_pad_sleep_set_threshold (touch_channel , touch_value * 0.1 ); //10%
143
+
144
+ // enable touchpad wakeup
145
+ esp_sleep_enable_touchpad_wakeup ();
146
+ esp_sleep_pd_config (ESP_PD_DOMAIN_RTC_PERIPH , ESP_PD_OPTION_ON );
90
147
}
91
148
92
149
bool alarm_touch_touchalarm_woke_us_up (void ) {
93
- return false ;
150
+ return woke_up ;
94
151
}
95
152
96
153
void alarm_touch_touchalarm_reset (void ) {
154
+ woke_up = false;
97
155
touch_channel = TOUCH_PAD_MAX ;
156
+ peripherals_touch_never_reset (false);
98
157
}
0 commit comments