Terminal related Controller commands changed, Terminal output bugfix, Controller event handling bugfix

This commit is contained in:
Joachim Stolberg 2019-01-29 20:52:38 +01:00
parent 132ca85c66
commit 0d12261620
4 changed files with 125 additions and 42 deletions

@ -2,6 +2,21 @@
## V2.02.01 (2019-01-29)
### Additions
### Removals
### Changes
- Terminal related Controller commands changed
### Fixes
- Terminal output bugfix
- Controller event handling bugfix
## V2.02 (2019-01-28) ## V2.02 (2019-01-28)
### Additions ### Additions

@ -25,17 +25,6 @@ sl_controller.register_function("get_input", {
" The device has to be connected with the controller." " The device has to be connected with the controller."
}) })
sl_controller.register_function("get_string", {
cmnd = function(self, num)
num = tostring(num or "")
return sl_controller.get_command(self.meta.number)
end,
help = ' $get_string() --> text sting or nil\n'..
' Read an entered string (command) from the Terminal.\n'..
' example: s = $get_string()\n'..
" The Terminal has to be connected with the controller."
})
sl_controller.register_function("get_status", { sl_controller.register_function("get_status", {
cmnd = function(self, num) cmnd = function(self, num)
num = tostring(num or "") num = tostring(num or "")
@ -181,16 +170,6 @@ sl_controller.register_action("display", {
' example: $display("0123", 1, "Hello ", name, " !")' ' example: $display("0123", 1, "Hello ", name, " !")'
}) })
sl_controller.register_action("terminal", {
cmnd = function(self, num, text)
text = tostring(text or "")
tubelib.send_message(num, self.meta.owner, nil, "term", text)
end,
help = " $terminal(num, text)\n"..
' Send a text line to the terminal with number "num".\n'..
' example: $terminal("0123", "Hello "..name)'
})
sl_controller.register_action("clear_screen", { sl_controller.register_action("clear_screen", {
cmnd = function(self, num) cmnd = function(self, num)
num = tostring(num or "") num = tostring(num or "")

@ -103,7 +103,7 @@ sl_controller.register_action("loopcycle", {
sl_controller.register_action("events", { sl_controller.register_action("events", {
cmnd = function(self, event) cmnd = function(self, event)
self.events = event or false self.meta.events = event or false
end, end,
help = "$events(true/false)\n".. help = "$events(true/false)\n"..
" Enable/disable event handling.\n".. " Enable/disable event handling.\n"..
@ -282,8 +282,9 @@ local function compile(pos, meta, number)
local code = safer_lua.init(pos, init, func.."\n"..loop, env, error) local code = safer_lua.init(pos, init, func.."\n"..loop, env, error)
if code then if code then
Cache[number] = {code=code, inputs={}} Cache[number] = {code=code, inputs={}, events=env.meta.events}
Cache[number].inputs.term = "" -- terminal inputs Cache[number].inputs.term = nil -- terminal inputs
Cache[number].inputs.msg = {} -- message queue
return true return true
end end
return false return false
@ -361,7 +362,6 @@ local function call_loop(pos, meta, elapsed)
local t = minetest.get_us_time() local t = minetest.get_us_time()
local number = meta:get_string("number") local number = meta:get_string("number")
if Cache[number] or compile(pos, meta, number) then if Cache[number] or compile(pos, meta, number) then
local cpu = meta:get_int("cpu") or 0 local cpu = meta:get_int("cpu") or 0
local code = Cache[number].code local code = Cache[number].code
local res = safer_lua.run_loop(pos, elapsed, code, error) local res = safer_lua.run_loop(pos, elapsed, code, error)
@ -375,6 +375,10 @@ local function call_loop(pos, meta, elapsed)
return false return false
end end
end end
-- further messages available?
if next(Cache[number].inputs["msg"]) then
minetest.after(1, call_loop, pos, meta, -1)
end
return res return res
end end
return false return false
@ -522,13 +526,21 @@ minetest.register_craft({
local function set_input(pos, number, input, val) local function set_input(pos, number, input, val)
if input and M(pos):get_int("state") == tubelib.RUNNING then if input and M(pos):get_int("state") == tubelib.RUNNING then
if Cache[number] and Cache[number].inputs then if Cache[number] and Cache[number].inputs then
Cache[number].inputs[input] = val if input == "msg" then
-- only one event per second if #Cache[number].inputs["msg"] < 10 then
local t = minetest.get_us_time() table.insert(Cache[number].inputs["msg"], val)
if not Cache[number].last_event or Cache[number].last_event < t then end
local meta = minetest.get_meta(pos) else
minetest.after(0.01, call_loop, pos, meta, -1) Cache[number].inputs[input] = val
Cache[number].last_event = t + 500000 -- add 500 ms end
if Cache[number].events then -- events enabled?
-- only one event per second
local t = minetest.get_us_time()
if not Cache[number].last_event or Cache[number].last_event < t then
local meta = minetest.get_meta(pos)
minetest.after(0.01, call_loop, pos, meta, -1)
Cache[number].last_event = t + 500000 -- add 500 ms
end
end end
end end
end end
@ -547,11 +559,17 @@ end
-- used for Terminal commands -- used for Terminal commands
function sl_controller.get_command(number) function sl_controller.get_command(number)
if Cache[number] and Cache[number].inputs then if Cache[number] and Cache[number].inputs then
local cmnd = Cache[number].inputs["term"] or "" local cmnd = Cache[number].inputs["term"]
Cache[number].inputs["term"] = "" Cache[number].inputs["term"] = nil
return cmnd return cmnd
end end
return "" end
-- used for queued messages
function sl_controller.get_msg(number)
if Cache[number] and Cache[number].inputs then
return table.remove(Cache[number].inputs["msg"], 1)
end
end end
tubelib.register_node("sl_controller:controller", {}, { tubelib.register_node("sl_controller:controller", {}, {
@ -565,6 +583,8 @@ tubelib.register_node("sl_controller:controller", {}, {
set_input(pos, number, payload, topic) set_input(pos, number, payload, topic)
elseif topic == "term" then elseif topic == "term" then
set_input(pos, number, "term", payload) set_input(pos, number, "term", payload)
elseif topic == "msg" then
set_input(pos, number, "msg", payload)
elseif topic == "state" then elseif topic == "state" then
local running = meta:get_int("running") or tubelib.STATE_STOPPED local running = meta:get_int("running") or tubelib.STATE_STOPPED
return tubelib.statestring(running) return tubelib.statestring(running)

@ -24,8 +24,9 @@ Local commands:
- help = this message - help = this message
- pub = switch to public use - pub = switch to public use
- priv = switch to private use - priv = switch to private use
Test commands: Global commands:
- send <num> on/off = send on/off event - send <num> on/off = send on/off event
- msg <num> <text> = send a text message
]] ]]
local function formspec1() local function formspec1()
@ -82,16 +83,25 @@ local function command(pos, cmnd, player)
output(pos, player..":$ "..cmnd) output(pos, player..":$ "..cmnd)
output(pos, "Switched to private use!") output(pos, "Switched to private use!")
elseif meta:get_int("public") == 1 or owner == player then elseif meta:get_int("public") == 1 or owner == player then
-- send <num> on/off
local num, topic = cmnd:match('^send%s+([0-9]+)%s+([onff]+)$') local num, topic = cmnd:match('^send%s+([0-9]+)%s+([onff]+)$')
if num and topic then if num and topic then
local own_number = meta:get_string("own_number") local own_number = meta:get_string("own_number")
output(pos, player..":$ send "..num.." "..topic) output(pos, player..":$ send "..num.." "..topic)
tubelib.send_message(num, owner, nil, topic, own_number) tubelib.send_message(num, owner, nil, topic, own_number)
else return
local number = meta:get_string("number") or "0000"
output(pos, player..":$ "..cmnd)
tubelib.send_message(number, owner, nil, "term", cmnd)
end end
-- msg <num> <text>
local num, text = cmnd:match('^msg%s+([0-9]+)%s+(.+)$')
if num and text then
local own_number = meta:get_string("own_number")
output(pos, player..":$ msg "..num.." "..text)
tubelib.send_message(num, owner, nil, "msg", {src=own_number, text=text})
return
end
local number = meta:get_string("number") or "0000"
output(pos, player..":$ "..cmnd)
tubelib.send_message(number, owner, nil, "term", cmnd)
end end
end end
end end
@ -203,11 +213,70 @@ minetest.register_craft({
}, },
}) })
tubelib.register_node("sl_controller:terminal", {"sl_controller:terminal2", "sl_controller:terminal3"}, { tubelib.register_node("sl_controller:terminal", {}, {
on_recv_message = function(pos, topic, payload) on_recv_message = function(pos, topic, payload)
if topic == "term" then if topic == "term" then
output(pos, payload) output(pos, payload)
return true return true
elseif topic == "msg" then
output(pos, payload.src..": "..payload.text)
return true
end end
end, end,
}) })
tubelib.register_node("sl_controller:terminal2", {}, {
on_recv_message = function(pos, topic, payload)
if topic == "term" then
output(pos, payload)
return true
elseif topic == "msg" then
output(pos, payload.src..": "..payload.text)
return true
end
end,
})
sl_controller.register_function("get_term", {
cmnd = function(self)
return sl_controller.get_command(self.meta.number)
end,
help = ' $get_term() --> text string or nil\n'..
' Read an entered string (command) from the Terminal.\n'..
' example: s = $get_term()\n'..
" The Terminal has to be connected to the controller."
})
sl_controller.register_action("put_term", {
cmnd = function(self, num, text)
text = tostring(text or "")
tubelib.send_message(num, self.meta.owner, nil, "term", text)
end,
help = " $put_term(num, text)\n"..
' Send a text line to the terminal with number "num".\n'..
' example: $put_term("0123", "Hello "..name)'
})
sl_controller.register_function("get_msg", {
cmnd = function(self)
local msg = sl_controller.get_msg(self.meta.number)
if msg then
return msg.src, msg.text
end
end,
help = ' $get_msg() --> number and text string or nil\n'..
' Read a received messages. Number is the node\n'..
' number of the sender.\n'..
' example: num,msg = $get_msg().'
})
sl_controller.register_action("send_msg", {
cmnd = function(self, num, text)
local msg = {src = self.meta.number, text = tostring(text or "")}
tubelib.send_message(num, self.meta.owner, nil, "msg", msg)
end,
help = " $send_msg(num, text)\n"..
' Send a message to the controller with number "num".\n'..
' example: $send_msg("0123", "test")'
})