31
31
#include "peripherals/touch.h"
32
32
#include "supervisor/esp_port.h"
33
33
34
+ static uint16_t touch_channel_mask ;
34
35
static volatile bool woke_up = false;
35
- static touch_pad_t touch_channel = TOUCH_PAD_MAX ;
36
36
37
37
void common_hal_alarm_touch_touchalarm_construct (alarm_touch_touchalarm_obj_t * self , const mcu_pin_obj_t * pin ) {
38
38
if (pin -> touch_channel == TOUCH_PAD_MAX ) {
@@ -50,14 +50,20 @@ mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(const size_t n_alarms, const mp
50
50
}
51
51
}
52
52
53
+ // Create TouchAlarm object.
53
54
alarm_touch_touchalarm_obj_t * alarm = m_new_obj (alarm_touch_touchalarm_obj_t );
54
55
alarm -> base .type = & alarm_touch_touchalarm_type ;
55
56
alarm -> pin = NULL ;
56
57
58
+ touch_pad_t wake_channel = touch_pad_get_current_meas_channel ();
59
+ if (wake_channel == TOUCH_PAD_MAX ) {
60
+ return alarm ;
61
+ }
62
+
57
63
// Map the pin number back to a pin object.
58
64
for (size_t i = 0 ; i < mcu_pin_globals .map .used ; i ++ ) {
59
65
const mcu_pin_obj_t * pin_obj = MP_OBJ_TO_PTR (mcu_pin_globals .map .table [i ].value );
60
- if (pin_obj -> touch_channel == touch_channel ) {
66
+ if (pin_obj -> touch_channel == wake_channel ) {
61
67
alarm -> pin = mcu_pin_globals .map .table [i ].value ;
62
68
break ;
63
69
}
@@ -83,46 +89,66 @@ void alarm_touch_touchalarm_set_alarm(const bool deep_sleep, const size_t n_alar
83
89
84
90
for (size_t i = 0 ; i < n_alarms ; i ++ ) {
85
91
if (MP_OBJ_IS_TYPE (alarms [i ], & alarm_touch_touchalarm_type )) {
86
- if (!touch_alarm_set ) {
87
- touch_alarm = MP_OBJ_TO_PTR (alarms [i ]);
88
- touch_alarm_set = true;
89
- } else {
90
- mp_raise_ValueError (translate ("Only one alarm.touch alarm can be set." ));
92
+ if (deep_sleep && touch_alarm_set ) {
93
+ mp_raise_ValueError (translate ("Only one TouchAlarm can be set in deep sleep." ));
91
94
}
95
+ touch_alarm = MP_OBJ_TO_PTR (alarms [i ]);
96
+ touch_channel_mask |= 1 << touch_alarm -> pin -> number ;
97
+ touch_alarm_set = true;
92
98
}
93
99
}
100
+
94
101
if (!touch_alarm_set ) {
95
102
return ;
96
103
}
97
104
98
- touch_channel = touch_alarm -> pin -> touch_channel ;
99
-
100
105
// configure interrupt for pretend to deep sleep
101
106
// this will be disabled if we actually deep sleep
102
107
103
- // intialize touchpad
108
+ // reset touch peripheral
104
109
peripherals_touch_reset ();
105
110
peripherals_touch_never_reset (true);
106
- peripherals_touch_init (touch_channel );
107
111
108
- // wait for touch data to reset
109
- mp_hal_delay_ms (10 );
112
+ for (uint8_t i = 1 ; i <= 14 ; i ++ ) {
113
+ if ((touch_channel_mask & 1 << i ) != 0 ) {
114
+ touch_pad_t touch_channel = (touch_pad_t )i ;
115
+ // intialize touchpad
116
+ peripherals_touch_init (touch_channel );
110
117
111
- // configure trigger threshold
112
- uint32_t touch_value ;
113
- touch_pad_read_benchmark (touch_channel , & touch_value );
114
- touch_pad_set_thresh (touch_channel , touch_value * 0.1 ); //10%
118
+ // wait for touch data to reset
119
+ mp_hal_delay_ms (10 );
120
+
121
+ // configure trigger threshold
122
+ uint32_t touch_value ;
123
+ touch_pad_read_benchmark (touch_channel , & touch_value );
124
+ touch_pad_set_thresh (touch_channel , touch_value * 0.1 ); //10%
125
+ }
126
+ }
115
127
116
128
// configure touch interrupt
117
129
touch_pad_timeout_set (true, SOC_TOUCH_PAD_THRESHOLD_MAX );
118
130
touch_pad_isr_register (touch_interrupt , NULL , TOUCH_PAD_INTR_MASK_ALL );
119
- touch_pad_intr_enable (TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE | TOUCH_PAD_INTR_MASK_TIMEOUT );
131
+ touch_pad_intr_enable (TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE );
120
132
}
121
133
122
134
void alarm_touch_touchalarm_prepare_for_deep_sleep (void ) {
123
- // intialize touchpad
135
+ if (!touch_channel_mask ) {
136
+ return ;
137
+ }
138
+
139
+ touch_pad_t touch_channel = TOUCH_PAD_MAX ;
140
+ for (uint8_t i = 1 ; i <= 14 ; i ++ ) {
141
+ if ((touch_channel_mask & 1 << i ) != 0 ) {
142
+ touch_channel = (touch_pad_t )i ;
143
+ break ;
144
+ }
145
+ }
146
+
147
+ // reset touch peripheral
124
148
peripherals_touch_never_reset (false);
125
149
peripherals_touch_reset ();
150
+
151
+ // intialize touchpad
126
152
peripherals_touch_init (touch_channel );
127
153
128
154
// configure touchpad for sleep
@@ -148,6 +174,6 @@ bool alarm_touch_touchalarm_woke_us_up(void) {
148
174
149
175
void alarm_touch_touchalarm_reset (void ) {
150
176
woke_up = false;
151
- touch_channel = TOUCH_PAD_MAX ;
177
+ touch_channel_mask = 0 ;
152
178
peripherals_touch_never_reset (false);
153
179
}
0 commit comments