log serial io to _G.modem-spew instead of stdout

This commit is contained in:
Daniel Barlow 2022-09-16 17:01:21 +01:00
parent 429a661346
commit 02ea16374c
1 changed files with 13 additions and 8 deletions

21
sms.fnl
View File

@ -23,9 +23,14 @@
(fn escape-readably [s]
(s:gsub "%c" (fn [x] (string.format "\\u{%.3x}" (string.byte x)))))
(fn log-xfer [direction message]
(when _G.modem-spew
(_G.modem-spew:write direction " " (escape-readably message) "\n"))
message)
(fn tx [fd s]
(write fd s)
(print (.. ">>> " (escape-readably s)))
(log-xfer :>>> s)
(nanosleep {:tv_sec 0 :tv_nsec (* 10 1000 1000)}))
(fn chars [i]
@ -47,15 +52,15 @@
(fn expect [fd pattern fail-pattern]
(let [b (read fd 1024)]
(if (> (# b) 0)
(do
(print (.. "<<< " (escape-readably b)))
(if (string.find b pattern)
(do (print "found" pattern) true)
(and fail-pattern (string.find b fail-pattern))
(error (.. "Expected " pattern ", got " (escape-readably b)))
(expect fd pattern)))
(if (string.find (log-xfer :<<< b) pattern)
(log-xfer :!!! pattern)
(and fail-pattern (string.find b fail-pattern))
(error (.. "Expected " pattern ", got " (escape-readably b)))
(expect fd pattern fail-pattern))
nil)))
(fn even? [x]
(= (% x 2) 0))