From a23c659eda3822fcc569f85a1ccd9800b77e2d25 Mon Sep 17 00:00:00 2001 From: Lucian Copeland Date: Sun, 20 Jun 2021 18:09:18 -0400 Subject: [PATCH] Add alarm tests --- tests/circuitpython-manual/alarm/deepsleep.py | 81 +++++++++++++++++++ tests/circuitpython-manual/alarm/esp/boot.py | 11 +++ tests/circuitpython-manual/alarm/esp/code.py | 19 +++++ .../circuitpython-manual/alarm/lightsleep.py | 77 ++++++++++++++++++ tests/circuitpython-manual/alarm/nrf/code.py | 29 +++++++ tests/circuitpython-manual/alarm/rp/boot.py | 11 +++ tests/circuitpython-manual/alarm/rp/code.py | 19 +++++ tests/circuitpython-manual/alarm/stm/code.py | 29 +++++++ 8 files changed, 276 insertions(+) create mode 100644 tests/circuitpython-manual/alarm/deepsleep.py create mode 100644 tests/circuitpython-manual/alarm/esp/boot.py create mode 100644 tests/circuitpython-manual/alarm/esp/code.py create mode 100644 tests/circuitpython-manual/alarm/lightsleep.py create mode 100644 tests/circuitpython-manual/alarm/nrf/code.py create mode 100644 tests/circuitpython-manual/alarm/rp/boot.py create mode 100644 tests/circuitpython-manual/alarm/rp/code.py create mode 100644 tests/circuitpython-manual/alarm/stm/code.py diff --git a/tests/circuitpython-manual/alarm/deepsleep.py b/tests/circuitpython-manual/alarm/deepsleep.py new file mode 100644 index 0000000000..ab4f9ea70a --- /dev/null +++ b/tests/circuitpython-manual/alarm/deepsleep.py @@ -0,0 +1,81 @@ +import alarm +import board +import time +import digitalio +import neopixel + +## WAKING PINS - uncomment appropriate pin per microcontroller +wake_pin = board.X1 # STM32F4 Pyboard +# wake_pin = board.GP0 # RP2040 Pico +# wake_pin = board.A4 # NRF52840 Feather +# wake_pin = board.IO5 # ESP32-S2 Saola + +## LED - use on RP2040 Pico, STM32F4 Pyboard +## PinAlarms blink 1x, TimeAlarms 2x, Startup 3x +led_pin = board.LED +led = digitalio.DigitalInOut(led_pin) +led.direction = digitalio.Direction.OUTPUT + + +def blink(num_blinks): + for i in range(num_blinks): + led.value = True + time.sleep(0.2) + led.value = False + time.sleep(0.2) + + +def show_timealarm(): + blink(2) + + +def show_pinalarm(): + blink(1) + + +def show_noalarm(): + blink(3) + + +## Comment out above if using Neopixel + +## NEOPIXEL - use on Circuitplayground Bluefruit, ESP32-S2 Saola +## TimeAlarms are red, PinAlarms are blue, Default is white +# np = neopixel.NeoPixel(board.NEOPIXEL, 1) +# def show_timealarm(): +# np[0] = (50, 0, 0) +# time.sleep(1) +# np[0] = (0, 0, 0) +# def show_pinalarm(): +# np[0] = (0, 0, 50) +# time.sleep(1) +# np[0] = (0, 0, 0) +# def show_noalarm(): +# np[0] = (50, 50, 50) +# time.sleep(1) +# np[0] = (0, 0, 0) +## Comment out above if using LED + +## Show which alarm woke the chip +print("Wake alarm:") +print(alarm.wake_alarm) +if isinstance(alarm.wake_alarm, alarm.time.TimeAlarm): + show_timealarm() +elif isinstance(alarm.wake_alarm, alarm.pin.PinAlarm): + show_pinalarm() +else: + show_noalarm() + +## USB enumeration may take 4-5s per restart +time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 10) + +## Deep sleep pin alarms may only accept a single configuration. +pin_alarm = alarm.pin.PinAlarm( + pin=wake_pin, value=True, edge=True, pull=True +) # STM32 must be this exact config +# pin_alarm = alarm.pin.PinAlarm(pin=wake_pin, value=False, edge=False, pull=True) # NRF and ESP32S2 must use level, not edge +# pin_alarm = alarm.pin.PinAlarm(pin=wake_pin, value=False, edge=True, pull=True) # RP2040 supports any config + +alarm.exit_and_deep_sleep_until_alarms(time_alarm, pin_alarm) +# alarm.exit_and_deep_sleep_until_alarms(pin_alarm) # Using TimeAlarm will reduce performance on the RP2040 +# alarm.exit_and_deep_sleep_until_alarms(time_alarm) # Using PinAlarm will reduce performance on the ESP32-S2 diff --git a/tests/circuitpython-manual/alarm/esp/boot.py b/tests/circuitpython-manual/alarm/esp/boot.py new file mode 100644 index 0000000000..0b34adb7be --- /dev/null +++ b/tests/circuitpython-manual/alarm/esp/boot.py @@ -0,0 +1,11 @@ +import board +import digitalio +import storage + +switch = digitalio.DigitalInOut(board.IO10) + +switch.direction = digitalio.Direction.INPUT +switch.pull = digitalio.Pull.UP + +# If the switch pin is connected to ground CircuitPython can write to the drive +storage.remount("/", switch.value) diff --git a/tests/circuitpython-manual/alarm/esp/code.py b/tests/circuitpython-manual/alarm/esp/code.py new file mode 100644 index 0000000000..d4a577cbd9 --- /dev/null +++ b/tests/circuitpython-manual/alarm/esp/code.py @@ -0,0 +1,19 @@ +import alarm +import board +import time +import microcontroller +import storage + +temperature = microcontroller.cpu.temperature +print("Temperature:", temperature) + +try: + with open("/log.txt", "a") as sdc: + sdc.write("{}\n".format(temperature)) +except OSError as e: + print("Cannot write to fs, is IO10 grounded?") + +## USB enumeration may take 4-5s per restart +time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 10) + +alarm.exit_and_deep_sleep_until_alarms(time_alarm) diff --git a/tests/circuitpython-manual/alarm/lightsleep.py b/tests/circuitpython-manual/alarm/lightsleep.py new file mode 100644 index 0000000000..b2878db4bb --- /dev/null +++ b/tests/circuitpython-manual/alarm/lightsleep.py @@ -0,0 +1,77 @@ +import alarm +import board +import time +import digitalio +import neopixel + +## WAKING PINS - uncomment appropriate pin per microcontroller +wake_pin = board.X1 # STM32F4 Pyboard +# wake_pin = board.GP0 # RP2040 Pico +# wake_pin = board.A4 # NRF52840 Feather +# wake_pin = board.IO5 # ESP32-S2 Saola + +## LED - use on RP2040 Pico, STM32F4 Pyboard +## PinAlarms blink 1x, TimeAlarms 2x, Startup 3x +led_pin = board.LED +led = digitalio.DigitalInOut(led_pin) +led.direction = digitalio.Direction.OUTPUT + + +def blink(num_blinks): + for i in range(num_blinks): + led.value = True + time.sleep(0.2) + led.value = False + time.sleep(0.2) + + +def show_timealarm(): + blink(2) + + +def show_pinalarm(): + blink(1) + + +def show_noalarm(): + blink(3) + + +## Comment out above if using Neopixel + +## NEOPIXEL - use on Circuitplayground Bluefruit, ESP32-S2 Saola +## TimeAlarms are red, PinAlarms are blue, Default is white +# np = neopixel.NeoPixel(board.NEOPIXEL, 1) +# def show_timealarm(): +# np[0] = (50, 0, 0) +# time.sleep(1) +# np[0] = (0, 0, 0) +# def show_pinalarm(): +# np[0] = (0, 0, 50) +# time.sleep(1) +# np[0] = (0, 0, 0) +# def show_noalarm(): +# np[0] = (50, 50, 50) +# time.sleep(1) +# np[0] = (0, 0, 0) +## Comment out above if using LED + +## PinAlarm only needs to be set once +pin_alarm = alarm.pin.PinAlarm(pin=wake_pin, value=False, edge=True, pull=True) # STM32, RP2040 +# pin_alarm = alarm.pin.PinAlarm(pin=wake_pin, value=False, edge=False, pull=True) # NRF, ESP32-S2, RP2040 + +while True: + ## TimeAlarms must be reset each time you sleep, since they use monotonic time + time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 10) + ret_alarm = alarm.light_sleep_until_alarms(pin_alarm, time_alarm) + + print("Returned alarm vs global alarm:") # These should be the same + print(ret_alarm) + print(alarm.wake_alarm) + + if isinstance(ret_alarm, alarm.time.TimeAlarm): + show_timealarm() + elif isinstance(ret_alarm, alarm.pin.PinAlarm): + show_pinalarm() + else: + show_noalarm() diff --git a/tests/circuitpython-manual/alarm/nrf/code.py b/tests/circuitpython-manual/alarm/nrf/code.py new file mode 100644 index 0000000000..b4379dc79d --- /dev/null +++ b/tests/circuitpython-manual/alarm/nrf/code.py @@ -0,0 +1,29 @@ +import time +import adafruit_ble +from adafruit_ble_eddystone import uid, url +import alarm + +np = neopixel.NeoPixel(board.NEOPIXEL, 1) + +radio = adafruit_ble.BLERadio() +# Reuse the BLE address as our Eddystone instance id. +eddystone_uid = uid.EddystoneUID(radio.address_bytes) +eddystone_url = url.EddystoneURL("https://adafru.it/discord") + +while True: + np[0] = (50, 0, 0) + # Alternate between advertising our ID and our URL. + radio.start_advertising(eddystone_uid) + time.sleep(0.5) + radio.stop_advertising() + + radio.start_advertising(eddystone_url) + time.sleep(0.5) + radio.stop_advertising() + + ## USB enumeration may take 4-5s per restart + time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 10) + + np[0] = (0, 0, 0) + + alarm.exit_and_deep_sleep_until_alarms(time_alarm) diff --git a/tests/circuitpython-manual/alarm/rp/boot.py b/tests/circuitpython-manual/alarm/rp/boot.py new file mode 100644 index 0000000000..93a7a66040 --- /dev/null +++ b/tests/circuitpython-manual/alarm/rp/boot.py @@ -0,0 +1,11 @@ +import board +import digitalio +import storage + +switch = digitalio.DigitalInOut(board.GP4) + +switch.direction = digitalio.Direction.INPUT +switch.pull = digitalio.Pull.UP + +# If the switch pin is connected to ground CircuitPython can write to the drive +storage.remount("/", switch.value) diff --git a/tests/circuitpython-manual/alarm/rp/code.py b/tests/circuitpython-manual/alarm/rp/code.py new file mode 100644 index 0000000000..4a2c7e8085 --- /dev/null +++ b/tests/circuitpython-manual/alarm/rp/code.py @@ -0,0 +1,19 @@ +import alarm +import board +import time +import microcontroller +import storage + +temperature = microcontroller.cpu.temperature +print("Temperature:", temperature) + +try: + with open("/log.txt", "a") as sdc: + sdc.write("{}\n".format(temperature)) +except OSError as e: + print("Cannot write to fs, is GP4 grounded?") + +## USB enumeration may take 4-5s per restart +time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 10) + +alarm.exit_and_deep_sleep_until_alarms(time_alarm) diff --git a/tests/circuitpython-manual/alarm/stm/code.py b/tests/circuitpython-manual/alarm/stm/code.py new file mode 100644 index 0000000000..342e42f87e --- /dev/null +++ b/tests/circuitpython-manual/alarm/stm/code.py @@ -0,0 +1,29 @@ +import alarm +import board +import time +import microcontroller +import sdioio +import storage +import neopixel + +np = neopixel.NeoPixel(board.NEOPIXEL, 1) +np[0] = (50, 0, 0) + +# SD Card +sd = sdioio.SDCard( + clock=board.SDIO_CLOCK, command=board.SDIO_COMMAND, data=board.SDIO_DATA, frequency=25000000 +) +vfs = storage.VfsFat(sd) +storage.mount(vfs, "/sd") + +with open("/sd/log.txt", "a") as sdc: + temperature = microcontroller.cpu.temperature + print("Temperature:", temperature) + sdc.write("{}\n".format(temperature)) + +## USB enumeration may take 4-5s per restart +time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 10) + +np[0] = (0, 0, 0) + +alarm.exit_and_deep_sleep_until_alarms(time_alarm)