Skip to content

Commit 9bdf633

Browse files
authored
Merge pull request #90 from FoamyGuy/pin_alarm_example
pin alarm deep sleep example and docs
2 parents 72951a1 + 27b5bfa commit 9bdf633

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

adafruit_magtag/magtag.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ def exit_and_deep_sleep(self, sleep_time: float) -> None:
122122
See https://circuitpython.readthedocs.io/en/latest/shared-bindings/alarm/index.html for more
123123
details.
124124
125+
Note: This function is for time based deep sleep only. If you want to use a PinAlarm
126+
to wake up from deep sleep you need to deinit() the pins and use the alarm module
127+
directly.
128+
125129
:param float sleep_time: The amount of time to sleep in seconds
126130
127131
"""

adafruit_magtag/peripherals.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ def play_tone(self, frequency: float, duration: float) -> None:
9898
self._speaker_enable.value = False
9999

100100
def deinit(self) -> None:
101-
"""Call deinit on all resources to free them"""
101+
"""Call deinit to free all resources used by peripherals.
102+
You must call this function before you can use the peripheral
103+
pins for other purposes like PinAlarm with deep sleep."""
102104
self.neopixels.deinit()
103105
if self._neopixel_disable is not None:
104106
self._neopixel_disable.deinit()

docs/examples.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@ Other Demos
1313
.. literalinclude:: ../examples/magtag_quote_demo.py
1414
:caption: examples/magtag_quote_demo.py
1515
:linenos:
16+
17+
Deep Sleep and wake up from Button press.
18+
19+
.. literalinclude:: ../examples/magtag_btn_sleep_demo.py
20+
:caption: examples/magtag_btn_sleep_demo.py
21+
:linenos:

examples/magtag_btn_sleep_demo.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# SPDX-FileCopyrightText: 2023 Tim Cocks
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
import time
6+
import alarm
7+
import board
8+
from adafruit_magtag.magtag import MagTag
9+
10+
IDLE_TIMEOUT = 10 # seconds idle, then sleep
11+
12+
magtag = MagTag()
13+
14+
magtag.add_text(
15+
text_position=(
16+
50,
17+
(magtag.graphics.display.height // 2) - 1,
18+
),
19+
text_scale=3,
20+
)
21+
22+
magtag.set_text("Awake")
23+
24+
button_colors = ((255, 0, 0), (255, 150, 0), (0, 255, 255), (180, 0, 255))
25+
button_tones = (1047, 1318, 1568, 2093)
26+
27+
now = time.monotonic()
28+
last_action_time = now
29+
while True:
30+
now = time.monotonic()
31+
if now - last_action_time >= 10.0:
32+
33+
magtag.set_text("Sleeping")
34+
magtag.peripherals.deinit()
35+
time.sleep(2)
36+
# go to sleep
37+
pin_alarm = alarm.pin.PinAlarm(pin=board.D11, value=False, pull=True)
38+
39+
# Exit the program, and then deep sleep until the alarm wakes us.
40+
alarm.exit_and_deep_sleep_until_alarms(pin_alarm)
41+
42+
for i, b in enumerate(magtag.peripherals.buttons):
43+
if not b.value:
44+
print("Button %c pressed" % chr((ord("A") + i)))
45+
last_action_time = now
46+
magtag.peripherals.neopixel_disable = False
47+
magtag.peripherals.neopixels.fill(button_colors[i])
48+
magtag.peripherals.play_tone(button_tones[i], 0.25)
49+
break
50+
else:
51+
magtag.peripherals.neopixel_disable = True
52+
time.sleep(0.01)

0 commit comments

Comments
 (0)