The WiPy machine.Timer class is very different to the esp8266 and esp32 implementations which are better candidates for a general Timer class. By moving the WiPy Timer docs to a completely separate file, under a new name machine.TimerWiPy, it gives a clean slate to define and write the docs for a better, general machine.Timer class. This is with the aim of eventually providing documentation that does not have conditional parts to it, conditional on the port. While the new docs are being defined it makes sense to keep the WiPy docs, since they describe its behaviour. Once the new Timer behaviour is defined the WiPy code can be changed to match it, and then the TimerWiPy docs would be removed.
1.9 KiB
machine
class Timer -- control hardware timers
Hardware timers deal with timing of periods and events. Timers are perhaps the most flexible and heterogeneous kind of hardware in MCUs and SoCs, differently greatly from a model to a model. MicroPython's Timer class defines a baseline operation of executing a callback with a given period (or once after some delay), and allow specific boards to define more non-standard behavior (which thus won't be portable to other boards).
See discussion of important constraints <machine_callbacks>
on
Timer callbacks.
Note
Memory can't be allocated inside irq handlers (an interrupt) and so
exceptions raised within a handler don't give much information. See
micropython.alloc_emergency_exception_buf
for how to
get around this limitation.
If you are using a WiPy board please refer to machine.TimerWiPy <machine.TimerWiPy>
instead of
this class.
Constructors
Construct a new timer object of the given id. Id of -1 constructs a virtual timer (if supported by a board).
Methods
Timer.init(*, mode=Timer.PERIODIC, period=-1, callback=None)
Initialise the timer. Example:
tim.init(period=100) # periodic with 100ms period
tim.init(mode=Timer.ONE_SHOT, period=1000) # one shot firing after 1000ms
Keyword arguments:
mode
can be one of:
Timer.ONE_SHOT
- The timer runs once until the configured period of the channel expires.Timer.PERIODIC
- The timer runs periodically at the configured frequency of the channel.
Timer.deinit()
Deinitialises the timer. Stops the timer, and disables the timer peripheral.
Constants
Timer.ONE_SHOT Timer.PERIODIC
Timer operating mode.