Timer
Last updated
Last updated
A timer class called LTimer is provided to expose the native LinkIt SDK timer functionality to Arduino developers. Please refer to the source code of File / Examples / LTimer / BlinkLED as an example. It blinks the on-board LED and an external LED with P6 pin by using two timers: one is changing the LED state in every 500ms and the other is changing in every 250ms. Here we introduce how this example is built and what action the corresponding APIs would take.
nclude the header file first before using the LTimer class.
Developers can use up to two timers at a time in the code. The corresponding enumeration IDs are LTIMER_0 and LTIMER_1. To use timers in the code, the first step is to create the instance of the timers. This code instantiates both LTIMER_0 and LTIMER_1:
After the timer instances are created, they need to be initialized. Call the .begin() function to initialize timers in the setup() part:
When the initialization is done, you can start the timer by calling the .start() function. Four parameters are needed to start a timer:
The purposes of parameters are:
[IN] timeoutMS: timeout time in milliseconds.
[IN] timerMode: one shot or repeat mode. The value can be LTIMER_ONESHOT_MODE or LTIMER_REPEAT_MODE. In the one shot mode, the callback function will be invoked only once when the timer is expired and in the repeat mode, the callback function would be invoked repeatedly for every timeoutMS interval until the timer is stopped / released.
[IN] callbackFunc: the callback function to be called when the timer is expired.
[IN] userData: data to be passed when the callback function is invoked.
And the function prototype of the callback function is:
The input parameter user_data would be the same data as the userData passed when starting a timer.
For a one shot mode timer, there is no need to stop it since it only runs once. However, for a repeat mode timer, developers might want to stop it in some situations. The .stop() function can be used to stop a timer. If the timer is needed later, another .start() can be called again to trigger its operation.
When a timer is no longer needed, the .end() function is used to release all resources allocated to it.