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.
Timer callback function
-
- Posts: 363
- Joined: Sun Apr 17, 2016 1:55 pm
Re: Timer callback function
Yes, callback is a function.
Re: Timer callback function
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: 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: then you'll get this error with your original code:
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 )
If you add these two lines:
Code: Select all
import micropython
micropython.alloc_emergency_exception_buf(100)
Code: Select all
>>> uncaught exception in Timer(1) interrupt handler
TypeError: function takes 0 positional arguments but 1 wer
Re: Timer callback function
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?
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?
Re: Timer callback function
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
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