-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnodemcu
executable file
·277 lines (249 loc) · 7.8 KB
/
nodemcu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/luanode
--#!/usr/bin/lua5.1
--#!/usr/bin/luajit
-- == NodeMCU/Linux ==
--
-- Copyright (c) 2018 by Rene K. Mueller <[email protected]>
--
-- License: MIT (see LICENSE file)
--
-- Description:
-- see http://github.com/Spiritdude/nodemcu-linux
-- Notes:
-- luajit has built-in ffi(), but function(...) arg isn't set, but needs `arg = {...}`
-- lua5.1 requires 3rd party luaffifb for ffi(), and it does function(...) arg set
--
-- History:
-- 2018/03/11: 0.0.7: '-e' added to execute strings
-- 2018/03/09: 0.0.6: parse command-line arguments (options etc)
-- 2018/03/05: 0.0.5: luanode tests: timers work fine, luanode.net not yet used
-- 2018/02/27: 0.0.3: 'nodemcu' takes arguments and are executed
-- 2018/02/25: 0.0.2: dofile() redefined so ./ and /usr/local/lib/nodemcu paths are considered (easier dev process)
-- 2018/02/24: 0.0.1: first version: node, tmr, file, gpio and rtctime with very reduced functionality
APPNAME = "NodeMCU/Linux"
VERSION = '0.0.7'
dofile = function(fn) -- we redefine so we extend it in manners we need
for i,k in pairs({'./','/usr/local/lib/nodemcu/'}) do
local fnf
if io.open(k..fn) then
fnf = k..fn
end
if arg[-1]:match("luanode") and io.open(k..fn:gsub("%.lua","-node.lua")) then
fnf = k..fn:gsub("%.lua","-node.lua")
end
if fnf then
if tmr and _syslog then -- might not be available yet
_syslog.print(_syslog.INFO,"dofile "..fnf)
end
local f = assert(loadfile(fnf))
return f()
end
end
_syslog.print(_syslog.ERROR,"file "..fn.." not found to execute")
return nil
end
dofile("modules/base/base.lua")
local opts = { }
local k2f = { v = 'verbose', h = 'help', s = 'silent', e = 'execute' }
local _arg = { }
for k,v in pairs(arg) do -- parse all command line arguments (including -1 and 0)
if k > 0 then
if v:match("^%-(%w+)$") then -- single char switch
local kx = v:match("^%-(%w+)$")
for i=1,kx:len() do
local a = kx:sub(i,i)
opts[k2f[a] or a] = (opts[k2f[a] or a] or 0) + 1
end
elseif v:match("^%-%-([%w%.]+)$") then -- long switch
local kx, vx = v:match("^%-%-([%w%.]+)$")
opts[kx] = opts[kx] or 1
elseif v:match("^%-%-([%w%.]+)=(.*)$") then -- key=value assignment
local kx, vx = v:match("^%-%-([%w%.]+)=(.*)$")
opts[kx] = vx
else
table.insert(_arg,v)
end
else
_arg[k] = v
end
end
arg = _arg
if opts.help then
print(APPNAME .. " " .. VERSION .. [[ USAGE: nodemcu {[options] .. } {[file1] .. }
options:
-v or -vv increase verbosity
--verbose=<n> define verbosity n = 0..10
-h or --help print this usage help
-s or --silent silent
-e or --execute execute rest of arguments as code
--version display version and exit
--package.path=<p> define or add package path, use '+' to add additional path
examples:
nodemcu boot and execute init.lua and enter Lua console
nodemcu --version
nodemcu --help
nodemcu -vvv
nodemcu --verbose=3
nodemcu test.lua boot and execute test.lua and exit
nodemcu -e 'table.foreach(_sysinfo,print)'
nodemcu --package.path=+./
]])
os.exit()
elseif opts.version then
print(APPNAME,VERSION)
os.exit()
end
if opts.verbose and opts.verbose > 3 then
print("opts:")
table.foreach(opts,function(k,v) print("\t",k,'=',v) end)
print("arg:")
table.foreach(arg,function(k,v) print("\t",k,'=',v) end)
end
_syslog.verbose(opts.verbose or 0)
-- Lua modules
dofile("modules/node/node.lua")
dofile("modules/tmr/tmr.lua")
_syslog.print(_syslog.INFO,"loading modules ('node' and 'tmr' already loaded)") -- only at this point we have functionality for _syslog.print()
--dofile("modules/wifi/wifi.lua")
dofile("modules/file/file.lua")
dofile("modules/gpio/gpio.lua")
dofile("modules/i2c/i2c.lua")
dofile("modules/net/net.lua")
dofile("modules/uart/uart.lua")
dofile("modules/rtctime/rtctime.lua")
dofile("modules/sjson/sjson.lua")
-- inherent built-in modules
bit = require("bit")
table.insert(node.modules,"bit")
struct = require("struct")
table.insert(node.modules,"struct")
--ffi = require("ffi")
--table.insert(node.modules,"ffi")
_syslog.print(_syslog.INFO,"modules bit, struct built-in added")
if math then
table.insert(node.modules,"math")
_syslog.print(_syslog.INFO,"module math added")
end
if opts['package.path'] then
local p = opts['package.path']
if p:match("^%+") then
package.path = package.path .. ":" .. p:match("^%+(.*)")
else
package.path = p
end
_syslog.print(_syslog.INFO,"package.path: "..package.path)
end
-- gather CPU info
_sysinfo = { }
local f = io.popen("lscpu")
repeat
local t = f:read("*line")
if t then
local k, v = t:match("([^:]+):%s+(.+)$")
k = k:lower()
k = k:gsub("%(s%)","s")
k = k:gsub(" ","_")
_sysinfo[k] = v
end
until t==nil
f:close()
-- gather OS info
local f = io.open('/etc/os-release')
repeat
local t = f:read("*line")
if t then
local k, v = t:match("([%w_]+)=(.*)$")
if v:match('"(.+)"') then
v = v:gsub('"(.+)"',function(a) return a end)
end
_sysinfo['os_'..string.lower(k)] = v
end
until t==nil
f:close()
--table.foreach(_sysinfo,print)
local bnr = ""
if false then
local bfn = 'misc/banner.co.txt'
if file.exists(bfn) then
local f = file.open(bfn,"r")
bnr = bnr .. f:read('*all')
f:close()
end
end
bnr = bnr .. APPNAME.." "..VERSION.." powered by ".._VERSION..", "..string.format("Device ID: %d / 0x%x",node.chipid(),node.chipid())
bnr = bnr .. "\n " .. _sysinfo.architecture..(_sysinfo.model_name and " ".._sysinfo.model_name or "").." (".._sysinfo.cpus.." core(s)"..(_sysinfo.cpu_max_mhz and ", "..int(_sysinfo.cpu_min_mhz).."-"..int(_sysinfo.cpu_max_mhz).."MHz" or "")..")"
bnr = bnr .. "\n " .. "modules: "
for i,v in ipairs(node.modules) do
bnr = bnr .. (i>1 and " " or "") .. v
end
if not opts.silent then
print(bnr)
if node._cpufreq then
print(" cpu freq table [MHz]: "..table.concat(node._cpufreq,", "))
end
end
if file.exists("init.lua") then
_syslog.print(_syslog.INFO,"execute init.lua")
dofile("init.lua")
else
_syslog.print(_syslog.WARN,"cannot open 'init.lua'")
end
if false then
local _tmr = coroutine.create(tmr._run_all)
while true do -- wasteful tmr.create():* testing
coroutine.resume(_tmr)
end
end
--table.foreach(arg,print)
if #arg >= 1 then
for i=1,#arg do
if opts.execute then
f = loadstring(arg[i])
else
f = dofile(arg[i])
end
if type(f)=='function' then
f()
end
end
else
--print("== Lua console started (CTRL-C will exit console)")
if arg[-1]:match("luanode") then
require("luanode.tty")
local stdin = luanode.tty.ReadStream(Stdio.stdinFD)
local stdout = luanode.tty.WriteStream(Stdio.stdoutFD)
stdout:write("> ")
stdin:on("data",function(self,s)
local f = loadstring(s)
local r
if type(f)=='function' then
r = f()
else
r = f
end
stdout:write("> ")
end)
stdin:resume()
process:loop()
else
while true do
io.write("> ")
local s = io.read()
local f = loadstring(s)
local r
if type(f)=='function' then
r = f()
else
r = f
end
if r then
r = tostring(r)
if r:match("%n$") then
io.write("> "..r)
else
io.write(": "..r.."\n")
end
end
end
end
end