Skip to content

Added deep and light sleep functions #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions adafruit_magtag/magtag.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ def __init__(
self._json_path = None
self.json_path = json_path

try:
import alarm # pylint: disable=import-outside-toplevel

self._alarm = alarm
except ImportError:
self._alarm = None

self._regexp_path = regexp_path

self.splash = self.graphics.splash
Expand Down Expand Up @@ -293,6 +300,59 @@ def set_text(self, val, index=0, auto_refresh=True):
if auto_refresh:
self.refresh()

def exit_and_deep_sleep(self, sleep_time):
"""
Stops the current program and enters deep sleep. The program is restarted from the beginning
after a certain period of time.

See https://circuitpython.readthedocs.io/en/latest/shared-bindings/alarm/index.html for more
details.

:param float sleep_time: The amount of time to sleep in seconds

"""
if self._alarm:
self.peripherals.neopixel_disable = True
self.peripherals.speaker_disable = True
pause = self._alarm.time.TimeAlarm(
monotonic_time=time.monotonic() + sleep_time
)
self._alarm.exit_and_deep_sleep_until_alarms(pause)
else:
raise NotImplementedError(
"Deep sleep not supported. Make sure you have the latest CircuitPython."
)

def enter_light_sleep(self, sleep_time):
"""
Enter light sleep and resume the program after a certain period of time.

See https://circuitpython.readthedocs.io/en/latest/shared-bindings/alarm/index.html for more
details.

:param float sleep_time: The amount of time to sleep in seconds

"""
if self._alarm:
neopixel_values = self.peripherals.neopixels
neopixel_state = self.peripherals.neopixel_disable
self.peripherals.neopixel_disable = True
speaker_state = self.peripherals.speaker_disable
self.peripherals.speaker_disable = True
pause = self._alarm.time.TimeAlarm(
monotonic_time=time.monotonic() + sleep_time
)
self._alarm.light_sleep_until_alarms(pause)
self.peripherals.neopixel_disable = neopixel_state
self.peripherals.speaker_disable = speaker_state
for i in range(4):
self.peripherals.neopixels[i] = neopixel_values[i]
gc.collect()
else:
raise NotImplementedError(
"Hardware light sleep not supported. Make sure you have the latest CircuitPython."
)

def get_local_time(self, location=None):
"""Accessor function for get_local_time()"""
return self.network.get_local_time(location=location)
Expand Down
8 changes: 8 additions & 0 deletions adafruit_magtag/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,3 +544,11 @@ def fetch_data(
return values[0]

return values

@property
def enabled(self):
"""
Return whether the WiFi is enabled

"""
return self._wifi.enabled
11 changes: 11 additions & 0 deletions adafruit_magtag/peripherals.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ def neopixel_disable(self):
def neopixel_disable(self, value):
self._neopixel_disable.value = value

@property
def speaker_disable(self):
"""
Enable or disable the speaker for power savings
"""
return not self._speaker_enable.value

@speaker_disable.setter
def speaker_disable(self, value):
self._speaker_enable.value = not value

@property
def button_a_pressed(self):
"""
Expand Down
8 changes: 8 additions & 0 deletions adafruit_magtag/wifi_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,11 @@ def ip_address(self):

"""
return wifi.radio.ipv4_address

@property
def enabled(self):
"""
Return whether the WiFi Radio is enabled

"""
return wifi.radio.enabled