Jump to content
C4 Forums | Control4

[DriverWorks] How to use timers?


Recommended Posts

Well in this driver for an alarm, when I press "#" the screen is cleared with:

elseif BUTTONMAP[buttonID] == "#" then	   C4:SendToNetwork(6001, 1002, tohex("03") .. "KD0C" .. tohex("0D")) 	   C4:SendToProxy(5001, "DISPLAY_TEXT", "<text> </text>")

Now I'm trying to just clear the screen for 5 seconds and display the last message. In order to do that I added a variable to track some messages that are displayed along the way. That variable is called "proxy".
And I added a timer of 5 seconds between clearing the screen and display the previous message, the thing is when I pressed "#" the screen only goes blank for about a fraction of seconds, almost instantly, probably it's bad initialized or something.

What I have is this:
 

function CancelclsTimer()  if (clsTimer ~= 0) then    C4:KillTimer(clsTimer)  end  clsTimer = 0end-- code...clsTimer = 0proxy = 0-- code...for Status in string.gmatch(strData, "MD00") do 		       alarm = nil       C4:SendToProxy(5001, "DISARMED","")	  C4:SetVariable("AwayMode","False","")       C4:SetVariable("NightMode","False","")       C4:SendToProxy(5001, "DISPLAY_TEXT", "<text>Disarmed</text>")	  proxy = 1   end   for AwayMode in string.gmatch(strData, "MD01") do        C4:SendToProxy(5001, "AWAY","")       C4:SetVariable("AwayMode","True","")	  C4:SendToProxy(5001, "DISPLAY_TEXT", "<text>Away Mode</text>")	  proxy = 2   end      for NightMode in string.gmatch(strData, "MD02") do 	       C4:SendToProxy(5001, "HOME","")       C4:SetVariable("NightMode","True","")	  C4:SendToProxy(5001, "DISPLAY_TEXT", "<text>Night Mode</text>")	  proxy = 3   end      for DayMode in string.gmatch(strData, "MD03") do        C4:SendToProxy(5001, "HOME","")       C4:SetVariable("DayMode","True","")	  C4:SendToProxy(5001, "DISPLAY_TEXT", "<text>Day Mode</text>")	  proxy = 4   end-- code...elseif BUTTONMAP[buttonID] == "#" then	   C4:SendToNetwork(6001, 1002, tohex("03") .. "KD0C" .. tohex("0D")) 	   C4:SendToProxy(5001, "DISPLAY_TEXT", "<text> </text>")	   --timer 5 seconds	   clsTimer = C4:AddTimer(5,"SECONDS")	   --if proxy = X then display previous message 	   if proxy == 1 then		C4:SendToProxy(5001, "DISPLAY_TEXT", "<text>Disarmed</text>")		proxy = 1	   elseif proxy == 2 then		C4:SendToProxy(5001, "DISPLAY_TEXT", "<text>Away Mode</text>")		proxy = 2	   elseif proxy == 3 then		C4:SendToProxy(5001, "DISPLAY_TEXT", "<text>Night Mode</text>")		proxy = 3	   elseif proxy == 4 then		C4:SendToProxy(5001, "DISPLAY_TEXT", "<text>Day Mode</text>")		proxy = 4	   end
Link to comment
Share on other sites


In DriverWorks, you create a timer, then you have to have code for when the timer expires.  The timer doesn't work like a delay in the code.  The AddTimer returns immediately, which is your issue.

 

You need an OnTimerExpired function like this:

 

function OnTimerExpired(idTimer)

  if (idTimer = gMyTimer) then  -- (should be clsTimer in your case)

    -- do the stuff you want to happen when the timer expires.

  end

end

 

 

RyanE

Link to comment
Share on other sites

Ok that makes sense. I just moved the lines below AddTimer to OnTimerExpired function, but know the driver does not even work. Where could be the problem? Functions like CancelclsTimer() and OnDriverDestroyed() are required?
 

