_init.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. --
  2. -- File: _init.lua
  3. --[[
  4. This is a template for the LFS equivalent of the SPIFFS init.lua.
  5. It is a good idea to such an _init.lua module to your LFS and do most of the LFS
  6. module related initialisaion in this. This example uses standard Lua features to
  7. simplify the LFS API.
  8. The first section adds a 'LFS' table to _G and uses the __index metamethod to
  9. resolve functions in the LFS, so you can execute the main function of module
  10. 'fred' by executing LFS.fred(params), etc. It also implements some standard
  11. readonly properties:
  12. LFS._time The Unix Timestamp when the luac.cross was executed. This can be
  13. used as a version identifier.
  14. LFS._config This returns a table of useful configuration parameters, hence
  15. print (("0x%6x"):format(LFS._config.lfs_base))
  16. gives you the parameter to use in the luac.cross -a option.
  17. LFS._list This returns a table of the LFS modules, hence
  18. print(table.concat(LFS._list,'\n'))
  19. gives you a single column listing of all modules in the LFS.
  20. ---------------------------------------------------------------------------------]]
  21. local index = node.flashindex
  22. local lfs_t = {
  23. __index = function(_, name)
  24. local fn_ut, ba, ma, size, modules = index(name)
  25. if not ba then
  26. return fn_ut
  27. elseif name == '_time' then
  28. return fn_ut
  29. elseif name == '_config' then
  30. local fs_ma, fs_size = file.fscfg()
  31. return {lfs_base = ba, lfs_mapped = ma, lfs_size = size,
  32. fs_mapped = fs_ma, fs_size = fs_size}
  33. elseif name == '_list' then
  34. return modules
  35. else
  36. return nil
  37. end
  38. end,
  39. __newindex = function(_, name, value)
  40. error("LFS is readonly. Invalid write to LFS." .. name, 2)
  41. end,
  42. }
  43. local G=getfenv()
  44. G.LFS = setmetatable(lfs_t,lfs_t)
  45. --[[-------------------------------------------------------------------------------
  46. The second section adds the LFS to the require searchlist, so that you can
  47. require a Lua module 'jean' in the LFS by simply doing require "jean". However
  48. note that this is at the search entry following the FS searcher, so if you also
  49. have jean.lc or jean.lua in SPIFFS, then this SPIFFS version will get loaded into
  50. RAM instead of using. (Useful, for development).
  51. See docs/en/lfs.md and the 'loaders' array in app/lua/loadlib.c for more details.
  52. ---------------------------------------------------------------------------------]]
  53. package.loaders[3] = function(module) -- loader_flash
  54. local fn, ba = index(module)
  55. return ba and "Module not in LFS" or fn
  56. end
  57. --[[-------------------------------------------------------------------------------
  58. You can add any other initialisation here, for example a couple of the globals
  59. are never used, so setting them to nil saves a couple of global entries
  60. ---------------------------------------------------------------------------------]]
  61. G.module = nil -- disable Lua 5.0 style modules to save RAM
  62. package.seeall = nil
  63. --[[-------------------------------------------------------------------------------
  64. These replaces the builtins loadfile & dofile with ones which preferentially
  65. loads the corresponding module from LFS if present. Flipping the search order
  66. is an exercise left to the reader.-
  67. ---------------------------------------------------------------------------------]]
  68. local lf, df = loadfile, dofile
  69. G.loadfile = function(n)
  70. local mod, ext = n:match("(.*)%.(l[uc]a?)");
  71. local fn, ba = index(mod)
  72. if ba or (ext ~= 'lc' and ext ~= 'lua') then return lf(n) else return fn end
  73. end
  74. G.dofile = function(n)
  75. local mod, ext = n:match("(.*)%.(l[uc]a?)");
  76. local fn, ba = index(mod)
  77. if ba or (ext ~= 'lc' and ext ~= 'lua') then return df(n) else return fn() end
  78. end