0
1
mirror of https://github.com/radio95-rnt/rds95.git synced 2026-02-26 20:33:53 +01:00
This commit is contained in:
2025-12-22 19:13:17 +01:00
parent 630fc747d6
commit ff9f3d1bb8
7 changed files with 265 additions and 22 deletions

16
.vscode/settings.json vendored
View File

@@ -56,6 +56,18 @@
"set_rds_program",
"set_rds_level",
"set_rds_rt_text_timeout",
"set_rds_program_defaults"
]
"set_rds_program_defaults",
"reset_rds",
"max_programs",
"on_init",
"get_rds_pi",
"get_rds_ecc",
"get_rds_pty",
"get_rds_slc_data",
"get_rds_ct",
"get_rds_dpty",
"get_rds_tp",
"get_rds_ta"
],
"Lua.runtime.plugin": "plugin.lua"
}

178
plugin.lua Normal file
View File

@@ -0,0 +1,178 @@
---@meta
-- Global Variables
---@type string
core_version = ""
---@type integer
max_programs = 0
---@type string|nil
data = "" -- Set during run_lua calls
---Starts the initialization sequence, also calls the on_init function
---@return nil
function set_rds_program_defaults() end
---Saves, loads and resets the state of the data
---@return nil
function reset_rds() end
---This function is called by the C core after the script is loaded.
---It should be defined by the user in the script.
---@return nil
function on_init() end
---@param pi integer
function set_rds_pi(pi) end
---@return integer
function get_rds_pi() end
---@param pty integer
function set_rds_pty(pty) end
---@return integer
function get_rds_pty() end
---@param ecc integer
function set_rds_ecc(ecc) end
---@return integer
function get_rds_ecc() end
---@param slc_data integer
function set_rds_slc_data(slc_data) end
---@return integer
function get_rds_slc_data() end
---@param ct integer 0 or 1
function set_rds_ct(ct) end
---@return integer
function get_rds_ct() end
---@param dpty integer 0 or 1
function set_rds_dpty(dpty) end
---@return integer
function get_rds_dpty() end
---@param tp integer 0 or 1
function set_rds_tp(tp) end
---@return integer
function get_rds_tp() end
---@param ta integer 0 or 1
function set_rds_ta(ta) end
---@return integer
function get_rds_ta() end
-- Feature Flags
---@param enabled integer 0 or 1
function set_rds_rt1_enabled(enabled) end
---@return integer
function get_rds_rt1_enabled() end
---@param enabled integer 0 or 1
function set_rds_rt2_enabled(enabled) end
---@return integer
function get_rds_rt2_enabled() end
---@param enabled integer 0 or 1
function set_rds_ptyn_enabled(enabled) end
---@return integer
function get_rds_ptyn_enabled() end
---@param rt_type integer 0 (RT A/B) or 1 (RT C)
function set_rds_rt_type(rt_type) end
---@return integer
function get_rds_rt_type() end
-- Modulation & Generation
---@param mode integer
function set_rds_rds2mod(mode) end
---@return integer
function get_rds_rds2mod() end
---@param rdsgen integer
function set_rds_rdsgen(rdsgen) end
---@return integer
function get_rds_rdsgen() end
---@param level number
function set_rds_level(level) end
---@return number
function get_rds_level() end
-- Program & Linking
---@param linkage integer
function set_rds_link(linkage) end
---@return integer
function get_rds_link() end
---@param program_idx integer 0 to (max_programs - 1)
function set_rds_program(program_idx) end
---@return integer
function get_rds_program() end
-- Timeouts and Periods
---@param period integer
function set_rds_rt_switching_period(period) end
---@return integer
function get_rds_rt_switching_period() end
---@param timeout integer
function set_rds_rt_text_timeout(timeout) end
---@return integer
function get_rds_rt_text_timeout() end
-- String Setters (Charset converted)
---@param ptyn string Program Type Name (max 8 chars)
function set_rds_ptyn(ptyn) end
---@param ps string Program Service (8 chars)
function set_rds_ps(ps) end
---@param tps string Text PS (Scrolling PS)
function set_rds_tps(tps) end
---@param rt1 string Radio Text 1 (max 64 chars)
function set_rds_rt1(rt1) end
---@param rt2 string Radio Text 2 (max 64 chars)
function set_rds_rt2(rt2) end
---@param lps string
function set_rds_lps(lps) end
---@return string
function get_rds_lps() end
function set_rds_ert(ert) end
---@return string
function get_rds_ert() end
function set_rds_grpseq(grpseq) end
---@return string
function get_rds_grpseq() end
function set_rds_grpseq2(grpseq) end
---@return string
function get_rds_grpseq2() end
-- RT Plus Tags
---Sets RT+ tags: type1, start1, len1, type2, start2, len2
---@param t1 integer
---@param s1 integer
---@param l1 integer
---@param t2 integer
---@param s2 integer
---@param l2 integer
function set_rds_rtplus_tags(t1, s1, l1, t2, s2, l2) end
---Gets RT+ tags: type1, start1, len1, type2, start2, len2
---@return integer, integer, integer, integer, integer, integer
function get_rds_rtplus_tags() end
---Sets eRT+ tags: type1, start1, len1, type2, start2, len2
---@param t1 integer
---@param s1 integer
---@param l1 integer
---@param t2 integer
---@param s2 integer
---@param l2 integer
function set_rds_ertplus_tags(t1, s1, l1, t2, s2, l2) end
---Gets eRT+ tags: type1, start1, len1, type2, start2, len2
---@return integer, integer, integer, integer, integer, integer
function get_rds_ertplus_tags() end

