From a615d23b65291307929b2a2542b594a7dc5b8f2d Mon Sep 17 00:00:00 2001 From: "Eugeniy E. Mikhailov" Date: Wed, 2 Jun 2021 14:04:11 -0400 Subject: added init with possibility of aborting --- init.lua | 57 ++++++++++++++++++++++++++++++--------------------------- init.old.lua | 30 ++++++++++++++++++++++++++++++ main.lua | 5 +++++ 3 files changed, 65 insertions(+), 27 deletions(-) create mode 100644 init.old.lua diff --git a/init.lua b/init.lua index 34c744e..25a33aa 100644 --- a/init.lua +++ b/init.lua @@ -1,30 +1,33 @@ --- Compile server code and remove original .lua files. --- This only happens the first time after the .lua files are uploaded. -local compileAndRemoveIfNeeded = function(f) - if file.open(f) then - file.close() - print('Compiling:', f) - node.compile(f) - file.remove(f) - collectgarbage() - end -end +-- allows to interrupt start up in the first 5 seconds by hitting +-- borrowed from https://bigdanzblog.wordpress.com/2015/04/24/esp8266-nodemcu-interrupting-init-lua-during-boot/ +function abortInit() + -- initailize abort boolean flag + abort = false + print('Press ENTER within 5 seconds to abort startup') + -- if is pressed, call abortTest + uart.on('data', '\r', abortTest, 0) + -- start timer to execute startup function in 5 seconds + tmr.create():alarm(5000,tmr.ALARM_SINGLE,startup) + end + +function abortTest(data) + -- user requested abort + abort = true + -- turns off uart scanning + uart.on('data') + end -local serverFiles = { - 'microwire.lua', - 'lmx2487lib.lua', - 'binaryLib.lua', - 'freq2lmx2487settings.lua' -} ---for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end +function startup() + uart.on('data') + -- if user requested abort, exit + if abort == true then + print('startup aborted') + return + end + -- otherwise, start up + print('in startup') + dofile('main.lua') + end -compileAndRemoveIfNeeded = nil -serverFiles = nil -collectgarbage() +tmr.create():alarm(1000,tmr.ALARM_SINGLE,abortInit) -- call abortInit after 1s - --- some diagnostic -print('chip: ',node.chipid()) -print('heap: ',node.heap()) - -dofile("main.lua") diff --git a/init.old.lua b/init.old.lua new file mode 100644 index 0000000..34c744e --- /dev/null +++ b/init.old.lua @@ -0,0 +1,30 @@ +-- Compile server code and remove original .lua files. +-- This only happens the first time after the .lua files are uploaded. +local compileAndRemoveIfNeeded = function(f) + if file.open(f) then + file.close() + print('Compiling:', f) + node.compile(f) + file.remove(f) + collectgarbage() + end +end + +local serverFiles = { + 'microwire.lua', + 'lmx2487lib.lua', + 'binaryLib.lua', + 'freq2lmx2487settings.lua' +} +--for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end + +compileAndRemoveIfNeeded = nil +serverFiles = nil +collectgarbage() + + +-- some diagnostic +print('chip: ',node.chipid()) +print('heap: ',node.heap()) + +dofile("main.lua") diff --git a/main.lua b/main.lua index 0d22c58..3ba8113 100644 --- a/main.lua +++ b/main.lua @@ -3,6 +3,11 @@ Eugeniy Mikhailov 1 June 2021 ]] +startup=nil +abortTest=nil +abortInit=nil + + dofile("binaryLib.lua") dofile("microwire.lua") dofile("lmx2487lib.lua") -- cgit v1.2.3