From f3121f15805630d83b4e93c78fac3c7d812f72c6 Mon Sep 17 00:00:00 2001 From: KubaPro010 Date: Wed, 24 Dec 2025 13:19:43 +0100 Subject: [PATCH] something --- plugin.lua | 27 +++++++++++++++- src/command.lua | 3 +- src/lua_rds.c | 84 +++++++++++++++++++++++++++++++++++++++--------- src/lua_rds.h | 1 + src/rds.c | 5 +++ src/rds_groups.c | 5 +++ 6 files changed, 107 insertions(+), 18 deletions(-) diff --git a/plugin.lua b/plugin.lua index f23c9ad..0a5fc9c 100644 --- a/plugin.lua +++ b/plugin.lua @@ -26,6 +26,24 @@ function on_init() end ---It should be defined by the user in the script. ---@return nil function on_start() end +---This function is called every minute +---It should be defined by the user in the script. +---@return nil +function tick() end +---This function is called in order to handle UDP data +---It should be defined by the user in the script. +---@param data string +---@return string +function data_handle(data) end +---This function is called when the group "L" is in the sequence +---It should be defined by the user in the script. +---@param b integer +---@param c integer +---@param d integer +---@return integer b +---@return integer c +---@return integer d +function group(a, b, c, d) end ---@param pi integer function set_rds_pi(pi) end @@ -239,4 +257,11 @@ function set_rds_udg(xy, groups) end ---Sets the X/Y of the UDG for RDS2 ---@param xy boolean ---@param groups table Table of tables, this should be up to 8 tables containing 4 integers -function set_rds_udg2(xy, groups) end \ No newline at end of file +function set_rds_udg2(xy, groups) end + +---Registers an ODA to be used in the O of the group sequence +---@param group integer +---@param group_version boolean +---@param id integer +---@param id_data integer +function register_oda(group, group_version, id, id_data) end \ No newline at end of file diff --git a/src/command.lua b/src/command.lua index 417624e..1589066 100644 --- a/src/command.lua +++ b/src/command.lua @@ -1,4 +1,4 @@ -if type(data) == "string" and data ~= nil then +function data_handle(data) local cmd, value = data:match("([^=]+)=([^=]+)") if cmd == nil then data = data:lower() @@ -64,6 +64,7 @@ if type(data) == "string" and data ~= nil then end end +---@diagnostic disable-next-line: need-check-nil cmd = cmd:lower() local eon_num, eon_type = cmd:match("^eon(%d+)([a-z]+)$") diff --git a/src/lua_rds.c b/src/lua_rds.c index 73482f1..8991dc2 100644 --- a/src/lua_rds.c +++ b/src/lua_rds.c @@ -515,23 +515,78 @@ void init_lua(RDSModulator* rds_mod) { } void run_lua(char *str, char *cmd_output) { - lua_pushstring(L, str); - lua_setglobal(L, "data"); - - int top = lua_gettop(L); - char path[128]; - snprintf(path, sizeof(path), "%s/.rds95.command.lua", getenv("HOME")); - if (luaL_loadfilex(L, path, NULL) == LUA_OK && lua_pcall(L, 0, 1, 0) == LUA_OK) { - if (lua_isstring(L, -1)) { - if(cmd_output) strcpy(cmd_output, lua_tostring(L, -1)); + const char *home = getenv("HOME"); + if (!home) return; + snprintf(path, sizeof(path), "%s/.rds95.command.lua", home); + + if (luaL_loadfile(L, path) != LUA_OK) { + fprintf(stderr, "Lua error loading file: %s\n", lua_tostring(L, -1)); + lua_pop(L, 1); + return; + } + if (lua_pcall(L, 0, 0, 0) != LUA_OK) { + fprintf(stderr, "Lua error running script: %s\n", lua_tostring(L, -1)); + lua_pop(L, 1); + return; + } + + lua_getglobal(L, "data_handle"); + + if (lua_isfunction(L, -1)) { + lua_pushstring(L, str); + if (lua_pcall(L, 1, 1, 0) == LUA_OK) { + if (lua_isstring(L, -1) && cmd_output) { + const char *res = lua_tostring(L, -1); + strcpy(cmd_output, res); + } + lua_pop(L, 1); + } else { + fprintf(stderr, "Lua error: %s\n", lua_tostring(L, -1)); + lua_pop(L, 1); } + } else if (lua_isstring(L, -1)) { + if (cmd_output) strcpy(cmd_output, lua_tostring(L, -1)); lua_pop(L, 1); } else { - const char *err = lua_tostring(L, -1); - fprintf(stderr, "Lua error: %s\n", err); lua_pop(L, 1); - lua_settop(L, top); + } +} + +void lua_group(RDSGroup* group) { + char path[128]; + const char *home = getenv("HOME"); + if (!home) return; + snprintf(path, sizeof(path), "%s/.rds95.command.lua", home); + + if (luaL_loadfile(L, path) != LUA_OK) { + fprintf(stderr, "Lua error loading file: %s\n", lua_tostring(L, -1)); + lua_pop(L, 1); + return; + } + if (lua_pcall(L, 0, 0, 0) != LUA_OK) { + fprintf(stderr, "Lua error running script: %s\n", lua_tostring(L, -1)); + lua_pop(L, 1); + return; + } + + lua_getglobal(L, "group"); + + if (lua_isfunction(L, -1)) { + lua_pushinteger(L, group->b); + lua_pushinteger(L, group->c); + lua_pushinteger(L, group->d); + if (lua_pcall(L, 3, 3, 0) == LUA_OK) { + group->d = luaL_checkinteger(L, -1); + group->c = luaL_checkinteger(L, -2); + group->b = luaL_checkinteger(L, -3); + lua_pop(L, 3); + } else { + fprintf(stderr, "Lua error: %s\n", lua_tostring(L, -1)); + lua_pop(L, 1); + } + } else { + lua_pop(L, 1); } } @@ -546,9 +601,6 @@ void lua_call_function(const char* function) { return; } - lua_pushnil(L); // Make sure the script doesn't parse any old command - lua_setglobal(L, "data"); - if (lua_pcall(L, 0, 0, 0) != LUA_OK) { const char *err = lua_tostring(L, -1); fprintf(stderr, "Lua error running script: %s\n", err); @@ -560,7 +612,7 @@ void lua_call_function(const char* function) { if (lua_isfunction(L, -1)) { if (lua_pcall(L, 0, 0, 0) != LUA_OK) { - fprintf(stderr, "Lua error running 'on_init': %s\n", lua_tostring(L, -1)); + fprintf(stderr, "Lua error: %s\n", lua_tostring(L, -1)); lua_pop(L, 1); } } else lua_pop(L, 1); diff --git a/src/lua_rds.h b/src/lua_rds.h index 7c83047..f518419 100644 --- a/src/lua_rds.h +++ b/src/lua_rds.h @@ -8,5 +8,6 @@ void init_lua(RDSModulator* rds_mod); void run_lua(char *str, char *cmd_output); +void lua_group(RDSGroup* group); void lua_call_function(const char* function); void destroy_lua(); \ No newline at end of file diff --git a/src/rds.c b/src/rds.c index eff1826..148897a 100644 --- a/src/rds.c +++ b/src/rds.c @@ -28,6 +28,7 @@ void get_rds_eon_group(RDSEncoder* enc, RDSGroup *group); void get_rds_ert_group(RDSEncoder* enc, RDSGroup *group); uint8_t get_rds_custom_groups(RDSEncoder* enc, RDSGroup *group); uint8_t get_rds_custom_groups2(RDSEncoder* enc, RDSGroup *group); +void get_rdsp_lua_group(RDSGroup *group); #define HANDLE_UDG_STREAM(chan_idx, udg_prefix) \ do { \ @@ -108,6 +109,9 @@ static void get_rds_sequence_group(RDSEncoder* enc, RDSGroup *group, char* grp, case 'T': get_rds_fasttuning_group(enc, group); break; + case 'L': + get_rdsp_lua_group(group); + break; case 'U': if(enc->state[enc->program].af_oda == 0) get_rds_oda_af_group(enc, group); else get_rdsp_oda_af_oda_group(group); @@ -153,6 +157,7 @@ static void get_rds_group(RDSEncoder* enc, RDSGroup *group, uint8_t stream) { utc = gmtime(&now); if (utc->tm_min != enc->state[enc->program].last_minute) { + lua_call_function("tick"); enc->state[enc->program].last_minute = utc->tm_min; uint8_t eon_has_ta = 0; diff --git a/src/rds_groups.c b/src/rds_groups.c index 494ad3e..4fb2fdb 100644 --- a/src/rds_groups.c +++ b/src/rds_groups.c @@ -1,5 +1,6 @@ #include "rds.h" #include "lib.h" +#include "lua_rds.h" uint16_t get_next_af(RDSEncoder* enc) { uint16_t out; @@ -364,4 +365,8 @@ uint8_t get_rds_custom_groups2(RDSEncoder* enc, RDSGroup *group) { return 1; } return 0; +} +void get_rdsp_lua_group(RDSGroup *group) { + lua_group(group); + group->is_type_b = (IS_TYPE_B(group->b) != 0); } \ No newline at end of file