-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from konnected-io/lfs
move application to LFS
- Loading branch information
Showing
29 changed files
with
211 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
# Compiled Lua sources | ||
luac.out | ||
|
||
# LFS image | ||
src/lfs/lfs.img | ||
|
||
# luarocks build files | ||
*.src.rock | ||
*.zip | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,30 @@ | ||
#!/bin/bash | ||
|
||
FIRMWARE_PATH=${PWD}/../nodemcu-firmware | ||
IMAGE_NAME=konnected-firmware-2-2-6 | ||
IMAGE_NAME=konnected-firmware-2-2-7 | ||
|
||
# Copy firmware configuration from this repository to the nodemcu-firmware repo | ||
cp firmware/2.2.0/app/include/* $FIRMWARE_PATH/app/include/ | ||
|
||
rm $FIRMWARE_PATH/local/fs/* | ||
cp src/* $FIRMWARE_PATH/local/fs | ||
|
||
docker run \ | ||
--rm -ti -v $FIRMWARE_PATH:/opt/nodemcu-firmware \ | ||
-v ${PWD}/scripts:/scripts marcelstoer/nodemcu-build bash /scripts/cross-compile | ||
|
||
rm $FIRMWARE_PATH/local/fs/*.lua | ||
rm $FIRMWARE_PATH/local/fs/init.lc | ||
cp src/init.lua $FIRMWARE_PATH/local/fs | ||
|
||
# Build NodeMCU firmware image | ||
docker run -e "INTEGER_ONLY=1" \ | ||
-e "IMAGE_NAME=${IMAGE_NAME}" \ | ||
--rm -ti -v $FIRMWARE_PATH:/opt/nodemcu-firmware marcelstoer/nodemcu-build build | ||
|
||
# Build LFS image with application files | ||
docker run -e "IMAGE_NAME=lfs" \ | ||
--rm -ti -v $FIRMWARE_PATH:/opt/nodemcu-firmware \ | ||
-v ${PWD}/src/lfs:/opt/lua marcelstoer/nodemcu-build lfs-image | ||
|
||
mv src/lfs/LFS_float_lfs.img src/lfs/lfs.img | ||
|
||
# Build SPIFFS image | ||
docker run \ | ||
--rm -ti -v $FIRMWARE_PATH:/opt/nodemcu-firmware \ | ||
-v ${PWD}/scripts:/scripts \ | ||
-v ${PWD}/src:/opt/lua marcelstoer/nodemcu-build bash /scripts/build-spiffs | ||
|
||
cp ${FIRMWARE_PATH}/bin/nodemcu_integer_${IMAGE_NAME}.bin firmware/${IMAGE_NAME}.bin | ||
cp ${FIRMWARE_PATH}/bin/0xf0000-0x1f400.img firmware/konnected-filesystem-0xf0000-2-2-6.img | ||
cp ${FIRMWARE_PATH}/bin/konnected-filesystem-0x100000.img firmware/konnected-filesystem-0x100000-2-2-7.img |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
|
||
/opt/nodemcu-firmware/tools/spiffsimg/spiffsimg -f /opt/nodemcu-firmware/bin/konnected-filesystem-0x%x.img -S 2MB -U 0x100000 -r /scripts/spiffs.lst -d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
#!/bin/bash | ||
|
||
FIRMWARE_NAME=konnected-firmware-2-2-6.bin | ||
FILESYSTEM_NAME=konnected-filesystem-0xf0000-2-2-6.img | ||
FIRMWARE_NAME=konnected-firmware-2-2-7.bin | ||
FILESYSTEM_NAME=konnected-filesystem-0x100000-2-2-7.img | ||
PORT=/dev/cu.wchusbserial1410 | ||
|
||
esptool.py --port=${PORT} write_flash --flash_mode dio 0x00000 firmware/${FIRMWARE_NAME} | ||
esptool.py --port=${PORT} write_flash --flash_mode dio 0xf0000 firmware/${FILESYSTEM_NAME} | ||
esptool.py --port=${PORT} write_flash --flash_mode dio 0x100000 firmware/${FILESYSTEM_NAME} | ||
|
||
tput bel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import /opt/lua/actuators.lua actuators.lua | ||
import /opt/lua/dht_sensors.lua dht_sensors.lua | ||
import /opt/lua/ds18b20_sensors.lua ds18b20_sensors.lua | ||
import /opt/lua/enduser_setup.html.gz enduser_setup.html.gz | ||
import /opt/lua/http_favicon.ico.gz http_favicon.ico.gz | ||
import /opt/lua/http_index.html.gz http_index.html.gz | ||
import /opt/lua/init.lua init.lua | ||
import /opt/lua/sensors.lua sensors.lua | ||
import /opt/lua/settings.lua settings.lua | ||
import /opt/lua/lfs/lfs.img lfs.img |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,16 @@ | ||
print("Heap: ", node.heap(), "Initializing Konnected (" .. string.gsub(wifi.sta.getmac(), ":", "") .. ")") | ||
|
||
-- load the application in LFS if needed | ||
if node.flashindex() == nil then | ||
node.flashreload("lfs.img") | ||
end | ||
|
||
pcall(node.flashindex("_init")) | ||
require("start") | ||
print("Heap: ", node.heap(), "Version: ", require("device").swVersion) | ||
print("Heap: ", node.heap(), "Connecting to Wifi..") | ||
print("Heap: ", node.heap(), "Application Version: ", require("device").swVersion) | ||
|
||
-- hack to ensure pin D8 stays low after boot so it can be used with a high-level trigger relay | ||
gpio.mode(8, gpio.OUTPUT) | ||
gpio.write(8, gpio.LOW) | ||
|
||
local startWifiSetup = function() | ||
print("Heap: ", node.heap(), "Entering Wifi setup mode") | ||
wifi.eventmon.unregister(wifi.eventmon.STA_DISCONNECTED) | ||
wifiFailTimer:unregister() | ||
wifiFailTimer = nil | ||
enduser_setup.manual(false) | ||
enduser_setup.start() | ||
failsafeTimer:start() | ||
end | ||
|
||
-- wait 30 seconds before entering wifi setup mode in case of a momentary outage | ||
wifiFailTimer = tmr.create() | ||
wifiFailTimer:register(30000, tmr.ALARM_SINGLE, function() startWifiSetup() end) | ||
|
||
-- failsafe: reboot after 5 minutes in case of extended wifi outage | ||
failsafeTimer = tmr.create() | ||
failsafeTimer:register(300000, tmr.ALARM_SINGLE, function() node.restart() end) | ||
|
||
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T) | ||
print("Heap: ", node.heap(), "Cannot connect to WiFi ", T.SSID, 'Reason Code:', T.reason) | ||
|
||
if T.reason == wifi.eventmon.reason.AUTH_EXPIRE then | ||
-- wifi password is incorrect, immediatly enter setup mode | ||
print("Heap: ", node.heap(), "Wifi password is incorrect") | ||
startWifiSetup() | ||
else | ||
wifiFailTimer:start() | ||
end | ||
end) | ||
|
||
if wifi.sta.getconfig() == "" then | ||
print("Heap: ", node.heap(), "WiFi not configured") | ||
startWifiSetup() | ||
print("Heap: ", node.heap(), "WiFi Setup started") | ||
end | ||
|
||
local _ = tmr.create():alarm(900, tmr.ALARM_AUTO, function(t) | ||
require("led_flip").flip() | ||
if wifi.sta.getip() then | ||
wifi.eventmon.unregister(wifi.eventmon.STA_DISCONNECTED) | ||
t:unregister() | ||
t = nil | ||
if wifiFailTimer then | ||
wifiFailTimer:unregister() | ||
wifiFailTimer = nil | ||
end | ||
failsafeTimer:unregister() | ||
failsafeTimer = nil | ||
print("Heap: ", node.heap(), "Wifi connected with IP: ", wifi.sta.getip()) | ||
|
||
gpio.write(4, gpio.HIGH) | ||
enduser_setup.stop() | ||
require("server") | ||
print("Heap: ", node.heap(), "Loaded: ", "server") | ||
require("application") | ||
print("Heap: ", node.heap(), "Loaded: ", "application") | ||
end | ||
end) | ||
|
||
dofile("wifi.lua") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
-- | ||
-- File: _init.lua | ||
--[[ | ||
This is a template for the LFS equivalent of the SPIFFS init.lua. | ||
It is a good idea to such an _init.lua module to your LFS and do most of the LFS | ||
module related initialisaion in this. This example uses standard Lua features to | ||
simplify the LFS API. | ||
The first section adds a 'LFS' table to _G and uses the __index metamethod to | ||
resolve functions in the LFS, so you can execute the main function of module | ||
'fred' by executing LFS.fred(params), etc. It also implements some standard | ||
readonly properties: | ||
LFS._time The Unix Timestamp when the luac.cross was executed. This can be | ||
used as a version identifier. | ||
LFS._config This returns a table of useful configuration parameters, hence | ||
print (("0x%6x"):format(LFS._config.lfs_base)) | ||
gives you the parameter to use in the luac.cross -a option. | ||
LFS._list This returns a table of the LFS modules, hence | ||
print(table.concat(LFS._list,'\n')) | ||
gives you a single column listing of all modules in the LFS. | ||
---------------------------------------------------------------------------------]] | ||
|
||
local index = node.flashindex | ||
|
||
local lfs_t = { | ||
__index = function(_, name) | ||
local fn_ut, ba, ma, size, modules = index(name) | ||
if not ba then | ||
return fn_ut | ||
elseif name == '_time' then | ||
return fn_ut | ||
elseif name == '_config' then | ||
local fs_ma, fs_size = file.fscfg() | ||
return {lfs_base = ba, lfs_mapped = ma, lfs_size = size, | ||
fs_mapped = fs_ma, fs_size = fs_size} | ||
elseif name == '_list' then | ||
return modules | ||
else | ||
return nil | ||
end | ||
end, | ||
|
||
__newindex = function(_, name, value) | ||
error("LFS is readonly. Invalid write to LFS." .. name, 2) | ||
end, | ||
|
||
} | ||
|
||
local G=getfenv() | ||
G.LFS = setmetatable(lfs_t,lfs_t) | ||
|
||
--[[------------------------------------------------------------------------------- | ||
The second section adds the LFS to the require searchlist, so that you can | ||
require a Lua module 'jean' in the LFS by simply doing require "jean". However | ||
note that this is at the search entry following the FS searcher, so if you also | ||
have jean.lc or jean.lua in SPIFFS, then this SPIFFS version will get loaded into | ||
RAM instead of using. (Useful, for development). | ||
See docs/en/lfs.md and the 'loaders' array in app/lua/loadlib.c for more details. | ||
---------------------------------------------------------------------------------]] | ||
|
||
package.loaders[3] = function(module) -- loader_flash | ||
local fn, ba = index(module) | ||
return ba and "Module not in LFS" or fn | ||
end | ||
|
||
--[[------------------------------------------------------------------------------- | ||
You can add any other initialisation here, for example a couple of the globals | ||
are never used, so setting them to nil saves a couple of global entries | ||
---------------------------------------------------------------------------------]] | ||
|
||
G.module = nil -- disable Lua 5.0 style modules to save RAM | ||
package.seeall = nil | ||
|
||
--[[------------------------------------------------------------------------------- | ||
These replaces the builtins loadfile & dofile with ones which preferentially | ||
loads the corresponding module from LFS if present. Flipping the search order | ||
is an exercise left to the reader.- | ||
---------------------------------------------------------------------------------]] | ||
|
||
local lf, df = loadfile, dofile | ||
G.loadfile = function(n) | ||
local mod, ext = n:match("(.*)%.(l[uc]a?)"); | ||
local fn, ba = index(mod) | ||
if ba or (ext ~= 'lc' and ext ~= 'lua') then return lf(n) else return fn end | ||
end | ||
|
||
G.dofile = function(n) | ||
local mod, ext = n:match("(.*)%.(l[uc]a?)"); | ||
local fn, ba = index(mod) | ||
if ba or (ext ~= 'lc' and ext ~= 'lua') then return df(n) else return fn() end | ||
end | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
print("Heap: ", node.heap(), "Connecting to Wifi..") | ||
local startWifiSetup = function() | ||
print("Heap: ", node.heap(), "Entering Wifi setup mode") | ||
wifi.eventmon.unregister(wifi.eventmon.STA_DISCONNECTED) | ||
wifiFailTimer:unregister() | ||
wifiFailTimer = nil | ||
enduser_setup.manual(false) | ||
enduser_setup.start() | ||
failsafeTimer:start() | ||
end | ||
|
||
-- wait 30 seconds before entering wifi setup mode in case of a momentary outage | ||
wifiFailTimer = tmr.create() | ||
wifiFailTimer:register(30000, tmr.ALARM_SINGLE, function() startWifiSetup() end) | ||
|
||
-- failsafe: reboot after 5 minutes in case of extended wifi outage | ||
failsafeTimer = tmr.create() | ||
failsafeTimer:register(300000, tmr.ALARM_SINGLE, function() node.restart() end) | ||
|
||
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T) | ||
print("Heap: ", node.heap(), "Cannot connect to WiFi ", T.SSID, 'Reason Code:', T.reason) | ||
|
||
if T.reason == wifi.eventmon.reason.AUTH_EXPIRE then | ||
-- wifi password is incorrect, immediatly enter setup mode | ||
print("Heap: ", node.heap(), "Wifi password is incorrect") | ||
startWifiSetup() | ||
else | ||
wifiFailTimer:start() | ||
end | ||
end) | ||
|
||
if wifi.sta.getconfig() == "" then | ||
print("Heap: ", node.heap(), "WiFi not configured") | ||
startWifiSetup() | ||
print("Heap: ", node.heap(), "WiFi Setup started") | ||
end | ||
|
||
local _ = tmr.create():alarm(900, tmr.ALARM_AUTO, function(t) | ||
require("led_flip").flip() | ||
if wifi.sta.getip() then | ||
wifi.eventmon.unregister(wifi.eventmon.STA_DISCONNECTED) | ||
t:unregister() | ||
t = nil | ||
if wifiFailTimer then | ||
wifiFailTimer:unregister() | ||
wifiFailTimer = nil | ||
end | ||
failsafeTimer:unregister() | ||
failsafeTimer = nil | ||
print("Heap: ", node.heap(), "Wifi connected with IP: ", wifi.sta.getip()) | ||
|
||
gpio.write(4, gpio.HIGH) | ||
enduser_setup.stop() | ||
require("server") | ||
print("Heap: ", node.heap(), "Loaded: ", "server") | ||
require("application") | ||
print("Heap: ", node.heap(), "Loaded: ", "application") | ||
end | ||
end) | ||
|
File renamed without changes.