]> hydra-www.ietfng.org Git - acmetensortoys-esp-lua_lamp/commitdiff
emulators: add touch emulator
authorNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Wed, 3 Jan 2018 22:13:13 +0000 (17:13 -0500)
committerNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Wed, 3 Jan 2018 22:13:13 +0000 (17:13 -0500)
And some general refactoring.

linux-draw-touchemu.lua [new file with mode: 0644]
linux-draw-xpm.lua
linux-draw.lua

diff --git a/linux-draw-touchemu.lua b/linux-draw-touchemu.lua
new file mode 100644 (file)
index 0000000..f54797c
--- /dev/null
@@ -0,0 +1,85 @@
+-- Expects to be invoked by linux-draw.lua (i.e. as argv[1]) and given the
+-- touch module as argv[2]; will further wrap a worker module given as
+-- argv[3].  Consumes from stdin and routes keys to the touch handler; keys
+-- 1-6 toggle the corresponding bits in the "touch sensor".
+
+bit = require "bit"
+function bit.isset(v,b)   return bit.band(v,bit.lshift(1,b)) ~= 0 end
+function bit.isclear(v,b) return bit.band(v,bit.lshift(1,b)) == 0 end
+
+local touchcb = nil
+
+gpio = { }
+gpio.LOW = 0
+gpio.HIGH = 1
+function gpio.trig(p,m,fn)
+  printerr("GPIO", "TRIG", fn or "nil")
+  touchcb = touchcb or fn -- set once, ignore subsequent
+end
+function gpio.write(p,v)
+  printerr("GPIO", p, v)
+end
+
+local touches = 0
+
+cap = {}
+function cap.mr(self,addr,fn)
+  printerr("CAP", addr)
+end
+function cap.rt(self)
+  return 0, touches
+end
+
+file = {}
+function file.list()
+  return { ["draw-happy.lc"] = 1, ["draw-heart.lc"] = 1}
+end
+
+touchtmr = tmr.create()
+
+if arg[1]
+ then local f = arg[1]; table.remove(arg,1) ; dofile(f)
+ else print("You probably meant to give a filename"); os.exit(1)
+end
+
+ws2812 = ws2812 or {}
+function ws2812.newBuffer()
+  return remotefb
+end
+function ws2812.write()
+  printerr("WS2182", "WRITE")
+end
+
+function lamp_announce(...)
+  print("ANNOUNCE", ...)
+end
+
+if arg[1]
+  then local f = arg[1]; table.remove(arg,1) ; dofile(f)
+  else print("You probably meant to give a filename"); os.exit(1)
+end
+
+dimfactor = 1
+isDim = true
+
+fns = {
+  ['q'] = function() os.execute("stty sane"); os.exit(0) end,
+  ['1'] = function() touches = bit.bxor(touches,   1) end,
+  ['2'] = function() touches = bit.bxor(touches,   2) end,
+  ['3'] = function() touches = bit.bxor(touches,   4) end,
+  ['4'] = function() touches = bit.bxor(touches,   8) end,
+  ['5'] = function() touches = bit.bxor(touches,  16) end,
+  ['6'] = function() touches = bit.bxor(touches,  32) end,
+  ['7'] = function() touches = bit.bxor(touches,  64) end,
+  ['8'] = function() touches = bit.bxor(touches, 128) end
+}
+
+os.execute("stty -icanon isig onlret")
+
+onStdin = function()
+  local ch = io.stdin:read(1)
+  local f = fns[ch] or (function() printerr("IGNORE", ch) end)
+  f()
+  printerr("TLOOP", touches, dimfactor, isDim)
+  if type(touchcb) == "function" then touchcb() end
+end
index 7a0bce32a70cdec9de1e1b312e93ce22edb74c2f..2f881cfe813bd8688a78ea1ed922763c9fde6d1b 100644 (file)
@@ -51,6 +51,7 @@ function remotefb.shift(self,n,m,i,j)
     table.insert(self,i,v)
   end
 end
+remotefb:fill(0,0,0)
 
 local function drawfailsafe(t,fb,p) fb:fill(0,0,0) end
 function loaddrawfn(name)
index 7e926e097ea8e09b9ed7cab62224fc435149b95f..b3f698fcbab139720c90036e74292e9576c7fa5f 100644 (file)
@@ -96,16 +96,24 @@ end
 
 remotefb = {}
 
-if arg[1] then dofile(arg[1]) else print("You probably meant to give a filename"); os.exit(1) end
-
-cqc:wrap(function()
-  while true do
-    cq.poll({ pollfd = 0, events = 'r' })
+-- can be overridden by modules we load!
+onStdin = function()
     local line = io.read() -- XXX :(
-    if line == nil or line == "" then return end
+    if line == nil or line == "" then return true end
     printerr("line: " .. line)
     local from, cmd = line:match("^(%S+)%s+(.*)$")
     dofile("lamp-remote.lua")(cmd)
+end
+
+if arg[1]
+ then local f = arg[1]; table.remove(arg,1) ; dofile(f)
+ else print("You probably meant to give a filename"); os.exit(1)
+end
+
+cqc:wrap(function()
+  while true do
+    cq.poll({ pollfd = 0, events = 'r' })
+    if onStdin() then break end
   end
 end)
 io.stdout:setvbuf("no")