Skip to content

Commit a23c659

Browse files
committed
Add alarm tests
1 parent 6dbeb75 commit a23c659

File tree

8 files changed

+276
-0
lines changed

8 files changed

+276
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import alarm
2+
import board
3+
import time
4+
import digitalio
5+
import neopixel
6+
7+
## WAKING PINS - uncomment appropriate pin per microcontroller
8+
wake_pin = board.X1 # STM32F4 Pyboard
9+
# wake_pin = board.GP0 # RP2040 Pico
10+
# wake_pin = board.A4 # NRF52840 Feather
11+
# wake_pin = board.IO5 # ESP32-S2 Saola
12+
13+
## LED - use on RP2040 Pico, STM32F4 Pyboard
14+
## PinAlarms blink 1x, TimeAlarms 2x, Startup 3x
15+
led_pin = board.LED
16+
led = digitalio.DigitalInOut(led_pin)
17+
led.direction = digitalio.Direction.OUTPUT
18+
19+
20+
def blink(num_blinks):
21+
for i in range(num_blinks):
22+
led.value = True
23+
time.sleep(0.2)
24+
led.value = False
25+
time.sleep(0.2)
26+
27+
28+
def show_timealarm():
29+
blink(2)
30+
31+
32+
def show_pinalarm():
33+
blink(1)
34+
35+
36+
def show_noalarm():
37+
blink(3)
38+
39+
40+
## Comment out above if using Neopixel
41+
42+
## NEOPIXEL - use on Circuitplayground Bluefruit, ESP32-S2 Saola
43+
## TimeAlarms are red, PinAlarms are blue, Default is white
44+
# np = neopixel.NeoPixel(board.NEOPIXEL, 1)
45+
# def show_timealarm():
46+
# np[0] = (50, 0, 0)
47+
# time.sleep(1)
48+
# np[0] = (0, 0, 0)
49+
# def show_pinalarm():
50+
# np[0] = (0, 0, 50)
51+
# time.sleep(1)
52+
# np[0] = (0, 0, 0)
53+
# def show_noalarm():
54+
# np[0] = (50, 50, 50)
55+
# time.sleep(1)
56+
# np[0] = (0, 0, 0)
57+
## Comment out above if using LED
58+
59+
## Show which alarm woke the chip
60+
print("Wake alarm:")
61+
print(alarm.wake_alarm)
62+
if isinstance(alarm.wake_alarm, alarm.time.TimeAlarm):
63+
show_timealarm()
64+
elif isinstance(alarm.wake_alarm, alarm.pin.PinAlarm):
65+
show_pinalarm()
66+
else:
67+
show_noalarm()
68+
69+
## USB enumeration may take 4-5s per restart
70+
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 10)
71+
72+
## Deep sleep pin alarms may only accept a single configuration.
73+
pin_alarm = alarm.pin.PinAlarm(
74+
pin=wake_pin, value=True, edge=True, pull=True
75+
) # STM32 must be this exact config
76+
# pin_alarm = alarm.pin.PinAlarm(pin=wake_pin, value=False, edge=False, pull=True) # NRF and ESP32S2 must use level, not edge
77+
# pin_alarm = alarm.pin.PinAlarm(pin=wake_pin, value=False, edge=True, pull=True) # RP2040 supports any config
78+
79+
alarm.exit_and_deep_sleep_until_alarms(time_alarm, pin_alarm)
80+
# alarm.exit_and_deep_sleep_until_alarms(pin_alarm) # Using TimeAlarm will reduce performance on the RP2040
81+
# alarm.exit_and_deep_sleep_until_alarms(time_alarm) # Using PinAlarm will reduce performance on the ESP32-S2
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import board
2+
import digitalio
3+
import storage
4+
5+
switch = digitalio.DigitalInOut(board.IO10)
6+
7+
switch.direction = digitalio.Direction.INPUT
8+
switch.pull = digitalio.Pull.UP
9+
10+
# If the switch pin is connected to ground CircuitPython can write to the drive
11+
storage.remount("/", switch.value)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import alarm
2+
import board
3+
import time
4+
import microcontroller
5+
import storage
6+
7+
temperature = microcontroller.cpu.temperature
8+
print("Temperature:", temperature)
9+
10+
try:
11+
with open("/log.txt", "a") as sdc:
12+
sdc.write("{}\n".format(temperature))
13+
except OSError as e:
14+
print("Cannot write to fs, is IO10 grounded?")
15+
16+
## USB enumeration may take 4-5s per restart
17+
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 10)
18+
19+
alarm.exit_and_deep_sleep_until_alarms(time_alarm)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import alarm
2+
import board
3+
import time
4+
import digitalio
5+
import neopixel
6+
7+
## WAKING PINS - uncomment appropriate pin per microcontroller
8+
wake_pin = board.X1 # STM32F4 Pyboard
9+
# wake_pin = board.GP0 # RP2040 Pico
10+
# wake_pin = board.A4 # NRF52840 Feather
11+
# wake_pin = board.IO5 # ESP32-S2 Saola
12+
13+
## LED - use on RP2040 Pico, STM32F4 Pyboard
14+
## PinAlarms blink 1x, TimeAlarms 2x, Startup 3x
15+
led_pin = board.LED
16+
led = digitalio.DigitalInOut(led_pin)
17+
led.direction = digitalio.Direction.OUTPUT
18+
19+
20+
def blink(num_blinks):
21+
for i in range(num_blinks):
22+
led.value = True
23+
time.sleep(0.2)
24+
led.value = False
25+
time.sleep(0.2)
26+
27+
28+
def show_timealarm():
29+
blink(2)
30+
31+
32+
def show_pinalarm():
33+
blink(1)
34+
35+
36+
def show_noalarm():
37+
blink(3)
38+
39+
40+
## Comment out above if using Neopixel
41+
42+
## NEOPIXEL - use on Circuitplayground Bluefruit, ESP32-S2 Saola
43+
## TimeAlarms are red, PinAlarms are blue, Default is white
44+
# np = neopixel.NeoPixel(board.NEOPIXEL, 1)
45+
# def show_timealarm():
46+
# np[0] = (50, 0, 0)
47+
# time.sleep(1)
48+
# np[0] = (0, 0, 0)
49+
# def show_pinalarm():
50+
# np[0] = (0, 0, 50)
51+
# time.sleep(1)
52+
# np[0] = (0, 0, 0)
53+
# def show_noalarm():
54+
# np[0] = (50, 50, 50)
55+
# time.sleep(1)
56+
# np[0] = (0, 0, 0)
57+
## Comment out above if using LED
58+
59+
## PinAlarm only needs to be set once
60+
pin_alarm = alarm.pin.PinAlarm(pin=wake_pin, value=False, edge=True, pull=True) # STM32, RP2040
61+
# pin_alarm = alarm.pin.PinAlarm(pin=wake_pin, value=False, edge=False, pull=True) # NRF, ESP32-S2, RP2040
62+
63+
while True:
64+
## TimeAlarms must be reset each time you sleep, since they use monotonic time
65+
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 10)
66+
ret_alarm = alarm.light_sleep_until_alarms(pin_alarm, time_alarm)
67+
68+
print("Returned alarm vs global alarm:") # These should be the same
69+
print(ret_alarm)
70+
print(alarm.wake_alarm)
71+
72+
if isinstance(ret_alarm, alarm.time.TimeAlarm):
73+
show_timealarm()
74+
elif isinstance(ret_alarm, alarm.pin.PinAlarm):
75+
show_pinalarm()
76+
else:
77+
show_noalarm()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import time
2+
import adafruit_ble
3+
from adafruit_ble_eddystone import uid, url
4+
import alarm
5+
6+
np = neopixel.NeoPixel(board.NEOPIXEL, 1)
7+
8+
radio = adafruit_ble.BLERadio()
9+
# Reuse the BLE address as our Eddystone instance id.
10+
eddystone_uid = uid.EddystoneUID(radio.address_bytes)
11+
eddystone_url = url.EddystoneURL("https://adafru.it/discord")
12+
13+
while True:
14+
np[0] = (50, 0, 0)
15+
# Alternate between advertising our ID and our URL.
16+
radio.start_advertising(eddystone_uid)
17+
time.sleep(0.5)
18+
radio.stop_advertising()
19+
20+
radio.start_advertising(eddystone_url)
21+
time.sleep(0.5)
22+
radio.stop_advertising()
23+
24+
## USB enumeration may take 4-5s per restart
25+
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 10)
26+
27+
np[0] = (0, 0, 0)
28+
29+
alarm.exit_and_deep_sleep_until_alarms(time_alarm)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import board
2+
import digitalio
3+
import storage
4+
5+
switch = digitalio.DigitalInOut(board.GP4)
6+
7+
switch.direction = digitalio.Direction.INPUT
8+
switch.pull = digitalio.Pull.UP
9+
10+
# If the switch pin is connected to ground CircuitPython can write to the drive
11+
storage.remount("/", switch.value)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import alarm
2+
import board
3+
import time
4+
import microcontroller
5+
import storage
6+
7+
temperature = microcontroller.cpu.temperature
8+
print("Temperature:", temperature)
9+
10+
try:
11+
with open("/log.txt", "a") as sdc:
12+
sdc.write("{}\n".format(temperature))
13+
except OSError as e:
14+
print("Cannot write to fs, is GP4 grounded?")
15+
16+
## USB enumeration may take 4-5s per restart
17+
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 10)
18+
19+
alarm.exit_and_deep_sleep_until_alarms(time_alarm)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import alarm
2+
import board
3+
import time
4+
import microcontroller
5+
import sdioio
6+
import storage
7+
import neopixel
8+
9+
np = neopixel.NeoPixel(board.NEOPIXEL, 1)
10+
np[0] = (50, 0, 0)
11+
12+
# SD Card
13+
sd = sdioio.SDCard(
14+
clock=board.SDIO_CLOCK, command=board.SDIO_COMMAND, data=board.SDIO_DATA, frequency=25000000
15+
)
16+
vfs = storage.VfsFat(sd)
17+
storage.mount(vfs, "/sd")
18+
19+
with open("/sd/log.txt", "a") as sdc:
20+
temperature = microcontroller.cpu.temperature
21+
print("Temperature:", temperature)
22+
sdc.write("{}\n".format(temperature))
23+
24+
## USB enumeration may take 4-5s per restart
25+
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 10)
26+
27+
np[0] = (0, 0, 0)
28+
29+
alarm.exit_and_deep_sleep_until_alarms(time_alarm)

0 commit comments

Comments
 (0)