@@ -95,6 +95,13 @@ def __init__(
95
95
# Font Cache
96
96
self ._fonts = {}
97
97
98
+ try :
99
+ import alarm # pylint: disable=import-outside-toplevel
100
+
101
+ self ._alarm = alarm
102
+ except ImportError :
103
+ self ._alarm = None
104
+
98
105
self ._regexp_path = regexp_path
99
106
100
107
self .splash = self .graphics .splash
@@ -309,6 +316,59 @@ def set_text(self, val, index=0, auto_refresh=True):
309
316
if auto_refresh :
310
317
self .refresh ()
311
318
319
+ def exit_and_deep_sleep (self , sleep_time ):
320
+ """
321
+ Stops the current program and enters deep sleep. The program is restarted from the beginning
322
+ after a certain period of time.
323
+
324
+ See https://circuitpython.readthedocs.io/en/latest/shared-bindings/alarm/index.html for more
325
+ details.
326
+
327
+ :param float sleep_time: The amount of time to sleep in seconds
328
+
329
+ """
330
+ if self ._alarm :
331
+ self .peripherals .neopixel_disable = True
332
+ self .peripherals .speaker_disable = True
333
+ pause = self ._alarm .time .TimeAlarm (
334
+ monotonic_time = time .monotonic () + sleep_time
335
+ )
336
+ self ._alarm .exit_and_deep_sleep_until_alarms (pause )
337
+ else :
338
+ raise NotImplementedError (
339
+ "Deep sleep not supported. Make sure you have the latest CircuitPython."
340
+ )
341
+
342
+ def enter_light_sleep (self , sleep_time ):
343
+ """
344
+ Enter light sleep and resume the program after a certain period of time.
345
+
346
+ See https://circuitpython.readthedocs.io/en/latest/shared-bindings/alarm/index.html for more
347
+ details.
348
+
349
+ :param float sleep_time: The amount of time to sleep in seconds
350
+
351
+ """
352
+ if self ._alarm :
353
+ neopixel_values = self .peripherals .neopixels
354
+ neopixel_state = self .peripherals .neopixel_disable
355
+ self .peripherals .neopixel_disable = True
356
+ speaker_state = self .peripherals .speaker_disable
357
+ self .peripherals .speaker_disable = True
358
+ pause = self ._alarm .time .TimeAlarm (
359
+ monotonic_time = time .monotonic () + sleep_time
360
+ )
361
+ self ._alarm .light_sleep_until_alarms (pause )
362
+ self .peripherals .neopixel_disable = neopixel_state
363
+ self .peripherals .speaker_disable = speaker_state
364
+ for i in range (4 ):
365
+ self .peripherals .neopixels [i ] = neopixel_values [i ]
366
+ gc .collect ()
367
+ else :
368
+ raise NotImplementedError (
369
+ "Hardware light sleep not supported. Make sure you have the latest CircuitPython."
370
+ )
371
+
312
372
def get_local_time (self , location = None ):
313
373
"""Accessor function for get_local_time()"""
314
374
return self .network .get_local_time (location = location )
0 commit comments