Timer callback function

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
diegoPy
Posts: 2
Joined: Wed Jan 03, 2018 2:39 pm

Timer callback function

Post by diegoPy » Wed Jan 03, 2018 2:49 pm

Can a callback be a function? The example only shows a lambda.


def serialCheck():
pyb.LED(1).toggle()

serialCheckTimer = pyb.Timer(1, freq=1)

#serialCheckTimer.callback( serialCheck )
serialCheckTimer.callback( lambda t:pyb.LED(1).toggle() )


Using the lambda, the led toggles. Using the function, the led doesn't toggle.

shaoziyang
Posts: 363
Joined: Sun Apr 17, 2016 1:55 pm

Re: Timer callback function

Post by shaoziyang » Wed Jan 03, 2018 3:39 pm

Yes, callback is a function.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Timer callback function

Post by dhylands » Wed Jan 03, 2018 3:40 pm

You can use a function or a class member. Here's a version which uses a class method:
https://github.com/dhylands/upy-example ... eat_irq.py

and here's one that uses a regular function:
https://github.com/dhylands/upy-example ... _irq_fn.py

Here's a corrected version of your function which works:

Code: Select all

import pyb

def serialCheck(tim):
    pyb.LED(4).toggle()

serialCheckTimer = pyb.Timer(1, freq=1)
serialCheckTimer.callback( serialCheck )
I added import pyb and also added the tim argument to the callback. I highly recommend that you read: docs.micropython.org/en/latest/pyboard/reference/isr_rules.html

If you add these two lines:

Code: Select all

import micropython
micropython.alloc_emergency_exception_buf(100)
then you'll get this error with your original code:

Code: Select all

>>> uncaught exception in Timer(1) interrupt handler
TypeError: function takes 0 positional arguments but 1 wer

diegoPy
Posts: 2
Joined: Wed Jan 03, 2018 2:39 pm

Re: Timer callback function

Post by diegoPy » Wed Jan 03, 2018 6:15 pm

Thanks dhylands.

A couple questions.

1. I added 'tim' as you suggested and it worked. Which is odd, because my timer is actually called serialCheckTimer. Any idea why this would work?

2. I've used C in microcontrollers and Python for everything else. This is the first time I use python on a uC. I'm still wrapping my head around how things should be done. My plan is to set a timer to check for serial data and push it to an object (buffer). Is there an example for this anywhere? Or a better way of doing this?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Timer callback function

Post by dhylands » Wed Jan 03, 2018 8:04 pm

The timer callback function is required to take one argument. `tim` is the timer object (which would be the same thing as serialCheckTimer in your example). This is documented here:
http://docs.micropython.org/en/latest/p ... r.callback

If you fail to make your function accept this argument, then you get the error: "TypeError: function takes 0 positional arguments but 1 were given".

I normally use the UART.any method to determine if a character has arrived and then read and process the byte. Here's an example:
https://github.com/dhylands/bioloid3/bl ... er.py#L142

Post Reply