View File

@@ -205,14 +205,6 @@ static void handle_grpseq(char *arg, RDSModulator* mod, char* output) {
strcpy(output, "+");
}
static void handle_reset(char *arg, RDSModulator* mod, char* output) {
(void)arg;
encoder_loadFromFile(mod->enc);
for(int i = 0; i < PROGRAMS; i++) reset_rds_state(mod->enc, i);
Modulator_loadFromFile(&mod->params);
strcpy(output, "+");
}
static void handle_eonen(char *arg, char *pattern, RDSModulator* mod, char* output) {
mod->enc->data[mod->enc->program].eon[atoi(pattern)-1].enabled = atoi(arg);
strcpy(output, "+");
@@ -321,9 +313,6 @@ static const command_handler_t commands_eq7[] = {
static const command_handler_t commands_eq8[] = {
{"ERTPRUN", handle_ertprun, 7},
};
static const command_handler_t commands_exact[] = {
{"RESET", handle_reset, 5},
};
static const pattern_command_handler_t pattern_commands[] = {
{"EON", "EN", handle_eonen},
@@ -406,14 +395,6 @@ void process_ascii_cmd(RDSModulator* mod, char *str, char *cmd_output) {
if(upper_str[i] >= 'a' && upper_str[i] <= 'z') upper_str[i] = upper_str[i] - 'a' + 'A';
}
for (size_t i = 0; i < sizeof(commands_exact) / sizeof(command_handler_t); i++) {
const command_handler_t *handler = &commands_exact[i];
if (cmd_len == handler->cmd_length && strcmp(upper_str, handler->cmd) == 0) {
handler->handler(NULL, mod, output);
return;
}
}
char *equals_pos = strchr(upper_str, '=');
if (equals_pos != NULL) {
cmd = upper_str;

View File

@@ -7,6 +7,9 @@ if type(data) == "string" then
elseif data == "init" then
set_rds_program_defaults()
return "+"
elseif data == "reset" then
reset_rds()
return "+"
end
end
cmd = cmd:lower()
@@ -115,7 +118,7 @@ if type(data) == "string" then
elseif cmd == "program" then
local program = tonumber(value)
if not program then return "-" end
if program < 1 then return "-" end
if program < 1 or program > max_programs then return "-" end
set_rds_program(program-1)
return "+"
elseif cmd == "level" then

View File

@@ -9,6 +9,16 @@ int lua_set_rds_program_defaults(lua_State *localL) {
return 0;
}
int lua_reset_rds(lua_State *localL) {
(void)localL;
encoder_saveToFile(mod->enc);
encoder_loadFromFile(mod->enc);
for(int i = 0; i < PROGRAMS; i++) reset_rds_state(mod->enc, i);
Modulator_saveToFile(&mod->params);
Modulator_loadFromFile(&mod->params);
return 0;
}
#define INT_SETTER(name) \
int lua_set_rds_##name(lua_State *localL) { \
mod->enc->data[mod->enc->program].name = luaL_checkinteger(localL, 1); \
@@ -203,6 +213,17 @@ int lua_get_rds_grpseq2(lua_State *localL) {
return 1;
}
int lua_set_rds_grpseq(lua_State *localL) {
const char* str = luaL_checklstring(localL, 1, NULL);
if(_strnlen(str, 2) < 1) set_rds_grpseq(mod->enc, DEFAULT_GRPSQC);
else set_rds_grpseq(mod->enc, str);
return 0;
}
int lua_get_rds_grpseq(lua_State *localL) {
lua_pushstring(localL, mod->enc->data[mod->enc->program].grp_sqc);
return 1;
}
void init_lua(RDSModulator* rds_mod) {
mod = rds_mod;
L = luaL_newstate();
@@ -217,8 +238,11 @@ void init_lua(RDSModulator* rds_mod) {
lua_pushstring(L, VERSION);
lua_setglobal(L, "core_version");
lua_pushinteger(L, PROGRAMS)
lua_setglobal(L, "max_programs");
lua_register(L, "set_rds_program_defaults", lua_set_rds_program_defaults);
lua_register(L, "reset_rds", lua_reset_rds);
lua_register(L, "set_rds_pi", lua_set_rds_pi);
lua_register(L, "get_rds_pi", lua_get_rds_pi);
@@ -262,6 +286,9 @@ void init_lua(RDSModulator* rds_mod) {
lua_register(L, "set_rds_rdsgen", lua_set_rds_rdsgen);
lua_register(L, "get_rds_rdsgen", lua_get_rds_rdsgen);
lua_register(L, "set_rds_grpseq", lua_set_rds_grpseq);
lua_register(L, "get_rds_grpseq", lua_get_rds_grpseq);
lua_register(L, "set_rds_grpseq2", lua_set_rds_grpseq2);
lua_register(L, "get_rds_grpseq2", lua_get_rds_grpseq2);
@@ -294,6 +321,9 @@ void init_lua(RDSModulator* rds_mod) {
lua_register(L, "set_rds_rtplus_tags", lua_set_rds_rtplus_tags);
lua_register(L, "get_rds_rtplus_tags", lua_get_rds_rtplus_tags);
lua_register(L, "set_rds_ertplus_tags", lua_set_rds_ertplus_tags);
lua_register(L, "get_rds_ertplus_tags", lua_get_rds_ertplus_tags);
}
void run_lua(char *str, char *cmd_output) {
@@ -318,6 +348,40 @@ void run_lua(char *str, char *cmd_output) {
}
}
void lua_on_init() {
char path[128];
snprintf(path, sizeof(path), "%s/.rds95.command.lua", getenv("HOME"));
if (luaL_loadfilex(L, path, NULL) != LUA_OK) {
const char *err = lua_tostring(L, -1);
fprintf(stderr, "Lua error loading file: %s\n", err);
lua_pop(L, 1);
return;
}
lua_pushnil();
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);
lua_pop(L, 1);
return;
}
lua_getglobal(L, "on_init");
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));
lua_pop(L, 1);
}
} else {
// printf("Note: 'on_init' function not found in Lua script. Skipping.\n");
lua_pop(L, 1);
}
}
void destroy_lua(void) {
if (L) {
lua_close(L);

View File

@@ -3,8 +3,10 @@
#include <lualib.h>
#include <lauxlib.h>
#include "rds.h"
#include "fs.h"
#include "modulator.h"
void init_lua(RDSModulator* rds_mod);
void run_lua(char *str, char *cmd_output);
void lua_on_init();
void destroy_lua();

View File

@@ -3,6 +3,7 @@
#include "fs.h"
#include "modulator.h"
#include "lib.h"
#include "lua_rds.h"
#include <time.h>
static uint16_t get_next_af(RDSEncoder* enc) {
@@ -695,6 +696,8 @@ void set_rds_defaults(RDSEncoder* enc, uint8_t program) {
reset_rds_state(enc, program);
enc->encoder_data.ascii_data.expected_encoder_addr = 255; // Unknown
lua_on_init();
}
void init_rds_encoder(RDSEncoder* enc) {