Not knowing what else to do, I started working at the code with deep sleep, and I post below what I came up with, which I could not really test, because when I load it on the QT Py RP2040 I get the following message on the REPL:
File "code.py", riga 17, in <module>
NotImplementedError: Sleep Memory not available
This really puzzled me, because the adafruit learn page doesn't mention boards not having sleep memory (apart those not having the alarm module maybe), plus I verified that the RP2040 has 264K of RAM.
[I edited the post to add this: I found this link https://github.com/adafruit/circuitpython/issues/3789 where it says that "Most chips with deep sleep capability provide backup RAM that remains energized during deep sleep. Allow access to this RAM, say via a microcontroller.nvm-style mechanism". I couldn't however find the exact format to code the said "microcontroller.nvm-style mechanism" for my code and I don't want to do too many trials because your page says that microcontroller.nvm writes on the flash with limited life... ]
The other thing I am not sure about is the format of struct.pack / struct.unpack. I have long forgotten my binary / bytes notions and I am supposing for multiple digit variables I might have to allocate more than one byte in sleep_memory... I didn't get any sintax errors and the code checks green, but I don't think that's enough...
I set up the code with two "phases", one in which it determines the random variable and goes to sleep, and the other in which it buzzes, logs the feedback and goes to sleep for the 30 minutes complement.
I used two stored counters (one to track the phase of the program and one to count the cycles) and one stored variable for the random sleeping times.
Looking forward to see your thoughts!
Angelo
Code: Select all
import time
import board
import random
import alarm # NUOVO
from digitalio import DigitalInOut, Direction, Pull
from adafruit_debouncer import Button
import struct
pulsante = DigitalInOut(board.A0)
pulsante.direction = Direction.INPUT
pulsante.pull = Pull.UP
vibrating_disc = DigitalInOut(board.A2)
vibrating_disc.direction = Direction.OUTPUT
switch = Button(pulsante)
if not alarm.wake_alarm: # zeroize counters on very first run
alarm.sleep_memory[1] = 1 # program phase: 1 sleeep / 2 buzz then sleep
alarm.sleep_memory[2] = 0 # program cycles (1+2) counter
if alarm.sleep_memory[1] == 1: # we are in code phase 1
alarm.sleep_memory[1] = 2 # after sleep we'll be in code phase 2
alarm.sleep_memory[2] = (alarm.sleep_memory[2] + 1) # update cycles counter
start_buzzing = random.randint(1, 1731) # 6.3" buzz, 60" pulsante e compl. 1800
delta = 1800 - (start_buzzing + 6.3 + 60)
alarm.sleep_memory[3] = struct.pack(delta) # UNSURE ON FORMAT - NEED MORE BYTES?
al = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + start_buzzing)
alarm.exit_and_deep_sleep_until_alarms(al)
elif alarm.sleep_memory[1] == 2: # we are in code phase 2
alarm.sleep_memory[1] = 1 # after sleeep we'll go back to phase 1
delta = struct.unpack(alarm.sleep_memory[3]) # UNSURE ABOUT FORMAT
vibrating_disc.value = True
time.sleep(0.3)
vibrating_disc.value = False
time.sleep(1.7)
vibrating_disc.value = True
time.sleep(0.3)
vibrating_disc.value = False
time.sleep(1.7)
vibrating_disc.value = True
time.sleep(0.3)
vibrating_disc.value = False
time.sleep(1.7)
vibrating_disc.value = True
time.sleep(0.3)
vibrating_disc.value = False
print_contatore = repr(alarm.sleep_memory[2]) # converte var in stampabile
start_time = time.monotonic()
while True:
switch.update() # all'inizio di ogni loop controlla il pulsante
timer = time.monotonic() - start_time
if timer < 60 and switch.short_count == 1:
file1 = open("mindfulness_log.txt", "a")
file1.write(print_contatore + "ok ")
file1.close()
elif timer < 60 and switch.short_count == 2:
file1 = open("mindfulness_log.txt", "a")
file1.write(print_contatore + "no ")
file1.close()
al = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + delta)
alarm.exit_and_deep_sleep_until_alarms(al)