function CancelclsTimer()  if (clsTimer ~= 0) then    C4:KillTimer(clsTimer)  end  clsTimer = 0end-- code...clsTimer = 0proxy = 0-- code...for Status in string.gmatch(strData, "MD00") do 		       alarm = nil       C4:SendToProxy(5001, "DISARMED","")	  C4:SetVariable("AwayMode","False","")       C4:SetVariable("NightMode","False","")       C4:SendToProxy(5001, "DISPLAY_TEXT", "<text>Disarmed</text>")	  proxy = 1   end   for AwayMode in string.gmatch(strData, "MD01") do        C4:SendToProxy(5001, "AWAY","")       C4:SetVariable("AwayMode","True","")	  C4:SendToProxy(5001, "DISPLAY_TEXT", "<text>Away Mode</text>")	  proxy = 2   end      for NightMode in string.gmatch(strData, "MD02") do 	       C4:SendToProxy(5001, "HOME","")       C4:SetVariable("NightMode","True","")	  C4:SendToProxy(5001, "DISPLAY_TEXT", "<text>Night Mode</text>")	  proxy = 3   end      for DayMode in string.gmatch(strData, "MD03") do        C4:SendToProxy(5001, "HOME","")       C4:SetVariable("DayMode","True","")	  C4:SendToProxy(5001, "DISPLAY_TEXT", "<text>Day Mode</text>")	  proxy = 4   end-- code...elseif BUTTONMAP[buttonID] == "#" then	   C4:SendToNetwork(6001, 1002, tohex("03") .. "KD0C" .. tohex("0D")) 	   C4:SendToProxy(5001, "DISPLAY_TEXT", "<text> </text>")	   --timer 5 seconds	   clsTimer = C4:AddTimer(5,"SECONDS")-- code...   function OnTimerExpired(idTimer)  if (idTimer == clsTimer) then    -- do the stuff you want to happen when the timer expires.    --if proxy = X then display previous message       if proxy == 1 then        C4:SendToProxy(5001, "DISPLAY_TEXT", "<text>Disarmed</text>")        proxy = 1       elseif proxy == 2 then        C4:SendToProxy(5001, "DISPLAY_TEXT", "<text>Away Mode</text>")        proxy = 2       elseif proxy == 3 then        C4:SendToProxy(5001, "DISPLAY_TEXT", "<text>Night Mode</text>")        proxy = 3       elseif proxy == 4 then        C4:SendToProxy(5001, "DISPLAY_TEXT", "<text>Day Mode</text>")        proxy = 4       end  endendfunction OnDriverDestroyed()-- Kill all timers in the system...  if (clsTimer ~= nil) then clsTimer = C4:KillTimer(clsTimer)  endend

I created the variable proxy just to keep record of the last message displayed, to when the screen is cleared after 5 seconds it will show the last state of the alarm. This is useful for example when the alarm is set to "Away Mode" and a door is opened. The alarm goes on. You close the door. Clear the screen pressing #. Should appear a message saying that the alarm is still on.

Link to comment
Share on other sites

There's no way for me to know why your driver doesn't work without having the full driver, and even if I had that, I wouldn't volunteer to figure it out for you.

 

Sorry.  Too much going on before CEDIA right now.

 

Good luck with your driver.

 

RyanE

Link to comment
Share on other sites

A quick glance shows a syntactical error: if (idTimer = clsTimer)

 

Any of these will break code.

 

 

Good catch.

 

One thing I do is execute *all* the code in the Lua window (or with Driver Editor), that'll ensure that the code will at least compile, and should load when the driver loads.

 

Thanks.

 

RyanE

 

Well that made the driver run, but the timer it's not working. When I press '#' to clear the screen nothing happens.

Could you guys please take a look to the CancelclsTimer() and OnDriverDestroyed() functions to see if they are (apparently) well coded?

Thank you all for your support and help!

 

Link to comment
Share on other sites

Just figured it out. I was executing the CancelclsTimer() function at the beginning of other functions but just moved it to immediately after running the AddTimer and now it's working.

One quick question, it's possible to get the last text sent to proxy? If so i don't need to have the variable 'proxy' to keep record of the last message sent.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.