aboutsummaryrefslogtreecommitdiff
path: root/diller/inter.lua
diff options
context:
space:
mode:
Diffstat (limited to 'diller/inter.lua')
-rw-r--r--diller/inter.lua73
1 files changed, 73 insertions, 0 deletions
diff --git a/diller/inter.lua b/diller/inter.lua
new file mode 100644
index 0000000..725edbd
--- /dev/null
+++ b/diller/inter.lua
@@ -0,0 +1,73 @@
+local P = {}
+
+function P.parse(line)
+ -- print("inter_parse: line="..line)
+
+ line = string.sub(line, 1, string.find(line, "\r"))
+ line = line:gsub("%s+", "")
+
+ local args = {}
+ local i = string.find(line, " ")
+ if not i then
+-- print("inter_parse: invalid line: no command, line="..line)
+ return string.len(line) > 0 and line or nil, args
+ end
+ local cmd = string.sub(line, 1, i - 1)
+ -- print("inter_parse: cmd="..cmd)
+
+ local last = i + 1
+ while i do
+ local key
+ local value
+
+ i = string.find(line, " ", last)
+ arg = string.sub(line, last, i)
+ arg = arg:gsub("%s+", "")
+
+ if #arg > 0 then
+-- print("inter_parse: arg: "..arg)
+ j = string.find(arg, "=")
+ if not j then
+ if #arg > 0 then
+ key = arg
+ args[key] = true
+-- print("inter_parse: key="..key.."=true")
+ end
+ else
+ key = string.sub(arg, 1, j-1)
+ value = string.sub(arg, j+1)
+ args[key] = value
+-- print("inter_parse: key="..key..", value="..value)
+ end
+ end
+
+ if not i then
+ break
+ end
+ last = i + 1
+ end
+
+ return cmd, args
+end
+
+local cb
+function inter_on_line(line)
+-- print("line:"..line)
+
+ local cmd, args = P.parse(line)
+ if not cmd then
+ return
+ end
+
+ if cb then
+ cb(cmd, args)
+ end
+end
+
+function P.init(callback)
+ cb = callback
+
+ uart.on("data", "\r", inter_on_line, 0)
+end
+
+return P