1
votes

Note: This is a copy of a question asked here

Hi

I am completely new to EPS8266 and Lua (but not to programming - my first CPU was an 8080...)
Using a nodemcu HUZZA from adafruit

Anyway I am testing some timer stuff and running into this:

tmr.alarm(0, 500, 1, function()
  print("I'm here")
  tmr.stop(0)
end)

Without the stop, the loop keeps printing, with it the tmr.stop(0) stops. ... so far so good.

But if I want to start the timer again like:

tmr.alarm(0, 500, 1, function()
  print("I'm here")
  tmr.stop(0)

  -- do some stuff

  tmr.start(0)
 end)

I get an error: PANIC: unprotected error in call to Lua API...

The documentation says that the tmr is still registered when stop is called.

A call to tmr.state(0) does the same. Only tmr.stop(0) seems to works as expected.

Thanks for your thoughts.

2
Probably, tmr.start is not allowed inside timer callback function? - Egor Skriptunoff
@I0sens any further input required here? - Marcel Stör

2 Answers

1
votes

The documentation says to no longer use static timers

Static timers are deprecated and will be removed later. Use the OO API initiated with tmr.create().

If you want complete control over when the functions in the timer callback are executed you need a ALARM_SEMI instance upon which you call start whenever needed. It'll fire exactly once for every time you call start on it.

local mytimer = tmr.create()
mytimer:register(500, tmr.ALARM_SEMI, function() print("I'm here") end)
-- do stuff here
-- then whenever needed trigger the timer
mytimer:start()

Note that mytimer is not unregistered and not garbage collected.

0
votes

Based on the documentation, you need to use tmr.ALARM_SEMI as your alarm mode.

ALARM_SEMI is described by the documentation as:

tmr.ALARM_SEMI manually repeating alarm (call tmr.start() to restart)

tmr.ALARM_SEMI is equal to 2. Based on that, this should work:

tmr.alarm(0, 500, 2, function()
      print("I'm here")
      tmr.stop(0)

      -- do some stuff

      tmr.start(0)
 end)