diff --git a/.travis.yml b/.travis.yml index 7994a09736..6a4f2ae70c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,6 +20,7 @@ addons: packages: - python-serial - srecord + - luarocks cache: - directories: - cache @@ -33,4 +34,5 @@ script: - echo "checking:" - find lua_modules lua_examples -iname "*.lua" -print0 | xargs -0 echo - find lua_modules lua_examples -iname "*.lua" -print0 | xargs -0 $LUACC -p -# - if [ "$OS" = "linux" ]; then bash "$TRAVIS_BUILD_DIR"/tools/travis/run-luacheck.sh || true ; fi +- if [ "$OS" = "linux" ]; then bash "$TRAVIS_BUILD_DIR"/tools/travis/run-luacheck-linux.sh; fi +- if [ "$OS" = "windows" ]; then bash "$TRAVIS_BUILD_DIR"/tools/travis/run-luacheck-windows.sh; fi diff --git a/lua_examples/adc_rgb.lua b/lua_examples/adc_rgb.lua index 6b094ced91..9da9d805df 100644 --- a/lua_examples/adc_rgb.lua +++ b/lua_examples/adc_rgb.lua @@ -1,43 +1,47 @@ -- --- Light sensor on ADC(0), RGB LED connected to gpio12(6) Green, gpio13(7) Blue & gpio15(8) Red. +-- Light sensor on adc0(A0), RGB LED connected to gpio12(D6) Green, gpio13(D7) Blue & gpio15(D8) Red. -- This works out of the box on the typical ESP8266 evaluation boards with Battery Holder -- -- It uses the input from the sensor to drive a "rainbow" effect on the RGB LED -- Includes a very "pseudoSin" function -- +-- Required C Modules: adc, tmr, pwm -function led(r,Sg,b) - pwm.setduty(8,r) - pwm.setduty(6,g) - pwm.setduty(7,b) +local redLed, greenLed, blueLed = 8, 6, 7 + +local function setRGB(r,g,b) + pwm.setduty(redLed, r) + pwm.setduty(greenLed, g) + pwm.setduty(blueLed, b) end --- this is perhaps the lightest weight sin function in existance --- Given an integer from 0..128, 0..512 appximating 256 + 256 * sin(idx*Pi/256) +-- this is perhaps the lightest weight sin function in existence +-- Given an integer from 0..128, 0..512 approximating 256 + 256 * sin(idx*Pi/256) -- This is first order square approximation of sin, it's accurate around 0 and any multiple of 128 (Pi/2), --- 92% accurate at 64 (Pi/4). -function pseudoSin (idx) - idx = idx % 128 - lookUp = 32 - idx % 64 - val = 256 - (lookUp * lookUp) / 4 - if (idx > 64) then - val = - val; - end - return 256+val +-- 92% accurate at 64 (Pi/4). +local function pseudoSin(idx) + idx = idx % 128 + local lookUp = 32 - idx % 64 + local val = 256 - (lookUp * lookUp) / 4 + if (idx > 64) then + val = - val; + end + return 256+val end -pwm.setup(6,500,512) -pwm.setup(7,500,512) -pwm.setup(8,500,512) -pwm.start(6) -pwm.start(7) -pwm.start(8) +do + pwm.setup(redLed, 500, 512) + pwm.setup(greenLed,500, 512) + pwm.setup(blueLed, 500, 512) + pwm.start(redLed) + pwm.start(greenLed) + pwm.start(blueLed) -tmr.alarm(1,20,1,function() - idx = 3 * adc.read(0) / 2 - r = pseudoSin(idx) - g = pseudoSin(idx + 43) - b = pseudoSin(idx + 85) - led(r,g,b) - idx = (idx + 1) % 128 - end) + tmr.create():alarm(20, tmr.ALARM_AUTO, function() + local idx = 3 * adc.read(0) / 2 + local r = pseudoSin(idx) + local g = pseudoSin(idx + 43) -- ~1/3rd of 128 + local b = pseudoSin(idx + 85) -- ~2/3rd of 128 + setRGB(r,g,b) + end) +end diff --git a/lua_examples/dcc/dcc.lua b/lua_examples/dcc/dcc.lua index 922b89d388..33cf1d1701 100644 --- a/lua_examples/dcc/dcc.lua +++ b/lua_examples/dcc/dcc.lua @@ -4,7 +4,7 @@ local PIN = 2 -- GPIO4 local addr = 0x12a -CV = {[29]=0, +local CV = {[29]=0, [1]=bit.band(addr, 0x3f), --CV_ACCESSORY_DECODER_ADDRESS_LSB (6 bits) [9]=bit.band(bit.rshift(addr,6), 0x7) --CV_ACCESSORY_DECODER_ADDRESS_MSB (3 bits) } @@ -37,18 +37,18 @@ end local function DCC_command(cmd, params) if not is_new(cmd, params) then return end - if cmd == dcc.DCC_IDLE then + if cmd == dcc.DCC_IDLE then return elseif cmd == dcc.DCC_TURNOUT then - print("Turnout command") + print("Turnout command") elseif cmd == dcc.DCC_SPEED then - print("Speed command") + print("Speed command") elseif cmd == dcc.DCC_FUNC then - print("Function command") + print("Function command") else print("Other command", cmd) end - + for i,j in pairs(params) do print(i, j) end @@ -69,18 +69,21 @@ local function CV_callback(operation, param) elseif operation == dcc.CV_VALID then oper = "Valid" result = 1 - elseif operation == CV_RESET then + elseif operation == dcc.CV_RESET then oper = "Reset" CV = {} end - print(("[CV_callback] %s CV %d%s"):format(oper, param.CV, param.Value and "\tValue: "..param.Value or "\tValue: nil")) + print(("[CV_callback] %s CV %d%s") + :format(oper, param.CV, param.Value and "\tValue: "..param.Value or "\tValue: nil")) return result end dcc.setup(PIN, DCC_command, - dcc.MAN_ID_DIY, 1, - --bit.bor(dcc.FLAGS_AUTO_FACTORY_DEFAULT, dcc.FLAGS_DCC_ACCESSORY_DECODER, dcc.FLAGS_MY_ADDRESS_ONLY), -- Accessories (turnouts) decoder - bit.bor(dcc.FLAGS_AUTO_FACTORY_DEFAULT), -- Cab (train) decoder + dcc.MAN_ID_DIY, 1, + -- Accessories (turnouts) decoder: + --bit.bor(dcc.FLAGS_AUTO_FACTORY_DEFAULT, dcc.FLAGS_DCC_ACCESSORY_DECODER, dcc.FLAGS_MY_ADDRESS_ONLY), + -- Cab (train) decoder + bit.bor(dcc.FLAGS_AUTO_FACTORY_DEFAULT), 0, -- ??? CV_callback) diff --git a/lua_examples/email/read_email_imap.lua b/lua_examples/email/read_email_imap.lua index 0729b66fce..97fa7b77b0 100644 --- a/lua_examples/email/read_email_imap.lua +++ b/lua_examples/email/read_email_imap.lua @@ -6,7 +6,7 @@ -- was tested with an AOL and Time Warner cable email accounts (GMail and other services who do -- not support no SSL access will not work). -require("imap") +local imap = require("imap") local IMAP_USERNAME = "email@domain.com" local IMAP_PASSWORD = "password" @@ -25,21 +25,13 @@ local SSID_PASSWORD = "password" local count = 0 -- we will send several IMAP commands/requests, this variable helps keep track of which one to send - --- configure the ESP8266 as a station -wifi.setmode(wifi.STATION) -wifi.sta.config(SSID,SSID_PASSWORD) -wifi.sta.autoconnect(1) - --- create an unencrypted connection -local imap_socket = net.createConnection(net.TCP,0) - +local imap_socket, timer --- -- @name setup -- @description A call back function used to begin reading email -- upon sucessfull connection to the IMAP server -function setup(sck) +local function setup(sck) -- Set the email user name and password, IMAP tag, and if debugging output is needed imap.config(IMAP_USERNAME, IMAP_PASSWORD, @@ -49,19 +41,16 @@ function setup(sck) imap.login(sck) end -imap_socket:on("connection",setup) -- call setup() upon connection -imap_socket:connect(IMAP_PORT,IMAP_SERVER) -- connect to the IMAP server - local subject = "" local from = "" -local message = "" +local body = "" --- -- @name do_next -- @description A call back function for a timer alarm used to check if the previous -- IMAP command reply has been processed. If the IMAP reply has been processed -- this function will call the next IMAP command function necessary to read the email -function do_next() +local function do_next() -- Check if the IMAP reply was processed if(imap.response_processed() == true) then @@ -75,15 +64,18 @@ function do_next() count = count + 1 elseif (count == 1) then -- After examining/selecting the INBOX folder we can begin to retrieve emails. - imap.fetch_header(imap_socket,imap.get_most_recent_num(),"SUBJECT") -- Retrieve the SUBJECT of the first/newest email + -- Retrieve the SUBJECT of the first/newest email + imap.fetch_header(imap_socket,imap.get_most_recent_num(),"SUBJECT") count = count + 1 elseif (count == 2) then subject = imap.get_header() -- store the SUBJECT response in subject - imap.fetch_header(imap_socket,imap.get_most_recent_num(),"FROM") -- Retrieve the FROM of the first/newest email + -- Retrieve the FROM of the first/newest email + imap.fetch_header(imap_socket,imap.get_most_recent_num(),"FROM") count = count + 1 elseif (count == 3) then from = imap.get_header() -- store the FROM response in from - imap.fetch_body_plain_text(imap_socket,imap.get_most_recent_num()) -- Retrieve the BODY of the first/newest email + -- Retrieve the BODY of the first/newest email + imap.fetch_body_plain_text(imap_socket,imap.get_most_recent_num()) count = count + 1 elseif (count == 4) then body = imap.get_body() -- store the BODY response in body @@ -92,9 +84,9 @@ function do_next() else -- display the email contents - -- create patterns to strip away IMAP protocl text from actual message - pattern1 = "(\*.+\}\r\n)" -- to remove "* n command (BODY[n] {n}" - pattern2 = "(%)\r\n.+)" -- to remove ") t1 OK command completed" + -- create patterns to strip away IMAP protocol text from actual message + local pattern1 = "%*.*}\n" -- to remove "* n command (BODY[n] {n}" + local pattern2 = "%)\n.+" -- to remove ") t1 OK command completed" from = string.gsub(from,pattern1,"") from = string.gsub(from,pattern2,"") @@ -108,7 +100,7 @@ function do_next() body = string.gsub(body,pattern2,"") print("Message: " .. body) - tmr.stop(0) -- Stop the timer alarm + timer:stop() -- Stop the timer alarm imap_socket:close() -- close the IMAP socket collectgarbage() -- clean up end @@ -116,5 +108,18 @@ function do_next() end --- A timer alarm is sued to check if an IMAP reply has been processed -tmr.alarm(0,1000,1, do_next) +do + -- configure the ESP8266 as a station + wifi.setmode(wifi.STATION) + wifi.sta.config(SSID,SSID_PASSWORD) + wifi.sta.autoconnect(1) + + -- create an unencrypted connection + imap_socket = net.createConnection(net.TCP,0) + imap_socket:on("connection",setup) -- call setup() upon connection + imap_socket:connect(IMAP_PORT,IMAP_SERVER) -- connect to the IMAP server + + -- A timer alarm is sued to check if an IMAP reply has been processed + timer = tmr.create() + timer:alarm(1000, tmr.ALARM_AUTO, do_next) +end diff --git a/lua_examples/email/send_email_smtp.lua b/lua_examples/email/send_email_smtp.lua index 4ba6585e53..b11e1928a9 100644 --- a/lua_examples/email/send_email_smtp.lua +++ b/lua_examples/email/send_email_smtp.lua @@ -1,16 +1,17 @@ --- -- Working Example: https://www.youtube.com/watch?v=CcRbFIJ8aeU --- @description a basic SMTP email example. You must use an account which can provide unencrypted authenticated access. --- This example was tested with an AOL and Time Warner email accounts. GMail does not offer unecrypted authenticated access. +-- @description a basic SMTP email example. You must use an account which can +-- provide unencrypted authenticated access. +-- This example was tested with an AOL and Time Warner email accounts. +-- GMail does not offer unencrypted authenticated access. -- To obtain your email's SMTP server and port simply Google it e.g. [my email domain] SMTP settings --- For example for timewarner you'll get to this page http://www.timewarnercable.com/en/support/faqs/faqs-internet/e-mailacco/incoming-outgoing-server-addresses.html +-- For example for timewarner you'll get to this page +-- http://www.timewarnercable.com/en/support/faqs/faqs-internet/e-mailacco/incoming-outgoing-server-addresses.html -- To Learn more about SMTP email visit: -- SMTP Commands Reference - http://www.samlogic.net/articles/smtp-commands-reference.htm -- See "SMTP transport example" in this page http://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol -- @author Miguel -require("base64") - -- The email and password from the account you want to send emails from local MY_EMAIL = "esp8266@domain.com" local EMAIL_PASSWORD = "123456" @@ -37,18 +38,18 @@ wifi.sta.autoconnect(1) local email_subject = "" local email_body = "" local count = 0 - +local timer local smtp_socket = nil -- will be used as socket to email server -- The display() function will be used to print the SMTP server's response -function display(sck,response) +local function display(sck, response) -- luacheck: no unused print(response) end -- The do_next() function is used to send the SMTP commands to the SMTP server in the required sequence. -- I was going to use socket callbacks but the code would not run callbacks after the first 3. -function do_next() +local function do_next() if(count == 0)then count = count+1 local IP_ADDRESS = wifi.sta.getip() @@ -58,10 +59,10 @@ function do_next() smtp_socket:send("AUTH LOGIN\r\n") elseif(count == 2) then count = count + 1 - smtp_socket:send(base64.enc(MY_EMAIL).."\r\n") + smtp_socket:send(encoder.toBase64(MY_EMAIL).."\r\n") elseif(count == 3) then count = count + 1 - smtp_socket:send(base64.enc(EMAIL_PASSWORD).."\r\n") + smtp_socket:send(encoder.toBase64(EMAIL_PASSWORD).."\r\n") elseif(count==4) then count = count+1 smtp_socket:send("MAIL FROM:<" .. MY_EMAIL .. ">\r\n") @@ -82,26 +83,27 @@ function do_next() smtp_socket:send(message.."\r\n.\r\n") elseif(count==8) then count = count+1 - tmr.stop(0) + timer:stop() smtp_socket:send("QUIT\r\n") else smtp_socket:close() end end --- The connectted() function is executed when the SMTP socket is connected to the SMTP server. +-- The connected() function is executed when the SMTP socket is connected to the SMTP server. -- This function will create a timer to call the do_next function which will send the SMTP commands -- in sequence, one by one, every 5000 seconds. -- You can change the time to be smaller if that works for you, I used 5000ms just because. -function connected(sck) - tmr.alarm(0,5000,1,do_next) +local function connected() + timer = tmr.create() + timer:alarm(5000, tmr.ALARM_AUTO, do_next) end -- @name send_email -- @description Will initiated a socket connection to the SMTP server and trigger the connected() function -- @param subject The email's subject -- @param body The email's body -function send_email(subject,body) +local function send_email(subject,body) count = 0 email_subject = subject email_body = body @@ -111,19 +113,13 @@ function send_email(subject,body) smtp_socket:connect(SMTP_PORT,SMTP_SERVER) end --- Send an email -send_email( - "ESP8266", -[[Hi, -How are your IoT projects coming along? -Best Wishes, -ESP8266]]) - - - - - - - - - +do + -- Send an email + send_email( + "ESP8266", + [[Hi, + How are your IoT projects coming along? + Best Wishes, + ESP8266]] + ) +end diff --git a/lua_examples/irsend.lua b/lua_examples/irsend.lua index ed1cb98bb1..dd3aec40e1 100644 --- a/lua_examples/irsend.lua +++ b/lua_examples/irsend.lua @@ -10,6 +10,7 @@ local M do -- const + -- luacheck: push no unused local NEC_PULSE_US = 1000000 / 38000 local NEC_HDR_MARK = 9000 local NEC_HDR_SPACE = 4500 @@ -17,6 +18,7 @@ do local NEC_ONE_SPACE = 1600 local NEC_ZERO_SPACE = 560 local NEC_RPT_SPACE = 2250 + -- luacheck: pop -- cache local gpio, bit = gpio, bit local mode, write = gpio.mode, gpio.write diff --git a/lua_examples/lfs/HTTP_OTA.lua b/lua_examples/lfs/HTTP_OTA.lua index 42472de5ae..513771b38e 100644 --- a/lua_examples/lfs/HTTP_OTA.lua +++ b/lua_examples/lfs/HTTP_OTA.lua @@ -10,7 +10,7 @@ local host, dir, image = ... local doRequest, firstRec, subsRec, finalise local n, total, size = 0, 0 -doRequest = function(sk,hostIP) +doRequest = function(socket, hostIP) -- luacheck: no unused if hostIP then local con = net.createConnection(net.TCP,0) con:connect(80,hostIP) diff --git a/lua_examples/lfs/_init.lua b/lua_examples/lfs/_init.lua index 0060811ed2..d3ad4fa563 100644 --- a/lua_examples/lfs/_init.lua +++ b/lua_examples/lfs/_init.lua @@ -46,7 +46,7 @@ local lfs_t = { end end, - __newindex = function(_, name, value) + __newindex = function(_, name, value) -- luacheck: no unused error("LFS is readonly. Invalid write to LFS." .. name, 2) end, diff --git a/lua_examples/lfs/dummy_strings.lua b/lua_examples/lfs/dummy_strings.lua index cee2eaaf54..f644d73424 100644 --- a/lua_examples/lfs/dummy_strings.lua +++ b/lua_examples/lfs/dummy_strings.lua @@ -24,8 +24,10 @@ end This will exclude any strings already in the ROM table, so the output is the list of putative strings that you should consider adding to LFS ROM table. ----------------------------------------------------------------------------------]] +--------------------------------------------------------------------------------- +]]-- +-- luacheck: ignore local preload = "?.lc;?.lua", "/\n;\n?\n!\n-", "@init.lua", "_G", "_LOADED", "_LOADLIB", "__add", "__call", "__concat", "__div", "__eq", "__gc", "__index", "__le", "__len", "__lt", "__mod", "__mode", "__mul", "__newindex", "__pow", diff --git a/lua_examples/lfs/lfs_fragments.lua b/lua_examples/lfs/lfs_fragments.lua index c5e53e06e4..47ccb8342e 100644 --- a/lua_examples/lfs/lfs_fragments.lua +++ b/lua_examples/lfs/lfs_fragments.lua @@ -5,8 +5,8 @@ -- then enter the following commands interactively through the UART: -- do - local _,ma,fa=node.flashindex() - for n,v in pairs{LFS_MAPPED=ma, LFS_BASE=fa, SPIFFS_BASE=sa} do + local sa, ma, fa = node.flashindex() + for n,v in pairs{LFS_MAPPED = ma, LFS_BASE = fa, SPIFFS_BASE = sa} do print(('export %s=""0x%x"'):format(n, v)) end end @@ -60,7 +60,5 @@ local initTimer = tmr.create() initTimer:register(1000, tmr.ALARM_SINGLE, function() local fi=node.flashindex; return pcall(fi and fi'_init') - end - ) + end) initTimer:start() - diff --git a/lua_examples/luaOTA/_doTick.lua b/lua_examples/luaOTA/_doTick.lua index e8a7238f8c..979cb00346 100644 --- a/lua_examples/luaOTA/_doTick.lua +++ b/lua_examples/luaOTA/_doTick.lua @@ -1,3 +1,4 @@ +-- luacheck: globals self if (self.timer) then self.timer:stop() end--SAFETRIM -- function _doTick(self) diff --git a/lua_examples/luaOTA/_provision.lua b/lua_examples/luaOTA/_provision.lua index 3b69fcae38..7eaceb2796 100644 --- a/lua_examples/luaOTA/_provision.lua +++ b/lua_examples/luaOTA/_provision.lua @@ -2,7 +2,7 @@ -- function _provision(self,socket,first_rec) local self, socket, first_rec = ... -local crypto, file, json, node, table = crypto, file, sjson, node, table +local crypto, file, json, node, table = crypto, file, sjson, node, table local stripdebug, gc = node.stripdebug, collectgarbage local buf = {} @@ -13,11 +13,10 @@ local function getbuf() -- upval: buf, table end -- Process a provisioning request record -local function receiveRec(socket, rec) -- upval: self, buf, crypto - -- Note that for 2nd and subsequent responses, we assme that the service has - -- "authenticated" itself, so any protocol errors are fatal and lkely to +local function receiveRec(sck, rec) -- upval: self, buf, crypto + -- Note that for 2nd and subsequent responses, we assume that the service has + -- "authenticated" itself, so any protocol errors are fatal and likely to -- cause a repeating boot, throw any protocol errors are thrown. - local config, file, log = self.config, file, self.log local cmdlen = (rec:find('\n',1, true) or 0) - 1 local cmd,hash = rec:sub(1,cmdlen-6), rec:sub(cmdlen-5,cmdlen) if cmdlen < 16 or @@ -25,7 +24,9 @@ local function receiveRec(socket, rec) -- upval: self, buf, crypto return error("Invalid command signature") end - local s; s, cmd = pcall(json.decode, cmd) + local s + s, cmd = pcall(json.decode, cmd) + if not s then error("JSON decode error") end local action,resp = cmd.a, {s = "OK"} local chunk @@ -59,15 +60,15 @@ local function receiveRec(socket, rec) -- upval: self, buf, crypto if not msg then gc(); gc() local code, name = string.dump(lcf), cmd.name:sub(1,-5) .. ".lc" - local s = file.open(name, "w+") - if s then + local f = file.open(name, "w+") + if f then for i = 1, #code, 1024 do - s = s and file.write(code:sub(i, ((i+1023)>#code) and i+1023 or #code)) + f = f and file.write(code:sub(i, ((i+1023)>#code) and i+1023 or #code)) end file.close() - if not s then file.remove(name) end + if not f then file.remove(name) end end - if s then + if f then resp.lcsize=#code print("Updated ".. name) else @@ -80,15 +81,15 @@ local function receiveRec(socket, rec) -- upval: self, buf, crypto buf = {} elseif action == "dl" then - local s = file.open(cmd.name, "w+") - if s then + local dlFile = file.open(cmd.name, "w+") + if dlFile then for i = 1, #buf do - s = s and file.write(buf[i]) + dlFile = dlFile and file.write(buf[i]) end file.close() end - if s then + if dlFile then print("Updated ".. cmd.name) else file.remove(cmd.name) @@ -109,13 +110,13 @@ local function receiveRec(socket, rec) -- upval: self, buf, crypto file.open(self.prefix.."config.json", "w+") file.writeline(json.encode(cmd)) file.close() - socket:close() + sck:close() print("Restarting to load new application") node.restart() -- reboot just schedules a restart return end end - self.socket_send(socket, resp, chunk) + self.socket_send(sck, resp, chunk) gc() end diff --git a/lua_examples/luaOTA/check.lua b/lua_examples/luaOTA/check.lua index 46dcd27310..e869adf388 100644 --- a/lua_examples/luaOTA/check.lua +++ b/lua_examples/luaOTA/check.lua @@ -9,14 +9,13 @@ -------------------------------------------------------------------------------- -- upvals -local crypto, file, json, net, node, table, wifi = - crypto, file, sjson, net, node, table, wifi -local error, pcall = error, pcall +local crypto, json, node, wifi = + crypto, sjson, node, wifi +local error = error local loadfile, gc = loadfile, collectgarbage -local concat, unpack = table.concat, unpack or table.unpack local self = {post = node.task.post, prefix = "luaOTA/", conf = {}} - +-- luacheck: globals DEBUG self.log = (DEBUG == true) and print or function() end self.modname = ... self.timer = tmr.create() @@ -27,15 +26,15 @@ end -------------------------------------------------------------------------------------- -- Utility Functions -setmetatable( self, {__index=function(self, func) --upval: loadfile +setmetatable( self, {__index=function(obj, func) --upval: loadfile -- The only __index calls in in LuaOTA are dynamically loaded functions. -- The convention is that functions starting with "_" are treated as -- call-once / ephemeral; the rest are registered in self - func = self.prefix .. func + func = obj.prefix .. func local f,msg = loadfile( func..".lc") if msg then f, msg = loadfile(func..".lua") end if msg then error (msg,2) end - if func:sub(8,8) ~= "_" then self[func] = f end + if func:sub(8,8) ~= "_" then obj[func] = f end return f end} ) diff --git a/lua_examples/luaOTA/default.lua b/lua_examples/luaOTA/default.lua index bfbaad7901..fa059e98a1 100644 --- a/lua_examples/luaOTA/default.lua +++ b/lua_examples/luaOTA/default.lua @@ -1,5 +1,4 @@ --- -local function enum(t,log) for k,v in pairs(t)do log(k,v) end end +-- luacheck: globals DEBUG return {entry = function(msg) package.loaded["luaOTA.default"]=nil local gc=collectgarbage; gc(); gc() diff --git a/lua_examples/luaOTA/luaOTAserver.lua b/lua_examples/luaOTA/luaOTAserver.lua index b1e07a9fe1..5356e29501 100644 --- a/lua_examples/luaOTA/luaOTAserver.lua +++ b/lua_examples/luaOTA/luaOTAserver.lua @@ -17,6 +17,8 @@ ]] +-- luacheck: std max + local socket = require "socket" local lfs = require "lfs" local md5 = require "md5" @@ -31,7 +33,6 @@ local receive_and_parse -- function(esp) local provision -- function(esp, config, files, inventory, fingerprint) local read_file -- function(fname) local save_file -- function(fname, data) -local compress_lua -- function(lua_file) local hmac -- function(data) -- Function-wide locals (can be upvalues) @@ -164,6 +165,7 @@ receive_and_parse = function(esp) local packed_cmd, sig = line:sub(1,#line-6),line:sub(-6) -- print("reply:", packed_cmd, sig) local status, cmd = pcall(json.decode, packed_cmd) + if not status then error("JSON decode error") end if not hmac or hmac(packed_cmd):sub(-6) == sig then if cmd and cmd.data == "number" then local data = esp:receive(cmd.data) @@ -183,7 +185,7 @@ provision = function(esp, config, inventory, fingerprint) local name, size, mtime, content = f.name, f.size, f.mtime, f.content if not cf[name] or cf[name] ~= mtime then -- Send the file - local func, action, cmd, buf + local action, cmd, buf if f.name:sub(-4) == ".lua" then assert(load(content, f.name)) -- check that the contents can compile if content:find("--SAFETRIM\n",1,true) then @@ -241,12 +243,11 @@ end -- Save contents to the given file ---------------------------------- -save_file = function(fname, data) +save_file = function(fname, data) -- luacheck: ignore local file = io.open(fname, "wb") file:write(data) file:close() end - -------------------------------------------------------------------------------------- main() -- now that all functions have been bound to locals, we can start the show :-) diff --git a/lua_examples/make_phone_call.lua b/lua_examples/make_phone_call.lua index 27058b6bd7..d5501a0e74 100644 --- a/lua_examples/make_phone_call.lua +++ b/lua_examples/make_phone_call.lua @@ -29,10 +29,12 @@ local TWILIO_ACCOUNT_SID = "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 - -- 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/Calls.json" -function build_post_request(host, uri, data_table) +local wifiTimer = tmr.create() + +local function build_post_request(host, uri, data_table) local data = "" @@ -40,7 +42,7 @@ function build_post_request(host, uri, data_table) data = data .. param.."="..value.."&" end - request = "POST "..uri.." HTTP/1.1\r\n".. + local request = "POST "..uri.." HTTP/1.1\r\n".. "Host: "..host.."\r\n".. "Connection: close\r\n".. "Content-Type: application/x-www-form-urlencoded\r\n".. @@ -53,7 +55,7 @@ function build_post_request(host, uri, data_table) return request end -local function display(sck,response) +local function display(socket, response) -- luacheck: no unused print(response) end @@ -69,7 +71,7 @@ local function make_call(from,to,body) To = to } - socket = net.createConnection(net.TCP,0) + local socket = net.createConnection(net.TCP,0) socket:on("receive",display) socket:connect(80,HOST) @@ -80,13 +82,13 @@ local function make_call(from,to,body) end) end -function check_wifi() +local function check_wifi() local ip = wifi.sta.getip() if(ip==nil) then print("Connecting...") else - tmr.stop(0) + wifiTimer:stop() print("Connected to AP!") print(ip) -- make a call with a voice message "your house is on fire" @@ -95,4 +97,4 @@ function check_wifi() end -tmr.alarm(0,2000,1,check_wifi) +wifiTimer:alarm(2000, tmr.ALARM_AUTO, check_wifi) diff --git a/lua_examples/mcp23008/mcp23008_buttons.lua b/lua_examples/mcp23008/mcp23008_buttons.lua index 1a71c15567..585a4fa696 100644 --- a/lua_examples/mcp23008/mcp23008_buttons.lua +++ b/lua_examples/mcp23008/mcp23008_buttons.lua @@ -14,23 +14,17 @@ -- Website: http://AllAboutEE.com --------------------------------------------------------------------------------------------- -require ("mcp23008") +local mcp23008 = require ("mcp23008") -- ESP-01 GPIO Mapping as per GPIO Table in https://github.com/nodemcu/nodemcu-firmware -gpio0, gpio2 = 3, 4 - --- Setup the MCP23008 -mcp23008.begin(0x0,gpio2,gpio0,i2c.SLOW) - -mcp23008.writeIODIR(0xff) -mcp23008.writeGPPU(0xff) +local gpio0, gpio2 = 3, 4 --- -- @name showButtons -- @description Shows the state of each GPIO pin -- @return void --------------------------------------------------------- -function showButtons() +local function showButtons() local gpio = mcp23008.readGPIO() -- read the GPIO/buttons states @@ -51,7 +45,13 @@ function showButtons() print("\r\n") end -tmr.alarm(0,2000,1,showButtons) -- run showButtons() every 2 seconds +do + -- Setup the MCP23008 + mcp23008.begin(0x0,gpio2,gpio0,i2c.SLOW) + mcp23008.writeIODIR(0xff) + mcp23008.writeGPPU(0xff) + tmr.create():alarm(2000, tmr.ALARM_AUTO, showButtons) -- run showButtons() every 2 seconds +end diff --git a/lua_examples/mcp23008/mcp23008_leds.lua b/lua_examples/mcp23008/mcp23008_leds.lua index 4f9b6463a0..e1e0633110 100644 --- a/lua_examples/mcp23008/mcp23008_leds.lua +++ b/lua_examples/mcp23008/mcp23008_leds.lua @@ -13,26 +13,17 @@ -- Website: http://AllAboutEE.com --------------------------------------------------------------------------------------------- -require ("mcp23008") +local mcp23008 = require ("mcp23008") -- ESP-01 GPIO Mapping as per GPIO Table in https://github.com/nodemcu/nodemcu-firmware -gpio0, gpio2 = 3, 4 - --- Setup MCP23008 -mcp23008.begin(0x0,gpio2,gpio0,i2c.SLOW) - -mcp23008.writeIODIR(0x00) -- make all GPIO pins as outputs -mcp23008.writeGPIO(0x00) -- make all GIPO pins off/low +local gpio0, gpio2 = 3, 4 --- -- @name count() -- @description Reads the value from the GPIO register, increases the read value by 1 -- and writes it back so the LEDs will display a binary count up to 255 or 0xFF in hex. local function count() - - local gpio = 0x00 - - gpio = mcp23008.readGPIO() + local gpio = mcp23008.readGPIO() if(gpio<0xff) then mcp23008.writeGPIO(gpio+1) @@ -41,5 +32,15 @@ local function count() end end --- Run count() every 100ms -tmr.alarm(0,100,1,count) + +do + -- Setup MCP23008 + mcp23008.begin(0x0,gpio2,gpio0,i2c.SLOW) + + mcp23008.writeIODIR(0x00) -- make all GPIO pins as outputs + mcp23008.writeGPIO(0x00) -- make all GIPO pins off/low + + -- Run count() every 100ms + tmr.create():alarm(100, tmr.ALARM_AUTO, count) +end + diff --git a/lua_examples/mqtt/mqtt2cloud.lua b/lua_examples/mqtt/mqtt2cloud.lua index a64e970a6d..753601d30f 100644 --- a/lua_examples/mqtt/mqtt2cloud.lua +++ b/lua_examples/mqtt/mqtt2cloud.lua @@ -1,33 +1,41 @@ -- test with cloudmqtt.com -m_dis={} -function dispatch(m,t,pl) - if pl~=nil and m_dis[t] then - m_dis[t](m,pl) - end +local m_dis = {} + +local function dispatch(m,t,pl) + if pl~=nil and m_dis[t] then + m_dis[t](m,pl) + end end -function topic1func(m,pl) - print("get1: "..pl) + +local function topic1func(_,pl) + print("get1: "..pl) end -function topic2func(m,pl) - print("get2: "..pl) + +local function topic2func(_,pl) + print("get2: "..pl) end -m_dis["/topic1"]=topic1func -m_dis["/topic2"]=topic2func --- Lua: mqtt.Client(clientid, keepalive, user, pass) -m=mqtt.Client("nodemcu1",60,"test","test123") -m:on("connect",function(m) - print("connection "..node.heap()) - m:subscribe("/topic1",0,function(m) print("sub done") end) - m:subscribe("/topic2",0,function(m) print("sub done") end) - m:publish("/topic1","hello",0,0) m:publish("/topic2","world",0,0) - end ) -m:on("offline", function(conn) + +do + m_dis["/topic1"] = topic1func + m_dis["/topic2"] = topic2func + -- Lua: mqtt.Client(clientid, keepalive, user, pass) + local m = mqtt.Client("nodemcu1", 60, "test", "test123") + m:on("connect",function(client) + print("connection "..node.heap()) + client:subscribe("/topic1",0,function() print("sub done") end) + client:subscribe("/topic2",0,function() print("sub done") end) + client:publish("/topic1","hello",0,0) + client:publish("/topic2","world",0,0) + end) + m:on("offline", function() print("disconnect to broker...") print(node.heap()) -end) -m:on("message",dispatch ) --- Lua: mqtt:connect( host, port, secure, auto_reconnect, function(client) ) -m:connect("m11.cloudmqtt.com",11214,0,1) -tmr.alarm(0,10000,1,function() local pl = "time: "..tmr.time() - m:publish("/topic1",pl,0,0) - end) + end) + m:on("message",dispatch ) + -- Lua: mqtt:connect( host, port, secure, function(client) ) + m:connect("m11.cloudmqtt.com",11214,0) + tmr.create():alarm(10000, tmr.ALARM_AUTO, function() + local pl = "time: "..tmr.time() + m:publish("/topic1",pl,0,0) + end) +end \ No newline at end of file diff --git a/lua_examples/mqtt/mqtt_file.lua b/lua_examples/mqtt/mqtt_file.lua index d528585078..ed5d71d920 100644 --- a/lua_examples/mqtt/mqtt_file.lua +++ b/lua_examples/mqtt/mqtt_file.lua @@ -1,22 +1,23 @@ -- test transfer files over mqtt. -m_dis={} -function dispatch(m,t,pl) - if pl~=nil and m_dis[t] then +local m_dis = {} + +local function dispatch(m, t, pl) + if pl ~= nil and m_dis[t] then m_dis[t](m,pl) end end -function pubfile(m,filename) +local function pubfile(m,filename) file.close() file.open(filename) repeat - local pl=file.read(1024) - if pl then m:publish("/topic2",pl,0,0) end - until not pl - file.close() + local pl = file.read(1024) + if pl then m:publish("/topic2", pl, 0, 0) end + until not pl + file.close() end -- payload(json): {"cmd":xxx,"content":xxx} -function topic1func(m,pl) +local function topic1func(m,pl) print("get1: "..pl) local pack = sjson.decode(pl) if pack.content then @@ -30,21 +31,22 @@ function topic1func(m,pl) end end -m_dis["/topic1"]=topic1func --- Lua: mqtt.Client(clientid, keepalive, user, pass) -m=mqtt.Client() -m:on("connect",function(m) - print("connection "..node.heap()) - m:subscribe("/topic1",0,function(m) print("sub done") end) - end ) -m:on("offline", function(conn) - print("disconnect to broker...") - print(node.heap()) -end) -m:on("message",dispatch ) --- Lua: mqtt:connect( host, port, secure, auto_reconnect, function(client) ) -m:connect("192.168.18.88",1883,0,1) - +do + m_dis["/topic1"]=topic1func + -- Lua: mqtt.Client(clientid, keepalive, user, pass) + local m = mqtt.Client() + m:on("connect",function(client) + print("connection "..node.heap()) + client:subscribe("/topic1", 0, function() print("sub done") end) + end) + m:on("offline", function() + print("disconnect to broker...") + print(node.heap()) + end) + m:on("message",dispatch ) + -- Lua: mqtt:connect( host, port, secure, function(client) ) + m:connect("192.168.18.88",1883,0) +end -- usage: -- another client(pc) subscribe to /topic2, will receive the test.lua content. -- and publish below message to /topic1 diff --git a/lua_examples/pcm/play_file.lua b/lua_examples/pcm/play_file.lua index d3ceefdc33..3a5f859465 100644 --- a/lua_examples/pcm/play_file.lua +++ b/lua_examples/pcm/play_file.lua @@ -6,7 +6,7 @@ -- **************************************************************************** -function cb_drained(d) +local function cb_drained() print("drained "..node.heap()) file.seek("set", 0) @@ -14,27 +14,29 @@ function cb_drained(d) --d:play(pcm.RATE_8K) end -function cb_stopped(d) +local function cb_stopped() print("playback stopped") file.seek("set", 0) end -function cb_paused(d) +local function cb_paused() print("playback paused") 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 -drv:on("data", function(drv) return file.read() end) + -- fetch data in chunks of FILE_READ_CHUNK (1024) from file + drv:on("data", function(driver) return file.read() end) -- luacheck: no unused --- get called back when all samples were read from the file -drv:on("drained", cb_drained) + -- get called back when all samples were read from the file + drv:on("drained", cb_drained) -drv:on("stopped", cb_stopped) -drv:on("paused", cb_paused) + drv:on("stopped", cb_stopped) + drv:on("paused", cb_paused) --- start playback -drv:play(pcm.RATE_8K) + -- start playback + drv:play(pcm.RATE_8K) +end diff --git a/lua_examples/send_text_message.lua b/lua_examples/send_text_message.lua index c454e0ca23..45d8b8d50d 100644 --- a/lua_examples/send_text_message.lua +++ b/lua_examples/send_text_message.lua @@ -29,10 +29,12 @@ local TWILIO_ACCOUNT_SID = "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 - -- 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" -function build_post_request(host, uri, data_table) +local wifiTimer = tmr.create() + +local function build_post_request(host, uri, data_table) local data = "" @@ -40,7 +42,7 @@ function build_post_request(host, uri, data_table) data = data .. param.."="..value.."&" end - request = "POST "..uri.." HTTP/1.1\r\n".. + local request = "POST "..uri.." HTTP/1.1\r\n".. "Host: "..host.."\r\n".. "Connection: close\r\n".. "Content-Type: application/x-www-form-urlencoded\r\n".. @@ -53,7 +55,7 @@ function build_post_request(host, uri, data_table) return request end -local function display(sck,response) +local function display(socket, response) -- luacheck: no unused print(response) end @@ -69,7 +71,7 @@ local function send_sms(from,to,body) To = to } - socket = net.createConnection(net.TCP,0) + local socket = net.createConnection(net.TCP,0) socket:on("receive",display) socket:connect(80,HOST) @@ -80,13 +82,13 @@ local function send_sms(from,to,body) end) end -function check_wifi() +local function check_wifi() local ip = wifi.sta.getip() if(ip==nil) then print("Connecting...") else - tmr.stop(0) + wifiTimer.stop() print("Connected to AP!") print(ip) -- send a text message with the text "Hello from your esp8266" @@ -95,4 +97,4 @@ function check_wifi() end -tmr.alarm(0,7000,1,check_wifi) +wifiTimer.alarm(7000, tmr.ALARM_AUTO, check_wifi) diff --git a/lua_examples/sjson-streaming.lua b/lua_examples/sjson-streaming.lua index d7981c5f05..c92d2a73ae 100644 --- a/lua_examples/sjson-streaming.lua +++ b/lua_examples/sjson-streaming.lua @@ -1,11 +1,18 @@ -- Test sjson and GitHub API local s = tls.createConnection() -s:on("connection", function(sck, c) - 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") +s:on("connection", function(sck) + 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) -function startswith(String, Start) +local function startswith(String, Start) return string.sub(String, 1, string.len(Start)) == Start end @@ -23,13 +30,13 @@ local decoder = sjson.decoder({ end } }) -local function handledata(s) - decoder:write(s) +local function handledata(sck) + decoder:write(sck) end -- 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 -s:on("receive", function(sck, c) +s:on("receive", function(socket, c) -- luacheck: no unused if partial then c = partial .. c partial = nil @@ -45,8 +52,8 @@ s:on("receive", function(sck, c) handledata(c) return end - local s, e = c:find("\r\n") - if s then + local str, e = c:find("\r\n") + if str then -- Throw away line c = c:sub(e + 1) else diff --git a/lua_examples/somfy.lua b/lua_examples/somfy.lua index a67553062d..8f594235e6 100644 --- a/lua_examples/somfy.lua +++ b/lua_examples/somfy.lua @@ -1,17 +1,20 @@ -- 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 -- {"window1":{"rc":1,"address":123},"window2":{"rc":1,"address":124}} local tmr_cache = tmr.create() local tmr_delay = tmr.create() -pin = 4 -gpio.mode(pin, gpio.OUTPUT, gpio.PULLUP) +local pin = 4 -function deepcopy(orig) +local function deepcopy(orig) local orig_type = type(orig) local copy if orig_type == 'table' then @@ -26,8 +29,8 @@ function deepcopy(orig) return copy end -function readconfig() - local cfg, ok, ln +local function readconfig() + local ln if file.exists(config_file.."cfg") then print("Reading config from "..config_file.."cfg") file.open(config_file.."cfg", "r+") @@ -47,7 +50,7 @@ function readconfig() config_saved = deepcopy(config) end -function writeconfighard() +local function writeconfighard() print("Saving config") file.remove(config_file.."bak") file.rename(config_file.."cfg", config_file.."bak") @@ -63,8 +66,8 @@ function writeconfighard() config_saved = deepcopy(config) end -function writeconfig() - tmr.stop(tmr_cache) +local function writeconfig() + tmr_cache:stop() local savenow = false local savelater = false @@ -87,12 +90,18 @@ function writeconfig() end if savelater then print("Saving config later") - tmr.alarm(tmr_cache, 65000, tmr.ALARM_SINGLE, writeconfighard) + tmr_cache:alarm(65000, tmr.ALARM_SINGLE, writeconfighard) 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 {} print("down: ".. remote) config[remote].rc=config[remote].rc+1 @@ -100,7 +109,7 @@ function down(remote, cb, par) writeconfig() end -function up(remote, cb, par) +local function up(remote, cb, par) par = par or {} print("up: ".. remote) config[remote].rc=config[remote].rc+1 @@ -108,7 +117,7 @@ function up(remote, cb, par) writeconfig() end -function downStep(remote, cb, par) +local function downStep(remote, cb, par) par = par or {} print("downStep: ".. remote) config[remote].rc=config[remote].rc+1 @@ -116,7 +125,7 @@ function downStep(remote, cb, par) writeconfig() end -function upStep(remote, cb, par) +local function upStep(remote, cb, par) -- luacheck: ignore par = par or {} print("upStep: ".. remote) config[remote].rc=config[remote].rc+1 @@ -124,14 +133,9 @@ function upStep(remote, cb, par) writeconfig() 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 #config == 0 then -- somfy.cfg does not exist config = sjson.decode([[{"window1":{"rc":1,"address":123},"window2":{"rc":1,"address":124}}]]) @@ -141,5 +145,11 @@ down('window1', wait, {60000, up, {'window1', 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' }}}}}}}}}}) diff --git a/lua_examples/tcp2uart.lua b/lua_examples/tcp2uart.lua index 638a2d770e..db38371eff 100644 --- a/lua_examples/tcp2uart.lua +++ b/lua_examples/tcp2uart.lua @@ -1,16 +1,18 @@ -uart.setup(0,9600,8,0,1,0) -sv=net.createServer(net.TCP, 60) -global_c = nil -sv:listen(9999, function(c) - if global_c~=nil then - global_c:close() - end - global_c=c - c:on("receive",function(sck,pl) uart.write(0,pl) end) -end) +do + uart.setup(0, 9600, 8, 0, 1, 0) + local sv = net.createServer(net.TCP, 60) + local global_c = nil + sv:listen(9999, function(c) + if global_c~=nil then + global_c:close() + end + global_c = c + c:on("receive",function(_, pl) uart.write(0, pl) end) + end) -uart.on("data",4, function(data) - if global_c~=nil then - global_c:send(data) - end -end, 0) + uart.on("data", 4, function(data) + if global_c ~= nil then + global_c:send(data) + end + end, 0) +end diff --git a/lua_examples/timezone/tz.lua b/lua_examples/timezone/tz.lua index 8cc6cc5ea7..05d4641de8 100644 --- a/lua_examples/timezone/tz.lua +++ b/lua_examples/timezone/tz.lua @@ -27,7 +27,7 @@ function M.getzones() return result end -function load(t) +local function load(t) local z = file.open(thezone .. ".zone", "r") local hdr = z:read(20) @@ -35,7 +35,8 @@ function load(t) if magic == "TZif" then 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 typeindex = z:read(timecnt) diff --git a/lua_examples/u8g2/graphics_test.lua b/lua_examples/u8g2/graphics_test.lua index 7287c0ed0c..4214530666 100644 --- a/lua_examples/u8g2/graphics_test.lua +++ b/lua_examples/u8g2/graphics_test.lua @@ -9,8 +9,13 @@ -- -- *************************************************************************** +-- display object +local disp + +local draw_state, loop_tmr = 0, tmr.create() + -- setup I2c and connect display -function init_i2c_display() +local function init_i2c_display() -- SDA and SCL can be assigned freely to available GPIOs local sda = 5 -- GPIO14 local scl = 6 -- GPIO12 @@ -18,9 +23,8 @@ function init_i2c_display() i2c.setup(0, sda, scl, i2c.SLOW) disp = u8g2.ssd1306_i2c_128x64_noname(0, sla) end - -- setup SPI and connect display -function init_spi_display() +local function init_spi_display() -- luacheck: no unused -- Hardware SPI CLK = GPIO14 -- Hardware SPI MOSI = GPIO13 -- Hardware SPI MISO = GPIO12 (not used) @@ -37,8 +41,7 @@ function init_spi_display() disp = u8g2.ssd1306_128x64_noname(1, cs, dc, res) end - -function u8g2_prepare() +local function u8g2_prepare() disp:setFont(u8g2.font_6x10_tf) disp:setFontRefHeightExtendedText() disp:setDrawColor(1) @@ -47,7 +50,7 @@ function u8g2_prepare() end -function u8g2_box_frame(a) +local function u8g2_box_frame(a) disp:drawStr( 0, 0, "drawBox") disp:drawBox(5,10,20,10) disp:drawBox(10+a,15,30,7) @@ -56,7 +59,7 @@ function u8g2_box_frame(a) disp:drawFrame(10+a,15+30,30,7) end -function u8g2_disc_circle(a) +local function u8g2_disc_circle(a) disp:drawStr( 0, 0, "drawDisc") disp:drawDisc(10,18,9) disp:drawDisc(24+a,16,7) @@ -65,13 +68,13 @@ function u8g2_disc_circle(a) disp:drawCircle(24+a,16+30,7) end -function u8g2_r_frame(a) +local function u8g2_r_frame(a) disp:drawStr( 0, 0, "drawRFrame/Box") disp:drawRFrame(5, 10,40,30, a+1) disp:drawRBox(50, 10,25,40, a+1) end -function u8g2_string(a) +local function u8g2_string(a) disp:setFontDirection(0) disp:drawStr(30+a,31, " 0") disp:setFontDirection(1) @@ -82,7 +85,7 @@ function u8g2_string(a) disp:drawStr(30,31-a, " 270") end -function u8g2_line(a) +local function u8g2_line(a) disp:drawStr( 0, 0, "drawLine") disp:drawLine(7+a, 10, 40, 55) disp:drawLine(7+a*2, 10, 60, 55) @@ -90,7 +93,7 @@ function u8g2_line(a) disp:drawLine(7+a*4, 10, 100, 55) end -function u8g2_triangle(a) +local function u8g2_triangle(a) local offset = a disp:drawStr( 0, 0, "drawTriangle") disp:drawTriangle(14,7, 45,30, 10,40) @@ -99,7 +102,7 @@ function u8g2_triangle(a) disp:drawTriangle(10+offset,40+offset, 45+offset,30+offset, 86+offset,53+offset) end -function u8g2_ascii_1() +local function u8g2_ascii_1() disp:drawStr( 0, 0, "ASCII page 1") for y = 0, 5 do for x = 0, 15 do @@ -108,7 +111,7 @@ function u8g2_ascii_1() end end -function u8g2_ascii_2() +local function u8g2_ascii_2() disp:drawStr( 0, 0, "ASCII page 2") for y = 0, 5 do for x = 0, 15 do @@ -117,7 +120,7 @@ function u8g2_ascii_2() end end -function u8g2_extra_page(a) +local function u8g2_extra_page(a) disp:drawStr( 0, 0, "Unicode") disp:setFont(u8g2.font_unifont_t_symbols) disp:setFontPosTop() @@ -129,9 +132,9 @@ function u8g2_extra_page(a) end end -cross_width = 24 -cross_height = 24 -cross_bits = string.char( +local cross_width = 24 +local cross_height = 24 +local cross_bits = string.char( 0x00, 0x18, 0x00, 0x00, 0x24, 0x00, 0x00, 0x24, 0x00, 0x00, 0x42, 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, @@ -139,24 +142,25 @@ cross_bits = string.char( 0x00, 0x81, 0x00, 0x00, 0x81, 0x00, 0x00, 0x42, 0x00, 0x00, 0x42, 0x00, 0x00, 0x42, 0x00, 0x00, 0x24, 0x00, 0x00, 0x24, 0x00, 0x00, 0x18, 0x00) -cross_fill_width = 24 -cross_fill_height = 24 -cross_fill_bits = string.char( +-- luacheck: push no unused +local cross_fill_width = 24 +local cross_fill_height = 24 +local cross_fill_bits = string.char( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x18, 0x64, 0x00, 0x26, 0x84, 0x00, 0x21, 0x08, 0x81, 0x10, 0x08, 0x42, 0x10, 0x10, 0x3C, 0x08, 0x20, 0x00, 0x04, 0x40, 0x00, 0x02, 0x80, 0x00, 0x01, 0x80, 0x18, 0x01, 0x80, 0x18, 0x01, 0x80, 0x00, 0x01, 0x40, 0x00, 0x02, 0x20, 0x00, 0x04, 0x10, 0x3C, 0x08, 0x08, 0x42, 0x10, 0x08, 0x81, 0x10, 0x84, 0x00, 0x21, 0x64, 0x00, 0x26, 0x18, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00) - -cross_block_width = 14 -cross_block_height = 14 -cross_block_bits = string.char( +-- luacheck: pop +local cross_block_width = 14 +local cross_block_height = 14 +local cross_block_bits = string.char( 0xFF, 0x3F, 0x01, 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) -function u8g2_bitmap_overlay(a) +local function u8g2_bitmap_overlay(a) local frame_size = 28 disp:drawStr(0, 0, "Bitmap overlay") @@ -177,7 +181,7 @@ function u8g2_bitmap_overlay(a) end end -function u8g2_bitmap_modes(transparent) +local function u8g2_bitmap_modes(transparent) local frame_size = 24 disp:drawBox(0, frame_size * 0.5, frame_size * 5, frame_size) @@ -201,7 +205,7 @@ function u8g2_bitmap_modes(transparent) end -function draw() +local function draw() u8g2_prepare() local d3 = bit.rshift(draw_state, 3) @@ -235,7 +239,7 @@ function draw() end -function loop() +local function loop() -- picture loop disp:clearBuffer() draw() @@ -251,13 +255,12 @@ function loop() loop_tmr:start() end +do + loop_tmr:register(100, tmr.ALARM_SEMI, loop) -draw_state = 0 -loop_tmr = tmr.create() -loop_tmr:register(100, tmr.ALARM_SEMI, loop) - -init_i2c_display() ---init_spi_display() + init_i2c_display() + --init_spi_display() -print("--- Starting Graphics Test ---") -loop_tmr:start() + print("--- Starting Graphics Test ---") + loop_tmr:start() +end diff --git a/lua_examples/ucglib/GT_box.lua b/lua_examples/ucglib/GT_box.lua index b21323f884..4b68c394d1 100644 --- a/lua_examples/ucglib/GT_box.lua +++ b/lua_examples/ucglib/GT_box.lua @@ -1,3 +1,4 @@ +-- luacheck: globals T r disp millis lcg_rnd local M, module = {}, ... _G[module] = M diff --git a/lua_examples/ucglib/GT_clip.lua b/lua_examples/ucglib/GT_clip.lua index bf3d9b4dd8..8348177db7 100644 --- a/lua_examples/ucglib/GT_clip.lua +++ b/lua_examples/ucglib/GT_clip.lua @@ -1,3 +1,4 @@ +-- luacheck: globals T r disp millis lcg_rnd local M, module = {}, ... _G[module] = M diff --git a/lua_examples/ucglib/GT_color_test.lua b/lua_examples/ucglib/GT_color_test.lua index 0b619498e9..e8cd84d795 100644 --- a/lua_examples/ucglib/GT_color_test.lua +++ b/lua_examples/ucglib/GT_color_test.lua @@ -1,3 +1,4 @@ +-- luacheck: globals T r disp millis lcg_rnd local M, module = {}, ... _G[module] = M @@ -7,10 +8,7 @@ function M.run() print("Running component color_test...") - local mx local c, x - mx = disp:getWidth() / 2 - --my = disp:getHeight() / 2 disp:setColor(0, 0, 0, 0) disp:drawBox(0, 0, disp:getWidth(), disp:getHeight()) diff --git a/lua_examples/ucglib/GT_cross.lua b/lua_examples/ucglib/GT_cross.lua index e4cc3e2d2d..502d1d12ba 100644 --- a/lua_examples/ucglib/GT_cross.lua +++ b/lua_examples/ucglib/GT_cross.lua @@ -1,3 +1,4 @@ +-- luacheck: globals T r disp millis lcg_rnd local M, module = {}, ... _G[module] = M diff --git a/lua_examples/ucglib/GT_fonts.lua b/lua_examples/ucglib/GT_fonts.lua index 3990f31744..b885806f9c 100644 --- a/lua_examples/ucglib/GT_fonts.lua +++ b/lua_examples/ucglib/GT_fonts.lua @@ -1,3 +1,4 @@ +-- luacheck: globals T r disp millis lcg_rnd local M, module = {}, ... _G[module] = M diff --git a/lua_examples/ucglib/GT_gradient.lua b/lua_examples/ucglib/GT_gradient.lua index e432cba6a8..33b6f8abd9 100644 --- a/lua_examples/ucglib/GT_gradient.lua +++ b/lua_examples/ucglib/GT_gradient.lua @@ -1,3 +1,4 @@ +-- luacheck: globals T r disp millis lcg_rnd local M, module = {}, ... _G[module] = M diff --git a/lua_examples/ucglib/GT_graphics_test.lua b/lua_examples/ucglib/GT_graphics_test.lua index 4db6800b8c..ba840cf39d 100644 --- a/lua_examples/ucglib/GT_graphics_test.lua +++ b/lua_examples/ucglib/GT_graphics_test.lua @@ -1,3 +1,4 @@ +-- luacheck: globals T r disp millis lcg_rnd local M, module = {}, ... _G[module] = M diff --git a/lua_examples/ucglib/GT_pixel_and_lines.lua b/lua_examples/ucglib/GT_pixel_and_lines.lua index 03d65c17f1..5b4b7a25a5 100644 --- a/lua_examples/ucglib/GT_pixel_and_lines.lua +++ b/lua_examples/ucglib/GT_pixel_and_lines.lua @@ -1,3 +1,4 @@ +-- luacheck: globals T r disp millis lcg_rnd local M, module = {}, ... _G[module] = M diff --git a/lua_examples/ucglib/GT_text.lua b/lua_examples/ucglib/GT_text.lua index 8a99d8df4b..60aed1b8ed 100644 --- a/lua_examples/ucglib/GT_text.lua +++ b/lua_examples/ucglib/GT_text.lua @@ -1,3 +1,4 @@ +-- luacheck: globals T r disp millis lcg_rnd local M, module = {}, ... _G[module] = M diff --git a/lua_examples/ucglib/GT_triangle.lua b/lua_examples/ucglib/GT_triangle.lua index d2e8ab88a7..0ed188e9c7 100644 --- a/lua_examples/ucglib/GT_triangle.lua +++ b/lua_examples/ucglib/GT_triangle.lua @@ -1,3 +1,4 @@ +-- luacheck: globals T r disp millis lcg_rnd local M, module = {}, ... _G[module] = M diff --git a/lua_examples/ucglib/GraphicsTest.lua b/lua_examples/ucglib/GraphicsTest.lua index b5bcf153c7..8cc3879fbb 100644 --- a/lua_examples/ucglib/GraphicsTest.lua +++ b/lua_examples/ucglib/GraphicsTest.lua @@ -1,5 +1,23 @@ +-- 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 -function init_spi_display() +local function init_spi_display() -- Hardware SPI CLK = GPIO14 -- Hardware SPI MOSI = GPIO13 -- Hardware SPI MISO = GPIO12 (not used) @@ -20,11 +38,8 @@ function init_spi_display() disp = ucg.st7735_18x128x160_hw_spi(bus, cs, dc, res) end - - - -- switch statement http://lua-users.org/wiki/SwitchStatement -function switch(c) +local function switch(c) local swtbl = { casevar = c, caseof = function (self, code) @@ -46,20 +61,7 @@ function switch(c) return swtbl end - -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 function set_clip_range() local x, y, w, h w = bit.band(lcg_rnd(), 31) h = bit.band(lcg_rnd(), 31) @@ -71,7 +73,7 @@ function set_clip_range() disp:setClipRange(x, y, w, h) end -function loop() +local function loop() if (loop_idx == 0) then switch(bit.band(r, 3)) : caseof { @@ -112,18 +114,12 @@ function loop() print("Heap: " .. node.heap()) end +do + init_spi_display() -T = 1000 - -r = 0 -loop_idx = 0 + disp:begin(ucg.FONT_MODE_TRANSPARENT) + disp:setFont(ucg.font_ncenR14_hr) + disp:clearScreen() -init_spi_display() - -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) + tmr.create():alarm(3000, tmr.ALARM_AUTO, function() loop() end) +end diff --git a/lua_examples/ucglib/HelloWorld.lua b/lua_examples/ucglib/HelloWorld.lua index 6c7c873105..d5b6215ef6 100644 --- a/lua_examples/ucglib/HelloWorld.lua +++ b/lua_examples/ucglib/HelloWorld.lua @@ -1,5 +1,7 @@ +local disp + -- setup SPI and connect display -function init_spi_display() +local function init_spi_display() -- Hardware SPI CLK = GPIO14 -- Hardware SPI MOSI = GPIO13 -- Hardware SPI MISO = GPIO12 (not used) @@ -20,17 +22,17 @@ function init_spi_display() disp = ucg.st7735_18x128x160_hw_spi(bus, cs, dc, res) end +do + init_spi_display() + disp:begin(ucg.FONT_MODE_TRANSPARENT) + disp:clearScreen() -init_spi_display() - -disp:begin(ucg.FONT_MODE_TRANSPARENT) -disp:clearScreen() + disp:setFont(ucg.font_ncenR12_tr); + disp:setColor(255, 255, 255); + disp:setColor(1, 255, 0,0); -disp:setFont(ucg.font_ncenR12_tr); -disp:setColor(255, 255, 255); -disp:setColor(1, 255, 0,0); - -disp:setPrintPos(0, 25) -disp:print("Hello World!") + disp:setPrintPos(0, 25) + disp:print("Hello World!") +end diff --git a/lua_examples/ucglib/UcgLogo.lua b/lua_examples/ucglib/UcgLogo.lua index 760fd0bd2f..644b90a5f7 100644 --- a/lua_examples/ucglib/UcgLogo.lua +++ b/lua_examples/ucglib/UcgLogo.lua @@ -1,5 +1,7 @@ +local disp + -- setup SPI and connect display -function init_spi_display() +local function init_spi_display() -- Hardware SPI CLK = GPIO14 -- Hardware SPI MOSI = GPIO13 -- Hardware SPI MISO = GPIO12 (not used) @@ -21,7 +23,7 @@ function init_spi_display() end -function upper_pin(x, y) +local function upper_pin(x, y) local w = 7 local h = 6 disp:setColor(0, 212, 212, 212) @@ -36,7 +38,7 @@ function upper_pin(x, y) disp:drawGradientLine(x+w, y, h, 1) end -function lower_pin(x, y) +local function lower_pin(x, y) local w = 7 local h = 5 disp:setColor(0, 212, 212, 212) @@ -56,7 +58,7 @@ function lower_pin(x, y) disp:drawPixel(x+w, y+h) end -function ic_body(x, y) +local function ic_body(x, y) local w = 4*14+4 local h = 31 disp:setColor(0, 60, 60, 60) @@ -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)) end -function draw_ucg_logo() +local function draw_ucg_logo() local a, b --ucg_Init(ucg, ucg_sdl_dev_cb, ucg_ext_none, (ucg_com_fnptr)0) @@ -156,12 +158,12 @@ function draw_ucg_logo() --disp:drawString(1, 61, 0, "code.google.com/p/ucglib/") 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() -draw_ucg_logo() + disp:setRotate180() + draw_ucg_logo() +end diff --git a/lua_examples/webap_toggle_pin.lua b/lua_examples/webap_toggle_pin.lua index 60de70a6e6..bacb3def23 100644 --- a/lua_examples/webap_toggle_pin.lua +++ b/lua_examples/webap_toggle_pin.lua @@ -1,21 +1,24 @@ +do wifi.setmode(wifi.SOFTAP) wifi.ap.config({ ssid = "test", pwd = "12345678" }) gpio.mode(1, gpio.OUTPUT) -srv = net.createServer(net.TCP) +local srv = net.createServer(net.TCP) srv:listen(80, function(conn) conn:on("receive", function(client, request) local buf = "" - local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP") + local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP") -- luacheck: no unused if (method == nil) then - _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP") + _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP") -- luacheck: no unused end + local _GET = {} if (vars ~= nil) then for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do _GET[k] = v end end - buf = buf .. "

