Commit e178ef25 authored by Ivan Pejić's avatar Ivan Pejić Committed by Damien George
Browse files

docs: Add additional example/note for Timer's callback usage.

Add example: using named function for the Timer's callback.
Add note: improving traceback inside interrupt timers.
parent 47098efb
......@@ -29,7 +29,8 @@ Functions
Allocate ``size`` bytes of RAM for the emergency exception buffer (a good
size is around 100 bytes). The buffer is used to create exceptions in cases
when normal RAM allocation would fail (eg within an interrupt handler).
when normal RAM allocation would fail (eg within an interrupt handler) and
therefore give useful traceback information in these situations.
A good way to use this function is to put it at the start of your main script
(eg or and then the emergency exception buffer will be active
......@@ -18,6 +18,13 @@ Example usage to toggle an LED at a fixed frequency::
tim.init(freq=2) # trigger at 2Hz
tim.callback(lambda t:pyb.LED(1).toggle())
Example using named function for the callback::
def tick(timer): # we will receive the timer object when being called
print(timer.counter()) # show current timer's counter value
tim = pyb.Timer(4, freq=1) # create a timer object using timer 4 - trigger at 1Hz
tim.callback(tick) # set the callback to our tick function
Further examples::
tim = pyb.Timer(4, freq=100) # freq in Hz
......@@ -32,6 +39,10 @@ Further examples::
the servo driver, and Timer 6 is used for timed ADC/DAC reading/writing.
It is recommended to use the other timers in your programs.
*Note:* Memory can't be allocated during a callback (an interrupt) and so
exceptions raised within a callback don't give much information. See
:func:`micropython.alloc_emergency_exception_buf` for how to get around this
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment