Skip to content

Commit 8eaf2b0

Browse files
committed
implement touch alarm
1 parent 8f9cd70 commit 8eaf2b0

File tree

8 files changed

+229
-8
lines changed

8 files changed

+229
-8
lines changed

locale/circuitpython.pot

Lines changed: 2 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-14 12:59-0500\n"
11+
"POT-Creation-Date: 2020-12-18 12:42+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"
@@ -1144,6 +1144,7 @@ msgstr ""
11441144

11451145
#: ports/atmel-samd/common-hal/audioio/AudioOut.c
11461146
#: ports/atmel-samd/common-hal/touchio/TouchIn.c
1147+
#: ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c
11471148
#: ports/esp32s2/common-hal/touchio/TouchIn.c shared-bindings/pwmio/PWMOut.c
11481149
#: shared-module/rgbmatrix/RGBMatrix.c
11491150
msgid "Invalid pin"

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@
2929
#include "py/objtuple.h"
3030
#include "py/runtime.h"
3131

32-
#include "shared-bindings/alarm/pin/PinAlarm.h"
3332
#include "shared-bindings/alarm/SleepMemory.h"
33+
#include "shared-bindings/alarm/pin/PinAlarm.h"
3434
#include "shared-bindings/alarm/time/TimeAlarm.h"
35-
#include "shared-bindings/microcontroller/__init__.h"
35+
#include "shared-bindings/alarm/touch/TouchAlarm.h"
36+
3637
#include "shared-bindings/wifi/__init__.h"
38+
#include "shared-bindings/microcontroller/__init__.h"
3739

3840
#include "supervisor/port.h"
3941
#include "supervisor/shared/workflow.h"
@@ -49,17 +51,20 @@ const alarm_sleep_memory_obj_t alarm_sleep_memory_obj = {
4951
},
5052
};
5153

52-
5354
void alarm_reset(void) {
54-
alarm_time_timealarm_reset();
5555
alarm_sleep_memory_reset();
56+
alarm_time_timealarm_reset();
57+
alarm_touch_touchalarm_reset();
5658
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
5759
}
5860

5961
STATIC esp_sleep_wakeup_cause_t _get_wakeup_cause(void) {
6062
if (alarm_time_timealarm_woke_us_up()) {
6163
return ESP_SLEEP_WAKEUP_TIMER;
6264
}
65+
if (alarm_touch_touchalarm_woke_us_up()) {
66+
return ESP_SLEEP_WAKEUP_TOUCHPAD;
67+
}
6368

6469
return esp_sleep_get_wakeup_cause();
6570
}
@@ -80,8 +85,7 @@ STATIC mp_obj_t _get_wake_alarm(size_t n_alarms, const mp_obj_t *alarms) {
8085
}
8186

8287
case ESP_SLEEP_WAKEUP_TOUCHPAD:
83-
// TODO: implement TouchIO
84-
// Wake up from touch on pad, esp_sleep_get_touchpad_wakeup_status()
88+
return alarm_touch_touchalarm_get_wakeup_alarm(n_alarms, alarms);
8589
break;
8690

