Clean up lua API

Rename modem_send() to send()
Rename send to write()
This commit is contained in:
Martin Lund 2024-11-30 11:03:40 +01:00
parent 4511d74a9e
commit c49faa7337
6 changed files with 55 additions and 44 deletions

View file

@ -401,12 +401,30 @@ expect(string, timeout)
On successful match it also returns the match string as second return value. On successful match it also returns the match string as second return value.
send(string) read(size, timeout)
Send string. Read from serial device. If timeout is 0 or not provided it will wait
forever until data is ready to read.
Returns number of bytes read on success, 0 on timeout, or -1 on error.
On success, returns read string as second return value.
read_line(timeout)
Read line from serial device. If timeout is 0 or not provided it will
wait forever until data is ready to read.
Returns number of bytes read on success, 0 on timeout, or -1 on error.
On success, returns the string that was read as second return value.
Also emits a single timestamp to stdout and log file per options.timestamp
and options.log.
write(string)
Write string to serial device.
Returns number of bytes written on success or -1 on error. Returns number of bytes written on success or -1 on error.
modem_send(file, protocol) send(file, protocol)
Send file using x/y-modem protocol. Send file using x/y-modem protocol.
Protocol can be any of XMODEM_1K, XMODEM_CRC, YMODEM. Protocol can be any of XMODEM_1K, XMODEM_CRC, YMODEM.
@ -421,14 +439,6 @@ tty_search()
Returns nil if no serial devices are found. Returns nil if no serial devices are found.
read(size, timeout)
Read from serial device. If timeout is 0 or not provided it will wait
forever until data is ready to read.
Returns number of bytes read on success, 0 on timeout, or -1 on error.
On success, returns read string as second return value.
set{line=state, ...} set{line=state, ...}
Set state of one or multiple tty modem lines. Set state of one or multiple tty modem lines.

View file

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

View file

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

View file

@ -1,13 +1,13 @@
read(1000, 8000) -- read initial config read(1000, 8000) -- read initial config
send("\n") write("\n")
read(650, 100) -- main menu read(650, 100) -- main menu
send("S") -- S menu write("S") -- S menu
n = 1 n = 1
while n > 0 do -- while not empty, read more while n > 0 do -- while not empty, read more
n, str = read_line(25) n, str = read_line(25)
end end
while true do while true do
send("t") -- query PV table write("t") -- query PV table
msleep(880) msleep(880)
n = 1 n = 1
while n > 0 do -- while not empty, read more while n > 0 do -- while not empty, read more

View file

@ -428,7 +428,8 @@ Send ctrl-t character
.PP .PP
Tio suppots Lua scripting to easily automate interaction with the tty device. Tio suppots Lua scripting to easily automate interaction with the tty device.
In addition to the Lua API tio makes the following functions available: In addition to the standard Lua API tio makes the following functions
available:
.TP 6n .TP 6n
@ -441,26 +442,6 @@ Returns 1 on successful match, 0 on timeout, or -1 on error.
On successful match it also returns the match string as second return value. On successful match it also returns the match string as second return value.
.IP "\fBsend(string)"
Send string.
Returns number of bytes written on success or -1 on error.
.IP "\fBmodem_send(file, protocol)"
Send file using x/y-modem protocol.
Protocol can be any of XMODEM_1K, XMODEM_CRC, YMODEM.
.IP "\fBtty_search()"
Search for serial devices.
Returns a table of number indexed tables, one for each serial device found.
Each of these tables contains the serial device information accessible via the
following string indexed elements "path", "tid", "uptime", "driver",
"description".
Returns nil if no serial devices are found.
.IP "\fBread(size, timeout)" .IP "\fBread(size, timeout)"
Read from serial device. If timeout is 0 or not provided it will wait forever Read from serial device. If timeout is 0 or not provided it will wait forever
until data is ready to read. until data is ready to read.
@ -480,6 +461,26 @@ On success, returns the string that was read as second return value. Also
emits a single timestamp to stdout and log file per options.timestamp emits a single timestamp to stdout and log file per options.timestamp
and options.log. and options.log.
.IP "\fBwrite(string)"
Write string to serial device.
Returns number of bytes written on success or -1 on error.
.IP "\fBsend(file, protocol)"
Send file using x/y-modem protocol.
Protocol can be any of XMODEM_1K, XMODEM_CRC, YMODEM.
.IP "\fBtty_search()"
Search for serial devices.
Returns a table of number indexed tables, one for each serial device found.
Each of these tables contains the serial device information accessible via the
following string indexed elements "path", "tid", "uptime", "driver",
"description".
Returns nil if no serial devices are found.
.IP "\fBset{line=state, ...}" .IP "\fBset{line=state, ...}"
Set state of one or multiple tty modem lines. Set state of one or multiple tty modem lines.

View file

@ -185,7 +185,7 @@ static int modem_send(lua_State *L)
} }
// lua: send(string) // lua: send(string)
static int send_(lua_State *L) static int write_(lua_State *L)
{ {
const char *string = lua_tostring(L, 1); const char *string = lua_tostring(L, 1);
int ret; int ret;
@ -550,8 +550,8 @@ static const struct luaL_Reg tio_lib[] =
{ "sleep", sleep_}, { "sleep", sleep_},
{ "msleep", msleep}, { "msleep", msleep},
{ "line_set", line_set}, { "line_set", line_set},
{ "modem_send", modem_send}, { "send", modem_send},
{ "send", send_}, { "write", write_},
{ "read", read_string}, { "read", read_string},
{ "read_line", read_line}, { "read_line", read_line},
{ "expect", expect}, { "expect", expect},