Commit 6926c66b authored by galjonsfigur's avatar galjonsfigur Committed by Marcel Stör
Browse files

Polish Lua examples (#2846)

* Add missing globals from luacheck config

* Fix luacheck warnings in all lua files

* Re-enable luacheck in Travis

* Speed up Travis by using preinstalled LuaRocks

* Fix more luacheck warnings in httpserver lua module

* Fix DCC module and add appropriate definitions to luacheck config.

* Change inline comments from ignoring block to only ignore specific line

* Add Luacheck for Windows and enable it for both Windows and Linux

* Change luacheck exceptions and fix errors from 1st round of polishing

* Add retry and timeout params to wget
parent 36df8d00
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
-- **************************************************************************** -- ****************************************************************************
function cb_drained(d) local function cb_drained()
print("drained "..node.heap()) print("drained "..node.heap())
file.seek("set", 0) file.seek("set", 0)
...@@ -14,27 +14,29 @@ function cb_drained(d) ...@@ -14,27 +14,29 @@ function cb_drained(d)
--d:play(pcm.RATE_8K) --d:play(pcm.RATE_8K)
end end
function cb_stopped(d) local function cb_stopped()
print("playback stopped") print("playback stopped")
file.seek("set", 0) file.seek("set", 0)
end end
function cb_paused(d) local function cb_paused()
print("playback paused") print("playback paused")
end end
file.open("jump_8k.u8", "r") do
file.open("jump_8k.u8", "r")
drv = pcm.new(pcm.SD, 1) local drv = pcm.new(pcm.SD, 1)
-- fetch data in chunks of FILE_READ_CHUNK (1024) from file -- fetch data in chunks of FILE_READ_CHUNK (1024) from file
drv:on("data", function(drv) return file.read() end) drv:on("data", function(driver) return file.read() end) -- luacheck: no unused
-- get called back when all samples were read from the file -- get called back when all samples were read from the file
drv:on("drained", cb_drained) drv:on("drained", cb_drained)
drv:on("stopped", cb_stopped) drv:on("stopped", cb_stopped)
drv:on("paused", cb_paused) drv:on("paused", cb_paused)
-- start playback -- start playback
drv:play(pcm.RATE_8K) drv:play(pcm.RATE_8K)
end
...@@ -29,10 +29,12 @@ local TWILIO_ACCOUNT_SID = "xxxxxx" ...@@ -29,10 +29,12 @@ local TWILIO_ACCOUNT_SID = "xxxxxx"
local TWILIO_TOKEN = "xxxxxx" local TWILIO_TOKEN = "xxxxxx"
local HOST = "iot-https-relay.appspot.com" -- visit http://iot-https-relay.appspot.com/ to learn more about this service local HOST = "iot-https-relay.appspot.com" -- visit http://iot-https-relay.appspot.com/ to learn more about this service
-- Please be sure to understand the security issues of using this relay app and use at your own risk. -- Please be sure to understand the security issues of using this relay app and use at your own risk.
local URI = "/twilio/Messages.json" local URI = "/twilio/Messages.json"
function build_post_request(host, uri, data_table) local wifiTimer = tmr.create()
local function build_post_request(host, uri, data_table)
local data = "" local data = ""
...@@ -40,7 +42,7 @@ function build_post_request(host, uri, data_table) ...@@ -40,7 +42,7 @@ function build_post_request(host, uri, data_table)
data = data .. param.."="..value.."&" data = data .. param.."="..value.."&"
end end
request = "POST "..uri.." HTTP/1.1\r\n".. local request = "POST "..uri.." HTTP/1.1\r\n"..
"Host: "..host.."\r\n".. "Host: "..host.."\r\n"..
"Connection: close\r\n".. "Connection: close\r\n"..
"Content-Type: application/x-www-form-urlencoded\r\n".. "Content-Type: application/x-www-form-urlencoded\r\n"..
...@@ -53,7 +55,7 @@ function build_post_request(host, uri, data_table) ...@@ -53,7 +55,7 @@ function build_post_request(host, uri, data_table)
return request return request
end end
local function display(sck,response) local function display(socket, response) -- luacheck: no unused
print(response) print(response)
end end
...@@ -69,7 +71,7 @@ local function send_sms(from,to,body) ...@@ -69,7 +71,7 @@ local function send_sms(from,to,body)
To = to To = to
} }
socket = net.createConnection(net.TCP,0) local socket = net.createConnection(net.TCP,0)
socket:on("receive",display) socket:on("receive",display)
socket:connect(80,HOST) socket:connect(80,HOST)
...@@ -80,13 +82,13 @@ local function send_sms(from,to,body) ...@@ -80,13 +82,13 @@ local function send_sms(from,to,body)
end) end)
end end
function check_wifi() local function check_wifi()
local ip = wifi.sta.getip() local ip = wifi.sta.getip()
if(ip==nil) then if(ip==nil) then
print("Connecting...") print("Connecting...")
else else
tmr.stop(0) wifiTimer.stop()
print("Connected to AP!") print("Connected to AP!")
print(ip) print(ip)
-- send a text message with the text "Hello from your esp8266" -- send a text message with the text "Hello from your esp8266"
...@@ -95,4 +97,4 @@ function check_wifi() ...@@ -95,4 +97,4 @@ function check_wifi()
end end
tmr.alarm(0,7000,1,check_wifi) wifiTimer.alarm(7000, tmr.ALARM_AUTO, check_wifi)
-- Test sjson and GitHub API -- Test sjson and GitHub API
local s = tls.createConnection() local s = tls.createConnection()
s:on("connection", function(sck, c) s:on("connection", function(sck)
sck:send("GET /repos/nodemcu/nodemcu-firmware/git/trees/master HTTP/1.0\r\nUser-agent: nodemcu/0.1\r\nHost: api.github.com\r\nConnection: close\r\nAccept: application/json\r\n\r\n") sck:send(
[[GET /repos/nodemcu/nodemcu-firmware/git/trees/master HTTP/1.0
User-agent: nodemcu/0.1
Host: api.github.com
Connection: close
Accept: application/json
]])
end) end)
function startswith(String, Start) local function startswith(String, Start)
return string.sub(String, 1, string.len(Start)) == Start return string.sub(String, 1, string.len(Start)) == Start
end end
...@@ -23,13 +30,13 @@ local decoder = sjson.decoder({ ...@@ -23,13 +30,13 @@ local decoder = sjson.decoder({
end end
} }
}) })
local function handledata(s) local function handledata(sck)
decoder:write(s) decoder:write(sck)
end end
-- The receive callback is somewhat gnarly as it has to deal with find the end of the header -- The receive callback is somewhat gnarly as it has to deal with find the end of the header
-- and having the newline sequence split across packets -- and having the newline sequence split across packets
s:on("receive", function(sck, c) s:on("receive", function(socket, c) -- luacheck: no unused
if partial then if partial then
c = partial .. c c = partial .. c
partial = nil partial = nil
...@@ -45,8 +52,8 @@ s:on("receive", function(sck, c) ...@@ -45,8 +52,8 @@ s:on("receive", function(sck, c)
handledata(c) handledata(c)
return return
end end
local s, e = c:find("\r\n") local str, e = c:find("\r\n")
if s then if str then
-- Throw away line -- Throw away line
c = c:sub(e + 1) c = c:sub(e + 1)
else else
......
-- Somfy module example (beside somfy module requires also SJSON module) -- Somfy module example (beside somfy module requires also SJSON module)
-- The rolling code number is stored in the file somfy.cfg. A cached write of the somfy.cfg file is implemented in order to reduce the number of write to the EEPROM memory. Together with the logic of the file module it should allow long lasting operation. -- The rolling code number is stored in the file somfy.cfg.
-- A cached write of the somfy.cfg file is implemented in order to reduce
-- the number of write to the EEPROM memory. Together with the logic of the
-- file module it should allow long lasting operation.
config_file = "somfy." local config_file = "somfy."
local config, config_saved
-- somfy.cfg looks like -- somfy.cfg looks like
-- {"window1":{"rc":1,"address":123},"window2":{"rc":1,"address":124}} -- {"window1":{"rc":1,"address":123},"window2":{"rc":1,"address":124}}
local tmr_cache = tmr.create() local tmr_cache = tmr.create()
local tmr_delay = tmr.create() local tmr_delay = tmr.create()
pin = 4 local pin = 4
gpio.mode(pin, gpio.OUTPUT, gpio.PULLUP)
function deepcopy(orig) local function deepcopy(orig)
local orig_type = type(orig) local orig_type = type(orig)
local copy local copy
if orig_type == 'table' then if orig_type == 'table' then
...@@ -26,8 +29,8 @@ function deepcopy(orig) ...@@ -26,8 +29,8 @@ function deepcopy(orig)
return copy return copy
end end
function readconfig() local function readconfig()
local cfg, ok, ln local ln
if file.exists(config_file.."cfg") then if file.exists(config_file.."cfg") then
print("Reading config from "..config_file.."cfg") print("Reading config from "..config_file.."cfg")
file.open(config_file.."cfg", "r+") file.open(config_file.."cfg", "r+")
...@@ -47,7 +50,7 @@ function readconfig() ...@@ -47,7 +50,7 @@ function readconfig()
config_saved = deepcopy(config) config_saved = deepcopy(config)
end end
function writeconfighard() local function writeconfighard()
print("Saving config") print("Saving config")
file.remove(config_file.."bak") file.remove(config_file.."bak")
file.rename(config_file.."cfg", config_file.."bak") file.rename(config_file.."cfg", config_file.."bak")
...@@ -63,8 +66,8 @@ function writeconfighard() ...@@ -63,8 +66,8 @@ function writeconfighard()
config_saved = deepcopy(config) config_saved = deepcopy(config)
end end
function writeconfig() local function writeconfig()
tmr.stop(tmr_cache) tmr_cache:stop()
local savenow = false local savenow = false
local savelater = false local savelater = false
...@@ -87,12 +90,18 @@ function writeconfig() ...@@ -87,12 +90,18 @@ function writeconfig()
end end
if savelater then if savelater then
print("Saving config later") print("Saving config later")
tmr.alarm(tmr_cache, 65000, tmr.ALARM_SINGLE, writeconfighard) tmr_cache:alarm(65000, tmr.ALARM_SINGLE, writeconfighard)
end end
end end
--======================================================================================================-- --======================================================================================================--
function down(remote, cb, par) local function wait(ms, cb, par)
par = par or {}
print("wait: ".. ms)
if cb then tmr_delay:alarm(ms, tmr.ALARM_SINGLE, function () cb(unpack(par)) end) end
end
local function down(remote, cb, par)
par = par or {} par = par or {}
print("down: ".. remote) print("down: ".. remote)
config[remote].rc=config[remote].rc+1 config[remote].rc=config[remote].rc+1
...@@ -100,7 +109,7 @@ function down(remote, cb, par) ...@@ -100,7 +109,7 @@ function down(remote, cb, par)
writeconfig() writeconfig()
end end
function up(remote, cb, par) local function up(remote, cb, par)
par = par or {} par = par or {}
print("up: ".. remote) print("up: ".. remote)
config[remote].rc=config[remote].rc+1 config[remote].rc=config[remote].rc+1
...@@ -108,7 +117,7 @@ function up(remote, cb, par) ...@@ -108,7 +117,7 @@ function up(remote, cb, par)
writeconfig() writeconfig()
end end
function downStep(remote, cb, par) local function downStep(remote, cb, par)
par = par or {} par = par or {}
print("downStep: ".. remote) print("downStep: ".. remote)
config[remote].rc=config[remote].rc+1 config[remote].rc=config[remote].rc+1
...@@ -116,7 +125,7 @@ function downStep(remote, cb, par) ...@@ -116,7 +125,7 @@ function downStep(remote, cb, par)
writeconfig() writeconfig()
end end
function upStep(remote, cb, par) local function upStep(remote, cb, par) -- luacheck: ignore
par = par or {} par = par or {}
print("upStep: ".. remote) print("upStep: ".. remote)
config[remote].rc=config[remote].rc+1 config[remote].rc=config[remote].rc+1
...@@ -124,14 +133,9 @@ function upStep(remote, cb, par) ...@@ -124,14 +133,9 @@ function upStep(remote, cb, par)
writeconfig() writeconfig()
end end
function wait(ms, cb, par)
par = par or {}
print("wait: ".. ms)
if cb then tmr.alarm(tmr_delay, ms, tmr.ALARM_SINGLE, function () cb(unpack(par)) end) end
end
--======================================================================================================-- --======================================================================================================--
gpio.mode(pin, gpio.OUTPUT, gpio.PULLUP)
if not config then readconfig() end if not config then readconfig() end
if #config == 0 then -- somfy.cfg does not exist if #config == 0 then -- somfy.cfg does not exist
config = sjson.decode([[{"window1":{"rc":1,"address":123},"window2":{"rc":1,"address":124}}]]) config = sjson.decode([[{"window1":{"rc":1,"address":123},"window2":{"rc":1,"address":124}}]])
...@@ -141,5 +145,11 @@ down('window1', ...@@ -141,5 +145,11 @@ down('window1',
wait, {60000, wait, {60000,
up, {'window1', up, {'window1',
wait, {9000, wait, {9000,
downStep, {'window1', downStep, {'window1', downStep, {'window1', downStep, {'window1', downStep, {'window1', downStep, {'window1', downStep, {'window1' downStep, {'window1',
downStep, {'window1',
downStep, {'window1',
downStep, {'window1',
downStep, {'window1',
downStep, {'window1',
downStep, {'window1'
}}}}}}}}}}) }}}}}}}}}})
uart.setup(0,9600,8,0,1,0) do
sv=net.createServer(net.TCP, 60) uart.setup(0, 9600, 8, 0, 1, 0)
global_c = nil local sv = net.createServer(net.TCP, 60)
sv:listen(9999, function(c) local global_c = nil
if global_c~=nil then sv:listen(9999, function(c)
global_c:close() if global_c~=nil then
end global_c:close()
global_c=c end
c:on("receive",function(sck,pl) uart.write(0,pl) end) global_c = c
end) c:on("receive",function(_, pl) uart.write(0, pl) end)
end)
uart.on("data",4, function(data) uart.on("data", 4, function(data)
if global_c~=nil then if global_c ~= nil then
global_c:send(data) global_c:send(data)
end end
end, 0) end, 0)
end
...@@ -27,7 +27,7 @@ function M.getzones() ...@@ -27,7 +27,7 @@ function M.getzones()
return result return result
end end
function load(t) local function load(t)
local z = file.open(thezone .. ".zone", "r") local z = file.open(thezone .. ".zone", "r")
local hdr = z:read(20) local hdr = z:read(20)
...@@ -35,7 +35,8 @@ function load(t) ...@@ -35,7 +35,8 @@ function load(t)
if magic == "TZif" then if magic == "TZif" then
local lens = z:read(24) local lens = z:read(24)
local ttisgmt_count, ttisdstcnt, leapcnt, timecnt, typecnt, charcnt = struct.unpack("> LLLLLL", lens) local ttisgmt_count, ttisdstcnt, leapcnt, timecnt, typecnt, charcnt -- luacheck: no unused
= struct.unpack("> LLLLLL", lens)
local times = z:read(4 * timecnt) local times = z:read(4 * timecnt)
local typeindex = z:read(timecnt) local typeindex = z:read(timecnt)
......
...@@ -9,8 +9,13 @@ ...@@ -9,8 +9,13 @@
-- --
-- *************************************************************************** -- ***************************************************************************
-- display object
local disp
local draw_state, loop_tmr = 0, tmr.create()
-- setup I2c and connect display -- setup I2c and connect display
function init_i2c_display() local function init_i2c_display()
-- SDA and SCL can be assigned freely to available GPIOs -- SDA and SCL can be assigned freely to available GPIOs
local sda = 5 -- GPIO14 local sda = 5 -- GPIO14
local scl = 6 -- GPIO12 local scl = 6 -- GPIO12
...@@ -18,9 +23,8 @@ function init_i2c_display() ...@@ -18,9 +23,8 @@ function init_i2c_display()
i2c.setup(0, sda, scl, i2c.SLOW) i2c.setup(0, sda, scl, i2c.SLOW)
disp = u8g2.ssd1306_i2c_128x64_noname(0, sla) disp = u8g2.ssd1306_i2c_128x64_noname(0, sla)
end end
-- setup SPI and connect display -- setup SPI and connect display
function init_spi_display() local function init_spi_display() -- luacheck: no unused
-- Hardware SPI CLK = GPIO14 -- Hardware SPI CLK = GPIO14
-- Hardware SPI MOSI = GPIO13 -- Hardware SPI MOSI = GPIO13
-- Hardware SPI MISO = GPIO12 (not used) -- Hardware SPI MISO = GPIO12 (not used)
...@@ -37,8 +41,7 @@ function init_spi_display() ...@@ -37,8 +41,7 @@ function init_spi_display()
disp = u8g2.ssd1306_128x64_noname(1, cs, dc, res) disp = u8g2.ssd1306_128x64_noname(1, cs, dc, res)
end end
local function u8g2_prepare()
function u8g2_prepare()
disp:setFont(u8g2.font_6x10_tf) disp:setFont(u8g2.font_6x10_tf)
disp:setFontRefHeightExtendedText() disp:setFontRefHeightExtendedText()
disp:setDrawColor(1) disp:setDrawColor(1)
...@@ -47,7 +50,7 @@ function u8g2_prepare() ...@@ -47,7 +50,7 @@ function u8g2_prepare()
end end
function u8g2_box_frame(a) local function u8g2_box_frame(a)
disp:drawStr( 0, 0, "drawBox") disp:drawStr( 0, 0, "drawBox")
disp:drawBox(5,10,20,10) disp:drawBox(5,10,20,10)
disp:drawBox(10+a,15,30,7) disp:drawBox(10+a,15,30,7)
...@@ -56,7 +59,7 @@ function u8g2_box_frame(a) ...@@ -56,7 +59,7 @@ function u8g2_box_frame(a)
disp:drawFrame(10+a,15+30,30,7) disp:drawFrame(10+a,15+30,30,7)
end end
function u8g2_disc_circle(a) local function u8g2_disc_circle(a)
disp:drawStr( 0, 0, "drawDisc") disp:drawStr( 0, 0, "drawDisc")
disp:drawDisc(10,18,9) disp:drawDisc(10,18,9)
disp:drawDisc(24+a,16,7) disp:drawDisc(24+a,16,7)
...@@ -65,13 +68,13 @@ function u8g2_disc_circle(a) ...@@ -65,13 +68,13 @@ function u8g2_disc_circle(a)
disp:drawCircle(24+a,16+30,7) disp:drawCircle(24+a,16+30,7)
end end
function u8g2_r_frame(a) local function u8g2_r_frame(a)
disp:drawStr( 0, 0, "drawRFrame/Box") disp:drawStr( 0, 0, "drawRFrame/Box")
disp:drawRFrame(5, 10,40,30, a+1) disp:drawRFrame(5, 10,40,30, a+1)
disp:drawRBox(50, 10,25,40, a+1) disp:drawRBox(50, 10,25,40, a+1)
end end
function u8g2_string(a) local function u8g2_string(a)
disp:setFontDirection(0) disp:setFontDirection(0)
disp:drawStr(30+a,31, " 0") disp:drawStr(30+a,31, " 0")
disp:setFontDirection(1) disp:setFontDirection(1)
...@@ -82,7 +85,7 @@ function u8g2_string(a) ...@@ -82,7 +85,7 @@ function u8g2_string(a)
disp:drawStr(30,31-a, " 270") disp:drawStr(30,31-a, " 270")
end end
function u8g2_line(a) local function u8g2_line(a)
disp:drawStr( 0, 0, "drawLine") disp:drawStr( 0, 0, "drawLine")
disp:drawLine(7+a, 10, 40, 55) disp:drawLine(7+a, 10, 40, 55)
disp:drawLine(7+a*2, 10, 60, 55) disp:drawLine(7+a*2, 10, 60, 55)
...@@ -90,7 +93,7 @@ function u8g2_line(a) ...@@ -90,7 +93,7 @@ function u8g2_line(a)
disp:drawLine(7+a*4, 10, 100, 55) disp:drawLine(7+a*4, 10, 100, 55)
end end
function u8g2_triangle(a) local function u8g2_triangle(a)
local offset = a local offset = a
disp:drawStr( 0, 0, "drawTriangle") disp:drawStr( 0, 0, "drawTriangle")
disp:drawTriangle(14,7, 45,30, 10,40) disp:drawTriangle(14,7, 45,30, 10,40)
...@@ -99,7 +102,7 @@ function u8g2_triangle(a) ...@@ -99,7 +102,7 @@ function u8g2_triangle(a)
disp:drawTriangle(10+offset,40+offset, 45+offset,30+offset, 86+offset,53+offset) disp:drawTriangle(10+offset,40+offset, 45+offset,30+offset, 86+offset,53+offset)
end end
function u8g2_ascii_1() local function u8g2_ascii_1()
disp:drawStr( 0, 0, "ASCII page 1") disp:drawStr( 0, 0, "ASCII page 1")
for y = 0, 5 do for y = 0, 5 do
for x = 0, 15 do for x = 0, 15 do
...@@ -108,7 +111,7 @@ function u8g2_ascii_1() ...@@ -108,7 +111,7 @@ function u8g2_ascii_1()
end end
end end
function u8g2_ascii_2() local function u8g2_ascii_2()
disp:drawStr( 0, 0, "ASCII page 2") disp:drawStr( 0, 0, "ASCII page 2")
for y = 0, 5 do for y = 0, 5 do
for x = 0, 15 do for x = 0, 15 do
...@@ -117,7 +120,7 @@ function u8g2_ascii_2() ...@@ -117,7 +120,7 @@ function u8g2_ascii_2()
end end
end end
function u8g2_extra_page(a) local function u8g2_extra_page(a)
disp:drawStr( 0, 0, "Unicode") disp:drawStr( 0, 0, "Unicode")
disp:setFont(u8g2.font_unifont_t_symbols) disp:setFont(u8g2.font_unifont_t_symbols)
disp:setFontPosTop() disp:setFontPosTop()
...@@ -129,9 +132,9 @@ function u8g2_extra_page(a) ...@@ -129,9 +132,9 @@ function u8g2_extra_page(a)
end end
end end
cross_width = 24 local cross_width = 24
cross_height = 24 local cross_height = 24
cross_bits = string.char( local cross_bits = string.char(
0x00, 0x18, 0x00, 0x00, 0x24, 0x00, 0x00, 0x24, 0x00, 0x00, 0x42, 0x00, 0x00, 0x18, 0x00, 0x00, 0x24, 0x00, 0x00, 0x24, 0x00, 0x00, 0x42, 0x00,
0x00, 0x42, 0x00, 0x00, 0x42, 0x00, 0x00, 0x81, 0x00, 0x00, 0x81, 0x00, 0x00, 0x42, 0x00, 0x00, 0x42, 0x00, 0x00, 0x81, 0x00, 0x00, 0x81, 0x00,
0xC0, 0x00, 0x03, 0x38, 0x3C, 0x1C, 0x06, 0x42, 0x60, 0x01, 0x42, 0x80, 0xC0, 0x00, 0x03, 0x38, 0x3C, 0x1C, 0x06, 0x42, 0x60, 0x01, 0x42, 0x80,
...@@ -139,24 +142,25 @@ cross_bits = string.char( ...@@ -139,24 +142,25 @@ cross_bits = string.char(
0x00, 0x81, 0x00, 0x00, 0x81, 0x00, 0x00, 0x42, 0x00, 0x00, 0x42, 0x00, 0x00, 0x81, 0x00, 0x00, 0x81, 0x00, 0x00, 0x42, 0x00, 0x00, 0x42, 0x00,
0x00, 0x42, 0x00, 0x00, 0x24, 0x00, 0x00, 0x24, 0x00, 0x00, 0x18, 0x00) 0x00, 0x42, 0x00, 0x00, 0x24, 0x00, 0x00, 0x24, 0x00, 0x00, 0x18, 0x00)
cross_fill_width = 24 -- luacheck: push no unused
cross_fill_height = 24 local cross_fill_width = 24
cross_fill_bits = string.char( local cross_fill_height = 24
local cross_fill_bits = string.char(
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x18, 0x64, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x18, 0x64, 0x00, 0x26,
0x84, 0x00, 0x21, 0x08, 0x81, 0x10, 0x08, 0x42, 0x10, 0x10, 0x3C, 0x08, 0x84, 0x00, 0x21, 0x08, 0x81, 0x10, 0x08, 0x42, 0x10, 0x10, 0x3C, 0x08,
0x20, 0x00, 0x04, 0x40, 0x00, 0x02, 0x80, 0x00, 0x01, 0x80, 0x18, 0x01, 0x20, 0x00, 0x04, 0x40, 0x00, 0x02, 0x80, 0x00, 0x01, 0x80, 0x18, 0x01,
0x80, 0x18, 0x01, 0x80, 0x00, 0x01, 0x40, 0x00, 0x02, 0x20, 0x00, 0x04, 0x80, 0x18, 0x01, 0x80, 0x00, 0x01, 0x40, 0x00, 0x02, 0x20, 0x00, 0x04,
0x10, 0x3C, 0x08, 0x08, 0x42, 0x10, 0x08, 0x81, 0x10, 0x84, 0x00, 0x21, 0x10, 0x3C, 0x08, 0x08, 0x42, 0x10, 0x08, 0x81, 0x10, 0x84, 0x00, 0x21,
0x64, 0x00, 0x26, 0x18, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00) 0x64, 0x00, 0x26, 0x18, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
-- luacheck: pop
cross_block_width = 14 local cross_block_width = 14
cross_block_height = 14 local cross_block_height = 14
cross_block_bits = string.char( local cross_block_bits = string.char(
0xFF, 0x3F, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0xFF, 0x3F, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20,
0xC1, 0x20, 0xC1, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0xC1, 0x20, 0xC1, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20,
0x01, 0x20, 0xFF, 0x3F) 0x01, 0x20, 0xFF, 0x3F)
function u8g2_bitmap_overlay(a) local function u8g2_bitmap_overlay(a)
local frame_size = 28 local frame_size = 28
disp:drawStr(0, 0, "Bitmap overlay") disp:drawStr(0, 0, "Bitmap overlay")
...@@ -177,7 +181,7 @@ function u8g2_bitmap_overlay(a) ...@@ -177,7 +181,7 @@ function u8g2_bitmap_overlay(a)
end end
end end
function u8g2_bitmap_modes(transparent) local function u8g2_bitmap_modes(transparent)
local frame_size = 24 local frame_size = 24
disp:drawBox(0, frame_size * 0.5, frame_size * 5, frame_size) disp:drawBox(0, frame_size * 0.5, frame_size * 5, frame_size)
...@@ -201,7 +205,7 @@ function u8g2_bitmap_modes(transparent) ...@@ -201,7 +205,7 @@ function u8g2_bitmap_modes(transparent)
end end
function draw() local function draw()
u8g2_prepare() u8g2_prepare()
local d3 = bit.rshift(draw_state, 3) local d3 = bit.rshift(draw_state, 3)
...@@ -235,7 +239,7 @@ function draw() ...@@ -235,7 +239,7 @@ function draw()
end end
function loop() local function loop()
-- picture loop -- picture loop
disp:clearBuffer() disp:clearBuffer()
draw() draw()
...@@ -251,13 +255,12 @@ function loop() ...@@ -251,13 +255,12 @@ function loop()
loop_tmr:start() loop_tmr:start()
end end
do
loop_tmr:register(100, tmr.ALARM_SEMI, loop)
draw_state = 0 init_i2c_display()
loop_tmr = tmr.create() --init_spi_display()
loop_tmr:register(100, tmr.ALARM_SEMI, loop)
init_i2c_display()
--init_spi_display()
print("--- Starting Graphics Test ---") print("--- Starting Graphics Test ---")
loop_tmr:start() loop_tmr:start()
end
-- luacheck: globals T r disp millis lcg_rnd
local M, module = {}, ... local M, module = {}, ...
_G[module] = M _G[module] = M
......
-- luacheck: globals T r disp millis lcg_rnd
local M, module = {}, ... local M, module = {}, ...
_G[module] = M _G[module] = M
......
-- luacheck: globals T r disp millis lcg_rnd
local M, module = {}, ... local M, module = {}, ...
_G[module] = M _G[module] = M
...@@ -7,10 +8,7 @@ function M.run() ...@@ -7,10 +8,7 @@ function M.run()
print("Running component color_test...") print("Running component color_test...")
local mx
local c, x local c, x
mx = disp:getWidth() / 2
--my = disp:getHeight() / 2
disp:setColor(0, 0, 0, 0) disp:setColor(0, 0, 0, 0)
disp:drawBox(0, 0, disp:getWidth(), disp:getHeight()) disp:drawBox(0, 0, disp:getWidth(), disp:getHeight())
......
-- luacheck: globals T r disp millis lcg_rnd
local M, module = {}, ... local M, module = {}, ...
_G[module] = M _G[module] = M
......
-- luacheck: globals T r disp millis lcg_rnd
local M, module = {}, ... local M, module = {}, ...
_G[module] = M _G[module] = M
......
-- luacheck: globals T r disp millis lcg_rnd
local M, module = {}, ... local M, module = {}, ...
_G[module] = M _G[module] = M
......
-- luacheck: globals T r disp millis lcg_rnd
local M, module = {}, ... local M, module = {}, ...
_G[module] = M _G[module] = M
......
-- luacheck: globals T r disp millis lcg_rnd
local M, module = {}, ... local M, module = {}, ...
_G[module] = M _G[module] = M
......
-- luacheck: globals T r disp millis lcg_rnd
local M, module = {}, ... local M, module = {}, ...
_G[module] = M _G[module] = M
......
-- luacheck: globals T r disp millis lcg_rnd
local M, module = {}, ... local M, module = {}, ...
_G[module] = M _G[module] = M
......
-- luacheck: new globals z T r disp lcg_rnd millis
z = 127 -- start value
T = 1000
r = 0
disp = nil
local loop_idx = 0
function lcg_rnd()
z = bit.band(65 * z + 17, 255)
return z
end
function millis()
local usec = tmr.now()
return usec/1000
end
-- setup SPI and connect display -- setup SPI and connect display
function init_spi_display() local function init_spi_display()
-- Hardware SPI CLK = GPIO14 -- Hardware SPI CLK = GPIO14
-- Hardware SPI MOSI = GPIO13 -- Hardware SPI MOSI = GPIO13
-- Hardware SPI MISO = GPIO12 (not used) -- Hardware SPI MISO = GPIO12 (not used)
...@@ -20,11 +38,8 @@ function init_spi_display() ...@@ -20,11 +38,8 @@ function init_spi_display()
disp = ucg.st7735_18x128x160_hw_spi(bus, cs, dc, res) disp = ucg.st7735_18x128x160_hw_spi(bus, cs, dc, res)
end end
-- switch statement http://lua-users.org/wiki/SwitchStatement -- switch statement http://lua-users.org/wiki/SwitchStatement
function switch(c) local function switch(c)
local swtbl = { local swtbl = {
casevar = c, casevar = c,
caseof = function (self, code) caseof = function (self, code)
...@@ -46,20 +61,7 @@ function switch(c) ...@@ -46,20 +61,7 @@ function switch(c)
return swtbl return swtbl
end end
local function set_clip_range()
z = 127 -- start value
function lcg_rnd()
z = bit.band(65 * z + 17, 255)
return z
end
function millis()
local usec = tmr.now()
return usec/1000
end
function set_clip_range()
local x, y, w, h local x, y, w, h
w = bit.band(lcg_rnd(), 31) w = bit.band(lcg_rnd(), 31)
h = bit.band(lcg_rnd(), 31) h = bit.band(lcg_rnd(), 31)
...@@ -71,7 +73,7 @@ function set_clip_range() ...@@ -71,7 +73,7 @@ function set_clip_range()
disp:setClipRange(x, y, w, h) disp:setClipRange(x, y, w, h)
end end
function loop() local function loop()
if (loop_idx == 0) then if (loop_idx == 0) then
switch(bit.band(r, 3)) : caseof { switch(bit.band(r, 3)) : caseof {
...@@ -112,18 +114,12 @@ function loop() ...@@ -112,18 +114,12 @@ function loop()
print("Heap: " .. node.heap()) print("Heap: " .. node.heap())
end end
do
init_spi_display()
T = 1000 disp:begin(ucg.FONT_MODE_TRANSPARENT)
disp:setFont(ucg.font_ncenR14_hr)
r = 0 disp:clearScreen()
loop_idx = 0
init_spi_display() tmr.create():alarm(3000, tmr.ALARM_AUTO, function() loop() end)
end
disp:begin(ucg.FONT_MODE_TRANSPARENT)
disp:setFont(ucg.font_ncenR14_hr)
disp:clearScreen()
tmr.register(0, 3000, tmr.ALARM_AUTO, function() loop() end)
tmr.start(0)
local disp
-- setup SPI and connect display -- setup SPI and connect display
function init_spi_display() local function init_spi_display()
-- Hardware SPI CLK = GPIO14 -- Hardware SPI CLK = GPIO14
-- Hardware SPI MOSI = GPIO13 -- Hardware SPI MOSI = GPIO13
-- Hardware SPI MISO = GPIO12 (not used) -- Hardware SPI MISO = GPIO12 (not used)
...@@ -20,17 +22,17 @@ function init_spi_display() ...@@ -20,17 +22,17 @@ function init_spi_display()
disp = ucg.st7735_18x128x160_hw_spi(bus, cs, dc, res) disp = ucg.st7735_18x128x160_hw_spi(bus, cs, dc, res)
end end
do
init_spi_display()
disp:begin(ucg.FONT_MODE_TRANSPARENT)
disp:clearScreen()
init_spi_display() disp:setFont(ucg.font_ncenR12_tr);
disp:setColor(255, 255, 255);
disp:begin(ucg.FONT_MODE_TRANSPARENT) disp:setColor(1, 255, 0,0);
disp:clearScreen()
disp:setFont(ucg.font_ncenR12_tr);
disp:setColor(255, 255, 255);
disp:setColor(1, 255, 0,0);
disp:setPrintPos(0, 25)
disp:setPrintPos(0, 25) disp:print("Hello World!")
disp:print("Hello World!") end
local disp
-- setup SPI and connect display -- setup SPI and connect display
function init_spi_display() local function init_spi_display()
-- Hardware SPI CLK = GPIO14 -- Hardware SPI CLK = GPIO14
-- Hardware SPI MOSI = GPIO13 -- Hardware SPI MOSI = GPIO13
-- Hardware SPI MISO = GPIO12 (not used) -- Hardware SPI MISO = GPIO12 (not used)
...@@ -21,7 +23,7 @@ function init_spi_display() ...@@ -21,7 +23,7 @@ function init_spi_display()
end end
function upper_pin(x, y) local function upper_pin(x, y)
local w = 7 local w = 7
local h = 6 local h = 6
disp:setColor(0, 212, 212, 212) disp:setColor(0, 212, 212, 212)
...@@ -36,7 +38,7 @@ function upper_pin(x, y) ...@@ -36,7 +38,7 @@ function upper_pin(x, y)
disp:drawGradientLine(x+w, y, h, 1) disp:drawGradientLine(x+w, y, h, 1)
end end
function lower_pin(x, y) local function lower_pin(x, y)
local w = 7 local w = 7
local h = 5 local h = 5
disp:setColor(0, 212, 212, 212) disp:setColor(0, 212, 212, 212)
...@@ -56,7 +58,7 @@ function lower_pin(x, y) ...@@ -56,7 +58,7 @@ function lower_pin(x, y)
disp:drawPixel(x+w, y+h) disp:drawPixel(x+w, y+h)
end end
function ic_body(x, y) local function ic_body(x, y)
local w = 4*14+4 local w = 4*14+4
local h = 31 local h = 31
disp:setColor(0, 60, 60, 60) disp:setColor(0, 60, 60, 60)
...@@ -77,7 +79,7 @@ function ic_body(x, y) ...@@ -77,7 +79,7 @@ function ic_body(x, y)
disp:drawDisc(x+w-1, y+h/2+1, 7, bit.bor(ucg.DRAW_UPPER_LEFT, ucg.DRAW_LOWER_LEFT)) disp:drawDisc(x+w-1, y+h/2+1, 7, bit.bor(ucg.DRAW_UPPER_LEFT, ucg.DRAW_LOWER_LEFT))
end end
function draw_ucg_logo() local function draw_ucg_logo()
local a, b local a, b
--ucg_Init(ucg, ucg_sdl_dev_cb, ucg_ext_none, (ucg_com_fnptr)0) --ucg_Init(ucg, ucg_sdl_dev_cb, ucg_ext_none, (ucg_com_fnptr)0)
...@@ -156,12 +158,12 @@ function draw_ucg_logo() ...@@ -156,12 +158,12 @@ function draw_ucg_logo()
--disp:drawString(1, 61, 0, "code.google.com/p/ucglib/") --disp:drawString(1, 61, 0, "code.google.com/p/ucglib/")
end end
do
init_spi_display()
init_spi_display() disp:begin(ucg.FONT_MODE_TRANSPARENT)
disp:clearScreen()
disp:begin(ucg.FONT_MODE_TRANSPARENT)
disp:clearScreen()
disp:setRotate180()
disp:setRotate180() draw_ucg_logo()
draw_ucg_logo() end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment