-- LUA default run file -- LUA default run file require "env" periphery = require('periphery') Serial = periphery.Serial COMMAND_POSITION = 0x19 COMMAND_POSITION_X = 0x13 COMMAND_POSITION_Y = 0x11 COMMAND_01H = 0x01 COMMAND_03H = 0x03 COMMAND_CURSOR_OFF = 0x0E COMMAND_BLINK = 0x0B COMMAND_UNBLINK = 0x0C COMMAND_CR = 0x0D COMMAND_LF = 0x0A blinkFlag = false posX = 0 posY = 0 LOGING = 1 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 doRead() local loops = 0 while loops < 100 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) 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_CR then carriageReturn() elseif c:byte() == COMMAND_LF then lineFeed() else if c:byte() >= 0x20 and c:byte() < 0x7F then --print(c.." at "..posX.." "..posY.."\n") posX = posX + 1 checkX(posX) local label = line[posY+1][posX+1] label:setText(c) label:draw() else print("Unknown: "..c:byte().."\n"); end end end end end function setup() serial = Serial("/dev/ttyAMA0",19200) instance = HMI.App.instance; layout = HMI.Layout(instance,200,48); mono = HMI.Font("/root/font/mono.ttf",34) 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(mono) line[1][j]:draw() end end number = {} for i=1, 10, 1 do number[i] = HMI.Cell(layout,1+20*(i-1),0,20,7,i-1); number[i]:setTextColor(HMI.Color.black); number[i]:setBackground(HMI.Color.grey); number[i]:setFont(mono) end control = {} for j=0, 1, 1 do for i=1, 10, 1 do control[i+j] = HMI.Cell(layout,1+20*(i-1),34+j*7,20,7,i-1); control[i+j]:setTextColor(HMI.Color.black); control[i+j]:setBackground(HMI.Color.grey); control[i+j]:setFont(mono) end end cnt = 0; end function loop() doRead() end io.stderr:write("Run LUA\n"); --------------------------------------------------