Skip to content

Commit 679b465

Browse files
authored
Merge pull request #3969 from microDev1/TouchAlarm
Fixes and Enhancement for Touch Alarm
2 parents 047708e + cd16f29 commit 679b465

File tree

3 files changed

+52
-26
lines changed

3 files changed

+52
-26
lines changed

locale/circuitpython.pot

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,12 +1488,12 @@ msgid ""
14881488
"%d bpp given"
14891489
msgstr ""
14901490

1491-
#: ports/esp32s2/common-hal/alarm/time/TimeAlarm.c
1492-
msgid "Only one alarm.time alarm can be set."
1491+
#: ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c
1492+
msgid "Only one TouchAlarm can be set in deep sleep."
14931493
msgstr ""
14941494

1495-
#: ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c
1496-
msgid "Only one alarm.touch alarm can be set."
1495+
#: ports/esp32s2/common-hal/alarm/time/TimeAlarm.c
1496+
msgid "Only one alarm.time alarm can be set."
14971497
msgstr ""
14981498

14991499
#: shared-module/displayio/ColorConverter.c

ports/esp32s2/common-hal/alarm/__init__.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ STATIC mp_obj_t _get_wake_alarm(size_t n_alarms, const mp_obj_t *alarms) {
9292
return alarm_pin_pinalarm_get_wakeup_alarm(n_alarms, alarms);
9393
}
9494

95-
case ESP_SLEEP_WAKEUP_TOUCHPAD:
95+
case ESP_SLEEP_WAKEUP_TOUCHPAD: {
9696
return alarm_touch_touchalarm_get_wakeup_alarm(n_alarms, alarms);
97-
break;
97+
}
9898

9999
case ESP_SLEEP_WAKEUP_UNDEFINED:
100100
default:

ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
#include "peripherals/touch.h"
3232
#include "supervisor/esp_port.h"
3333

34+
static uint16_t touch_channel_mask;
3435
static volatile bool woke_up = false;
35-
static touch_pad_t touch_channel = TOUCH_PAD_MAX;
3636

3737
void common_hal_alarm_touch_touchalarm_construct(alarm_touch_touchalarm_obj_t *self, const mcu_pin_obj_t *pin) {
3838
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
5050
}
5151
}
5252

53+
// Create TouchAlarm object.
5354
alarm_touch_touchalarm_obj_t *alarm = m_new_obj(alarm_touch_touchalarm_obj_t);
5455
alarm->base.type = &alarm_touch_touchalarm_type;
5556
alarm->pin = NULL;
5657

58+
touch_pad_t wake_channel = touch_pad_get_current_meas_channel();
59+
if (wake_channel == TOUCH_PAD_MAX) {
60+
return alarm;
61+
}
62+
5763
// Map the pin number back to a pin object.
5864
for (size_t i = 0; i < mcu_pin_globals.map.used; i++) {
5965
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) {
6167
alarm->pin = mcu_pin_globals.map.table[i].value;
6268
break;
6369
}
@@ -83,46 +89,66 @@ void alarm_touch_touchalarm_set_alarm(const bool deep_sleep, const size_t n_alar
8389

8490
for (size_t i = 0; i < n_alarms; i++) {
8591
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."));
9194
}
95+
touch_alarm = MP_OBJ_TO_PTR(alarms[i]);
96+
touch_channel_mask |= 1 << touch_alarm->pin->number;
97+
touch_alarm_set = true;
9298
}
9399
}
100+
94101
if (!touch_alarm_set) {
95102
return;
96103
}
97104

98-
touch_channel = touch_alarm->pin->touch_channel;
99-
100105
// configure interrupt for pretend to deep sleep
101106
// this will be disabled if we actually deep sleep
102107

103-
// intialize touchpad
108+
// reset touch peripheral
104109
peripherals_touch_reset();
105110
peripherals_touch_never_reset(true);
106-
peripherals_touch_init(touch_channel);
107111

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);
110117

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+
}
115127

116128
// configure touch interrupt
117129
touch_pad_timeout_set(true, SOC_TOUCH_PAD_THRESHOLD_MAX);
118130
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);
120132
}
121133

122134
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
124148
peripherals_touch_never_reset(false);
125149
peripherals_touch_reset();
150+
151+
// intialize touchpad
126152
peripherals_touch_init(touch_channel);
127153

128154
// configure touchpad for sleep
@@ -148,6 +174,6 @@ bool alarm_touch_touchalarm_woke_us_up(void) {
148174

149175
void alarm_touch_touchalarm_reset(void) {
150176
woke_up = false;
151-
touch_channel = TOUCH_PAD_MAX;
177+
touch_channel_mask = 0;
152178
peripherals_touch_never_reset(false);
153179
}

0 commit comments

Comments
 (0)