How to use micropython.schedule effectively

The official PYBD running MicroPython, and its accessories.
Target audience: Users with a PYBD
Post Reply
Stevo52
Posts: 38
Joined: Sun Feb 03, 2019 10:28 pm

How to use micropython.schedule effectively

Post by Stevo52 » Sun Jul 19, 2020 4:48 am

Hi,

Another noob question from me I guess..

I am wanting to use micropython.schedule for two timer callbacks, to avoid random memory allocation errors when the callback triggers.

A single instance works fine, but a second instance doesn't seem to trigger the 2nd callback from the following.

Code: Select all

# After startup create 1 and 5 second HB timers
timHB_1 = Timer(13, freq=1, callback=micropython.schedule(hb1_CB(1),None))
timHB_1.init(freq=1)
time.sleep(0.1)
# Brief sleep not necessary if not 'scheduling' next timer? but doesn't allow the two scheduled callbacks to work

# timHB_5 = Timer(14, freq=0.2, callback=micropython.schedule(hb5_CB(1),None)) 
# Don't seem to be able to use multiple instances of .schedule?

timHB_5 = Timer(14, freq=0.2, callback=hb5_CB)
timHB_5.init(freq=0.2)
The 'simple' code is used to set up 2 timers to manage various tasks, the call back routines just set flags (global variables) that are checked and reset in other portions of the code. Those portions work fine and don't contribute to the issue.

I only get the memory allocation if the first timer callback (1 second) is called directly. As written above, the second callback (5 seconds) works fine as long as the first one is scheduled callback. I haven't found a detailed explanation of micropython.scheduleso I am not sure if I can even use multiple schedule items but the text indicates there is a 'list' of scheduled calls. Do I have to number the schedule calls?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to use micropython.schedule effectively

Post by pythoncoder » Sun Jul 19, 2020 10:15 am

I suggest you re-read the docs on the args for timer callbacks and for micropython.schedule. The following sample works: you might want to start from that basis and adapt it to do what you intend.

Code: Select all

from pyb import Timer
import micropython
import time
def cb(arg):  # Scheduled callback
    print(arg)

def cb1(tim):  # Hard IRQ
    micropython.schedule(cb, 'Timer 1')
def cb2(tim):
    micropython.schedule(cb, 'Timer 2')
t1 = Timer(1, freq=1.1, callback=cb1) 
t2 = Timer(2, freq=1, callback=cb2) 
Peter Hinch
Index to my micropython libraries.

Stevo52
Posts: 38
Joined: Sun Feb 03, 2019 10:28 pm

Re: How to use micropython.schedule effectively

Post by Stevo52 » Sun Jul 19, 2020 11:11 am

Thanks Peter,

That makes sense now you have shown code that explains how it works. I have edited my code using your example and it works fine i.e. the way I expected it to work.

I was a bit confused by the documentation for Timer and micropython.schedule together. I tried unsuccessfully to find any code examples for what I was trying to do and ended up with the code I posted by playing with an example on Stack Overflow. It was sort of working OK, but didn't feel right - hence my question.

To be honest though, I have found that some of the micropython documentation a bit 'light on' and is often missing details and clear example code for the benefit of newcomers. But I usually manage to nut it out with the help of searches. I am not usually trying to do anything earth shattering so others have usually worked it out already. But I'll become more proficient with experience, I am sure :-)

Post Reply