Files
pitts-touchscreen/3B/archive/root_11.bak/run.lua

309 lines
8.0 KiB
Lua

-- LUA default run file
-- LUA default run file
require "env"
periphery = require('periphery')
Serial = periphery.Serial
evdev = require "evdev"
touch = evdev.Device("/dev/input/event0")
COMMAND_POSITION = 0x19
COMMAND_POSITION_X = 0x13
COMMAND_POSITION_Y = 0x11
COMMAND_HOME = 0x0C
COMMAND_PASSWORD = 0x7F
COMMAND_01H = 0x01
COMMAND_03H = 0x03
COMMAND_CURSOR_ON = 0x0F
COMMAND_CURSOR_OFF = 0x0E
COMMAND_BLINK = 0x0B
COMMAND_UNBLINK = 0x0C
COMMAND_CR = 0x0D
COMMAND_LF = 0x0A
blinkFlag = false
posX = 0
posY = 0
touchY = 0
touchX = 0
LOGING = 1
-- 1 2 3 4 5 6 7 8 9 10
-- 11 12 13 14 15 16 17 18 19 20
controlLabels = {"<-" ,"<<" ,"^" ,"^^" ,"vv" , "v" ,">>" ,"->" ,"Help" ,"Del",
"Done" ," " ,"Insert","," ,"Space" , " " ,"=" ,"Remove","Enter" ," "}
controlFonts = {"C" ,"C" ,"C" ,"C" ,"C" , "C" ,"C" ,"C" ,"T" ,"C",
"C" ,"T" ,"T" ,"C" ,"C" , "T" ,"C" ,"T" ,"C" ,"T"}
controlPos = {{02,06}, {01,18}, {02,02}, {01,30}, {01,22}, {18,02}, {01,34}, {02,44}, {02,10}, {02,50},
{10,01}, {10,01}, {06,01}, {10,50}, {14,50}, {14,50}, {06,50}, {15,02}, {18,50}, {18,50}}
local function errLog(msg)
io.stderr:write(msg)
end
function logPrint(msg,aux)
if ( LOGING ) then
if aux then
print(msg.." , "..aux)
else
print(msg);
end
end
end
function dbgPrint(msg,aux)
if ( DEBUG ) then
if aux then
print(msg.." , "..aux)
else
print(msg);
end
end
end
function checkY(y)
if y < 0 then
y = 0
end
if y > 5 then
y = 5
end
posY = y
return y
end
function checkX(x)
if x < 0 then
x = 39;
posY = posY - 1
checkY(posY)
end
if x > 39 then
if posY == 5 then
x = 39;
else
x = 0;
posY = posY + 1
checkY(posY);
end
end
posX = x
return x
end
function gotoXY(x,y)
checkX(x);
checkY(y);
end
function lineFeed(void)
posY = posY + 1
gotoXY(posX, checkY(posY));
end
function carriageReturn(void)
gotoXY(0, posY);
end
function clearAndHome(void)
for i=1, 6, 1 do
for j=1, 40, 1 do
line[i][j]:setTextColor(HMI.Color.green);
line[i][j]:setBackground(HMI.Color.black);
line[i][j]:setText(" ")
line[i][j]:draw()
end
end
posX = 0
posY = 0
end
function doRead()
local loops = 0
while loops < 30 do
loops = loops + 1
if serial:poll(1) then
local c = serial:read(1)
if c:byte() == COMMAND_POSITION then
local cmd = serial:read(1)
if cmd:byte() == COMMAND_POSITION_X then
local x = string.byte(serial:read(1))
gotoXY(x,posY)
elseif cmd:byte() == COMMAND_POSITION_Y then
local y = string.byte(serial:read(1))
gotoXY(posX,y)
elseif cmd:byte() == COMMAND_HOME then
clearAndHome();
end
elseif c:byte() == COMMAND_03H then
serial:read(1)
serial:read(1)
elseif c:byte() == COMMAND_BLINK then
blinkFlag = true
elseif c:byte() == COMMAND_UNBLINK then
blinkFlag = false
elseif c:byte() == COMMAND_CURSOR_OFF then
-- curs_set(0);
elseif c:byte() == COMMAND_CURSOR_ON then
-- curs_set(0);
elseif c:byte() == COMMAND_CR then
carriageReturn()
elseif c:byte() == COMMAND_LF then
lineFeed()
else
if c:byte() == COMMAND_PASSWORD then
c = '*'
end
if c:byte() >= 0x20 and c:byte() < 0x7F then
--print(c.." at "..posX.." "..posY.."\n")
local label = line[posY+1][posX+1]
label:setText(c)
if blinkFlag then
label:setTextColor(HMI.Color.black);
label:setBackground(HMI.Color.green);
else
label:setTextColor(HMI.Color.green);
label:setBackground(HMI.Color.black);
end
posX = posX + 1
checkX(posX)
label:draw()
else
print("Unknown: "..c:byte().."\n");
end
end
end
end
end
function doTouch()
local x = 0;
local y = 0;
if touch:avail() > 0 then
local timestamp, eventType, eventCode, value = touch:read()
if eventType == evdev.EV_ABS then
if eventCode == 53 then
touchX = value
elseif eventCode == 54 then
touchY = value
end
end
if eventType == 1 then
if eventCode == 330 then
if touchY > 0 and touchY < 79 then
idx = math.floor(touchX/80)+1
x = controlPos[idx][2]
y = controlPos[idx][1]
print("Ctrl1: ",y,x,idx)
end
if touchY > 400 and touchY < 479 then
idx = math.floor(touchX/80)+11
x = controlPos[idx][2]
y = controlPos[idx][1]
print("Ctrl2: ",y,x,idx)
end
if touchY > 320 and touchY < 399 then
x = 8 + math.floor(touchX/80)*4
y = 19
print("Numbs: ",y,x)
end
if touchY > 80 and touchY < 319 then
y = math.floor((touchY-80)/240*10+6)
x = math.floor(touchX/720*36+7)
print("Lines: ",y,x)
end
if x > 0 and y > 0 then
if value == 1 then
print("down")
serial:write(string.char(0x5F,y,x))
serial:flush()
--serial:write(string.char(0x5F,x,y))
else
print("up")
serial:write(string.char(0x6F,y,x))
serial:flush()
end
end
end
end
end
end
function setup()
serial = Serial{device="/dev/ttyAMA0", baudrate=9600, databits=7, parity="odd", stopbits=1}
instance = HMI.App.instance;
layout = HMI.Layout(instance,200,48);
monoChar = HMI.Font("/root/font/mono.ttf",34)
monoText = HMI.Font("/root/font/mono.ttf",20)
line = {}
for i=1, 6, 1 do
line[i] = {}
for j=1, 40, 1 do
line[i][j] = HMI.Label(layout,5*(j-1),8+4*(i-1),5,4," ");
line[i][j]:setTextColor(HMI.Color.green);
line[i][j]:setBackground(HMI.Color.black);
line[i][j]:setFont(monoChar)
line[i][j]:draw()
end
end
number = {}
for i=1, 10, 1 do
number[i] = HMI.Cell(layout,1+20*(i-1),34,20,7,i-1);
number[i]:setTextColor(HMI.Color.black);
number[i]:setBackground(HMI.Color.white);
number[i]:setFont(monoChar)
end
control = {}
for j=0, 1, 1 do
for i=1, 10, 1 do
local idx = i+j*10
if idx ~= 12 and idx ~= 16 and idx ~= 20 then
if idx == 11 or idx == 15 or idx == 19 then
control[idx] = HMI.Cell(layout,1+20*(i-1),j*41,40,7,i-1);
else
control[idx] = HMI.Cell(layout,1+20*(i-1),j*41,20,7,i-1);
end
if controlLabels[idx] then
control[idx]:setText(controlLabels[idx])
end
control[idx]:setTextColor(HMI.Color.black);
control[idx]:setBackground(HMI.Color.grey);
if controlFonts[idx] == 'T' then
control[idx]:setFont(monoText)
else
control[i+j*10]:setFont(monoChar)
end
end
end
end
cnt = 0;
end
function loop()
doRead()
doTouch()
end
io.stderr:write("Run LUA\n");
--------------------------------------------------