Timer

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.

Instantiation

nclude the header file first before using the LTimer class.

#include "LTimer.h"

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:

LTimer timer0(LTIMER_0);
LTimer timer1(LTIMER_1);

Initialization

After the timer instances are created, they need to be initialized. Call the .begin() function to initialize timers in the setup() part:

timer0.begin();
timer1.begin();

Execution

When the initialization is done, you can start the timer by calling the .start() function. Four parameters are needed to start a timer:

LTimerStatus start(
  uint32_t timeoutMS,
  LTimerMode timerMode,
  ltimer_callback_t callbackFunc,
  void *userData
);

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:

typedef void (*ltimer_callback_t)(void *user_data);

The input parameter user_data would be the same data as the userData passed when starting a timer.

Stop

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.

Release

When a timer is no longer needed, the .end() function is used to release all resources allocated to it.

Example Code

#include "Arduino.h"
#include "LTimer.h"

int led0 = 7;
int led1 = 6;
int val0 = 0;
int val1 = 0;

// instantiation
LTimer timer0(LTIMER_0);
LTimer timer1(LTIMER_1);

// callback function for timer0
void _callback0(void *usr_data)
{
  val0 = !val0;
  digitalWrite(led0, val0);
}

// callback function for timer1
void _callback1(void *usr_data)
{
  val1 = !val1;
  digitalWrite(led1, val1);
}

void setup() {
  pinMode(led0, OUTPUT);
  pinMode(led1, OUTPUT);
  Serial.begin(115200);

  // initialization
  timer0.begin();
  timer1.begin();

  // start the execution
  timer0.start(500, LTIMER_REPEAT_MODE, _callback0, NULL);
  timer1.start(250, LTIMER_REPEAT_MODE, _callback1, NULL);
}

void loop() {
}

Last updated