From 7c8ab50bb8d62a2ea626656cffa415aea50b1ebd Mon Sep 17 00:00:00 2001 From: Nathaniel Wesley Filardo Date: Wed, 3 May 2017 22:27:35 -0400 Subject: [PATCH] examples/lamp: implement more of tmr better draw-flower uses more of the API --- linux-draw-xpm.lua | 2 +- linux-draw.lua | 33 ++++++++++++++++++++------------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/linux-draw-xpm.lua b/linux-draw-xpm.lua index 4b80ade..ac52a8e 100644 --- a/linux-draw-xpm.lua +++ b/linux-draw-xpm.lua @@ -78,4 +78,4 @@ function dodraw() io.write("\n\n") -- XXX? WTF? io.flush() end -doremotedraw = dodraw +doremotedraw = function() remotetmr:start(); dodraw() end diff --git a/linux-draw.lua b/linux-draw.lua index 4b6e452..33251a4 100644 --- a/linux-draw.lua +++ b/linux-draw.lua @@ -38,28 +38,35 @@ end -- how backwards is this!? We are using tq as faked above for tmr support -- since it's not obvious to me how to remove pending events in cqueues. tmr = {} -tmr.ALARM_AUTO = 0 -function tmr.unregister(self) +tmr.ALARM_SINGLE = 0 +tmr.ALARM_SEMI = 1 +tmr.ALARM_AUTO = 2 +function tmr._start(self) + if self.fn then self.tqe = tq:queue(self.period,self.fn) end +end +function tmr.stop(self) if self.tqe then tq:dequeue(self.tqe); self.tqe = nil end end +function tmr.unregister(self) + self.fn = nil +end function tmr.register(self,period,mode,fn) - tmr.unregister(self) + tmr.stop(self) - local fnwrap + self.period = period if mode == tmr.ALARM_AUTO then - -- persist by re-registering after fire - fnwrap = function() tmr.register(self,period,mode,fn); fn() end + self.fn = function() tmr._start(self); fn() end else - fnwrap = fn + self.fn = fn end - - self.tqe = tq:queue(period,fnwrap) end +function tmr.start(self) + tmr.stop(self) + tmr._start(self) +end +tmr_mt = { __index = tmr } -remotetmr = {} -remotetmr.tqe = nil -remotetmr.register = tmr.register -remotetmr.unregister = tmr.unregister +remotetmr = setmetatable({}, tmr_mt) remotefb = {} -- 2.50.1