Overhaul Lua API

Lua API moved into a tio library table and names adjusted to Lua stdlib style.
Regex in expect() replaced with Lua patterns so binary data can be handled.
New tio.alwaysecho variable allows enabling and disabling echo to console.
Read and write functions now manage complex retry and timeout logic internally,
giving the user a simple "nil if fail" API like the rest of Lua.
exit() was removed, os.exit() already exists in the Lua standard library.
This commit is contained in:
ii8 2025-06-13 15:49:33 +01:00 committed by Martin Lund
parent 381c0b7823
commit 86f48a2fb6
9 changed files with 318 additions and 465 deletions

View file

@ -69,7 +69,7 @@ color = 13
[esp32]
device = /dev/ttyUSB0
color = 14
script = set{DTR=high,RTS=low}; msleep(100); set{DTR=low,RTS=high}; msleep(100); set{RTS=low}
script = tio.set{DTR=high,RTS=low}; tio.msleep(100); tio.set{DTR=low,RTS=high}; tio.msleep(100); tio.set{RTS=low}
script-run = always
[buspirate]

View file

@ -13,14 +13,13 @@ local logins = {
},
}
local found, match_str = expect("\\w+- login:", 10)
if (1 == found) then
local hostname = string.match(match_str, "^%w+")
local hostname = tio.expect("^(%g+) login:", 10)
if hostname then
local login = logins[hostname]
if (nil ~= login) then
write(login.username .. "\n")
expect("Password:")
write(login.password .. "\n")
tio.write(login.username .. "\n")
tio.expect("Password:")
tio.write(login.password .. "\n")
else
io.write("\r\nDon't know login info for " .. hostname .. "\r\n")
end

View file

@ -1,5 +1,5 @@
set{DTR=high, RTS=low}
msleep(100)
set{DTR=low, RTS=high}
msleep(100)
set{RTS=toggle}
tio.set{DTR=high, RTS=low}
tio.msleep(100)
tio.set{DTR=low, RTS=high}
tio.msleep(100)
tio.set{RTS=toggle}

View file

@ -1,14 +1,14 @@
read(1000, 6000) -- initial config
write("\n")
msleep(100)
read(650, 60) -- main menu
write("S") -- S menu
msleep(30)
read(650, 60)
write("t") -- Parallel Value Table
read(650, 60)
tio.read(1000, 6000) -- initial config
tio.write("\n")
tio.msleep(100)
tio.read(650, 60) -- main menu
tio.write("S") -- S menu
tio.msleep(30)
tio.read(650, 60)
tio.write("t") -- Parallel Value Table
tio.read(650, 60)
while true do
msleep(1000)
write("t")
read(650, 50) -- repeat PVT forever
tio.msleep(1000)
tio.write("t")
tio.read(650, 50) -- repeat PVT forever
end

View file

@ -1,17 +1,15 @@
read(1000, 8000) -- read initial config
write("\n")
read(650, 100) -- main menu
write("S") -- S menu
n = 1
while n > 0 do -- while not empty, read more
n, str = read_line(25)
end
tio.read(1000, 8000) -- read initial config
tio.write("\n")
tio.read(650, 100) -- main menu
tio.write("S") -- S menu
repeat
str = tio.readline(25)
until str == nil
while true do
write("t") -- query PV table
msleep(880)
n = 1
while n > 0 do -- while not empty, read more
n, str = read_line(60)
msleep(60)
end
tio.write("t") -- query PV table
tio.msleep(880)
repeat
str = tio.readline(60)
tio.msleep(60)
until str == nil
end

View file

@ -1,6 +1,6 @@
io.write("Searching... ")
local device = tty_search()
local device = tio.ttysearch()
io.write("done\r\n")