Skip to content

Commit c7f6802

Browse files
committed
add pretend-to-sleep functionality
1 parent ecd7c08 commit c7f6802

File tree

8 files changed

+90
-38
lines changed

8 files changed

+90
-38
lines changed

locale/circuitpython.pot

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2020-12-18 20:40+0530\n"
11+
"POT-Creation-Date: 2020-12-22 22:54+0530\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -1463,7 +1463,7 @@ msgstr ""
14631463
msgid "Only one alarm.time alarm can be set."
14641464
msgstr ""
14651465

1466-
#: ports/esp32s2/common-hal/alarm/__init__.c
1466+
#: ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c
14671467
msgid "Only one alarm.touch alarm can be set."
14681468
msgstr ""
14691469

@@ -1841,7 +1841,7 @@ msgstr ""
18411841
msgid "Total data to write is larger than outgoing_packet_length"
18421842
msgstr ""
18431843

1844-
#: ports/esp32s2/common-hal/alarm/__init__.c
1844+
#: ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c
18451845
msgid "TouchAlarm not available in light sleep"
18461846
msgstr ""
18471847

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ mp_obj_t common_hal_alarm_get_wake_alarm(void) {
103103
// Set up light sleep or deep sleep alarms.
104104
STATIC void _setup_sleep_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms) {
105105
bool time_alarm_set = false;
106-
bool touch_alarm_set = false;
107106
alarm_time_time_alarm_obj_t *time_alarm = MP_OBJ_NULL;
108107

109108
for (size_t i = 0; i < n_alarms; i++) {
@@ -115,23 +114,13 @@ STATIC void _setup_sleep_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t
115114
}
116115
time_alarm = MP_OBJ_TO_PTR(alarms[i]);
117116
time_alarm_set = true;
118-
} else if (MP_OBJ_IS_TYPE(alarms[i], &alarm_touch_touchalarm_type)) {
119-
if (!touch_alarm_set) {
120-
if (deep_sleep) {
121-
alarm_touch_touchalarm_set_alarm(MP_OBJ_TO_PTR(alarms[i]));
122-
touch_alarm_set = true;
123-
} else {
124-
mp_raise_NotImplementedError(translate("TouchAlarm not available in light sleep"));
125-
}
126-
} else {
127-
mp_raise_ValueError(translate("Only one alarm.touch alarm can be set."));
128-
}
129117
}
130118
}
131119

132120
if (time_alarm_set) {
133121
alarm_time_timealarm_set_alarm(time_alarm);
134122
}
123+
alarm_touch_touchalarm_set_alarm(deep_sleep, n_alarms, alarms);
135124
}
136125

137126
STATIC void _idle_until_alarm(void) {

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

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@
2727
#include "shared-bindings/alarm/touch/TouchAlarm.h"
2828
#include "shared-bindings/microcontroller/__init__.h"
2929

30+
#include "esp_sleep.h"
3031
#include "peripherals/touch.h"
32+
#include "supervisor/esp_port.h"
3133

32-
#include "esp_sleep.h"
34+
static volatile bool woke_up = false;
35+
static touch_pad_t touch_channel = TOUCH_PAD_MAX;
3336

3437
void common_hal_alarm_touch_touchalarm_construct(alarm_touch_touchalarm_obj_t *self, const mcu_pin_obj_t *pin) {
3538
if (pin->touch_channel == TOUCH_PAD_MAX) {
@@ -39,24 +42,22 @@ void common_hal_alarm_touch_touchalarm_construct(alarm_touch_touchalarm_obj_t *s
3942
self->pin = pin;
4043
}
4144

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) {
4346
// First, check to see if we match any given alarms.
4447
for (size_t i = 0; i < n_alarms; i++) {
4548
if (MP_OBJ_IS_TYPE(alarms[i], &alarm_touch_touchalarm_type)) {
4649
return alarms[i];
4750
}
4851
}
4952

50-
gpio_num_t pin_number = esp_sleep_get_touchpad_wakeup_status();
51-
5253
alarm_touch_touchalarm_obj_t *alarm = m_new_obj(alarm_touch_touchalarm_obj_t);
5354
alarm->base.type = &alarm_touch_touchalarm_type;
5455
alarm->pin = NULL;
5556

5657
// Map the pin number back to a pin object.
5758
for (size_t i = 0; i < mcu_pin_globals.map.used; i++) {
5859
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) {
6061
alarm->pin = mcu_pin_globals.map.table[i].value;
6162
break;
6263
}
@@ -65,16 +66,67 @@ mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t
6566
return alarm;
6667
}
6768

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

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);
74124
}
75125

