Skip to content

Commit ecd7c08

Browse files
committed
expose wake pin parameter and more tweaks
1 parent a60fabd commit ecd7c08

File tree

4 files changed

+54
-16
lines changed

4 files changed

+54
-16
lines changed

locale/circuitpython.pot

Lines changed: 9 additions & 1 deletion
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 12:42+0530\n"
11+
"POT-Creation-Date: 2020-12-18 20:40+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,6 +1463,10 @@ msgstr ""
14631463
msgid "Only one alarm.time alarm can be set."
14641464
msgstr ""
14651465

1466+
#: ports/esp32s2/common-hal/alarm/__init__.c
1467+
msgid "Only one alarm.touch alarm can be set."
1468+
msgstr ""
1469+
14661470
#: shared-module/displayio/ColorConverter.c
14671471
msgid "Only one color can be transparent at a time"
14681472
msgstr ""
@@ -1837,6 +1841,10 @@ msgstr ""
18371841
msgid "Total data to write is larger than outgoing_packet_length"
18381842
msgstr ""
18391843

1844+
#: ports/esp32s2/common-hal/alarm/__init__.c
1845+
msgid "TouchAlarm not available in light sleep"
1846+
msgstr ""
1847+
18401848
#: py/obj.c
18411849
msgid "Traceback (most recent call last):\n"
18421850
msgstr ""

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ 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;
106107
alarm_time_time_alarm_obj_t *time_alarm = MP_OBJ_NULL;
107108

108109
for (size_t i = 0; i < n_alarms; i++) {
@@ -115,7 +116,16 @@ STATIC void _setup_sleep_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t
115116
time_alarm = MP_OBJ_TO_PTR(alarms[i]);
116117
time_alarm_set = true;
117118
} else if (MP_OBJ_IS_TYPE(alarms[i], &alarm_touch_touchalarm_type)) {
118-
alarm_touch_touchalarm_set_alarm(MP_OBJ_TO_PTR(alarms[i]));
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+
}
119129
}
120130
}
121131

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void common_hal_alarm_touch_touchalarm_construct(alarm_touch_touchalarm_obj_t *s
3535
if (pin->touch_channel == TOUCH_PAD_MAX) {
3636
mp_raise_ValueError(translate("Invalid pin"));
3737
}
38+
claim_pin(pin);
3839
self->pin = pin;
3940
}
4041

@@ -64,15 +65,15 @@ mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t
6465
return alarm;
6566
}
6667

67-
static uint16_t sleep_touch_pin;
68+
static touch_pad_t touch_channel = TOUCH_PAD_MAX;
6869

6970
void alarm_touch_touchalarm_set_alarm(alarm_touch_touchalarm_obj_t *self) {
70-
sleep_touch_pin |= 1 << self->pin->number;
71+
touch_channel = (touch_pad_t)self->pin->number;
7172
esp_sleep_enable_touchpad_wakeup();
7273
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
7374
}
7475

75-
static void configure_sleep_touch_pin(touch_pad_t touch_channel) {
76+
void alarm_touch_touchalarm_prepare_for_deep_sleep(void) {
7677
// intialize touchpad
7778
peripherals_touch_init(touch_channel);
7879

@@ -88,18 +89,10 @@ static void configure_sleep_touch_pin(touch_pad_t touch_channel) {
8889
touch_pad_sleep_set_threshold(touch_channel, touch_value * 0.1); //10%
8990
}
9091

91-
void alarm_touch_touchalarm_prepare_for_deep_sleep(void) {
92-
for (uint8_t i = 1; i <= 14; i++) {
93-
if ((sleep_touch_pin & 1 << i) != 0) {
94-
configure_sleep_touch_pin((touch_pad_t)i);
95-
}
96-
}
97-
}
98-
9992
bool alarm_touch_touchalarm_woke_us_up(void) {
10093
return false;
10194
}
10295

10396
void alarm_touch_touchalarm_reset(void) {
104-
sleep_touch_pin = 0;
97+
touch_channel = TOUCH_PAD_MAX;
10598
}

shared-bindings/alarm/touch/TouchAlarm.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,18 @@
2727
#include "shared-bindings/alarm/touch/TouchAlarm.h"
2828
#include "shared-bindings/board/__init__.h"
2929

30+
#include "py/objproperty.h"
31+
3032
//| class TouchAlarm:
3133
//| """Trigger an alarm when touch is detected."""
3234
//|
3335
//| def __init__(self, *pin: microcontroller.Pin) -> None:
34-
//| """Create an alarm that will be triggered when the
35-
//| given pin is touched.
36+
//| """Create an alarm that will be triggered when the given pin is touched.
37+
//| The alarm is not active until it is passed to an `alarm`-enabling function, such as
38+
//| `alarm.light_sleep_until_alarms()` or `alarm.exit_and_deep_sleep_until_alarms()`.
3639
//|
40+
//| :param microcontroller.Pin pin: The pin to monitor. On some ports, the choice of pin
41+
//| may be limited due to hardware restrictions, particularly for deep-sleep alarms.
3742
//| """
3843
//| ...
3944
//|
@@ -57,8 +62,30 @@ STATIC mp_obj_t alarm_touch_touchalarm_make_new(const mp_obj_type_t *type,
5762
return MP_OBJ_FROM_PTR(self);
5863
}
5964

65+
//| pin: microcontroller.Pin
66+
//| """The trigger pin."""
67+
//|
68+
STATIC mp_obj_t alarm_touch_touchalarm_obj_get_pin(mp_obj_t self_in) {
69+
alarm_touch_touchalarm_obj_t *self = MP_OBJ_TO_PTR(self_in);
70+
return MP_OBJ_FROM_PTR(self->pin);
71+
}
72+
MP_DEFINE_CONST_FUN_OBJ_1(alarm_touch_touchalarm_get_pin_obj, alarm_touch_touchalarm_obj_get_pin);
73+
74+
const mp_obj_property_t alarm_touch_touchalarm_pin_obj = {
75+
.base.type = &mp_type_property,
76+
.proxy = {(mp_obj_t)&alarm_touch_touchalarm_get_pin_obj,
77+
(mp_obj_t)&mp_const_none_obj,
78+
(mp_obj_t)&mp_const_none_obj},
79+
};
80+
81+
STATIC const mp_rom_map_elem_t alarm_touch_touchalarm_locals_dict_table[] = {
82+
{ MP_ROM_QSTR(MP_QSTR_pin), MP_ROM_PTR(&alarm_touch_touchalarm_pin_obj) },
83+
};
84+
STATIC MP_DEFINE_CONST_DICT(alarm_touch_touchalarm_locals_dict, alarm_touch_touchalarm_locals_dict_table);
85+
6086
const mp_obj_type_t alarm_touch_touchalarm_type = {
6187
{ &mp_type_type },
6288
.name = MP_QSTR_TouchAlarm,
6389
.make_new = alarm_touch_touchalarm_make_new,
90+
.locals_dict = (mp_obj_t)&alarm_touch_touchalarm_locals_dict,
6491
};

0 commit comments

Comments
 (0)