I am trying to have a system where most code executes in an infinite loop. A "background" timer runs at a 100msec rate, and the associated callback sets a flag, which is used in the infinite loop. I have seen on the forum that interrupts should not allocate memory, and perhaps not do other things, so I merely want to set a flag. I don't care precisely when the 100msec tasks are done, just that they get done around every 100 msec. The tasks, may grow in size and include all sorts of things, which might not play well with an interrupt, so I want to move them safely outside the interrupt.
The simplest code is attached. I see LED(1) toggling (on an o'scope!) but nothing else. It looks like maybe a scoping problem? Clearly hmsInt() is being called, so hmsFlag must be being set True. But it does not seem to be seen in the infinite loop. And no error messages.
Clearly imperfect understanding on my part, so any insights would help!
Code: Select all
#
main.py -- put your code here!
import pyb
hms = 0 # hundred msec ticks
hmsFlag = False
seconds = 0 # seconds
def hundredmsTasks():
pyb.LED(2).toggle()
if hms < 100:
hms += 1
else:
hms = 0
print("!")
def hmsInt():
pyb.LED(1).toggle()
hmsFlag = True
tim7 = pyb.Timer(7, freq=100)
tim7.callback(lambda t: hmsInt())
while True:
if hmsFlag:
hundredmsTasks()
hmsFlag = False
# other code goes here