implemented read_ts as a lua function

This commit is contained in:
Keith Hill 2024-10-25 11:22:56 -05:00
parent ab678e6c88
commit 9d74fe9e74
5 changed files with 93 additions and 4 deletions

16
examples/lua/pvt.lua Normal file
View file

@ -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

View file

@ -1,12 +1,12 @@
project('tio', 'c', project('tio', 'c',
version : '3.8', version : '3.8.1',
license : [ 'GPL-2'], license : [ 'GPL-2'],
meson_version : '>= 0.53.2', meson_version : '>= 0.53.2',
default_options : [ 'warning_level=2', 'buildtype=release', 'c_std=gnu99' ] default_options : [ 'warning_level=2', 'buildtype=release', 'c_std=gnu99' ]
) )
# The tag date of the project_version(), update when the version bumps. # 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 # Test for dynamic baudrate configuration interface
compiler = meson.get_compiler('c') compiler = meson.get_compiler('c')

View file

@ -37,6 +37,7 @@
#include "log.h" #include "log.h"
#include "script.h" #include "script.h"
#include "fs.h" #include "fs.h"
#include "timestamp.h"
#define MAX_BUFFER_SIZE 2000 // Maximum size of circular buffer #define MAX_BUFFER_SIZE 2000 // Maximum size of circular buffer
@ -301,6 +302,77 @@ error:
return 2; 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<bytes_read; i++)
{
putchar(buffer[i]);
if (option.log)
{
log_putc(buffer[i]);
}
}
}
ret = bytes_read;
error:
lua_pushnumber(L, ret);
if (buffer != NULL)
{
lua_pushstring(L, buffer);
free(buffer);
}
return 2;
}
// lua: expect(string, timeout) // lua: expect(string, timeout)
static int expect(lua_State *L) static int expect(lua_State *L)
{ {
@ -457,6 +529,7 @@ static const struct luaL_Reg tio_lib[] =
{ "modem_send", modem_send}, { "modem_send", modem_send},
{ "send", send_}, { "send", send_},
{ "read", read_string}, { "read", read_string},
{ "read_ts", read_ts },
{ "expect", expect}, { "expect", expect},
{ "exit", exit_}, { "exit", exit_},
{ "tty_search", tty_search_}, { "tty_search", tty_search_},

View file

@ -29,8 +29,6 @@
#include "options.h" #include "options.h"
#include "timestamp.h" #include "timestamp.h"
#define TIME_STRING_SIZE_MAX 24
char *timestamp_current_time(void) char *timestamp_current_time(void)
{ {
static char time_string[TIME_STRING_SIZE_MAX]; static char time_string[TIME_STRING_SIZE_MAX];

View file

@ -32,5 +32,7 @@ typedef enum
TIMESTAMP_END, TIMESTAMP_END,
} timestamp_t; } timestamp_t;
#define TIME_STRING_SIZE_MAX 24
char *timestamp_current_time(void); char *timestamp_current_time(void);