From 7d3fd6993bdedf2ff96acd15fb31dfdc6ed93ca7 Mon Sep 17 00:00:00 2001 From: Nathaniel Wesley Filardo Date: Wed, 3 Jan 2018 17:13:13 -0500 Subject: [PATCH] emulators: add touch emulator And some general refactoring. --- linux-draw-touchemu.lua | 85 +++++++++++++++++++++++++++++++++++++++++ linux-draw-xpm.lua | 1 + linux-draw.lua | 20 +++++++--- 3 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 linux-draw-touchemu.lua diff --git a/linux-draw-touchemu.lua b/linux-draw-touchemu.lua new file mode 100644 index 0000000..f54797c --- /dev/null +++ b/linux-draw-touchemu.lua @@ -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 diff --git a/linux-draw-xpm.lua b/linux-draw-xpm.lua index 7a0bce3..2f881cf 100644 --- a/linux-draw-xpm.lua +++ b/linux-draw-xpm.lua @@ -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) diff --git a/linux-draw.lua b/linux-draw.lua index 7e926e0..b3f698f 100644 --- a/linux-draw.lua +++ b/linux-draw.lua @@ -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") -- 2.50.1