-
Notifications
You must be signed in to change notification settings - Fork 0
/
sleep.py
65 lines (49 loc) · 1.69 KB
/
sleep.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Utility functions and class for putting the microcontroller to sleep.
"""
import time
import adafruit_logging as logging
# pylint: disable=import-error
import alarm
# pylint: disable=too-few-public-methods
# There is no Enum class in CircuitPython so this is a bare class.
class SleepKind:
"""
Sleep kind.
"""
LIGHT = 1
DEEP = 2
def __init__(self, kind: int):
if kind not in (self.LIGHT, self.DEEP):
raise ValueError("not a valid kind")
self.kind = kind
def __repr__(self):
return self.__str__()
def __str__(self):
if self.kind == self.LIGHT:
return "light"
if self.kind == self.DEEP:
return "deep"
return "N/A"
def enter_sleep(sleep_period: int, sleep_kind: SleepKind) -> None:
"""
Enters light or deep sleep.
"""
logger = logging.getLogger("")
logger.info(f"Going to {sleep_kind} sleep for {sleep_period} seconds")
#
# time.monotonic() will start losing precision rather quickly,
# however for the deep sleep this is not a problem because the internal
# clock gets reset on deep sleep. It is assumed that light sleep
# is always followed by deep sleep.
#
now = time.monotonic()
logger.debug(f"time now: {now}")
logger.debug(f"alarm time: {now + sleep_period}")
# Create an alarm that will trigger sleep_period number of seconds from now.
time_alarm = alarm.time.TimeAlarm(monotonic_time=now + sleep_period)
if sleep_kind.kind == SleepKind.LIGHT:
alarm.light_sleep_until_alarms(time_alarm)
else:
# Exit and deep sleep until the alarm wakes us.
alarm.exit_and_deep_sleep_until_alarms(time_alarm)