Luat Timer#

Basic information#

  • Date of drafting: 2019-11-25

  • Designer: wendal

Why do you need a timer?#

  1. The need for single timing, for example, to perform temperature measurement after 10 minutes

  2. Cycle timing needs, network once every 5 minutes, send heartbeat

  3. sys.waitThe delay execution of lua is realized through the timer mechanism.

Design ideas and boundaries#

  1. design based on rtos timer api

  2. After the timer expires, the message is produced and sent to the message bus for consumption by rtos.receive

  3. The underlying support loop timing.

C API(Platform layer)#

data structure#

#define LUAT_TIMER_MAXID ((size_t) 0xFFFF)

Interface API#

uint32_t luat_timer_start(luat_timer_t* timer);
uint32_t luat_timer_stop(luat_timer_t* timer);

Lua API#



timer.HW -- "HW" Hardware Timer
timer.OS -- "OS" Software Timer

Start Timer#

-- timerout Timeout duration, value, 1-0xFFFFFFFF, unit: milliseconds, meaningful if greater than 0
-- repeat   Extra number of repetitions, value, 1-0xFFFFFFFF, unit milliseconds, default 0
local t = timer.start(timeout, _repeat, function() end)
if not t then
    -- Startup success
    -- Start failed, id may be full or timeout value is wrong

Turn off timer (including delete)#

-- timer_id clock id, numeric value, 0-0xFF, depends on LUAT_TIMER_MAXID
-- As long as a numeric id is passed in, the timer_stop will always succeed.

Relevant knowledge points#

  • Luat core mechanism

  • Message Bus