mirror of
https://github.com/radio95-rnt/rds95.git
synced 2026-02-27 04:43:52 +01:00
oda in lua?
This commit is contained in:
@@ -402,56 +402,6 @@ int lua_set_rds_udg2(lua_State *localL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lua_register_oda(lua_State *localL) {
|
||||
if (!lua_isboolean(localL, 2)) return luaL_error(localL, "boolean expected, got %s", luaL_typename(localL, 2));
|
||||
uint8_t id = mod->enc->state[mod->enc->program].user_oda.oda_len++;
|
||||
if(mod->enc->state[mod->enc->program].user_oda.oda_len >= ODAS) return luaL_error(localL, "There can't be more than %d registered ODAs", ODAS);
|
||||
if(mod->enc->state[mod->enc->program].user_oda.odas[id].group != 0) return luaL_error(localL, "internal error");
|
||||
mod->enc->state[mod->enc->program].user_oda.odas[id].group = luaL_checkinteger(localL, 1);
|
||||
switch (mod->enc->state[mod->enc->program].user_oda.odas[id].group) {
|
||||
case 14:
|
||||
case 15:
|
||||
case 2:
|
||||
case 0:
|
||||
return luaL_error(localL, "Invalid group");
|
||||
break;
|
||||
case 10:
|
||||
case 4:
|
||||
case 1:
|
||||
if(mod->enc->state[mod->enc->program].user_oda.odas[id].group_version == 0) return luaL_error(localL, "Invalid group");
|
||||
default:
|
||||
break;
|
||||
}
|
||||
mod->enc->state[mod->enc->program].user_oda.odas[id].group_version = lua_toboolean(localL, 2);
|
||||
mod->enc->state[mod->enc->program].user_oda.odas[id].id = luaL_checkinteger(localL, 3);
|
||||
mod->enc->state[mod->enc->program].user_oda.odas[id].id_data = luaL_checkinteger(localL, 4);
|
||||
lua_pushinteger(localL, id);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lua_set_oda_id_data(lua_State *localL) {
|
||||
uint8_t idx = luaL_checkinteger(localL, 1);
|
||||
if(idx >= ODAS) return luaL_error(localL, "There can't be more than %d registered ODAs", ODAS);
|
||||
if(mod->enc->state[mod->enc->program].user_oda.odas[idx].group == 0) return luaL_error(localL, "this oda is not registered");
|
||||
mod->enc->state[mod->enc->program].user_oda.odas[idx].id_data = luaL_checkinteger(localL, 2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lua_set_oda_handler(lua_State *localL) {
|
||||
uint8_t idx = luaL_checkinteger(localL, 1);
|
||||
if(idx >= ODAS) return luaL_error(localL, "There can't be more than %d registered ODAs", ODAS);
|
||||
if(mod->enc->state[mod->enc->program].user_oda.odas[idx].group == 0) return luaL_error(localL, "this oda is not registered");
|
||||
if(mod->enc->state[mod->enc->program].user_oda.odas[idx].group == 3) return luaL_error(localL, "this oda cannot have a handler");
|
||||
luaL_checktype(localL, 2, LUA_TFUNCTION);
|
||||
lua_pushvalue(localL, 2);
|
||||
if(mod->enc->state[mod->enc->program].user_oda.odas[idx].lua_handler != 0) luaL_unref(localL, LUA_REGISTRYINDEX, mod->enc->state[mod->enc->program].user_oda.odas[idx].lua_handler);
|
||||
mod->enc->state[mod->enc->program].user_oda.odas[idx].lua_handler = luaL_ref(localL, LUA_REGISTRYINDEX);
|
||||
int index = *unload_refs;
|
||||
unload_refs[index] = mod->enc->state[mod->enc->program].user_oda.odas[idx].lua_handler;
|
||||
(*unload_refs)++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void init_lua(RDSModulator* rds_mod) {
|
||||
static int mutex_initialized = 0;
|
||||
mod = rds_mod;
|
||||
@@ -474,8 +424,6 @@ void init_lua(RDSModulator* rds_mod) {
|
||||
lua_setglobal(L, "eon_count");
|
||||
lua_pushinteger(L, LUA_USER_DATA);
|
||||
lua_setglobal(L, "user_data_len");
|
||||
lua_pushinteger(L, ODAS);
|
||||
lua_setglobal(L, "oda_count");
|
||||
|
||||
lua_register(L, "set_rds_program_defaults", lua_set_rds_program_defaults);
|
||||
lua_register(L, "reset_rds", lua_reset_rds);
|
||||
@@ -566,10 +514,6 @@ void init_lua(RDSModulator* rds_mod) {
|
||||
lua_register(L, "set_rds_udg", lua_set_rds_udg);
|
||||
lua_register(L, "set_rds_udg2", lua_set_rds_udg2);
|
||||
|
||||
lua_register(L, "register_oda", lua_register_oda);
|
||||
lua_register(L, "set_oda_id_data", lua_set_oda_id_data);
|
||||
lua_register(L, "set_oda_handler", lua_set_oda_handler);
|
||||
|
||||
lua_register(L, "set_userdata", lua_set_userdata);
|
||||
lua_register(L, "set_userdata_offset", lua_set_userdata_offset);
|
||||
lua_register(L, "get_userdata", lua_get_userdata);
|
||||
@@ -649,8 +593,8 @@ int lua_rds2_group(RDSGroup* group, int stream) {
|
||||
|
||||
if (lua_isfunction(L, -1)) {
|
||||
lua_pushinteger(L, stream);
|
||||
if (lua_pcall(L, 1, 4, 0) == LUA_OK) {
|
||||
if (!lua_isinteger(L, -1)) {
|
||||
if (lua_pcall(L, 1, 5, 0) == LUA_OK) {
|
||||
if (!lua_isboolean(L, -1)) {
|
||||
pthread_mutex_unlock(&lua_mutex);
|
||||
return 0;
|
||||
}
|
||||
@@ -666,11 +610,19 @@ int lua_rds2_group(RDSGroup* group, int stream) {
|
||||
pthread_mutex_unlock(&lua_mutex);
|
||||
return 0;
|
||||
}
|
||||
group->d = luaL_checkinteger(L, -1);
|
||||
group->c = luaL_checkinteger(L, -2);
|
||||
group->b = luaL_checkinteger(L, -3);
|
||||
group->a = luaL_checkinteger(L, -4);
|
||||
lua_pop(L, 3);
|
||||
if (!lua_isinteger(L, -5)) {
|
||||
pthread_mutex_unlock(&lua_mutex);
|
||||
return 0;
|
||||
}
|
||||
if(lua_toboolean(L, -1) == 0) {
|
||||
pthread_mutex_unlock(&lua_mutex);
|
||||
return 0;
|
||||
}
|
||||
group->d = luaL_checkinteger(L, -2);
|
||||
group->c = luaL_checkinteger(L, -3);
|
||||
group->b = luaL_checkinteger(L, -4);
|
||||
group->a = luaL_checkinteger(L, -5);
|
||||
lua_pop(L, 4);
|
||||
} else fprintf(stderr, "Lua error: %s at 'rds2_group'\n", lua_tostring(L, -1));
|
||||
lua_pop(L, 1);
|
||||
} else lua_pop(L, 1);
|
||||
|
||||
29
src/rds.c
29
src/rds.c
@@ -91,16 +91,12 @@ static void get_rds_sequence_group(RDSEncoder* enc, RDSGroup *group, char* grp,
|
||||
get_rds_fasttuning_group(enc, group);
|
||||
break;
|
||||
case 'L':
|
||||
if(get_rdsp_lua_group(group, *grp) == 0) get_rds_ps_group(enc, group);
|
||||
break;
|
||||
case 'O':
|
||||
get_rds_user_oda_group(enc, group);
|
||||
break;
|
||||
case 'R':
|
||||
case 'P':
|
||||
case 'S':
|
||||
case 'O':
|
||||
case 'K':
|
||||
if(get_rds_user_oda_group_content(enc, group) == 0) get_rds_ps_group(enc, group);
|
||||
if(get_rdsp_lua_group(group, *grp) == 0) get_rds_ps_group(enc, group);
|
||||
break;
|
||||
case 'U':
|
||||
if(enc->state[enc->program].af_oda == 0) get_rds_oda_af_group(enc, group);
|
||||
@@ -128,16 +124,7 @@ static uint8_t check_rds_good_group(RDSEncoder* enc, char* grp) {
|
||||
if(*grp == 'Y' && enc->data[enc->program].udg2_len != 0) good_group = 1;
|
||||
if(*grp == 'F' && enc->data[enc->program].lps[0] != '\0') good_group = 1;
|
||||
if(*grp == 'T') good_group = 1;
|
||||
if(*grp == 'L') good_group = 1;
|
||||
if(*grp == 'O' && enc->state[enc->program].user_oda.oda_len != 0) good_group = 1;
|
||||
if(*grp == 'K') {
|
||||
for (int i = 0; i < enc->state->user_oda.oda_len; i++) {
|
||||
if (enc->state->user_oda.odas[i].lua_handler != 0) {
|
||||
good_group = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(*grp == 'L' || *grp = 'R' || *grp = 'P' || *grp = 'S' || *grp = 'O' || *grp = 'K') good_group = 1;
|
||||
if(*grp == 'U' && enc->data[enc->program].af_oda.num_afs) good_group = 1;
|
||||
return good_group;
|
||||
}
|
||||
@@ -247,8 +234,14 @@ static void get_rds_group(RDSEncoder* enc, RDSGroup *group, uint8_t stream) {
|
||||
|
||||
goto group_coded_rds2;
|
||||
} else if(enc->encoder_data.rds2_mode == 2) {
|
||||
lua_rds2_group(group, stream);
|
||||
if(group->a == 0) group->is_type_b = (IS_TYPE_B(group->b) != 0);
|
||||
int generated = lua_rds2_group(group, stream);
|
||||
if(group->a == 0 && generated) group->is_type_b = (IS_TYPE_B(group->b) != 0);
|
||||
else if(generated == 0) {
|
||||
group->b = enc->state[enc->program].last_stream0_group[0];
|
||||
group->c = enc->state[enc->program].last_stream0_group[1];
|
||||
group->d = enc->state[enc->program].last_stream0_group[2];
|
||||
group->is_type_b = enc->state[enc->program].last_stream0_group_type_b;
|
||||
}
|
||||
goto group_coded_rds2;
|
||||
}
|
||||
}
|
||||
|
||||
19
src/rds.h
19
src/rds.h
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
#include "common.h"
|
||||
#define LUA_USER_DATA 1280
|
||||
#define ODAS 32
|
||||
#define LUA_USER_DATA 1152
|
||||
|
||||
/* The RDS error-detection code generator polynomial is
|
||||
* x^10 + x^8 + x^7 + x^5 + x^4 + x^3 + x^0
|
||||
@@ -108,20 +107,6 @@ typedef struct {
|
||||
uint8_t af_state : 6;
|
||||
} RDSEONState;
|
||||
|
||||
typedef struct {
|
||||
uint8_t group : 4;
|
||||
uint8_t group_version : 1;
|
||||
uint16_t id;
|
||||
uint16_t id_data;
|
||||
int lua_handler;
|
||||
} RDSODA;
|
||||
typedef struct {
|
||||
uint8_t oda_len;
|
||||
uint8_t oda_pointer;
|
||||
uint8_t oda_runner_pointer;
|
||||
RDSODA odas[ODAS];
|
||||
} RDSODAState;
|
||||
|
||||
typedef struct {
|
||||
uint8_t ps_update : 1;
|
||||
uint8_t tps_update : 1;
|
||||
@@ -179,8 +164,6 @@ typedef struct {
|
||||
|
||||
uint16_t last_stream0_group[3];
|
||||
uint8_t last_stream0_group_type_b : 1;
|
||||
|
||||
RDSODAState user_oda;
|
||||
} RDSState;
|
||||
|
||||
typedef struct {
|
||||
|
||||
@@ -311,36 +311,4 @@ int get_rdsp_lua_group(RDSGroup *group, const char grp) {
|
||||
int generated = lua_group(group, grp);
|
||||
if(generated) group->is_type_b = (IS_TYPE_B(group->b) != 0);
|
||||
return generated;
|
||||
}
|
||||
|
||||
void get_rds_user_oda_group(RDSEncoder* enc, RDSGroup *group) {
|
||||
uint8_t pointer = enc->state[enc->program].user_oda.oda_pointer++;
|
||||
if(enc->state[enc->program].user_oda.oda_pointer >= enc->state[enc->program].user_oda.oda_len) enc->state[enc->program].user_oda.oda_pointer = 0;
|
||||
RDSODA oda = enc->state[enc->program].user_oda.odas[pointer];
|
||||
|
||||
group->b |= 3 << 12;
|
||||
group->b |= oda.group << 1;
|
||||
group->b |= oda.group_version;
|
||||
group->c = oda.id_data;
|
||||
group->d = oda.id;
|
||||
}
|
||||
int get_rds_user_oda_group_content(RDSEncoder* enc, RDSGroup *group) {
|
||||
RDSODAState *oda_state = &enc->state[enc->program].user_oda;
|
||||
|
||||
if (oda_state->oda_len == 0) return 0;
|
||||
for (uint8_t i = 0; i < oda_state->oda_len; i++) {
|
||||
uint8_t current_idx = oda_state->oda_runner_pointer;
|
||||
|
||||
oda_state->oda_runner_pointer = (oda_state->oda_runner_pointer + 1) % oda_state->oda_len;
|
||||
|
||||
if (oda_state->odas[current_idx].lua_handler != 0 && oda_state->odas[current_idx].group != 3) {
|
||||
lua_group_ref(group, oda_state->odas[current_idx].lua_handler);
|
||||
group->b |= oda_state->odas[current_idx].group << 12;
|
||||
group->b |= oda_state->odas[current_idx].group_version << 11;
|
||||
group->is_type_b = (IS_TYPE_B(group->b) != 0);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user