8791
case ESP_SLEEP_WAKEUP_UNDEFINED:
@@ -110,6 +114,8 @@ STATIC void _setup_sleep_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t
110114
}
111115
time_alarm = MP_OBJ_TO_PTR(alarms[i]);
112116
time_alarm_set = true;
117+
} else if (MP_OBJ_IS_TYPE(alarms[i], &alarm_touch_touchalarm_type)) {
118+
alarm_touch_touchalarm_set_alarm(MP_OBJ_TO_PTR(alarms[i]));
113119
}
114120
}
115121

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "shared-bindings/alarm/touch/TouchAlarm.h"
28+
29+
#include "esp_sleep.h"
30+
31+
void common_hal_alarm_touch_touchalarm_construct(alarm_touch_touchalarm_obj_t *self, const mcu_pin_obj_t *pin) {
32+
33+
}
34+
35+
mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t *alarms) {
36+
return mp_const_none;
37+
}
38+
39+
void alarm_touch_touchalarm_set_alarm(alarm_touch_touchalarm_obj_t *self) {
40+
41+
}
42+
43+
bool alarm_touch_touchalarm_woke_us_up(void) {
44+
return false;
45+
}
46+
47+
void alarm_touch_touchalarm_reset(void) {
48+
49+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_COMMON_HAL_ALARM_TOUCH_TOUCHALARM_H
28+
#define MICROPY_INCLUDED_COMMON_HAL_ALARM_TOUCH_TOUCHALARM_H
29+
30+
#include "py/obj.h"
31+
#include "common-hal/microcontroller/Pin.h"
32+
33+
typedef struct {
34+
mp_obj_base_t base;
35+
const mcu_pin_obj_t *pin;
36+
} alarm_touch_touchalarm_obj_t;
37+
38+
// 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);
40+
// Check for the wake up alarm from pretend deep sleep.
41+
void alarm_touch_touchalarm_set_alarm(alarm_touch_touchalarm_obj_t *self);
42+
bool alarm_touch_touchalarm_woke_us_up(void);
43+
void alarm_touch_touchalarm_reset(void);
44+
45+
#endif // MICROPY_INCLUDED_COMMON_HAL_ALARM_TOUCH_TOUCHALARM_H

py/circuitpy_defns.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ SRC_COMMON_HAL_ALL = \
308308
alarm/__init__.c \
309309
alarm/pin/PinAlarm.c \
310310
alarm/time/TimeAlarm.c \
311+
alarm/touch/TouchAlarm.c \
311312
analogio/AnalogIn.c \
312313
analogio/AnalogOut.c \
313314
analogio/__init__.c \

shared-bindings/alarm/__init__.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "shared-bindings/alarm/SleepMemory.h"
3333
#include "shared-bindings/alarm/pin/PinAlarm.h"
3434
#include "shared-bindings/alarm/time/TimeAlarm.h"
35+
#include "shared-bindings/alarm/touch/TouchAlarm.h"
3536
#include "shared-bindings/supervisor/Runtime.h"
3637
#include "shared-bindings/time/__init__.h"
3738
#include "supervisor/shared/autoreload.h"
@@ -72,7 +73,8 @@
7273
void validate_objs_are_alarms(size_t n_args, const mp_obj_t *objs) {
7374
for (size_t i = 0; i < n_args; i++) {
7475
if (MP_OBJ_IS_TYPE(objs[i], &alarm_pin_pin_alarm_type) ||
75-
MP_OBJ_IS_TYPE(objs[i], &alarm_time_time_alarm_type)) {
76+
MP_OBJ_IS_TYPE(objs[i], &alarm_time_time_alarm_type) ||
77+
MP_OBJ_IS_TYPE(objs[i], &alarm_touch_touchalarm_type)) {
7678
continue;
7779
}
7880
mp_raise_TypeError_varg(translate("Expected an alarm"));
@@ -182,6 +184,18 @@ STATIC const mp_obj_module_t alarm_time_module = {
182184
.globals = (mp_obj_dict_t*)&alarm_time_globals,
183185
};
184186

187+
STATIC const mp_map_elem_t alarm_touch_globals_table[] = {
188+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_touch) },
189+
{ MP_ROM_QSTR(MP_QSTR_TouchAlarm), MP_OBJ_FROM_PTR(&alarm_touch_touchalarm_type) },
190+
};
191+
192+
STATIC MP_DEFINE_CONST_DICT(alarm_touch_globals, alarm_touch_globals_table);
193+
194+
STATIC const mp_obj_module_t alarm_touch_module = {
195+
.base = { &mp_type_module },
196+
.globals = (mp_obj_dict_t*)&alarm_touch_globals,
197+
};
198+
185199
// The module table is mutable because .wake_alarm is a mutable attribute.
186200
STATIC mp_map_elem_t alarm_module_globals_table[] = {
187201
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_alarm) },
@@ -195,6 +209,7 @@ STATIC mp_map_elem_t alarm_module_globals_table[] = {
195209

196210
{ MP_ROM_QSTR(MP_QSTR_pin), MP_OBJ_FROM_PTR(&alarm_pin_module) },
197211
{ MP_ROM_QSTR(MP_QSTR_time), MP_OBJ_FROM_PTR(&alarm_time_module) },
212+
{ MP_ROM_QSTR(MP_QSTR_touch), MP_OBJ_FROM_PTR(&alarm_touch_module) },
198213

199214
{ MP_ROM_QSTR(MP_QSTR_SleepMemory), MP_OBJ_FROM_PTR(&alarm_sleep_memory_type) },
200215
{ MP_ROM_QSTR(MP_QSTR_sleep_memory), MP_OBJ_FROM_PTR(&alarm_sleep_memory_obj) },
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "shared-bindings/alarm/touch/TouchAlarm.h"
28+
#include "shared-bindings/board/__init__.h"
29+
30+
//| class TouchAlarm:
31+
//| """Trigger an alarm when touch is detected."""
32+
//|
33+
//| def __init__(self, *pin: microcontroller.Pin) -> None:
34+
//| """Create an alarm that will be triggered when the
35+
//| given pin is touched.
36+
//|
37+
//| """
38+
//| ...
39+
//|
40+
STATIC mp_obj_t alarm_touch_touchalarm_make_new(const mp_obj_type_t *type,
41+
mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
42+
alarm_touch_touchalarm_obj_t *self = m_new_obj(alarm_touch_touchalarm_obj_t);
43+
self->base.type = &alarm_touch_touchalarm_type;
44+
45+
enum { ARG_pin };
46+
static const mp_arg_t allowed_args[] = {
47+
{ MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ },
48+
};
49+
50+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
51+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
52+
53+
const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[ARG_pin].u_obj);
54+
55+
common_hal_alarm_touch_touchalarm_construct(self, pin);
56+
57+
return MP_OBJ_FROM_PTR(self);
58+
}
59+
60+
const mp_obj_type_t alarm_touch_touchalarm_type = {
61+
{ &mp_type_type },
62+
.name = MP_QSTR_TouchAlarm,
63+
.make_new = alarm_touch_touchalarm_make_new,
64+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TOUCH_TOUCHALARM_H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TOUCH_TOUCHALARM_H
29+
30+
#include "py/obj.h"
31+
#include "py/runtime.h"
32+
33+
#include "common-hal/microcontroller/Pin.h"
34+
#include "common-hal/alarm/touch/TouchAlarm.h"
35+
36+
extern const mp_obj_type_t alarm_touch_touchalarm_type;
37+
38+
extern void common_hal_alarm_touch_touchalarm_construct(alarm_touch_touchalarm_obj_t *self, const mcu_pin_obj_t *pin);
39+
40+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TOUCH_TOUCHALARM_H

0 commit comments

Comments
 (0)