76126
void alarm_touch_touchalarm_prepare_for_deep_sleep(void) {
77127
// intialize touchpad
128+
peripherals_touch_never_reset(false);
129+
peripherals_touch_reset();
78130
peripherals_touch_init(touch_channel);
79131

80132
// configure touchpad for sleep
@@ -84,15 +136,22 @@ void alarm_touch_touchalarm_prepare_for_deep_sleep(void) {
84136
// wait for touch data to reset
85137
mp_hal_delay_ms(10);
86138

139+
// configure trigger threshold
87140
uint32_t touch_value;
88141
touch_pad_sleep_channel_read_smooth(touch_channel, &touch_value);
89142
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);
90147
}
91148

92149
bool alarm_touch_touchalarm_woke_us_up(void) {
93-
return false;
150+
return woke_up;
94151
}
95152

96153
void alarm_touch_touchalarm_reset(void) {
154+
woke_up = false;
97155
touch_channel = TOUCH_PAD_MAX;
156+
peripherals_touch_never_reset(false);
98157
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ typedef struct {
3636
} alarm_touch_touchalarm_obj_t;
3737

3838
// Find the alarm object that caused us to wake up or create an equivalent one.
39-
mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t *alarms);
39+
mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(const size_t n_alarms, const mp_obj_t *alarms);
4040
// Check for the wake up alarm from pretend deep sleep.
41-
void alarm_touch_touchalarm_set_alarm(alarm_touch_touchalarm_obj_t *self);
41+
void alarm_touch_touchalarm_set_alarm(const bool deep_sleep, const size_t n_alarms, const mp_obj_t *alarms);
4242
void alarm_touch_touchalarm_prepare_for_deep_sleep(void);
4343
bool alarm_touch_touchalarm_woke_us_up(void);
4444
void alarm_touch_touchalarm_reset(void);

ports/esp32s2/common-hal/touchio/TouchIn.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@
2727
#include "shared-bindings/touchio/TouchIn.h"
2828

2929
#include "py/runtime.h"
30-
#include "driver/touch_pad.h"
3130
#include "peripherals/touch.h"
3231

3332
static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
3433
uint32_t touch_value;
35-
touch_pad_read_raw_data((touch_pad_t)self->pin->touch_channel, &touch_value);
34+
touch_pad_read_raw_data(self->pin->touch_channel, &touch_value);
3635
if (touch_value > UINT16_MAX) {
3736
return UINT16_MAX;
3837
}
@@ -47,14 +46,11 @@ void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self,
4746
claim_pin(pin);
4847

4948
// initialize touchpad
50-
peripherals_touch_init((touch_pad_t)pin->touch_channel);
49+
peripherals_touch_init(pin->touch_channel);
5150

5251
// wait for touch data to reset
5352
mp_hal_delay_ms(10);
5453

55-
// Initial values for pins will vary, depending on what peripherals the pins
56-
// share on-chip.
57-
5854
// Set a "touched" threshold not too far above the initial value.
5955
// For simple finger touch, the values may vary as much as a factor of two,
6056
// but for touches using fruit or other objects, the difference is much less.

ports/esp32s2/peripherals/pins.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ typedef struct {
4444
gpio_num_t number;
4545
uint8_t adc_index:2;
4646
uint8_t adc_channel:6;
47-
uint8_t touch_channel;
47+
touch_pad_t touch_channel;
4848
} mcu_pin_obj_t;
4949

5050
extern const mcu_pin_obj_t pin_GPIO0;

ports/esp32s2/peripherals/touch.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,25 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "components/soc/include/hal/gpio_types.h"
28+
// above include fixes build error in [email protected]
2729
#include "peripherals/touch.h"
2830

2931
static bool touch_inited = false;
32+
static bool touch_never_reset = false;
3033

3134
void peripherals_touch_reset(void) {
32-
if (touch_inited) {
35+
if (touch_inited && !touch_never_reset) {
3336
touch_pad_deinit();
3437
touch_inited = false;
3538
}
3639
}
3740

38-
void peripherals_touch_init(touch_pad_t touchpad) {
41+
void peripherals_touch_never_reset(const bool enable) {
42+
touch_never_reset = enable;
43+
}
44+
45+
void peripherals_touch_init(const touch_pad_t touchpad) {
3946
if (!touch_inited) {
4047
touch_pad_init();
4148
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);

ports/esp32s2/peripherals/touch.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "driver/touch_pad.h"
3131

3232
extern void peripherals_touch_reset(void);
33-
extern void peripherals_touch_init(touch_pad_t touchpad);
33+
extern void peripherals_touch_never_reset(const bool enable);
34+
extern void peripherals_touch_init(const touch_pad_t touchpad);
3435

3536
#endif // MICROPY_INCLUDED_ESP32S2_PERIPHERALS_TOUCH_HANDLER_H

0 commit comments

Comments
 (0)