aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/shared-bindings/alarm
diff options
context:
space:
mode:
Diffstat (limited to 'circuitpython/shared-bindings/alarm')
-rw-r--r--circuitpython/shared-bindings/alarm/SleepMemory.c184
-rw-r--r--circuitpython/shared-bindings/alarm/SleepMemory.h40
-rw-r--r--circuitpython/shared-bindings/alarm/__init__.c261
-rw-r--r--circuitpython/shared-bindings/alarm/__init__.h67
-rw-r--r--circuitpython/shared-bindings/alarm/pin/PinAlarm.c127
-rw-r--r--circuitpython/shared-bindings/alarm/pin/PinAlarm.h43
-rw-r--r--circuitpython/shared-bindings/alarm/time/TimeAlarm.c140
-rw-r--r--circuitpython/shared-bindings/alarm/time/TimeAlarm.h39
-rw-r--r--circuitpython/shared-bindings/alarm/touch/TouchAlarm.c87
-rw-r--r--circuitpython/shared-bindings/alarm/touch/TouchAlarm.h40
10 files changed, 1028 insertions, 0 deletions
diff --git a/circuitpython/shared-bindings/alarm/SleepMemory.c b/circuitpython/shared-bindings/alarm/SleepMemory.c
new file mode 100644
index 0000000..7de36f0
--- /dev/null
+++ b/circuitpython/shared-bindings/alarm/SleepMemory.c
@@ -0,0 +1,184 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
+ * Copyright (c) 2020 Dan Halbert for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "py/binary.h"
+#include "py/objproperty.h"
+#include "py/runtime.h"
+#include "py/runtime0.h"
+
+#include "shared-bindings/alarm/SleepMemory.h"
+#include "supervisor/shared/translate.h"
+
+//| class SleepMemory:
+//| """Store raw bytes in RAM that persists during deep sleep.
+//| The class acts as a ``bytearray``.
+//| If power is lost, the memory contents are lost.
+//|
+//| Note that this class can't be imported and used directly. The sole
+//| instance of :class:`SleepMemory` is available at
+//| :attr:`alarm.sleep_memory`.
+//|
+//| Usage::
+//|
+//| import alarm
+//| alarm.sleep_memory[0] = True
+//| alarm.sleep_memory[1] = 12
+//| """
+
+//| def __init__(self) -> None:
+//| """Not used. Access the sole instance through `alarm.sleep_memory`."""
+//| ...
+//|
+//| def __bool__(self) -> bool:
+//| """``sleep_memory`` is ``True`` if its length is greater than zero.
+//| This is an easy way to check for its existence.
+//| """
+//| ...
+//|
+//| def __len__(self) -> int:
+//| """Return the length. This is used by (`len`)"""
+//| ...
+//|
+STATIC mp_obj_t alarm_sleep_memory_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
+ alarm_sleep_memory_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ uint16_t len = common_hal_alarm_sleep_memory_get_length(self);
+ switch (op) {
+ case MP_UNARY_OP_BOOL:
+ return mp_obj_new_bool(len != 0);
+ case MP_UNARY_OP_LEN:
+ return MP_OBJ_NEW_SMALL_INT(len);
+ default:
+ return MP_OBJ_NULL; // op not supported
+ }
+}
+
+STATIC const mp_rom_map_elem_t alarm_sleep_memory_locals_dict_table[] = {
+};
+
+STATIC MP_DEFINE_CONST_DICT(alarm_sleep_memory_locals_dict, alarm_sleep_memory_locals_dict_table);
+
+//| @overload
+//| def __getitem__(self, index: slice) -> bytearray: ...
+//| @overload
+//| def __getitem__(self, index: int) -> int:
+//| """Returns the value at the given index."""
+//| ...
+//|
+//| @overload
+//| def __setitem__(self, index: slice, value: ReadableBuffer) -> None: ...
+//| @overload
+//| def __setitem__(self, index: int, value: int) -> None:
+//| """Set the value at the given index."""
+//| ...
+//|
+STATIC mp_obj_t alarm_sleep_memory_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) {
+ if (value == MP_OBJ_NULL) {
+ // delete item
+ // slice deletion
+ return MP_OBJ_NULL; // op not supported
+ } else {
+ alarm_sleep_memory_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ if (0) {
+ #if MICROPY_PY_BUILTINS_SLICE
+ } else if (mp_obj_is_type(index_in, &mp_type_slice)) {
+ mp_bound_slice_t slice;
+ if (!mp_seq_get_fast_slice_indexes(common_hal_alarm_sleep_memory_get_length(self), index_in, &slice)) {
+ mp_raise_NotImplementedError(translate("only slices with step=1 (aka None) are supported"));
+ }
+ if (value != MP_OBJ_SENTINEL) {
+ #if MICROPY_PY_ARRAY_SLICE_ASSIGN
+ // Assign
+ size_t src_len = slice.stop - slice.start;
+ uint8_t *src_items;
+ if (mp_obj_is_type(value, &mp_type_array) ||
+ mp_obj_is_type(value, &mp_type_bytearray) ||
+ mp_obj_is_type(value, &mp_type_memoryview) ||
+ mp_obj_is_type(value, &mp_type_bytes)) {
+ mp_buffer_info_t bufinfo;
+ mp_get_buffer_raise(value, &bufinfo, MP_BUFFER_READ);
+ if (bufinfo.len != src_len) {
+ mp_raise_ValueError(translate("Slice and value different lengths."));
+ }
+ src_len = bufinfo.len;
+ src_items = bufinfo.buf;
+ if (1 != mp_binary_get_size('@', bufinfo.typecode, NULL)) {
+ mp_raise_ValueError(translate("Array values should be single bytes."));
+ }
+ } else {
+ mp_raise_NotImplementedError(translate("array/bytes required on right side"));
+ }
+
+ if (!common_hal_alarm_sleep_memory_set_bytes(self, slice.start, src_items, src_len)) {
+ mp_raise_RuntimeError(translate("Unable to write to sleep_memory."));
+ }
+ return mp_const_none;
+ #else
+ return MP_OBJ_NULL; // op not supported
+ #endif
+ } else {
+ // Read slice.
+ size_t len = slice.stop - slice.start;
+ uint8_t *items = m_new(uint8_t, len);
+ common_hal_alarm_sleep_memory_get_bytes(self, slice.start, items, len);
+ return mp_obj_new_bytearray_by_ref(len, items);
+ }
+ #endif
+ } else {
+ // Single index rather than slice.
+ size_t index = mp_get_index(self->base.type, common_hal_alarm_sleep_memory_get_length(self),
+ index_in, false);
+ if (value == MP_OBJ_SENTINEL) {
+ // load
+ uint8_t value_out;
+ common_hal_alarm_sleep_memory_get_bytes(self, index, &value_out, 1);
+ return MP_OBJ_NEW_SMALL_INT(value_out);
+ } else {
+ // store
+ mp_int_t byte_value = mp_obj_get_int(value);
+ if (byte_value > 0xff || byte_value < 0) {
+ mp_raise_ValueError(translate("Bytes must be between 0 and 255."));
+ }
+ uint8_t short_value = byte_value;
+ if (!common_hal_alarm_sleep_memory_set_bytes(self, index, &short_value, 1)) {
+ mp_raise_RuntimeError(translate("Unable to write to sleep_memory."));
+ }
+ return mp_const_none;
+ }
+ }
+ }
+}
+
+const mp_obj_type_t alarm_sleep_memory_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_SleepMemory,
+ .flags = MP_TYPE_FLAG_EXTENDED,
+ .locals_dict = (mp_obj_t)&alarm_sleep_memory_locals_dict,
+ MP_TYPE_EXTENDED_FIELDS(
+ .subscr = alarm_sleep_memory_subscr,
+ .unary_op = alarm_sleep_memory_unary_op,
+ ),
+};
diff --git a/circuitpython/shared-bindings/alarm/SleepMemory.h b/circuitpython/shared-bindings/alarm/SleepMemory.h
new file mode 100644
index 0000000..c1667ec
--- /dev/null
+++ b/circuitpython/shared-bindings/alarm/SleepMemory.h
@@ -0,0 +1,40 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
+ * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_SLEEPMEMORY_H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_SLEEPMEMORY_H
+
+#include "common-hal/alarm/SleepMemory.h"
+
+extern const mp_obj_type_t alarm_sleep_memory_type;
+
+uint32_t common_hal_alarm_sleep_memory_get_length(alarm_sleep_memory_obj_t *self);
+
+bool common_hal_alarm_sleep_memory_set_bytes(alarm_sleep_memory_obj_t *self, uint32_t start_index, const uint8_t *values, uint32_t len);
+void common_hal_alarm_sleep_memory_get_bytes(alarm_sleep_memory_obj_t *self, uint32_t start_index, uint8_t *values, uint32_t len);
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_SLEEPMEMORY_H
diff --git a/circuitpython/shared-bindings/alarm/__init__.c b/circuitpython/shared-bindings/alarm/__init__.c
new file mode 100644
index 0000000..7fd14b7
--- /dev/null
+++ b/circuitpython/shared-bindings/alarm/__init__.c
@@ -0,0 +1,261 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Dan Halbert for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "py/obj.h"
+#include "py/runtime.h"
+
+#include "shared-bindings/alarm/__init__.h"
+#include "shared-bindings/alarm/SleepMemory.h"
+#include "shared-bindings/alarm/pin/PinAlarm.h"
+#include "shared-bindings/alarm/time/TimeAlarm.h"
+#include "shared-bindings/alarm/touch/TouchAlarm.h"
+#include "shared-bindings/supervisor/Runtime.h"
+#include "shared-bindings/time/__init__.h"
+#include "supervisor/shared/workflow.h"
+
+//| """Alarms and sleep
+//|
+//| Provides alarms that trigger based on time intervals or on external events, such as pin
+//| changes.
+//| The program can simply wait for these alarms, or go to sleep and be awoken when they trigger.
+//|
+//| There are two supported levels of sleep: light sleep and deep sleep.
+//|
+//| Light sleep keeps sufficient state so the program can resume after sleeping.
+//| It does not shut down WiFi, BLE, or other communications, or ongoing activities such
+//| as audio playback. It reduces power consumption to the extent possible that leaves
+//| these continuing activities running. In some cases there may be no decrease in power consumption.
+//|
+//| Deep sleep shuts down power to nearly all of the microcontroller including the CPU and RAM. This can save
+//| a more significant amount of power, but CircuitPython must restart ``code.py`` from the beginning when
+//| awakened.
+//|
+//| For both light sleep and deep sleep, if CircuitPython is connected to a host computer,
+//| maintaining the connection takes priority and power consumption may not be reduced.
+//|
+//| For more information about working with alarms and light/deep sleep in CircuitPython,
+//| see `this Learn guide <https://learn.adafruit.com/deep-sleep-with-circuitpython>`_.
+//| """
+
+//| sleep_memory: SleepMemory
+//| """Memory that persists during deep sleep.
+//| This object is the sole instance of `alarm.SleepMemory`."""
+//|
+
+//| wake_alarm: Optional[circuitpython_typing.Alarm]
+//| """The most recently triggered alarm. If CircuitPython was sleeping, the alarm that woke it from sleep.
+//| If no alarm occured since the last hard reset or soft restart, value is ``None``.
+//| """
+//|
+
+// wake_alarm is implemented as a dictionary entry, so there's no code here.
+
+STATIC void validate_objs_are_alarms(size_t n_args, const mp_obj_t *objs) {
+ for (size_t i = 0; i < n_args; i++) {
+ if (mp_obj_is_type(objs[i], &alarm_pin_pinalarm_type) ||
+ mp_obj_is_type(objs[i], &alarm_time_timealarm_type) ||
+ mp_obj_is_type(objs[i], &alarm_touch_touchalarm_type)) {
+ continue;
+ }
+ mp_raise_TypeError_varg(translate("Expected an alarm"));
+ }
+}
+
+//| def light_sleep_until_alarms(*alarms: circuitpython_typing.Alarm) -> circuitpython_typing.Alarm:
+//| """Go into a light sleep until awakened one of the alarms. The alarm causing the wake-up
+//| is returned, and is also available as `alarm.wake_alarm`.
+//|
+//| If no alarms are specified, return immediately.
+//|
+//| **If CircuitPython is connected to a host computer, the connection will be maintained,
+//| and the microcontroller may not actually go into a light sleep.**
+//| This allows the user to interrupt an existing program with ctrl-C,
+//| and to edit the files in CIRCUITPY, which would not be possible in true light sleep.
+//| Thus, to use light sleep and save significant power,
+//| it may be necessary to disconnect from the host.
+//| """
+//| ...
+//|
+STATIC mp_obj_t alarm_light_sleep_until_alarms(size_t n_args, const mp_obj_t *args) {
+ if (n_args == 0) {
+ return mp_const_none;
+ }
+
+ validate_objs_are_alarms(n_args, args);
+
+ mp_obj_t alarm = common_hal_alarm_light_sleep_until_alarms(n_args, args);
+ shared_alarm_save_wake_alarm(alarm);
+ return alarm;
+}
+MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(alarm_light_sleep_until_alarms_obj, 1, MP_OBJ_FUN_ARGS_MAX, alarm_light_sleep_until_alarms);
+
+//| def exit_and_deep_sleep_until_alarms(*alarms: circuitpython_typing.Alarm) -> None:
+//| """Exit the program and go into a deep sleep, until awakened by one of the alarms.
+//| This function does not return.
+//|
+//| When awakened, the microcontroller will restart and will run ``boot.py`` and ``code.py``
+//| from the beginning.
+//|
+//| After restart, an alarm *equivalent* to the one that caused the wake-up
+//| will be available as `alarm.wake_alarm`.
+//| Its type and/or attributes may not correspond exactly to the original alarm.
+//| For time-base alarms, currently, an `alarm.time.TimeAlarm()` is created.
+//|
+//| If no alarms are specified, the microcontroller will deep sleep until reset.
+//|
+//| **If CircuitPython is connected to a host computer via USB or BLE
+//| the first time a deep sleep is requested,
+//| the connection will be maintained and the system will not go into deep sleep.**
+//| This allows the user to interrupt an existing program with ctrl-C,
+//| and to edit the files in CIRCUITPY, which would not be possible in true deep sleep.
+//|
+//| If CircuitPython goes into a true deep sleep, and USB or BLE is reconnected,
+//| the next deep sleep will still be a true deep sleep. You must do a hard reset
+//| or power-cycle to exit a true deep sleep loop.
+//|
+//| Here is skeletal example that deep-sleeps and restarts every 60 seconds:
+//|
+//| .. code-block:: python
+//|
+//| import alarm
+//| import time
+//|
+//| print("Waking up")
+//|
+//| # Set an alarm for 60 seconds from now.
+//| time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 60)
+//|
+//| # Deep sleep until the alarm goes off. Then restart the program.
+//| alarm.exit_and_deep_sleep_until_alarms(time_alarm)
+//| """
+//| ...
+//|
+STATIC mp_obj_t alarm_exit_and_deep_sleep_until_alarms(size_t n_args, const mp_obj_t *args) {
+ validate_objs_are_alarms(n_args, args);
+
+ // Validate the alarms and set them.
+ common_hal_alarm_set_deep_sleep_alarms(n_args, args);
+
+ // Raise an exception, which will be processed in main.c.
+ mp_raise_type_arg(&mp_type_DeepSleepRequest, NULL);
+
+ // Doesn't get here.
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(alarm_exit_and_deep_sleep_until_alarms_obj, 1, MP_OBJ_FUN_ARGS_MAX, alarm_exit_and_deep_sleep_until_alarms);
+
+STATIC const mp_map_elem_t alarm_pin_globals_table[] = {
+ { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_pin) },
+
+ { MP_ROM_QSTR(MP_QSTR_PinAlarm), MP_OBJ_FROM_PTR(&alarm_pin_pinalarm_type) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(alarm_pin_globals, alarm_pin_globals_table);
+
+STATIC const mp_obj_module_t alarm_pin_module = {
+ .base = { &mp_type_module },
+ .globals = (mp_obj_dict_t *)&alarm_pin_globals,
+};
+
+STATIC const mp_map_elem_t alarm_time_globals_table[] = {
+ { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_time) },
+
+ { MP_ROM_QSTR(MP_QSTR_TimeAlarm), MP_OBJ_FROM_PTR(&alarm_time_timealarm_type) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(alarm_time_globals, alarm_time_globals_table);
+
+STATIC const mp_obj_module_t alarm_time_module = {
+ .base = { &mp_type_module },
+ .globals = (mp_obj_dict_t *)&alarm_time_globals,
+};
+
+STATIC const mp_map_elem_t alarm_touch_globals_table[] = {
+ { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_touch) },
+ { MP_ROM_QSTR(MP_QSTR_TouchAlarm), MP_OBJ_FROM_PTR(&alarm_touch_touchalarm_type) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(alarm_touch_globals, alarm_touch_globals_table);
+
+STATIC const mp_obj_module_t alarm_touch_module = {
+ .base = { &mp_type_module },
+ .globals = (mp_obj_dict_t *)&alarm_touch_globals,
+};
+
+// The module table is mutable because .wake_alarm is a mutable attribute.
+STATIC mp_map_elem_t alarm_module_globals_table[] = {
+ { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_alarm) },
+
+ // wake_alarm is a mutable attribute.
+ { MP_ROM_QSTR(MP_QSTR_wake_alarm), mp_const_none },
+
+ { MP_ROM_QSTR(MP_QSTR_light_sleep_until_alarms), MP_OBJ_FROM_PTR(&alarm_light_sleep_until_alarms_obj) },
+ { MP_ROM_QSTR(MP_QSTR_exit_and_deep_sleep_until_alarms),
+ MP_OBJ_FROM_PTR(&alarm_exit_and_deep_sleep_until_alarms_obj) },
+
+ { MP_ROM_QSTR(MP_QSTR_pin), MP_OBJ_FROM_PTR(&alarm_pin_module) },
+ { MP_ROM_QSTR(MP_QSTR_time), MP_OBJ_FROM_PTR(&alarm_time_module) },
+ { MP_ROM_QSTR(MP_QSTR_touch), MP_OBJ_FROM_PTR(&alarm_touch_module) },
+
+ { MP_ROM_QSTR(MP_QSTR_SleepMemory), MP_OBJ_FROM_PTR(&alarm_sleep_memory_type) },
+ { MP_ROM_QSTR(MP_QSTR_sleep_memory), MP_OBJ_FROM_PTR(&alarm_sleep_memory_obj) },
+};
+STATIC MP_DEFINE_MUTABLE_DICT(alarm_module_globals, alarm_module_globals_table);
+
+// Fetch value from module dict.
+mp_obj_t shared_alarm_get_wake_alarm(void) {
+ mp_map_elem_t *elem =
+ mp_map_lookup(&alarm_module_globals.map, MP_ROM_QSTR(MP_QSTR_wake_alarm), MP_MAP_LOOKUP);
+ if (elem) {
+ return elem->value;
+ } else {
+ return NULL;
+ }
+}
+
+// Initialize .wake_alarm value.
+void shared_alarm_save_wake_alarm(mp_obj_t alarm) {
+ // Equivalent of:
+ // alarm.wake_alarm = alarm
+ mp_map_elem_t *elem =
+ mp_map_lookup(&alarm_module_globals.map, MP_ROM_QSTR(MP_QSTR_wake_alarm), MP_MAP_LOOKUP);
+ if (elem) {
+ elem->value = alarm;
+ }
+}
+
+const mp_obj_module_t alarm_module = {
+ .base = { &mp_type_module },
+ .globals = (mp_obj_dict_t *)&alarm_module_globals,
+};
+
+extern void port_idle_until_interrupt(void);
+
+MP_WEAK void common_hal_alarm_pretending_deep_sleep(void) {
+ port_idle_until_interrupt();
+}
+
+MP_REGISTER_MODULE(MP_QSTR_alarm, alarm_module, CIRCUITPY_ALARM);
diff --git a/circuitpython/shared-bindings/alarm/__init__.h b/circuitpython/shared-bindings/alarm/__init__.h
new file mode 100644
index 0000000..eb67917
--- /dev/null
+++ b/circuitpython/shared-bindings/alarm/__init__.h
@@ -0,0 +1,67 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Dan Halbert for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ALARM___INIT___H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_ALARM___INIT___H
+
+#include "py/obj.h"
+
+#include "common-hal/alarm/__init__.h"
+
+// Light sleep fully self-contained and does not exit user code. It will return
+// the same alarm object that was orignally passed in, unlike deep sleep, which
+// must create an identical copy due to the VM reset
+extern mp_obj_t common_hal_alarm_light_sleep_until_alarms(size_t n_alarms, const mp_obj_t *alarms);
+
+// Deep sleep is a two step process. Alarms are set when the VM is valid but
+// everything is reset before entering deep sleep. Furthermore, deep sleep may
+// not actually happen if the user is connected to the device. In this case, the
+// supervisor will idle using `port_wait_for_interrupt`. After each call, it will
+// call alarm_woken_from_sleep to see if we've been woken by an alarm and if so,
+// it will exit idle as if deep sleep was exited
+extern void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *alarms);
+
+extern NORETURN void common_hal_alarm_enter_deep_sleep(void);
+
+// May be used to re-initialize peripherals like GPIO, if the VM reset returned
+// them to a default state
+extern void common_hal_alarm_pretending_deep_sleep(void);
+
+// Fetches value from module dict.
+extern mp_obj_t shared_alarm_get_wake_alarm(void);
+
+// Creates a new alarm object after exiting deep sleep (real or fake)
+extern mp_obj_t common_hal_alarm_create_wake_alarm(void);
+
+// Saves alarm to global array
+void shared_alarm_save_wake_alarm(mp_obj_t alarm);
+
+// True if an alarm is alerting. Used in light sleep/fake deep sleep
+extern bool common_hal_alarm_woken_from_sleep(void);
+
+extern void common_hal_alarm_gc_collect(void);
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ALARM___INIT___H
diff --git a/circuitpython/shared-bindings/alarm/pin/PinAlarm.c b/circuitpython/shared-bindings/alarm/pin/PinAlarm.c
new file mode 100644
index 0000000..ff34716
--- /dev/null
+++ b/circuitpython/shared-bindings/alarm/pin/PinAlarm.c
@@ -0,0 +1,127 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Dan Halbert for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "shared-bindings/board/__init__.h"
+#include "shared-bindings/microcontroller/__init__.h"
+#include "shared-bindings/microcontroller/Pin.h"
+#include "shared-bindings/alarm/pin/PinAlarm.h"
+
+#include "py/nlr.h"
+#include "py/obj.h"
+#include "py/objproperty.h"
+#include "py/runtime.h"
+#include "supervisor/shared/translate.h"
+
+//| class PinAlarm:
+//| """Trigger an alarm when a pin changes state."""
+//|
+//| def __init__(self, pin: microcontroller.Pin, value: bool, edge: bool = False, pull: bool = False) -> None:
+//| """Create an alarm triggered by a `microcontroller.Pin` level. The alarm is not active
+//| until it is passed to an `alarm`-enabling function, such as `alarm.light_sleep_until_alarms()` or
+//| `alarm.exit_and_deep_sleep_until_alarms()`.
+//|
+//| :param microcontroller.Pin pin: The pin to monitor. On some ports, the choice of pin
+//| may be limited due to hardware restrictions, particularly for deep-sleep alarms.
+//| :param bool value: When active, trigger when the pin value is high (``True``) or low (``False``).
+//| On some ports, multiple `PinAlarm` objects may need to have coordinated values
+//| for deep-sleep alarms.
+//| :param bool edge: If ``True``, trigger only when there is a transition to the specified
+//| value of ``value``. If ``True``, if the alarm becomes active when the pin value already
+//| matches ``value``, the alarm is not triggered: the pin must transition from ``not value``
+//| to ``value`` to trigger the alarm. On some ports, edge-triggering may not be available,
+//| particularly for deep-sleep alarms.
+//| :param bool pull: Enable a pull-up or pull-down which pulls the pin to the level opposite
+//| that of ``value``. For instance, if ``value`` is set to ``True``, setting ``pull``
+//| to ``True`` will enable a pull-down, to hold the pin low normally until an outside signal
+//| pulls it high.
+//| """
+//| ...
+//|
+STATIC mp_obj_t alarm_pin_pinalarm_make_new(const mp_obj_type_t *type, mp_uint_t n_args, size_t n_kw, const mp_obj_t *all_args) {
+ alarm_pin_pinalarm_obj_t *self = m_new_obj(alarm_pin_pinalarm_obj_t);
+ self->base.type = &alarm_pin_pinalarm_type;
+ enum { ARG_pin, ARG_value, ARG_edge, ARG_pull };
+ static const mp_arg_t allowed_args[] = {
+ { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ },
+ { MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_BOOL },
+ { MP_QSTR_edge, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
+ { MP_QSTR_pull, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
+ };
+ mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
+ mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
+
+ const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[ARG_pin].u_obj);
+
+ common_hal_alarm_pin_pinalarm_construct(self,
+ pin,
+ args[ARG_value].u_bool,
+ args[ARG_edge].u_bool,
+ args[ARG_pull].u_bool);
+
+ return MP_OBJ_FROM_PTR(self);
+}
+
+//| pin: microcontroller.Pin
+//| """The trigger pin."""
+//|
+STATIC mp_obj_t alarm_pin_pinalarm_obj_get_pin(mp_obj_t self_in) {
+ alarm_pin_pinalarm_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ const mcu_pin_obj_t *pin = common_hal_alarm_pin_pinalarm_get_pin(self);
+ if (pin == NULL) {
+ return mp_const_none;
+ }
+ return MP_OBJ_FROM_PTR(pin);
+}
+MP_DEFINE_CONST_FUN_OBJ_1(alarm_pin_pinalarm_get_pin_obj, alarm_pin_pinalarm_obj_get_pin);
+
+MP_PROPERTY_GETTER(alarm_pin_pinalarm_pin_obj,
+ (mp_obj_t)&alarm_pin_pinalarm_get_pin_obj);
+
+//| value: bool
+//| """The value on which to trigger."""
+//|
+STATIC mp_obj_t alarm_pin_pinalarm_obj_get_value(mp_obj_t self_in) {
+ alarm_pin_pinalarm_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ return mp_obj_new_bool(common_hal_alarm_pin_pinalarm_get_value(self));
+}
+MP_DEFINE_CONST_FUN_OBJ_1(alarm_pin_pinalarm_get_value_obj, alarm_pin_pinalarm_obj_get_value);
+
+MP_PROPERTY_GETTER(alarm_pin_pinalarm_value_obj,
+ (mp_obj_t)&alarm_pin_pinalarm_get_value_obj);
+
+STATIC const mp_rom_map_elem_t alarm_pin_pinalarm_locals_dict_table[] = {
+ { MP_ROM_QSTR(MP_QSTR_pin), MP_ROM_PTR(&alarm_pin_pinalarm_pin_obj) },
+ { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&alarm_pin_pinalarm_value_obj) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(alarm_pin_pinalarm_locals_dict, alarm_pin_pinalarm_locals_dict_table);
+
+const mp_obj_type_t alarm_pin_pinalarm_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_PinAlarm,
+ .make_new = alarm_pin_pinalarm_make_new,
+ .locals_dict = (mp_obj_t)&alarm_pin_pinalarm_locals_dict,
+};
diff --git a/circuitpython/shared-bindings/alarm/pin/PinAlarm.h b/circuitpython/shared-bindings/alarm/pin/PinAlarm.h
new file mode 100644
index 0000000..ba74bab
--- /dev/null
+++ b/circuitpython/shared-bindings/alarm/pin/PinAlarm.h
@@ -0,0 +1,43 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Dan Halbert for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_PIN_PINALARM_H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_PIN_PINALARM_H
+
+#include "py/obj.h"
+#include "py/objtuple.h"
+#include "common-hal/microcontroller/Pin.h"
+#include "common-hal/alarm/pin/PinAlarm.h"
+
+extern const mp_obj_type_t alarm_pin_pinalarm_type;
+
+void common_hal_alarm_pin_pinalarm_construct(alarm_pin_pinalarm_obj_t *self, const mcu_pin_obj_t *pin, bool value, bool edge, bool pull);
+extern const mcu_pin_obj_t *common_hal_alarm_pin_pinalarm_get_pin(alarm_pin_pinalarm_obj_t *self);
+extern bool common_hal_alarm_pin_pinalarm_get_value(alarm_pin_pinalarm_obj_t *self);
+extern bool common_hal_alarm_pin_pinalarm_get_edge(alarm_pin_pinalarm_obj_t *self);
+extern bool common_hal_alarm_pin_pinalarm_get_pull(alarm_pin_pinalarm_obj_t *self);
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_PIN_PINALARM_H
diff --git a/circuitpython/shared-bindings/alarm/time/TimeAlarm.c b/circuitpython/shared-bindings/alarm/time/TimeAlarm.c
new file mode 100644
index 0000000..ab0274a
--- /dev/null
+++ b/circuitpython/shared-bindings/alarm/time/TimeAlarm.c
@@ -0,0 +1,140 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Dan Halbert for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "py/nlr.h"
+#include "py/obj.h"
+#include "py/objproperty.h"
+#include "py/runtime.h"
+
+#include "shared-bindings/alarm/time/TimeAlarm.h"
+#include "shared-bindings/rtc/__init__.h"
+#include "shared-bindings/time/__init__.h"
+
+#include "supervisor/shared/translate.h"
+
+#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
+mp_obj_t MP_WEAK rtc_get_time_source_time(void) {
+ mp_raise_RuntimeError(translate("RTC is not supported on this board"));
+}
+#endif
+
+//| class TimeAlarm:
+//| """Trigger an alarm when the specified time is reached."""
+//|
+//| def __init__(self, monotonic_time: Optional[float] = None, epoch_time: Optional[int] = None) -> None:
+//| """Create an alarm that will be triggered when `time.monotonic()` would equal
+//| ``monotonic_time``, or when `time.time()` would equal ``epoch_time``.
+//| Only one of the two arguments can be given.
+//| The alarm is not active until it is passed to an
+//| `alarm`-enabling function, such as `alarm.light_sleep_until_alarms()` or
+//| `alarm.exit_and_deep_sleep_until_alarms()`.
+//|
+//| If the given time is in the past when sleep occurs, the alarm will be triggered
+//| immediately.
+//| """
+//| ...
+//|
+STATIC mp_obj_t alarm_time_timealarm_make_new(const mp_obj_type_t *type,
+ size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
+ alarm_time_timealarm_obj_t *self = m_new_obj(alarm_time_timealarm_obj_t);
+ self->base.type = &alarm_time_timealarm_type;
+
+ enum { ARG_monotonic_time, ARG_epoch_time };
+ static const mp_arg_t allowed_args[] = {
+ { MP_QSTR_monotonic_time, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
+ { MP_QSTR_epoch_time, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
+ };
+
+ mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
+ mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
+
+ bool have_monotonic = args[ARG_monotonic_time].u_obj != mp_const_none;
+ bool have_epoch = args[ARG_epoch_time].u_obj != mp_const_none;
+
+ if (!(have_monotonic ^ have_epoch)) {
+ mp_raise_ValueError(translate("Supply one of monotonic_time or epoch_time"));
+ }
+
+ mp_float_t monotonic_time = 0; // To avoid compiler warning.
+ if (have_monotonic) {
+ monotonic_time = mp_obj_get_float(args[ARG_monotonic_time].u_obj);
+ }
+
+ mp_float_t monotonic_time_now = common_hal_time_monotonic_ms() / 1000.0;
+
+ if (have_epoch) {
+ #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
+ mp_raise_ValueError(translate("epoch_time not supported on this board"));
+ #else
+ mp_uint_t epoch_time_secs = mp_obj_int_get_checked(args[ARG_epoch_time].u_obj);
+
+ timeutils_struct_time_t tm;
+ struct_time_to_tm(rtc_get_time_source_time(), &tm);
+ mp_uint_t epoch_secs_now = timeutils_seconds_since_epoch(tm.tm_year, tm.tm_mon, tm.tm_mday,
+ tm.tm_hour, tm.tm_min, tm.tm_sec);
+ // How far in the future (in secs) is the requested time?
+ mp_int_t epoch_diff = epoch_time_secs - epoch_secs_now;
+ // Convert it to a future monotonic time.
+ monotonic_time = monotonic_time_now + epoch_diff;
+ #endif
+ }
+
+ if (monotonic_time < monotonic_time_now) {
+ mp_raise_ValueError(translate("Time is in the past."));
+ }
+
+ common_hal_alarm_time_timealarm_construct(self, monotonic_time);
+
+ return MP_OBJ_FROM_PTR(self);
+}
+
+//| monotonic_time: float
+//| """When this time is reached, the alarm will trigger, based on the `time.monotonic()` clock.
+//| The time may be given as ``epoch_time`` in the constructor, but it is returned
+//| by this property only as a `time.monotonic()` time.
+//| """
+//|
+STATIC mp_obj_t alarm_time_timealarm_obj_get_monotonic_time(mp_obj_t self_in) {
+ alarm_time_timealarm_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ return mp_obj_new_float(common_hal_alarm_time_timealarm_get_monotonic_time(self));
+}
+MP_DEFINE_CONST_FUN_OBJ_1(alarm_time_timealarm_get_monotonic_time_obj, alarm_time_timealarm_obj_get_monotonic_time);
+
+MP_PROPERTY_GETTER(alarm_time_timealarm_monotonic_time_obj,
+ (mp_obj_t)&alarm_time_timealarm_get_monotonic_time_obj);
+
+STATIC const mp_rom_map_elem_t alarm_time_timealarm_locals_dict_table[] = {
+ { MP_ROM_QSTR(MP_QSTR_monotonic_time), MP_ROM_PTR(&alarm_time_timealarm_monotonic_time_obj) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(alarm_time_timealarm_locals_dict, alarm_time_timealarm_locals_dict_table);
+
+const mp_obj_type_t alarm_time_timealarm_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_TimeAlarm,
+ .make_new = alarm_time_timealarm_make_new,
+ .locals_dict = (mp_obj_t)&alarm_time_timealarm_locals_dict,
+};
diff --git a/circuitpython/shared-bindings/alarm/time/TimeAlarm.h b/circuitpython/shared-bindings/alarm/time/TimeAlarm.h
new file mode 100644
index 0000000..0a4b9ca
--- /dev/null
+++ b/circuitpython/shared-bindings/alarm/time/TimeAlarm.h
@@ -0,0 +1,39 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Dan Halbert for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TIME_TIMEALARM_H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TIME_TIMEALARM_H
+
+#include "py/obj.h"
+
+#include "common-hal/alarm/time/TimeAlarm.h"
+
+extern const mp_obj_type_t alarm_time_timealarm_type;
+
+extern void common_hal_alarm_time_timealarm_construct(alarm_time_timealarm_obj_t *self, mp_float_t monotonic_time);
+extern mp_float_t common_hal_alarm_time_timealarm_get_monotonic_time(alarm_time_timealarm_obj_t *self);
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TIME_TIMEALARM_H
diff --git a/circuitpython/shared-bindings/alarm/touch/TouchAlarm.c b/circuitpython/shared-bindings/alarm/touch/TouchAlarm.c
new file mode 100644
index 0000000..ce5074f
--- /dev/null
+++ b/circuitpython/shared-bindings/alarm/touch/TouchAlarm.c
@@ -0,0 +1,87 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 microDev
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "shared-bindings/alarm/touch/TouchAlarm.h"
+#include "shared-bindings/board/__init__.h"
+
+#include "py/objproperty.h"
+
+//| class TouchAlarm:
+//| """Trigger an alarm when touch is detected."""
+//|
+//| def __init__(self, *pin: microcontroller.Pin) -> None:
+//| """Create an alarm that will be triggered when the given pin is touched.
+//| The alarm is not active until it is passed to an `alarm`-enabling function, such as
+//| `alarm.light_sleep_until_alarms()` or `alarm.exit_and_deep_sleep_until_alarms()`.
+//|
+//| :param microcontroller.Pin pin: The pin to monitor. On some ports, the choice of pin
+//| may be limited due to hardware restrictions, particularly for deep-sleep alarms.
+//| """
+//| ...
+//|
+STATIC mp_obj_t alarm_touch_touchalarm_make_new(const mp_obj_type_t *type,
+ size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
+ alarm_touch_touchalarm_obj_t *self = m_new_obj(alarm_touch_touchalarm_obj_t);
+ self->base.type = &alarm_touch_touchalarm_type;
+
+ enum { ARG_pin };
+ static const mp_arg_t allowed_args[] = {
+ { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ },
+ };
+
+ mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
+ mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
+
+ const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[ARG_pin].u_obj);
+
+ common_hal_alarm_touch_touchalarm_construct(self, pin);
+
+ return MP_OBJ_FROM_PTR(self);
+}
+
+//| pin: microcontroller.Pin
+//| """The trigger pin."""
+//|
+STATIC mp_obj_t alarm_touch_touchalarm_obj_get_pin(mp_obj_t self_in) {
+ alarm_touch_touchalarm_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ return MP_OBJ_FROM_PTR(self->pin);
+}
+MP_DEFINE_CONST_FUN_OBJ_1(alarm_touch_touchalarm_get_pin_obj, alarm_touch_touchalarm_obj_get_pin);
+
+MP_PROPERTY_GETTER(alarm_touch_touchalarm_pin_obj,
+ (mp_obj_t)&alarm_touch_touchalarm_get_pin_obj);
+
+STATIC const mp_rom_map_elem_t alarm_touch_touchalarm_locals_dict_table[] = {
+ { MP_ROM_QSTR(MP_QSTR_pin), MP_ROM_PTR(&alarm_touch_touchalarm_pin_obj) },
+};
+STATIC MP_DEFINE_CONST_DICT(alarm_touch_touchalarm_locals_dict, alarm_touch_touchalarm_locals_dict_table);
+
+const mp_obj_type_t alarm_touch_touchalarm_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_TouchAlarm,
+ .make_new = alarm_touch_touchalarm_make_new,
+ .locals_dict = (mp_obj_t)&alarm_touch_touchalarm_locals_dict,
+};
diff --git a/circuitpython/shared-bindings/alarm/touch/TouchAlarm.h b/circuitpython/shared-bindings/alarm/touch/TouchAlarm.h
new file mode 100644
index 0000000..1b70d4d
--- /dev/null
+++ b/circuitpython/shared-bindings/alarm/touch/TouchAlarm.h
@@ -0,0 +1,40 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 microDev
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TOUCH_TOUCHALARM_H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TOUCH_TOUCHALARM_H
+
+#include "py/obj.h"
+#include "py/runtime.h"
+
+#include "common-hal/microcontroller/Pin.h"
+#include "common-hal/alarm/touch/TouchAlarm.h"
+
+extern const mp_obj_type_t alarm_touch_touchalarm_type;
+
+extern void common_hal_alarm_touch_touchalarm_construct(alarm_touch_touchalarm_obj_t *self, const mcu_pin_obj_t *pin);
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TOUCH_TOUCHALARM_H