From 484566083ead3baf5789d8f9677c771b219bc416 Mon Sep 17 00:00:00 2001 From: bsongis Date: Fri, 18 Jul 2014 17:39:30 +0200 Subject: [PATCH] A little refactoring to avoid all the sprintf and too many string comparaisons. Completely untested --- radio/src/lua.cpp | 178 +++++++++++++++++----------------------- radio/src/myeeprom.h | 10 +-- radio/util/luaexport.py | 23 +++--- 3 files changed, 89 insertions(+), 122 deletions(-) diff --git a/radio/src/lua.cpp b/radio/src/lua.cpp index 20fe476bf..44f300ec4 100644 --- a/radio/src/lua.cpp +++ b/radio/src/lua.cpp @@ -34,6 +34,7 @@ * */ +#include #include #include "opentx.h" #include "stamp-opentx.h" @@ -48,7 +49,7 @@ extern "C" { } #endif -#include "lua_exports.cpp" //this line must be after lua headers +#include "lua_exports.cpp" // this line must be after lua headers #define lua_registernumber(L, n, i) (lua_pushnumber(L, (i)), lua_setglobal(L, (n))) #define lua_registerint(L, n, i) (lua_pushinteger(L, (i)), lua_setglobal(L, (n))) @@ -56,7 +57,8 @@ extern "C" { #define lua_pushtableboolean(L, k, v) (lua_pushstring(L, (k)), lua_pushboolean(L, (v)), lua_settable(L, -3)) #define lua_pushtableinteger(L, k, v) (lua_pushstring(L, (k)), lua_pushinteger(L, (v)), lua_settable(L, -3)) #define lua_pushtablenumber(L, k, v) (lua_pushstring(L, (k)), lua_pushnumber(L, (v)), lua_settable(L, -3)) -#define lua_pushtablestring(L, k, v) { char tmp[sizeof(v)+1]; strncpy(tmp, (v), sizeof(v)); tmp[sizeof(v)] = '\0'; lua_pushstring(L, (k)); lua_pushstring(L, tmp); lua_settable(L, -3); } +#define lua_pushtablestring(L, k, v) (lua_pushstring(L, (k)), lua_pushstring(L, (v)), lua_settable(L, -3)) +#define lua_pushtablenzstring(L, k, v) { char tmp[sizeof(v)+1]; strncpy(tmp, (v), sizeof(v)); tmp[sizeof(v)] = '\0'; lua_pushstring(L, (k)); lua_pushstring(L, tmp); lua_settable(L, -3); } #define lua_pushtablezstring(L, k, v) { char tmp[sizeof(v)+1]; zchar2str(tmp, (v), sizeof(v)); lua_pushstring(L, (k)); lua_pushstring(L, tmp); lua_settable(L, -3); } #define lua_registerlib(L, name, tab) (luaL_newmetatable(L, name), luaL_setfuncs(L, tab, 0), lua_setglobal(L, name)) @@ -102,7 +104,7 @@ static void luaGetValueAndPush(int src) { if (src >= MIXSRC_FIRST_TELEM && src <= MIXSRC_LAST_TELEM) { // telemetry values - if ((src != MIXSRC_FIRST_TELEM-1+TELEM_TX_VOLTAGE) && !TELEMETRY_STREAMING() ) { + if ((src != MIXSRC_FIRST_TELEM-1+TELEM_TX_VOLTAGE) && !TELEMETRY_STREAMING()) { // telemetry not working, return zero for telemetry sources // except for "tx voltage" lua_pushinteger(L, (int)0); @@ -121,7 +123,7 @@ static void luaGetValueAndPush(int src) case MIXSRC_FIRST_TELEM-1+TELEM_MAX_CURRENT: case MIXSRC_FIRST_TELEM-1+TELEM_ASPEED: case MIXSRC_FIRST_TELEM-1+TELEM_MAX_ASPEED: - //theese need to be divided by 10 + // these need to be divided by 10 lua_pushnumber(L, getValue(src)/10.0); break; @@ -135,7 +137,7 @@ static void luaGetValueAndPush(int src) case MIXSRC_FIRST_TELEM-1+TELEM_A2: case MIXSRC_FIRST_TELEM-1+TELEM_A3: case MIXSRC_FIRST_TELEM-1+TELEM_A4: - //convert raw Ax values to calibrated values + // convert raw Ax values to calibrated values idx -= (MIXSRC_FIRST_TELEM+TELEM_A1-1); lua_pushnumber(L, applyChannelRatio(idx, getValue(src))/100.0); break; @@ -147,7 +149,7 @@ static void luaGetValueAndPush(int src) case MIXSRC_FIRST_TELEM-1+TELEM_ACCy: case MIXSRC_FIRST_TELEM-1+TELEM_ACCz: case MIXSRC_FIRST_TELEM-1+TELEM_VSPEED: - //theese need to be divided by 100 + // these need to be divided by 100 lua_pushnumber(L, getValue(src)/100.0); break; @@ -158,108 +160,76 @@ static void luaGetValueAndPush(int src) } } -//these are used when returning fields that are defined as multiples -static char foundName[10]; -static char foundDesc[50]; -static LuaField foundField = {0, foundName, foundDesc}; +struct LuaField { + uint16_t id; + char desc[50]; +}; + +#define FIND_FIELD_DESC 0x01 /** - Return filed data for a given field name - - Note: function is non-reentrant, - it uses global variable foundField for return value + Return field data for a given field name */ -const LuaField * luaFindFieldByName(const char * name) +bool luaFindFieldByName(const char * name, LuaField & field, unsigned int flags=0) { // TODO better search method (binary lookup) - for(int n = 0; n < (int)DIM(luaFields); ++n) { - if (!strcmp(name, luaFields[n].name)) { - return &luaFields[n]; + for (unsigned int n=0; n= luaMultipleFields[n].id) && (id <= luaMultipleFields[n].id+luaMultipleFields[n].end-luaMultipleFields[n].start)) { - int i = id - luaMultipleFields[n].id + luaMultipleFields[n].start; - snprintf(foundName, sizeof(foundName), luaMultipleFields[n].name, i); - snprintf(foundDesc, sizeof(foundDesc), luaMultipleFields[n].desc, i); - foundField.id = id; - return &foundField; - } - } - return 0; // not found -} - -// TODO this could be removed and luaGetFieldInfo() used instead -// get a filed ids for one or more fields -static int luaGetFields(lua_State *L) -{ - // get number of arguments - int n = lua_gettop(L); - // return requested filed ids as multiple return values - for (int i = 1; i <= n; i++) - { - const char * what = luaL_checkstring(L, i); - TRACE("luaGetFields(): %s", what); - const LuaField * field = luaFindFieldByName(what); - if (field) { - lua_pushinteger(L, field->id); - } - else { - lua_pushnil(L); - } - } - return n; -} - -//get a detailed info about particular field +// get a detailed info about particular field static int luaGetFieldInfo(lua_State *L) { - const LuaField * field; - if (lua_isnumber(L, 1)) { - int id = luaL_checkinteger(L, 1); - // TRACE("luaGetFieldInfo(): int %d", id); - field = luaFindFieldById(id); - } - else { - const char * what = luaL_checkstring(L, 1); - // TRACE("luaGetFieldInfo(): str %s", what); - field = luaFindFieldByName(what); - } - if (field) { + const char * what = luaL_checkstring(L, 1); + LuaField field; + bool found = luaFindFieldByName(what, field, FIND_FIELD_DESC); + if (found) { lua_newtable(L); - lua_pushtableinteger(L, "id", field->id); - lua_pushstring(L, "name"); lua_pushstring(L, field->name); lua_settable(L, -3); - lua_pushstring(L, "desc"); lua_pushstring(L, field->desc); lua_settable(L, -3); + lua_pushtableinteger(L, "id", field.id); + lua_pushtablestring(L, "name", what); + lua_pushtablestring(L, "desc", field.desc); return 1; } return 0; @@ -272,12 +242,12 @@ static int luaGetValue(lua_State *L) src = luaL_checkinteger(L, 1); } else { - // convert from filed name to its id + // convert from field name to its id const char *name = luaL_checkstring(L, 1); - const LuaField * field = luaFindFieldByName(name); - if (field) { - // TRACE("luaGetValue(): %d", field->id); - src = field->id; + LuaField field; + bool found = luaFindFieldByName(name, field); + if (found) { + src = field.id; } } if (src >= EXTRA_FIRST ) { @@ -396,10 +366,11 @@ static int luaLcdDrawChannel(lua_State *L) channel = luaL_checkinteger(L, 3); } else { - const char *what = luaL_checkstring(L, 3); - const LuaField * field = luaFindFieldByName(what); - if (field) { - channel = field->id; + const char * what = luaL_checkstring(L, 3); + LuaField field; + bool found = luaFindFieldByName(what, field); + if (found) { + channel = field.id; } } int att = luaL_checkinteger(L, 4); @@ -935,7 +906,7 @@ static int luaModelGetCustomFunction(lua_State *L) lua_pushtableinteger(L, "switch", CFN_SWITCH(cfn)); lua_pushtableinteger(L, "func", CFN_FUNC(cfn)); if (CFN_FUNC(cfn) == FUNC_PLAY_TRACK || CFN_FUNC(cfn) == FUNC_BACKGND_MUSIC || CFN_FUNC(cfn) == FUNC_PLAY_SCRIPT) { - lua_pushtablestring(L, "name", cfn->play.name); + lua_pushtablenzstring(L, "name", cfn->play.name); } else { lua_pushtableinteger(L, "value", cfn->all.val); @@ -1263,7 +1234,6 @@ void luaInit() lua_register(L, "getVersion", luaGetVersion); lua_register(L, "getGeneralSettings", luaGetGeneralSettings); lua_register(L, "getValue", luaGetValue); - lua_register(L, "getFields", luaGetFields); lua_register(L, "getFieldInfo", luaGetFieldInfo); lua_register(L, "playFile", luaPlayFile); diff --git a/radio/src/myeeprom.h b/radio/src/myeeprom.h index 31b500919..7005ec7d7 100644 --- a/radio/src/myeeprom.h +++ b/radio/src/myeeprom.h @@ -1459,7 +1459,7 @@ enum MixSources { MIXSRC_NONE, #if defined(PCBTARANIS) - MIXSRC_FIRST_INPUT, LUA_EXPORT_MULTIPLE("input%d", "Input [I%d]", 1, MAX_INPUTS) + MIXSRC_FIRST_INPUT, LUA_EXPORT_MULTIPLE("input", "Input [I%d]", MAX_INPUTS) MIXSRC_LAST_INPUT = MIXSRC_FIRST_INPUT+MAX_INPUTS-1, MIXSRC_FIRST_LUA, @@ -1540,18 +1540,18 @@ enum MixSources { MIXSRC_TRN, #endif MIXSRC_FIRST_LOGICAL_SWITCH, - MIXSRC_SW1 = MIXSRC_FIRST_LOGICAL_SWITCH, LUA_EXPORT_MULTIPLE("ls%d", "Logical switch L%d", 1, NUM_LOGICAL_SWITCH) + MIXSRC_SW1 = MIXSRC_FIRST_LOGICAL_SWITCH, LUA_EXPORT_MULTIPLE("ls", "Logical switch L%d", NUM_LOGICAL_SWITCH) MIXSRC_SW9 = MIXSRC_SW1 + 8, MIXSRC_SWA, MIXSRC_SWB, MIXSRC_SWC, MIXSRC_LAST_LOGICAL_SWITCH = MIXSRC_FIRST_LOGICAL_SWITCH+NUM_LOGICAL_SWITCH-1, - MIXSRC_FIRST_TRAINER, LUA_EXPORT_MULTIPLE("trn%d", "Trainer input %d", 1, NUM_TRAINER) + MIXSRC_FIRST_TRAINER, LUA_EXPORT_MULTIPLE("trn", "Trainer input %d", NUM_TRAINER) MIXSRC_LAST_TRAINER = MIXSRC_FIRST_TRAINER+NUM_TRAINER-1, MIXSRC_FIRST_CH, - MIXSRC_CH1 = MIXSRC_FIRST_CH, LUA_EXPORT_MULTIPLE("ch%d", "Channel CH%d", 1, NUM_CHNOUT) + MIXSRC_CH1 = MIXSRC_FIRST_CH, LUA_EXPORT_MULTIPLE("ch", "Channel CH%d", NUM_CHNOUT) MIXSRC_CH2, MIXSRC_CH3, MIXSRC_CH4, @@ -1569,7 +1569,7 @@ enum MixSources { MIXSRC_CH16, MIXSRC_LAST_CH = MIXSRC_CH1+NUM_CHNOUT-1, - MIXSRC_GVAR1, LUA_EXPORT_MULTIPLE("gvar%d", "Global variable %d", 1, MAX_GVARS) + MIXSRC_GVAR1, LUA_EXPORT_MULTIPLE("gvar", "Global variable %d", MAX_GVARS) MIXSRC_LAST_GVAR = MIXSRC_GVAR1+MAX_GVARS-1, MIXSRC_FIRST_TELEM, diff --git a/radio/util/luaexport.py b/radio/util/luaexport.py index df3c4e0cf..0bfbb1a71 100644 --- a/radio/util/luaexport.py +++ b/radio/util/luaexport.py @@ -42,15 +42,13 @@ def LEXP_TELEMETRY(name, description): checkName(name) exports.append( ("MIXSRC_FIRST_TELEM-1+"+CONSTANT_VALUE, name, description) ) -def LEXP_MULTIPLE(nameFormat, descriptionFormat, firstValue, numOfValues): - # print "LEXP_MULTIPLE %s, %s, %s, %s" % (nameFormat, descriptionFormat, firstValue, numOfValues) - first = int(firstValue) - last = first + int(numOfValues) - 1 - for v in range(first, last+1): - name = nameFormat % v +def LEXP_MULTIPLE(nameFormat, descriptionFormat, valuesCount): + # print "LEXP_MULTIPLE %s, %s, %s" % (nameFormat, descriptionFormat, valuesCount) + for v in range(valuesCount): + name = nameFormat + str(v) # print name checkName(name) - exports_multiple.append( (CONSTANT_VALUE, nameFormat, descriptionFormat, first, last) ) + exports_multiple.append( (CONSTANT_VALUE, nameFormat, descriptionFormat, valuesCount) ) def LEXP_EXTRA(name, description, value, condition = "true"): # print "LEXP_EXTRA %s, %s, %s, %s" % (name, description, value, condition) @@ -93,6 +91,7 @@ while True: # warning = True # continue # dups_cnst.append(CONSTANT_VALUE) + # print cmd eval(cmd) except: print "ERROR: problem with the definition: %s" % line @@ -106,19 +105,17 @@ out = open(outputFile, "w") out.write("//This file was generated by luaexport.py script on %s\n\n\n" % time.asctime()) header = """ -struct LuaField { +struct LuaSingleField { uint16_t id; const char * name; const char * desc; - //const uint8_t attr; }; struct LuaMultipleField { uint16_t id; const char * name; const char * desc; - uint8_t start; - uint8_t end; + uint8_t count; }; """ @@ -127,7 +124,7 @@ out.write(header) out.write(""" // The list of Lua fields // this aray is alphabetically sorted by the second field (name) -const LuaField luaFields[] = { +const LuaSingleField luaSingleFields[] = { """) exports.sort(key = lambda x: x[1]) #sort by name data = [" {%s, \"%s\", \"%s\"}" % export for export in exports] @@ -139,7 +136,7 @@ out.write(""" // The list of Lua fields that have a range of values const LuaMultipleField luaMultipleFields[] = { """) -data = [" {%s, \"%s\", \"%s\", %d, %d}" % export for export in exports_multiple] +data = [" {%s, \"%s\", \"%s\", %d}" % export for export in exports_multiple] out.write(",\n".join(data)) out.write("\n};\n\n") print "Generated %d items in luaMultipleFields[]" % len(exports_multiple)