From 9d74fe9e74d9564c5c78279b5ab0a4682838f14f Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Fri, 25 Oct 2024 11:22:56 -0500 Subject: [PATCH] implemented read_ts as a lua function --- examples/lua/pvt.lua | 16 ++++++++++ meson.build | 4 +-- src/script.c | 73 ++++++++++++++++++++++++++++++++++++++++++++ src/timestamp.c | 2 -- src/timestamp.h | 2 ++ 5 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 examples/lua/pvt.lua diff --git a/examples/lua/pvt.lua b/examples/lua/pvt.lua new file mode 100644 index 0000000..fe58910 --- /dev/null +++ b/examples/lua/pvt.lua @@ -0,0 +1,16 @@ +send("\n") +msleep(100) +-- read buffer with 0.5 sec delay +read(160, 500) +send("S") +msleep(100) +-- read buffer +read(160, 500) +-- query Parallel Value Table +send("t") +print(read(300, 500)) +while true do + sleep(1) + send("t") + print(read(300, 500)) +end diff --git a/meson.build b/meson.build index 810f9dd..416b2bd 100644 --- a/meson.build +++ b/meson.build @@ -1,12 +1,12 @@ project('tio', 'c', - version : '3.8', + version : '3.8.1', license : [ 'GPL-2'], meson_version : '>= 0.53.2', default_options : [ 'warning_level=2', 'buildtype=release', 'c_std=gnu99' ] ) # The tag date of the project_version(), update when the version bumps. -version_date = '2024-08-31' +version_date = '2024-10-24' # Test for dynamic baudrate configuration interface compiler = meson.get_compiler('c') diff --git a/src/script.c b/src/script.c index d9efb09..2f61f3b 100644 --- a/src/script.c +++ b/src/script.c @@ -37,6 +37,7 @@ #include "log.h" #include "script.h" #include "fs.h" +#include "timestamp.h" #define MAX_BUFFER_SIZE 2000 // Maximum size of circular buffer @@ -301,6 +302,77 @@ error: return 2; } +// lua: ret,string = read_ts(size, timeout) +static int read_ts(lua_State *L) +{ + int size = lua_tointeger(L, 1) + 1; //plus one for null-terminated string + int timeout = lua_tointeger(L, 2); + int ret = 0; + + char *buffer = malloc(size); + if (buffer == NULL) + { + ret = -1; // Error + goto error; + } + + if (timeout == 0) + { + timeout = -1; // Wait forever + } + + ssize_t bytes_read = read_poll(device_fd, buffer, size, timeout); + if (bytes_read < 0) + { + ret = -1; // Error + goto error; + } + else if (bytes_read == 0) + { + ret = 0; // Timeout + goto error; + } + else + { + buffer[bytes_read] = (char)0; + } + + if (option.timestamp) + { + char *pTimeStampNow; + pTimeStampNow = timestamp_current_time(); + if (pTimeStampNow) + { + tio_printf("%s", buffer); //does timestamps for us + if (option.log) + { + log_printf("\n[%s] %s", pTimeStampNow, buffer); + } + } + } else { + for (ssize_t i=0; i