Skip to content

Instantly share code, notes, and snippets.

@dnc40085
Last active October 27, 2019 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dnc40085/45e3af2fea22003b3ac2 to your computer and use it in GitHub Desktop.
Save dnc40085/45e3af2fea22003b3ac2 to your computer and use it in GitHub Desktop.
FlashMod example
Flashmod was created by dpwhittaker (http://www.esp8266.com/viewtopic.php?f=19&t=1940)
Instructions for this example:
1. Upload "flashmod.lua" to your ESP8266.
2. Run flashmod.lua with "dofile("flashmod.lua")", this will create the file "flash_flashMod.lc" then it will delete "flashmod.lua" since it is no longer needed.
3. Upload init.lua then restart your ESP8266.
4.upload and run "module_1.lua". This will create the files "Mod1_function_1.lc", "Mod1_function_2.lc", "Mod1_function_3.lc" then it will delete "flashmod.lua" since it is no longer needed.
5.upload and run "module_2.lua". This will create the files "Mod2_init.lc", "Mod2_function_1.lc", "Mod2_function_2.lc", "Mod2_function_3.lc" then it will delete "flashmod.lua" since it is no longer needed.
6.upload and run "main.lua"
7.observe the results
8.Adapt to your own uses
9.????
10. PROFIT!!!!
Note: If multiple modules share the same name("MOD_NAME = "Mod1""), running "flashmod("Mod1")" will load all files prefixed with "Mod1_" into the same table, this can be useful when writing large modules, BE AWARE: ANY DUPLICATE FUNCTIONS WILL BE OVERWRITTEN by the one contained in the most recently executed Lua file.
flash = {MOD_NAME = "flash"}
function flash.flashMod(tbl)
if type(tbl) == "string" then tbl = {MOD_NAME=tbl} end
for k,v in pairs(tbl) do
if type(v) == "function" then
file.open(string.format("%s_%s.lc", tbl.MOD_NAME, k), "w+")
file.write(string.dump(v))
file.close()
tbl[k] = nil
end
end
return setmetatable(tbl, {
__index = function(t, k)
return assert(loadfile(string.format("%s_%s.lc",t.MOD_NAME,k)))
end
})
end
flash.flashMod(flash)
flash = nil
module = nil
package = nil
newproxy = nil
require = nil
collectgarbage()
function flashMod(tbl) return loadfile("flash_flashMod.lc")(tbl) end
file.remove("flashmod.lua")
print("Start init.lua")
function flashMod(tbl) return loadfile("flash_flashMod.lc")(tbl) end
if file.open("main.lua") then
print("Starting main.lua in 3 seconds")
tmr.alarm(0, 3000, 0, function() dofile("main.lua") end)
end
print("End init.lua")
print("Start Main.lua")
tmp1=flashMod("Mod1")
tmp1:function_1()
tmp1:function_2()
tmp1:function_3()
tmp1=flashMod("Mod2")
tmp1:init()
tmp1:function_1()
tmp1:function_2()
tmp1:function_3()
print("End Main.lua")
local mod = {MOD_NAME = "Mod1"}
function mod:function_1()
print("\nthis is \"Flash_function_1\" in \"Module_1\"")
end
function mod:function_2()
print("\nthis is \"Flash_function_2\" in \"Module_1\"")
end
function mod:function_3()
print("\nthis is \"Flash_function_3\" in \"Module_1\"")
self:function_1()
self:function_2()
end
flashMod(mod)
file.remove("module_1.lua")
local mod = {MOD_NAME = "Mod2"}
function mod:init()
--Put variables that the module needs here
--Note: these variables are NOT stored in flash
self.chipID=node.chipid()
self.flashID=node.flashid()
end
function mod:function_1()
print("\nthis is \"Flash_function_1\" in \"Module_2\"")
--NOTE: if mod:init() is not ran first then flashMod will return this error: "flashmod.lua:15: cannot open Mod2_chipID.lc"
print("the chip id is: "..self.chipID)
end
function mod:function_2()
print("\nthis is \"Flash_function_2\" in \"Module_2\"")
print("flashID is: "..self.flashID)
end
function mod:function_3()
print("\nthis is \"Flash_function_3\" in \"Module_2\"")
self:function_1()
self:function_2()
end
flashMod(mod)
file.remove("module_2.lua")
@TerryE
Copy link

TerryE commented Jun 17, 2015

Note that unlike node.compile(), string.dump() leaves the debug content in the bytecode file, so the files (and the reloaded bytecode) are about 60% larger.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment