]> hydra-www.ietfng.org Git - acmetensortoys-esp-lua_lamp/commitdiff
examples/lamp: implement more of tmr better
authorNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Thu, 4 May 2017 02:27:35 +0000 (22:27 -0400)
committerNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Thu, 4 May 2017 02:27:35 +0000 (22:27 -0400)
draw-flower uses more of the API

linux-draw-xpm.lua
linux-draw.lua

index 4b80ade72e1882b84b35625b4975190114349315..ac52a8e6970b5a791c675fa70e5e86cf70cf4a0a 100644 (file)
@@ -78,4 +78,4 @@ function dodraw()
   io.write("\n\n") -- XXX? WTF?
   io.flush()
 end
-doremotedraw = dodraw
+doremotedraw = function() remotetmr:start(); dodraw() end
index 4b6e45292b55dab317e1ddffbef48f41e32e568a..33251a4059c5df401225fd0a9e7f7a186a7cc9f9 100644 (file)
@@ -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 = {}