From 9e51d2e4ee57c4f77b66872fb46601d6f28802bc Mon Sep 17 00:00:00 2001 From: yabu76 Date: Tue, 23 Sep 2025 19:15:30 +0900 Subject: [PATCH] Add Lua API twrite The twrite Lua API is a variation of the write API, with the output character mapping and output-delay / output-line-delay features enabled. Since it processes each character internally, writing speed is slower than the write API. ~~~~~~~~~~~~~~~~~~~~~~~ twrite(string) Translate string with output character mapping and write the translated string to serial device with output-delay / output-line-delay. --- src/script.c | 18 ++++++++++++++++++ src/tty.h | 2 ++ 2 files changed, 20 insertions(+) diff --git a/src/script.c b/src/script.c index cb104bd..998c085 100644 --- a/src/script.c +++ b/src/script.c @@ -286,6 +286,23 @@ static int api_write(lua_State *L) return 1; } +// lua: tio.twrite(string) +static int api_twrite(lua_State *L) +{ + size_t len = 0; + const char *string = luaL_checklstring(L, 1, &len); + + for (; len > 0; --len, string++) + { + forward_to_tty(device_fd, *string); + } + tty_sync(device_fd); + + lua_getglobal(L, "tio"); + + return 1; +} + // lua: tio.read(size, timeout) static int api_read(lua_State *L) { @@ -462,6 +479,7 @@ static const struct luaL_Reg tio_lib[] = { "line_set", line_set}, { "send", api_send}, { "write", api_write}, + { "twrite", api_twrite}, { "read", api_read}, { "readline", api_readline}, { "ttysearch", api_ttysearch}, diff --git a/src/tty.h b/src/tty.h index 39c64af..eb3dfd4 100644 --- a/src/tty.h +++ b/src/tty.h @@ -84,3 +84,5 @@ void tty_input_thread_wait_ready(void); void tty_line_set(int fd, tty_line_config_t line_config[]); void tty_search(void); GList *tty_search_for_serial_devices(void); +void forward_to_tty(int fd, char output_char); +void tty_sync(int fd);