diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-10-11 14:07:42 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-10-11 14:07:42 +0200 |
commit | d9da54abcc784ecca5e1c0c415820a32e68c2296 (patch) | |
tree | c5eee8f9944a3a8e89fdc011b1d77b09b0c95737 /diller/inter.lua | |
parent | 5fb5e20aa00ead18785c14fe788cdfdbced14f63 (diff) | |
download | esp-playground-d9da54abcc784ecca5e1c0c415820a32e68c2296.tar.gz esp-playground-d9da54abcc784ecca5e1c0c415820a32e68c2296.tar.bz2 esp-playground-d9da54abcc784ecca5e1c0c415820a32e68c2296.tar.xz esp-playground-d9da54abcc784ecca5e1c0c415820a32e68c2296.zip |
o Initial import of diller firmware.
Diffstat (limited to 'diller/inter.lua')
-rw-r--r-- | diller/inter.lua | 73 |
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 |