Hi guys
There seems to be some bugs concerning the RTC.wakeup() code.
The simple example here works for me: Use of callback function in sleep mode - OpenMV Products - OpenMV Forums
But when I initially tried to get the callback routine to set a value that I could access outside the callback routine, it behaved as though this is not possible, as shown in the code below: I would expect “triggered” would be set True by the callback at line 32 so the red LED would toggle. However the green LED toggles.
Then I discovered that if I specify the callback with a dummy argument it works as expected - see the two alternatives at lines 40 and 41.
But if I call rtc.wakeup() once, outside the “while (True)” loop (at line 37 instead of line 41), the code behaves as though “triggered” is set True the first time the callback is called but not subsequently.
There is further confusion regarding reading the values of globals: the callback reads the value of the global “choose_blue” with both of the line 40 and 41 invocations, as can be seen if you set that True at line 9. But if the invocation is done just once, then the invocation without the dummy variable at line 36 works, but the invation with the dummy variable at line 37 shows that “choose_blue” is read the first time the callback is called but not subsequently.
This looks like a bug rather than a feature… how can I use globals with the callback reliably?
(I am using an H7 with firmware 3.9.6 and IDE 2.6.7)
import pyb, machine, time
global triggered
global choose_blue
triggered = False
choose_blue = False
#choose_blue = True
red_led = pyb.LED(1)
red_led.off()
green_led = pyb.LED(2)
green_led.off()
blue_led = pyb.LED(3)
blue_led.off()
def toggleRed():
red_led.toggle()
def toggleGreen():
green_led.toggle()
def toggleBlue():
blue_led.toggle()
def test(arg):
global triggered
global choose_blue
if(choose_blue):
toggleBlue()
triggered = True
rtc = pyb.RTC()
#rtc.wakeup(1000, test)
#rtc.wakeup(1000, test(1))
while (True):
rtc.wakeup(1000, test)
#rtc.wakeup(1000, test(1))
machine.sleep()
if (triggered == True):
toggleRed()
else:
toggleGreen()
triggered = False