Hello, this is NodeMCU.

Turn PIN1 " local _on, _off = "", "" if (_GET.pin == "ON") then _on = " selected=true" @@ -29,3 +32,4 @@ srv:listen(80, function(conn) end) conn:on("sent", function(c) c:close() end) end) +end \ No newline at end of file diff --git a/lua_modules/bh1750/bh1750.lua b/lua_modules/bh1750/bh1750.lua index 5a06611ee3..bbe939dc6b 100644 --- a/lua_modules/bh1750/bh1750.lua +++ b/lua_modules/bh1750/bh1750.lua @@ -33,14 +33,14 @@ local function read_data(ADDR, commands, length) i2c.start(id) i2c.address(id, ADDR,i2c.RECEIVER) tmr.delay(200000) - c = i2c.read(id, length) + local c = i2c.read(id, length) i2c.stop(id) return c end local function read_lux() - dataT = read_data(GY_30_address, CMD, 2) + local dataT = read_data(GY_30_address, CMD, 2) --Make it more faster - UT = dataT:byte(1) * 256 + dataT:byte(2) + local UT = dataT:byte(1) * 256 + dataT:byte(2) l = (UT*1000/12) return(l) end diff --git a/lua_modules/bh1750/bh1750_Example1.lua b/lua_modules/bh1750/bh1750_Example1.lua index 911aa8fccc..a563d5e9f6 100644 --- a/lua_modules/bh1750/bh1750_Example1.lua +++ b/lua_modules/bh1750/bh1750_Example1.lua @@ -6,19 +6,17 @@ -- -- MIT license, http://opensource.org/licenses/MIT -- *************************************************************************** -tmr.alarm(0, 10000, 1, function() +local bh1750 = require("bh1750") - SDA_PIN = 6 -- sda pin, GPIO12 - SCL_PIN = 5 -- scl pin, GPIO14 +local sda = 6 -- sda pin, GPIO12 +local scl = 5 -- scl pin, GPIO14 - bh1750 = require("bh1750") - bh1750.init(SDA_PIN, SCL_PIN) - bh1750.read(OSS) - l = bh1750.getlux() - print("lux: "..(l / 100).."."..(l % 100).." lx") +do + bh1750.init(sda, scl) - -- release module - bh1750 = nil - package.loaded["bh1750"]=nil - -end) + tmr.create():alarm(10000, tmr.ALARM_AUTO, function() + bh1750.read() + local l = bh1750.getlux() + print("lux: "..(l / 100).."."..(l % 100).." lx") + end) +end diff --git a/lua_modules/bh1750/bh1750_Example2.lua b/lua_modules/bh1750/bh1750_Example2.lua index 0fb0b1d891..115ffae361 100644 --- a/lua_modules/bh1750/bh1750_Example2.lua +++ b/lua_modules/bh1750/bh1750_Example2.lua @@ -10,42 +10,42 @@ --Ps 需要改动的地方LW_GATEWAY(乐联的设备标示),USERKEY(乐联userkey) --Ps You nees to rewrite the LW_GATEWAY(Lelian's Device ID),USERKEY(Lelian's userkey) - -tmr.alarm(0, 60000, 1, function() - SDA_PIN = 6 -- sda pin, GPIO12 - SCL_PIN = 5 -- scl pin, GPIO14 - - BH1750 = require("BH1750") - BH1750.init(SDA_PIN, SCL_PIN) - BH1750.read(OSS) - l = BH1750.getlux() - - --定义数据变量格式 Define the veriables formate - PostData = "[{\"Name\":\"T\",\"Value\":\"" ..(l / 100).."."..(l % 100).."\"}]" - --创建一个TCP连接 Create a TCP Connection - socket=net.createConnection(net.TCP, 0) - --域名解析IP地址并赋值 DNS...it - socket:dns("www.lewei50.com", function(conn, ip) - ServerIP = ip - print("Connection IP:" .. ServerIP) - end) - ---开始连接服务器 Connect the sever -socket:connect(80, ServerIP) - socket:on("connection", function(sck) end) - ---HTTP请求头定义 HTTP Head -socket:send("POST /api/V1/gateway/UpdateSensors/LW_GATEWAY HTTP/1.1\r\n" .. - "Host: www.lewei50.com\r\n" .. - "Content-Length: " .. string.len(PostData) .. "\r\n" .. - "userkey: USERKEY\r\n\r\n" .. - PostData .. "\r\n") - ---HTTP响应内容 Print the HTTP response -socket:on("receive", function(sck, response) - print(response) - end) - end) - - - +local bh1750 = require("bh1750") + +local sda = 6 -- sda pin, GPIO12 +local scl = 5 -- scl pin, GPIO14 +local ServerIP + +do + bh1750.init(sda, scl) + + tmr.create():alarm(60000, tmr.ALARM_AUTO, function() + bh1750.read() + local l = bh1750.getlux() + --定义数据变量格式 Define the veriables formate + local PostData = "[{\"Name\":\"T\",\"Value\":\"" ..(l / 100).."."..(l % 100).."\"}]" + --创建一个TCP连接 Create a TCP Connection + local socket = net.createConnection(net.TCP, 0) + --域名解析IP地址并赋值 DNS...it + socket:dns("www.lewei50.com", function(_, ip) + ServerIP = ip + print("Connection IP:" .. ServerIP) + end) + + --开始连接服务器 Connect the sever + socket:connect(80, ServerIP) + socket:on("connection", function() end) + + --HTTP请求头定义 HTTP Head + socket:send("POST /api/V1/gateway/UpdateSensors/LW_GATEWAY HTTP/1.1\r\n" .. + "Host: www.lewei50.com\r\n" .. + "Content-Length: " .. string.len(PostData) .. "\r\n" .. + "userkey: USERKEY\r\n\r\n" .. + PostData .. "\r\n") + + --HTTP响应内容 Print the HTTP response + socket:on("receive", function(sck, response) -- luacheck: no unused + print(response) + end) + end) +end diff --git a/lua_modules/ds18b20/ds18b20-example.lua b/lua_modules/ds18b20/ds18b20-example.lua index 8354695db0..8f9eacb931 100644 --- a/lua_modules/ds18b20/ds18b20-example.lua +++ b/lua_modules/ds18b20/ds18b20-example.lua @@ -1,14 +1,17 @@ -t = require("ds18b20") -pin = 3 -- gpio0 = 3, gpio2 = 4 +local t = require("ds18b20") +local pin = 3 -- gpio0 = 3, gpio2 = 4 -local function readout(temp) +local function readout(temps) if t.sens then - print("Total number of DS18B20 sensors: ".. #t.sens) - for i, s in ipairs(t.sens) do - print(string.format(" sensor #%d address: %s%s", i, ('%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X'):format(s:byte(1,8)), s:byte(9) == 1 and " (parasite)" or "")) + print("Total number of DS18B20 sensors: ".. #t.sens) + for i, s in ipairs(t.sens) do + print(string.format(" sensor #%d address: %s%s", i, + ('%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X'):format(s:byte(1,8)), + s:byte(9) == 1 and " (parasite)" or "")) end end - for addr, temp in pairs(temp) do + + for addr, temp in pairs(temps) do print(string.format("Sensor %s: %s °C", ('%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X'):format(addr:byte(1,8)), temp)) end @@ -17,36 +20,35 @@ local function readout(temp) --package.loaded["ds18b20"]=nil end -t:enable_debug() -file.remove("ds18b20_save.lc") -- remove saved addresses -print("=============================================", node.heap()) -print("first call, no addresses in flash, search is performed") -t:read_temp(readout, pin, t.C) +do + t:enable_debug() + file.remove("ds18b20_save.lc") -- remove saved addresses + print("=============================================", node.heap()) + print("first call, no addresses in flash, search is performed") + t:read_temp(readout, pin, t.C) -tmr.create():alarm(2000, tmr.ALARM_SINGLE, function() + tmr.create():alarm(2000, tmr.ALARM_SINGLE, function() print("=============================================", node.heap()) print("second readout, no new search, found addresses are used") t:read_temp(readout, pin) -tmr.create():alarm(2000, tmr.ALARM_SINGLE, function() - print("=============================================", node.heap()) - print("force search again") - t:read_temp(readout, pin, nil, true) - -tmr.create():alarm(2000, tmr.ALARM_SINGLE, function() - print("=============================================", node.heap()) - print("save search results") - t:read_temp(readout, pin, nil, false, true) - -tmr.create():alarm(2000, tmr.ALARM_SINGLE, function() - print("=============================================", node.heap()) - print("use saved addresses") - t.sens={} - t:read_temp(readout, pin) -end) - -end) - -end) - -end) + tmr.create():alarm(2000, tmr.ALARM_SINGLE, function() + print("=============================================", node.heap()) + print("force search again") + t:read_temp(readout, pin, nil, true) + + tmr.create():alarm(2000, tmr.ALARM_SINGLE, function() + print("=============================================", node.heap()) + print("save search results") + t:read_temp(readout, pin, nil, false, true) + + tmr.create():alarm(2000, tmr.ALARM_SINGLE, function() + print("=============================================", node.heap()) + print("use saved addresses") + t.sens={} + t:read_temp(readout, pin) + end) + end) + end) + end) +end diff --git a/lua_modules/ds18b20/ds18b20-web.lua b/lua_modules/ds18b20/ds18b20-web.lua index 408612f978..bc401d3ea2 100644 --- a/lua_modules/ds18b20/ds18b20-web.lua +++ b/lua_modules/ds18b20/ds18b20-web.lua @@ -1,35 +1,37 @@ -t = require('ds18b20') +local t = require('ds18b20') -port = 80 -pin = 3 -- gpio0 = 3, gpio2 = 4 -gconn = {} -- global variable for connection +local port = 80 +local pin = 3 -- gpio0 = 3, gpio2 = 4 +local gconn = {} -- local variable for connection -function readout(temp) +local function readout(temps) local resp = "HTTP/1.1 200 OK\nContent-Type: text/html\nRefresh: 5\n\n" .. - "" .. - "" .. - "ESP8266
" + "" .. + "" .. + "ESP8266
" - for addr, temp in pairs(temp) do - resp = resp .. string.format("Sensor %s: %s ℃
", ('%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X '):format(addr:byte(1,8)), temp) + for addr, temp in pairs(temps) do + resp = resp .. string.format("Sensor %s: %s ℃
", + ('%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X '):format(addr:byte(1,8)), temp) end resp = resp .. - "Node ChipID: " .. node.chipid() .. "
" .. - "Node MAC: " .. wifi.sta.getmac() .. "
" .. - "Node Heap: " .. node.heap() .. "
" .. - "Timer Ticks: " .. tmr.now() .. "
" .. - "" + "Node ChipID: " .. node.chipid() .. "
" .. + "Node MAC: " .. wifi.sta.getmac() .. "
" .. + "Node Heap: " .. node.heap() .. "
" .. + "Timer Ticks: " .. tmr.now() .. "
" .. + "" gconn:send(resp) gconn:on("sent",function(conn) conn:close() end) end -srv=net.createServer(net.TCP) -srv:listen(port, - function(conn) - gconn = conn - -- t:read_temp(readout) -- default pin value is 3 - t:read_temp(readout, pin) - end -) +do + local srv = net.createServer(net.TCP) + srv:listen(port, + function(conn) + gconn = conn + -- t:read_temp(readout) -- default pin value is 3 + t:read_temp(readout, pin) + end) +end diff --git a/lua_modules/ds18b20/ds18b20.lua b/lua_modules/ds18b20/ds18b20.lua index 50d9a34152..a40e27c0f9 100644 --- a/lua_modules/ds18b20/ds18b20.lua +++ b/lua_modules/ds18b20/ds18b20.lua @@ -3,23 +3,25 @@ -- NODEMCU TEAM -- LICENCE: http://opensource.org/licenses/MIT -- @voborsky, @devsaurus, TerryE 26 Mar 2017 ----------------------------------------------------------------------------------------------------------------------------------------------------------------- +-------------------------------------------------------------------------------- local modname = ... -- Used modules and functions -local table, string, ow, tmr, print, type, tostring, pcall, ipairs = - table, string, ow, tmr, print, type, tostring, pcall, ipairs +local type, tostring, pcall, ipairs = + type, tostring, pcall, ipairs -- Local functions -local ow_setup, ow_search, ow_select, ow_read, ow_read_bytes, ow_write, ow_crc8, ow_reset, ow_reset_search, ow_skip, ow_depower = - ow.setup, ow.search, ow.select, ow.read, ow.read_bytes, ow.write, ow.crc8, ow.reset, ow.reset_search, ow.skip, ow.depower +local ow_setup, ow_search, ow_select, ow_read, ow_read_bytes, ow_write, ow_crc8, + ow_reset, ow_reset_search, ow_skip, ow_depower = + ow.setup, ow.search, ow.select, ow.read, ow.read_bytes, ow.write, ow.crc8, + ow.reset, ow.reset_search, ow.skip, ow.depower + local node_task_post, node_task_LOW_PRIORITY = node.task.post, node.task.LOW_PRIORITY local string_char, string_dump = string.char, string.dump local now, tmr_create, tmr_ALARM_SINGLE = tmr.now, tmr.create, tmr.ALARM_SINGLE local table_sort, table_concat = table.sort, table.concat local math_floor = math.floor local file_open = file.open - -table, string, tmr, ow = nil, nil, nil, nil +local conversion local DS18B20FAMILY = 0x28 local DS1920FAMILY = 0x10 -- and DS18S20 series @@ -50,8 +52,6 @@ local function to_string(addr, esc) end end -local conversion - local function readout(self) local next = false local sens = self.sens @@ -62,7 +62,7 @@ local function readout(self) local addr = s:sub(1,8) ow_select(pin, addr) -- select the sensor ow_write(pin, READ_SCRATCHPAD, MODE) - data = ow_read_bytes(pin, 9) + local data = ow_read_bytes(pin, 9) local t=(data:byte(1)+data:byte(2)*256) -- t is actually signed so process the sign bit and adjust for fractional bits @@ -116,7 +116,7 @@ local function readout(self) end end -conversion = function (self) +conversion = (function (self) local sens = self.sens local powered_only = true for _, s in ipairs(sens) do powered_only = powered_only and s:byte(9) ~= 1 end @@ -125,7 +125,7 @@ conversion = function (self) ow_reset(pin) ow_skip(pin) -- select the sensor ow_write(pin, CONVERT_T, MODE) -- and start conversion - for i, s in ipairs(sens) do status[i] = 1 end + for i, _ in ipairs(sens) do status[i] = 1 end else for i, s in ipairs(sens) do if status[i] == 0 then @@ -140,12 +140,11 @@ conversion = function (self) end end tmr_create():alarm(750, tmr_ALARM_SINGLE, function() return readout(self) end) -end +end) local function _search(self, lcb, lpin, search, save) self.temp = {} if search then self.sens = {}; status = {} end - local temp = self.temp local sens = self.sens pin = lpin or pin @@ -167,7 +166,7 @@ local function _search(self, lcb, lpin, search, save) -- search the first device addr = ow_search(pin) else - for i, s in ipairs(sens) do status[i] = 0 end + for i, _ in ipairs(sens) do status[i] = 0 end end local function cycle() debugPrint("cycle") diff --git a/lua_modules/ds3231/ds3231-example.lua b/lua_modules/ds3231/ds3231-example.lua index 340bb1be74..81263d0ee0 100644 --- a/lua_modules/ds3231/ds3231-example.lua +++ b/lua_modules/ds3231/ds3231-example.lua @@ -1,15 +1,17 @@ +local ds3231 = require("ds3231") -- ESP-01 GPIO Mapping -gpio0, gpio2 = 3, 4 -i2c.setup(gpio0, gpio2, scl, i2c.SLOW) -- call i2c.setup() only once +local gpio0, gpio2 = 3, 4 -require("ds3231") +do + i2c.setup(0, gpio0, gpio2, i2c.SLOW) -- call i2c.setup() only once -second, minute, hour, day, date, month, year = ds3231.getTime(); + local second, minute, hour, day, date, month, year = ds3231.getTime(); -- luacheck: no unused --- Get current time -print(string.format("Time & Date: %s:%s:%s %s/%s/%s", hour, minute, second, date, month, year)) + -- Get current time + print(string.format("Time & Date: %s:%s:%s %s/%s/%s", hour, minute, second, date, month, year)) --- Don't forget to release it after use -ds3231 = nil -package.loaded["ds3231"]=nil + -- Don't forget to release it after use + ds3231 = nil -- luacheck: no unused + package.loaded["ds3231"] = nil +end \ No newline at end of file diff --git a/lua_modules/ds3231/ds3231-web.lua b/lua_modules/ds3231/ds3231-web.lua index db55854ca9..da08717e02 100644 --- a/lua_modules/ds3231/ds3231-web.lua +++ b/lua_modules/ds3231/ds3231-web.lua @@ -1,54 +1,53 @@ +local ds3231 = require('ds3231') -- ESP-01 GPIO Mapping -gpio0, gpio2 = 3, 4 -i2c.setup(gpio0, gpio2, scl, i2c.SLOW) -- call i2c.setup() only once - -require('ds3231') - -port = 80 - -days = { - [1] = "Sunday", - [2] = "Monday", - [3] = "Tuesday", - [4] = "Wednesday", - [5] = "Thursday", - [6] = "Friday", - [7] = "Saturday" +local gpio0, gpio2 = 3, 4 +local port = 80 +local days = { + [1] = "Sunday", + [2] = "Monday", + [3] = "Tuesday", + [4] = "Wednesday", + [5] = "Thursday", + [6] = "Friday", + [7] = "Saturday" } -months = { - [1] = "January", - [2] = "Febuary", - [3] = "March", - [4] = "April", - [5] = "May", - [6] = "June", - [7] = "July", - [8] = "August", - [9] = "September", - [10] = "October", - [11] = "November", - [12] = "December" +local months = { + [1] = "January", + [2] = "Febuary", + [3] = "March", + [4] = "April", + [5] = "May", + [6] = "June", + [7] = "July", + [8] = "August", + [9] = "September", + [10] = "October", + [11] = "November", + [12] = "December" } -srv=net.createServer(net.TCP) -srv:listen(port, - function(conn) - - second, minute, hour, day, date, month, year = ds3231.getTime() - prettyTime = string.format("%s, %s %s %s %s:%s:%s", days[day], date, months[month], year, hour, minute, second) - - conn:send("HTTP/1.1 200 OK\nContent-Type: text/html\nRefresh: 5\n\n" .. - "" .. - "" .. - "ESP8266
" .. - "Time and Date: " .. prettyTime .. "
" .. - "Node ChipID : " .. node.chipid() .. "
" .. - "Node MAC : " .. wifi.sta.getmac() .. "
" .. - "Node Heap : " .. node.heap() .. "
" .. - "Timer Ticks : " .. tmr.now() .. "
" .. - "") - conn:on("sent",function(conn) conn:close() end) - end -) +do + i2c.setup(0, gpio0, gpio2, i2c.SLOW) -- call i2c.setup() only once + + local srv = net.createServer(net.TCP) + srv:listen(port, function(conn) + local second, minute, hour, day, date, month, year = ds3231.getTime() + local prettyTime = string.format("%s, %s %s %s %s:%s:%s", + days[day], date, months[month], year, hour, minute, second) + + conn:send("HTTP/1.1 200 OK\nContent-Type: text/html\nRefresh: 5\n\n" .. + "" .. + "" .. + "ESP8266
" .. + "Time and Date: " .. prettyTime .. "
" .. + "Node ChipID : " .. node.chipid() .. "
" .. + "Node MAC : " .. wifi.sta.getmac() .. "
" .. + "Node Heap : " .. node.heap() .. "
" .. + "Timer Ticks : " .. tmr.now() .. "
" .. + "") + + conn:on("sent",function(sck) sck:close() end) + end) +end diff --git a/lua_modules/ds3231/ds3231.lua b/lua_modules/ds3231/ds3231.lua index e7f848b7ce..b0c2f7cbcd 100644 --- a/lua_modules/ds3231/ds3231.lua +++ b/lua_modules/ds3231/ds3231.lua @@ -114,7 +114,7 @@ function M.reloadAlarms () i2c.write(id, 0x0F) i2c.write(id, d) i2c.stop(id) - print('[LOG] Alarm '..almId..' reloaded') + print('[LOG] Alarms reloaded') end -- Enable alarmId bit. Let it to be triggered diff --git a/lua_modules/email/imap.lua b/lua_modules/email/imap.lua index b3c2a95cbc..8f7cf4adc3 100644 --- a/lua_modules/email/imap.lua +++ b/lua_modules/email/imap.lua @@ -22,8 +22,6 @@ _G[moduleName] = M local USERNAME = "" local PASSWORD = "" -local SERVER = "" -local PORT = "" local TAG = "" local DEBUG = false @@ -45,10 +43,10 @@ end --- -- @name display -- @description A generic IMAP response processing function. --- Can disply the IMAP response if DEBUG is set to true. --- Sets the reponse processed variable to true when the string "complete" +-- Can display the IMAP response if DEBUG is set to true. +-- Sets the response processed variable to true when the string "complete" -- is found in the IMAP reply/response -local function display(socket, response) +local function display(socket, response) -- luacheck: no unused -- If debuggins is enabled print the IMAP response if(DEBUG) then @@ -67,7 +65,7 @@ end --- -- @name config -- @description Initiates the IMAP settings -function M.config(username,password,tag,debug) +function M.config(username, password, tag, debug) USERNAME = username PASSWORD = password TAG = tag @@ -96,13 +94,13 @@ end -- @description Gets the most recent email number from the EXAMINE command. -- i.e. if EXAMINE returns "* 4 EXISTS" this means that there are 4 emails, -- so the latest/newest will be identified by the number 4 -local function set_most_recent_num(socket,response) +local function set_most_recent_num(socket, response) -- luacheck: no unused if(DEBUG) then print(response) end - local _, _, num = string.find(response,"([0-9]+) EXISTS(\.)") -- the _ and _ keep the index of the string found + local _, _, num = string.find(response,"([0-9]+) EXISTS") -- the _ and _ keep the index of the string found -- but we don't care about that. if(num~=nil) then @@ -117,7 +115,7 @@ end --- -- @name examine -- @description IMAP examines the given mailbox/folder. Sends the IMAP EXAMINE command -function M.examine(socket,mailbox) +function M.examine(socket, mailbox) response_processed = false socket:send(TAG .. " EXAMINE " .. mailbox .. "\r\n") @@ -135,7 +133,7 @@ end -- @name set_header -- @description Records the IMAP header field response in a variable -- so that it may be read later -local function set_header(socket,response) +local function set_header(socket, response) -- luacheck: no unused if(DEBUG) then print(response) end @@ -152,7 +150,7 @@ end -- @param socket The IMAP socket to use -- @param msg_number The email number to read e.g. 1 will read fetch the latest/newest email -- @param field A header field such as SUBJECT, FROM, or DATE -function M.fetch_header(socket,msg_number,field) +function M.fetch_header(socket, msg_number, field) header = "" -- we are getting a new header so clear this variable response_processed = false socket:send(TAG .. " FETCH " .. msg_number .. " BODY[HEADER.FIELDS (" .. field .. ")]\r\n") @@ -171,7 +169,7 @@ end -- @name set_body -- @description Records the IMAP body response in a variable -- so that it may be read later -local function set_body(socket,response) +local function set_body(_, response) if(DEBUG) then print(response) @@ -188,7 +186,7 @@ end -- @description Sends the IMAP command to fetch a plain text version of the email's body -- @param socket The IMAP socket to use -- @param msg_number The email number to obtain e.g. 1 will obtain the latest email -function M.fetch_body_plain_text(socket,msg_number) +function M.fetch_body_plain_text(socket, msg_number) response_processed = false body = "" -- clear the body variable since we'll be fetching a new email socket:send(TAG .. " FETCH " .. msg_number .. " BODY[1]\r\n") diff --git a/lua_modules/fifo/fifosocktest.lua b/lua_modules/fifo/fifosocktest.lua index b2424c46a6..4ac9f2196d 100644 --- a/lua_modules/fifo/fifosocktest.lua +++ b/lua_modules/fifo/fifosocktest.lua @@ -11,7 +11,7 @@ local vprint = (verbose > 0) and print or function() end -- Mock up enough of the nodemcu tmr structure, but pretend that nothing -- happens between ticks. This won't exercise the optimistic corking logic, -- but that's probably fine. --- +-- luacheck: push ignore tmr = {} tmr.ALARM_SINGLE = 0 function tmr.create() @@ -19,6 +19,7 @@ function tmr.create() function r:alarm(_i, _t, cb) vprint("TMR") cb() end return r end +-- luacheck: pop -- -- Mock up enough of the nodemcu net.socket type; have it log all the sends @@ -28,7 +29,7 @@ local outs = {} local fakesock = { cb = nil, on = function(this, _, cb) this.cb = cb end, - send = function(this, s) vprint("SEND", (verbose > 1) and s) table.insert(outs, s) end, + send = function(this, s) vprint("SEND", (verbose > 1) and s) table.insert(outs, s) end -- luacheck: no unused } local function sent() vprint("SENT") fakesock.cb() end @@ -68,25 +69,25 @@ sent() ; fchecke() -- Hit default FSMALLLIM while building up fsendc("abracadabra lots small") -for i = 1, 32 do fsend("a") end +for i = 1, 32 do fsend("a") end -- luacheck: no unused nocoal() -for i = 1, 4 do fsend("a") end +for i = 1, 4 do fsend("a") end -- luacheck: no unused sent() ; fcheck(string.rep("a", 32)) sent() ; fcheck(string.rep("a", 4)) sent() ; fchecke() -- Hit string length while building up fsendc("abracadabra overlong") -for i = 1, 10 do fsend(string.rep("a",32)) end +for i = 1, 10 do fsend(string.rep("a",32)) end -- luacheck: no unused sent() ; fcheck(string.rep("a", 320)) sent() ; fchecke() -- Hit neither before sending a big string fsendc("abracadabra mid long") -for i = 1, 6 do fsend(string.rep("a",32)) end +for i = 1, 6 do fsend(string.rep("a",32)) end -- luacheck: no unused fsend(string.rep("b", 256)) nocoal() -for i = 1, 6 do fsend(string.rep("c",32)) end +for i = 1, 6 do fsend(string.rep("c",32)) end -- luacheck: no unused sent() ; fcheck(string.rep("a", 192) .. string.rep("b", 256)) sent() ; fcheck(string.rep("c", 192)) sent() ; fchecke() @@ -109,33 +110,36 @@ sent() ; fcheck(string.rep("c",512)) sent() ; fchecke() -- test a lazy generator -local ix = 0 -local function gen() vprint("GEN", ix); ix = ix + 1; return ("a" .. ix), ix < 3 and gen end -fsend(gen) -fsend("b") -fcheck("a1") -sent() ; fcheck("a2") -sent() ; fcheck("a3") -sent() ; fcheck("b") -sent() ; fchecke() - +do + local ix = 0 + local function gen() vprint("GEN", ix); ix = ix + 1; return ("a" .. ix), ix < 3 and gen end + fsend(gen) + fsend("b") + fcheck("a1") + sent() ; fcheck("a2") + sent() ; fcheck("a3") + sent() ; fcheck("b") + sent() ; fchecke() +end -- test a completion-like callback that does send text -local ix = 0 -local function gen() vprint("GEN"); ix = 1; return "efgh", nil end -fsend("abcd"); fsend(gen); fsend("ijkl") -assert (ix == 0) - fcheck("abcd"); assert (ix == 0) -sent() ; fcheck("efgh"); assert (ix == 1); ix = 0 -sent() ; fcheck("ijkl"); assert (ix == 0) -sent() ; fchecke() - +do + local ix = 0 + local function gen() vprint("GEN"); ix = 1; return "efgh", nil end + fsend("abcd"); fsend(gen); fsend("ijkl") + assert (ix == 0) + fcheck("abcd"); assert (ix == 0) + sent() ; fcheck("efgh"); assert (ix == 1); ix = 0 + sent() ; fcheck("ijkl"); assert (ix == 0) + sent() ; fchecke() +end -- and one that doesn't -local ix = 0 -local function gen() vprint("GEN"); ix = 1; return nil, nil end -fsend("abcd"); fsend(gen); fsend("ijkl") -assert (ix == 0) - fcheck("abcd"); assert (ix == 0) -sent() ; fcheck("ijkl"); assert (ix == 1); ix = 0 -sent() ; fchecke() ; assert (ix == 0) - +do + local ix = 0 + local function gen() vprint("GEN"); ix = 1; return nil, nil end + fsend("abcd"); fsend(gen); fsend("ijkl") + assert (ix == 0) + fcheck("abcd"); assert (ix == 0) + sent() ; fcheck("ijkl"); assert (ix == 1); ix = 0 + sent() ; fchecke() ; assert (ix == 0) +end print("All tests OK") diff --git a/lua_modules/ftp/ftpserver.lua b/lua_modules/ftp/ftpserver.lua index b488221ab1..6a2d65de05 100644 --- a/lua_modules/ftp/ftpserver.lua +++ b/lua_modules/ftp/ftpserver.lua @@ -19,8 +19,8 @@ Note that FTP also exposes a number of really private properties (which could be stores in local / upvals) as FTP properties for debug purposes. ]] -local file,net,wifi,node,string,table,tmr,pairs,print,pcall, tostring = - file,net,wifi,node,string,table,tmr,pairs,print,pcall, tostring +local file, net, wifi, node, table, tmr, pairs, print, pcall, tostring = + file, net, wifi, node, table, tmr, pairs, print, pcall, tostring local post = node.task.post local FTP, cnt = {client = {}}, 0 @@ -90,18 +90,18 @@ function FTP.createServer(user, pass, dbgFlag) -- upval: FTP (, debug, tostring -- debug("Sending: %s", rec) return CNX.cmdSocket:send(rec.."\r\n", cb) end, --- send() - close = function(sock) -- upval: client, CNX (,debug, pcall, type) + close = function(socket) -- upval: client, CNX (,debug, pcall, type) -- debug("Closing CNX.socket=%s, sock=%s", tostring(CNX.socket), tostring(sock)) for _,s in ipairs{'cmdSocket', 'dataServer', 'dataSocket'} do local sck; sck,CNX[s] = CNX[s], nil -- debug("closing CNX.%s=%s", s, tostring(sck)) if type(sck)=='userdata' then pcall(sck.close, sck) end end - client[sock] = nil + client[socket] = nil end -- CNX.close() } - local function validateUser(sock, data) -- upval: CNX, FTP (, debug, processCommand) + local function validateUser(socket, data) -- upval: CNX, FTP (, debug, processCommand) -- validate the logon and if then switch to processing commands -- debug("Authorising: %s", data) @@ -118,8 +118,8 @@ function FTP.createServer(user, pass, dbgFlag) -- upval: FTP (, debug, tostring elseif CNX.validUser and cmd == 'PASS' then if arg == FTP.pass then CNX.cwd = '/' - sock:on("receive", function(sock,data) - processCommand(CNX,sock,data) + socket:on("receive", function(socketObj, dataObj) + processCommand(CNX,socketObj, dataObj) end) -- logged on so switch to command mode msg = "230 Login successful. Username & password correct; proceed." else @@ -134,8 +134,8 @@ function FTP.createServer(user, pass, dbgFlag) -- upval: FTP (, debug, tostring return CNX.send(msg) end - local port,ip = sock:getpeer() - -- debug("Connection accepted: (userdata) %s client %s:%u", tostring(sock), ip, port) + local port,ip = sock:getpeer() -- luacheck: no unused + --debug("Connection accepted: (userdata) %s client %s:%u", tostring(sock), ip, port) sock:on("receive", validateUser) sock:on("disconnection", CNX.close) FTP.client[sock]=CNX @@ -177,8 +177,8 @@ end -- FTP.close() -- -- Find strings are used do this lookup and minimise long if chains. ------------------------------------------------------------------------------ -processCommand = function(cxt, sock, data) -- upvals: (, debug, processBareCmds, processSimpleCmds, processDataCmds) - +-- upvals: (, debug, processBareCmds, processSimpleCmds, processDataCmds) +processCommand = function(cxt, socket, data) -- luacheck: no unused debug("Command: %s", data) data = data:gsub('[\r\n]+$', '') -- chomp trailing CRLF local cmd, arg = data:match('([a-zA-Z]+) *(.*)') @@ -331,7 +331,7 @@ processDataCmds = function(cxt, cmd, arg) -- upval: FTP (, pairs, file, tostrin pattern = arg:gsub('*','[^/%%.]*') end - for k,v in pairs(fileSize) do + for k, _ in pairs(fileSize) do if k:match(pattern) then nameList[#nameList+1] = k else @@ -341,8 +341,8 @@ processDataCmds = function(cxt, cmd, arg) -- upval: FTP (, pairs, file, tostrin table.sort(nameList) function cxt.getData() -- upval: cmd, fileSize, nameList (, table) - local list, user, v = {}, FTP.user - for i = 1,10 do + local list, user = {}, FTP.user + for i = 1,10 do -- luacheck: no unused if #nameList == 0 then break end local f = table.remove(nameList, 1) list[#list+1] = (cmd == "LIST") and @@ -395,9 +395,9 @@ end -- processDataCmds(cmd, arg, send) -- ---------------- Open a new data server and port --------------------------- dataServer = function(cxt, n) -- upval: (pcall, net, ftpDataOpen, debug, tostring) - local dataServer = cxt.dataServer - if dataServer then -- close any existing listener - pcall(dataServer.close, dataServer) + local dataSrv = cxt.dataServer + if dataSrv then -- close any existing listener + pcall(dataSrv.close, dataSrv) end if n then -- Open a new listener if needed. Note that this is only used to establish @@ -425,10 +425,11 @@ ftpDataOpen = function(cxt, dataSocket) -- upval: (debug, tostring, post, pcall) cxt.dataServer = nil local function cleardown(skt,type) -- upval: cxt (, debug, tostring, post, pcall) + -- luacheck: push no unused type = type==1 and "disconnection" or "reconnection" local which = cxt.setData and "setData" or (cxt.getData and cxt.getData or "neither") - -- debug("Cleardown entered from %s with %s", type, which) - + --debug("Cleardown entered from %s with %s", type, which) + -- luacheck: pop if cxt.setData then cxt.fileClose() cxt.setData = nil @@ -446,8 +447,9 @@ ftpDataOpen = function(cxt, dataSocket) -- upval: (debug, tostring, post, pcall) local on_hold = false dataSocket:on("receive", function(skt, rec) --upval: cxt, on_hold (, debug, tstring, post, node, pcall) - local which = cxt.setData and "setData" or (cxt.getData and cxt.getData or "neither") - -- debug("Received %u data bytes with %s", #rec, which) + + local which = cxt.setData and "setData" or (cxt.getData and cxt.getData or "neither")-- luacheck: no unused + --debug("Received %u data bytes with %s", #rec, which) if not cxt.setData then return end @@ -476,7 +478,8 @@ ftpDataOpen = function(cxt, dataSocket) -- upval: (debug, tostring, post, pcall) function cxt.sender(skt) -- upval: cxt (, debug) debug ("entering sender") if not cxt.getData then return end - local rec, skt = cxt.getData(), cxt.dataSocket + skt = skt or cxt.dataSocket + local rec = cxt.getData() if rec and #rec > 0 then -- debug("Sending %u data bytes", #rec) skt:send(rec) diff --git a/lua_modules/hdc1000/HDC1000-example.lua b/lua_modules/hdc1000/HDC1000-example.lua index d4c08c7633..c2e10c0486 100644 --- a/lua_modules/hdc1000/HDC1000-example.lua +++ b/lua_modules/hdc1000/HDC1000-example.lua @@ -1,14 +1,17 @@ -HDC1000 = require("HDC1000") +local HDC1000 = require("HDC1000") -sda = 1 -scl = 2 -drdyn = false +local sda, scl = 1, 2 +local drdyn = false -i2c.setup(0, sda, scl, i2c.SLOW) -- call i2c.setup() only once -HDC1000.setup(drdyn) -HDC1000.config() -- default values are used if called with no arguments. prototype is config(address, resolution, heater) +do + i2c.setup(0, sda, scl, i2c.SLOW) -- call i2c.setup() only once + HDC1000.setup(drdyn) + -- prototype is config(address, resolution, heater) + HDC1000.config() -- default values are used if called with no arguments. -print(string.format("Temperature: %.2f °C\nHumidity: %.2f %%", HDC1000.getTemp(), HDC1000.getHumi())) + print(string.format("Temperature: %.2f °C\nHumidity: %.2f %%", HDC1000.getTemp(), HDC1000.getHumi())) -HDC1000 = nil -package.loaded["HDC1000"]=nil + -- Don't forget to release it after use + HDC1000 = nil -- luacheck: no unused + package.loaded["HDC1000"] = nil +end \ No newline at end of file diff --git a/lua_modules/hdc1000/HDC1000.lua b/lua_modules/hdc1000/HDC1000.lua index 8067afe9b7..40b0180c60 100644 --- a/lua_modules/hdc1000/HDC1000.lua +++ b/lua_modules/hdc1000/HDC1000.lua @@ -40,9 +40,9 @@ local HDC1000_TEMP_HUMI_14BIT = 0x00 local function read16() i2c.start(id) i2c.address(id, HDC1000_ADDR, i2c.RECEIVER) - data_temp = i2c.read(0, 2) + local data_temp = i2c.read(0, 2) i2c.stop(id) - data = bit.lshift(string.byte(data_temp, 1, 1), 8) + string.byte(data_temp, 2, 2) + local data = bit.lshift(string.byte(data_temp, 1, 1), 8) + string.byte(data_temp, 2, 2) return data end diff --git a/lua_modules/http/http-example.lua b/lua_modules/http/http-example.lua index ea03a46e5d..9a8509955b 100644 --- a/lua_modules/http/http-example.lua +++ b/lua_modules/http/http-example.lua @@ -8,7 +8,7 @@ require("httpserver").createServer(80, function(req, res) -- analyse method and url print("+R", req.method, req.url, node.heap()) -- setup handler of headers, if any - req.onheader = function(self, name, value) + req.onheader = function(self, name, value) -- luacheck: ignore print("+H", name, value) -- E.g. look for "content-type" header, -- setup body parser to particular format @@ -21,7 +21,7 @@ require("httpserver").createServer(80, function(req, res) -- end end -- setup handler of body, if any - req.ondata = function(self, chunk) + req.ondata = function(self, chunk) -- luacheck: ignore print("+B", chunk and #chunk, node.heap()) if not chunk then -- reply diff --git a/lua_modules/http/httpserver.lua b/lua_modules/http/httpserver.lua index 50717e1e96..bce4dfb899 100644 --- a/lua_modules/http/httpserver.lua +++ b/lua_modules/http/httpserver.lua @@ -48,7 +48,7 @@ do csend("\r\n") end end - local send_header = function(self, name, value) + local send_header = function(self, name, value) -- luacheck: ignore -- NB: quite a naive implementation csend(name) csend(": ") @@ -88,13 +88,13 @@ do local req, res local buf = "" local method, url - local ondisconnect = function(conn) - conn.on("sent", nil) + local ondisconnect = function(connection) + connection.on("sent", nil) collectgarbage("collect") end -- header parser local cnt_len = 0 - local onheader = function(conn, k, v) + local onheader = function(connection, k, v) -- luacheck: ignore -- TODO: look for Content-Type: header -- to help parse body -- parse content length to know body length @@ -111,19 +111,19 @@ do end -- body data handler local body_len = 0 - local ondata = function(conn, chunk) + local ondata = function(connection, chunk) -- luacheck: ignore -- feed request data to request handler if not req or not req.ondata then return end req:ondata(chunk) -- NB: once length of seen chunks equals Content-Length: - -- ondata(conn) is called + -- ondata(conn) is called body_len = body_len + #chunk -- print("-B", #chunk, body_len, cnt_len, node.heap()) if body_len >= cnt_len then req:ondata() end end - local onreceive = function(conn, chunk) + local onreceive = function(connection, chunk) -- merge chunks in buffer if buf then buf = buf .. chunk @@ -139,12 +139,12 @@ do buf = buf:sub(e + 2) -- method, url? if not method then - local i + local i, _ -- luacheck: ignore -- NB: just version 1.1 assumed _, i, method, url = line:find("^([A-Z]+) (.-) HTTP/1.1$") if method then -- make request and response objects - req = make_req(conn, method, url) + req = make_req(connection, method, url) res = make_res(csend, cfini) end -- spawn request handler @@ -156,17 +156,17 @@ do -- header seems ok? if k then k = k:lower() - onheader(conn, k, v) + onheader(connection, k, v) end -- headers end else -- NB: we feed the rest of the buffer as starting chunk of body - ondata(conn, buf) + ondata(connection, buf) -- buffer no longer needed buf = nil -- NB: we explicitly reassign receive handler so that -- next received chunks go directly to body handler - conn:on("receive", ondata) + connection:on("receive", ondata) -- parser done break end diff --git a/lua_modules/lm92/lm92.lua b/lua_modules/lm92/lm92.lua index b8f331b80d..802373e50e 100644 --- a/lua_modules/lm92/lm92.lua +++ b/lua_modules/lm92/lm92.lua @@ -24,7 +24,6 @@ local address = 0 local function read_reg(reg_addr, len) local ret={} local c - local x i2c.start(id) i2c.address(id, address ,i2c.TRANSMITTER) i2c.write(id,reg_addr) @@ -32,9 +31,9 @@ local function read_reg(reg_addr, len) i2c.start(id) i2c.address(id, address,i2c.RECEIVER) c=i2c.read(id,len) - for x=1,len,1 do - tc=string.byte(c,x) - table.insert(ret,tc) + for x = 1, len, 1 do + local tc = string.byte(c, x) + table.insert(ret, tc) end i2c.stop(id) return ret @@ -64,7 +63,7 @@ function M.setup(a) if (a ~= nil) and (a >= 0x48) and (a <= 0x4b ) then address = a i2c.start(id) - res = i2c.address(id, address, i2c.TRANSMITTER) --verify that the address is valid + local res = i2c.address(id, address, i2c.TRANSMITTER) --verify that the address is valid i2c.stop(id) if (res == false) then print("device not found") diff --git a/lua_modules/mcp23008/mcp23008.lua b/lua_modules/mcp23008/mcp23008.lua index dc5e4122d7..3e2bf9a3be 100644 --- a/lua_modules/mcp23008/mcp23008.lua +++ b/lua_modules/mcp23008/mcp23008.lua @@ -17,6 +17,7 @@ _G[moduleName] = M local MCP23008_ADDRESS = 0x20 -- Registers' address as defined in the MCP23008's datashseet +-- luacheck: push no unused local MCP23008_IODIR = 0x00 local MCP23008_IPOL = 0x01 local MCP23008_GPINTEN = 0x02 @@ -28,7 +29,7 @@ local MCP23008_INTF = 0x07 local MCP23008_INTCAP = 0x08 local MCP23008_GPIO = 0x09 local MCP23008_OLAT = 0x0A - +-- luacheck: pop -- Default value for i2c communication local id = 0 @@ -75,8 +76,7 @@ local function read(registerAddress) i2c.start(id) -- Read the data form the register i2c.address(id,MCP23008_ADDRESS,i2c.RECEIVER) -- send the MCP's address and read bit - local data = 0x00 - data = i2c.read(id,1) -- we expect only one byte of data + local data = i2c.read(id,1) -- we expect only one byte of data i2c.stop(id) return string.byte(data) -- i2c.read returns a string so we convert to it's int value diff --git a/lua_modules/redis/redis.lua b/lua_modules/redis/redis.lua index cc4bf64db6..94005e09b3 100644 --- a/lua_modules/redis/redis.lua +++ b/lua_modules/redis/redis.lua @@ -55,7 +55,7 @@ do -- FIXME: this suddenly occurs. timeout? --print("-FD") end) - _fd:on("receive", function(fd, s) + _fd:on("receive", function(fd, s) --luacheck: no unused --print("IN", s) -- TODO: subscription to all channels -- lookup message pattern to determine channel and payload diff --git a/lua_modules/yeelink/Example_for_Yeelink_Lib.lua b/lua_modules/yeelink/Example_for_Yeelink_Lib.lua index 129bc47878..c000046bd1 100644 --- a/lua_modules/yeelink/Example_for_Yeelink_Lib.lua +++ b/lua_modules/yeelink/Example_for_Yeelink_Lib.lua @@ -10,13 +10,13 @@ wifi.setmode(wifi.STATION) --Step1: Connect to Wifi wifi.sta.config("SSID","Password") -dht = require("dht_lib") --Step2: "Require" the libs -yeelink = require("yeelink_lib") +local dht = require("dht_lib") --Step2: "Require" the libs +local yeelink = require("yeelink_lib") yeelink.init(23333,23333,"You api-key",function() --Step3: Register the callback function print("Yeelink Init OK...") - tmr.alarm(1,60000,1,function() --Step4: Have fun~ (Update your data) + tmr.create():alarm(60000, tmr.ALARM_AUTO, function() --Step4: Have fun~ (Update your data) dht.read(4) yeelink.update(dht.getTemperature()) diff --git a/lua_modules/yeelink/yeelink_lib.lua b/lua_modules/yeelink/yeelink_lib.lua index 004bbe0e44..70c6c4a415 100644 --- a/lua_modules/yeelink/yeelink_lib.lua +++ b/lua_modules/yeelink/yeelink_lib.lua @@ -22,7 +22,7 @@ local apikey = "" --================================ local debug = true --<<<<<<<<<<<<< Don't forget to "false" it before using --================================ -local sk=net.createConnection(net.TCP, 0) +local sk = net.createConnection(net.TCP, 0) local datapoint = 0 @@ -31,10 +31,11 @@ local datapoint = 0 if wifi.sta.getip() == nil then print("Please Connect WIFI First") - tmr.alarm(1,1000,1,function () + local wifiTimer = tmr.create() + wifiTimer:alarm(1000, tmr.ALARM_AUTO,function () if wifi.sta.getip() ~= nil then - tmr.stop(1) - sk:dns("api.yeelink.net",function(conn,ip) + wifiTimer:stop() + sk:dns("api.yeelink.net",function(_,ip) dns=ip print("DNS YEELINK OK... IP: "..dns) end) @@ -42,12 +43,9 @@ if wifi.sta.getip() == nil then end) end -sk:dns("api.yeelink.net",function(conn,ip) - -dns=ip - -print("DNS YEELINK OK... IP: "..dns) - +sk:dns("api.yeelink.net",function(conn, ip) -- luacheck: no unused + dns = ip + print("DNS YEELINK OK... IP: "..dns) end) --========Set the init function=========== @@ -61,12 +59,12 @@ function M.init(_device, _sensor, _apikey) sensor = tostring(_sensor) apikey = _apikey if dns == "0.0.0.0" then - tmr.alarm(2,5000,1,function () - if dns == "0.0.0.0" then - print("Waiting for DNS...") - end - end) - return false + tmr.create():alarm(5000,tmr.ALARM_AUTO,function () + if dns == "0.0.0.0" then + print("Waiting for DNS...") + end + end) + return false else return dns end @@ -96,7 +94,7 @@ function M.update(_datapoint) datapoint = tostring(_datapoint) - sk:on("connection", function(conn) + sk:on("connection", function() print("connect OK...") @@ -116,7 +114,7 @@ function M.update(_datapoint) end) - sk:on("receive", function(sck, content) + sk:on("receive", function(conn, content) -- luacheck: no unused if debug then print("\r\n"..content.."\r\n") diff --git a/tools/luacheck_config.lua b/tools/luacheck_config.lua index 34a8b6eb59..85147ec788 100644 --- a/tools/luacheck_config.lua +++ b/tools/luacheck_config.lua @@ -173,6 +173,32 @@ stds.nodemcu_libs = { toHex = empty } }, + dcc = { + fields = { + CV_READ = empty, + CV_RESET = empty, + CV_VALID = empty, + CV_WRITE = empty, + DCC_ACCESSORY = empty, + DCC_FUNC = empty, + DCC_IDLE = empty, + DCC_RAW = empty, + DCC_RESET = empty, + DCC_SERVICEMODE = empty, + DCC_SPEED = empty, + DCC_SPEED_RAW = empty, + DCC_TURNOUT = empty, + FLAGS_AUTO_FACTORY_DEFAULT = empty, + FLAGS_DCC_ACCESSORY_DECODER = empty, + FLAGS_MY_ADDRESS_ONLY = empty, + FLAGS_OUTPUT_ADDRESS_MODE = empty, + MAN_ID_DIY = empty, + MAN_ID_JMRI = empty, + MAN_ID_SILICON_RAILWAY = empty, + close = empty, + setup = empty + } + }, dht = { fields = { ERROR_CHECKSUM = empty, @@ -200,12 +226,17 @@ stds.nodemcu_libs = { }, file = { fields = { + chdir = empty, close = empty, exists = empty, flush = empty, + format = empty, + fscfg = empty, fsinfo = empty, getcontents = empty, list = empty, + mount = empty, + n = empty, on = empty, open = empty, putcontents = empty, @@ -393,7 +424,10 @@ stds.nodemcu_libs = { }, task = { fields = { - post = empty + post = empty, + LOW_PRIORITY = empty, + MEDIUM_PRIORITY = empty, + HIGH_PRIORITY = empty } } } @@ -576,6 +610,13 @@ stds.nodemcu_libs = { transaction = empty } }, + struct = { + fields = { + pack = empty, + size = empty, + unpack = empty + } + }, switec = { fields = { close = empty, @@ -876,7 +917,8 @@ stds.nodemcu_libs = { }, pack = empty, unpack = empty, - size = empty + size = empty, + package = {fields = {seeall = read_write}} } } diff --git a/tools/luacheck_config_helper.lua b/tools/luacheck_config_helper.lua index f7b5bb2e62..58bce3fab1 100755 --- a/tools/luacheck_config_helper.lua +++ b/tools/luacheck_config_helper.lua @@ -114,6 +114,12 @@ local function printTables(fileName) if not findBegin then findBegin, _, field = string.find(line, "LROT_TABENTRY%(%s?(%g+),") end + if not findBegin then + findBegin, _, field = string.find(line, "LROT_FUNCENTRY_S%(%s?(%g+),") + end + if not findBegin then + findBegin, _, field = string.find(line, "LROT_FUNCENTRY_F%(%s?(%g+),") + end if findBegin then if not string.find(field, "__") then diff --git a/tools/travis/run-luacheck-linux.sh b/tools/travis/run-luacheck-linux.sh new file mode 100755 index 0000000000..929f92e4d2 --- /dev/null +++ b/tools/travis/run-luacheck-linux.sh @@ -0,0 +1,72 @@ +#!/bin/bash + +set -e + +exists() { + if command -v "$1" >/dev/null 2>&1 + then + return 0 + else + return 1 + fi +} + +usage() { +echo " +usage: bash tools/travis/run-luacheck.sh [-s] + +Avarible options are: +-s: Standalone mode: Lua, LuaRocks and luacheck +will be installed in nodemcu-firmare/cache folder. + +By default script will use luarocks installed in host system. +" +} + +install_tools() { + if ! exists luarocks; then + echo "LuaRocks not found!" + exit 1 + fi + + eval "`luarocks path --bin`" #Set PATH for luacheck + #In Travis Path it's not changed by LuaRocks for some unknown reason + if [ "${TRAVIS}" = "true" ]; then + export PATH=$PATH:/home/travis/.luarocks/bin + fi + + if ! exists luacheck; then + echo "Installing luacheck" + luarocks install --local luacheck || exit + fi + +} + +install_tools_standalone() { + if ! [ -x cache/localua/bin/luarocks ]; then + echo "Installing Lua 5.3 and LuaRocks" + bash tools/travis/localua.sh cache/localua || exit + fi + + if ! [ -x cache/localua/bin/luacheck ]; then + echo "Installing luacheck" + cache/localua/bin/luarocks install luacheck || exit + fi +} + +if [[ $1 == "" ]]; then + install_tools + else + while getopts "s" opt + do + case $opt in + (s) install_tools_standalone ;; + (*) usage; exit 1 ;; + esac + done +fi + +echo "Static analysys of" +find lua_modules lua_examples -iname "*.lua" -print0 | xargs -0 echo + +(find lua_modules lua_examples -iname "*.lua" -print0 | xargs -0 luacheck --config tools/luacheck_config.lua) || exit diff --git a/tools/travis/run-luacheck-windows.sh b/tools/travis/run-luacheck-windows.sh new file mode 100644 index 0000000000..682510de66 --- /dev/null +++ b/tools/travis/run-luacheck-windows.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +set -e + +#Download luacheck binary if nessesary +if ! [ -x "cache/luacheck.exe" ]; then + wget --tries=5 --timeout=10 --waitretry=10 --read-timeout=10 --retry-connrefused -O cache/luacheck.exe https://github.com/mpeterv/luacheck/releases/download/0.23.0/luacheck.exe +fi + +echo "Static analysys of" +find lua_modules lua_examples -iname "*.lua" -print0 | xargs -0 echo + +(find lua_modules lua_examples -iname "*.lua" -print0 | xargs -0 cache/luacheck.exe --config tools/luacheck_config.lua) || exit diff --git a/tools/travis/run-luacheck.sh b/tools/travis/run-luacheck.sh deleted file mode 100644 index d97241f715..0000000000 --- a/tools/travis/run-luacheck.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash - -set -e - -echo "Installing Lua 5.3, LuaRocks and Luacheck" -( - cd "$TRAVIS_BUILD_DIR" || exit - bash tools/travis/localua.sh cache/localua || exit - cache/localua/bin/luarocks install luacheck || exit -) - -( - echo "Static analysys of:" - find lua_modules lua_examples -iname "*.lua" -print0 | xargs -0 echo -) - -(find lua_modules lua_examples -iname "*.lua" -print0 | xargs -0 cache/localua/bin/luacheck --config tools/luacheck_config.lua) || exit