-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.lua
82 lines (72 loc) · 2.32 KB
/
run.lua
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
--- Simple run script.
-- This script will execute a state machine from a library. This script must be
-- placed in the same directory with hsm.lua.
-- @usage lua run.lua [debug] [forever] [sleep=none|os|socket] [time=ahsm|os|socket] <fsm.lua>
-- @usage [debug] enables the ahsm logging using print.
-- @usage [forever] do not not stop even if the machine becomes idle.
-- @usage [sleep=none|os|socket] selects a sleeping mode.
-- "none" is polling, "os" uses a call to the sleep pogram,
-- and "socket" uses luasockets sleep().
-- @usage [time=ahsm|os|socket] sets the ahsm.gettime function.
-- "ahsm" keeps it unchanged, "os" sets to call to os.time(),
-- and "socket" set to luasockets gettime()
-- @script run.lua
local forever = false
local config = {
sleep = 'none', -- 'os', 'socket'
time = 'ahsm', -- 'socket'
}
local function exit_on_error(err)
io.stderr:write( (err or '')..'\n' )
io.stderr:write( 'syntax:\n lua run.lua [debug] [forever] [sleep=none|os|socket] [time=ahsm|os|socket] <fsm.lua>\n' )
os.exit(1)
end
local ahsm = require 'ahsm'
-- get parameters
if #arg==0 then exit_on_error('Missing parameter') end
local filename = arg[#arg]
for i = 1, #arg-1 do
local param = arg[i]
if param == 'debug' then
ahsm.debug = require 'tools/debug_plain'.out
elseif param == 'forever' then
forever = true
else
local k, v = string.match(param, '^([^=]*)=(.*)$')
if k and v then
if not config[k] then
exit_on_error ('Unknown parameter '..tostring(k))
end
config[k] = v
else
exit_on_error ('Error parsing: '..tostring(param))
end
end
end
-- initialize libs
local socket
if config.time=='socket' or config.sleep=='socket' then
socket = require 'socket'
end
if config.time == 'socket' then
ahsm.get_time = socket.gettime
elseif config.time == 'os' then
ahsm.get_time = os.time
end
-- load hsm
local root = assert(dofile(filename))
local hsm = ahsm.init( root ) -- create fsm from root composite state
-- run hsm
repeat
local next_t = hsm.loop()
if config.sleep ~= 'none' then
if next_t then
local dt = next_t-ahsm.get_time()
if config.sleep == 'os' then
if dt>0 and os.execute("sleep "..dt) ~= 0 then break end
elseif config.sleep == 'socket' then
socket.sleep(dt)
end
end
end
until (not forever and not next_t)