A little refactoring to avoid all the sprintf and too many string

comparaisons. Completely untested
This commit is contained in:
bsongis
2014-07-18 22:13:02 +02:00
parent 7f3778fc3c
commit 484566083e
3 changed files with 89 additions and 122 deletions
+74 -104
View File
@@ -34,6 +34,7 @@
* *
*/ */
#include <ctype.h>
#include <stdio.h> #include <stdio.h>
#include "opentx.h" #include "opentx.h"
#include "stamp-opentx.h" #include "stamp-opentx.h"
@@ -48,7 +49,7 @@ extern "C" {
} }
#endif #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_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))) #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_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_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_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_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)) #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) { if (src >= MIXSRC_FIRST_TELEM && src <= MIXSRC_LAST_TELEM) {
// telemetry values // 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 // telemetry not working, return zero for telemetry sources
// except for "tx voltage" // except for "tx voltage"
lua_pushinteger(L, (int)0); 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_MAX_CURRENT:
case MIXSRC_FIRST_TELEM-1+TELEM_ASPEED: case MIXSRC_FIRST_TELEM-1+TELEM_ASPEED:
case MIXSRC_FIRST_TELEM-1+TELEM_MAX_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); lua_pushnumber(L, getValue(src)/10.0);
break; break;
@@ -135,7 +137,7 @@ static void luaGetValueAndPush(int src)
case MIXSRC_FIRST_TELEM-1+TELEM_A2: case MIXSRC_FIRST_TELEM-1+TELEM_A2:
case MIXSRC_FIRST_TELEM-1+TELEM_A3: case MIXSRC_FIRST_TELEM-1+TELEM_A3:
case MIXSRC_FIRST_TELEM-1+TELEM_A4: 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); idx -= (MIXSRC_FIRST_TELEM+TELEM_A1-1);
lua_pushnumber(L, applyChannelRatio(idx, getValue(src))/100.0); lua_pushnumber(L, applyChannelRatio(idx, getValue(src))/100.0);
break; break;
@@ -147,7 +149,7 @@ static void luaGetValueAndPush(int src)
case MIXSRC_FIRST_TELEM-1+TELEM_ACCy: case MIXSRC_FIRST_TELEM-1+TELEM_ACCy:
case MIXSRC_FIRST_TELEM-1+TELEM_ACCz: case MIXSRC_FIRST_TELEM-1+TELEM_ACCz:
case MIXSRC_FIRST_TELEM-1+TELEM_VSPEED: 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); lua_pushnumber(L, getValue(src)/100.0);
break; break;
@@ -158,108 +160,76 @@ static void luaGetValueAndPush(int src)
} }
} }
//these are used when returning fields that are defined as multiples struct LuaField {
static char foundName[10]; uint16_t id;
static char foundDesc[50]; char desc[50];
static LuaField foundField = {0, foundName, foundDesc}; };
#define FIND_FIELD_DESC 0x01
/** /**
Return filed data for a given field name Return field data for a given field name
Note: function is non-reentrant,
it uses global variable foundField for return value
*/ */
const LuaField * luaFindFieldByName(const char * name) bool luaFindFieldByName(const char * name, LuaField & field, unsigned int flags=0)
{ {
// TODO better search method (binary lookup) // TODO better search method (binary lookup)
for(int n = 0; n < (int)DIM(luaFields); ++n) { for (unsigned int n=0; n<DIM(luaSingleFields); ++n) {
if (!strcmp(name, luaFields[n].name)) { if (!strcmp(name, luaSingleFields[n].name)) {
return &luaFields[n]; field.id = luaSingleFields[n].id;
if (flags & FIND_FIELD_DESC) {
strncpy(field.desc, luaSingleFields[n].desc, sizeof(field.desc)-1);
field.desc[sizeof(field.desc)-1] = '\0';
}
else {
field.desc[0] = '\0';
}
return true;
} }
} }
//search in multiples
for(int n = 0; n < (int)DIM(luaMultipleFields); ++n) { // search in multiples
for(int i = luaMultipleFields[n].start; i < luaMultipleFields[n].end; i++){ unsigned int len = strlen(name);
//char buf[10]; for (unsigned int n=0; n<DIM(luaMultipleFields); ++n) {
snprintf(foundName, sizeof(foundName), luaMultipleFields[n].name, i); const char * fieldName = luaMultipleFields[n].name;
// TRACE("luaFindFieldByName(): %s", foundName); unsigned int fieldLen = strlen(fieldName);
if (!strcmp(name, foundName)) { if (!strncmp(name, fieldName, fieldLen)) {
snprintf(foundDesc, sizeof(foundDesc), luaMultipleFields[n].desc, i); unsigned int index;
foundField.id = luaMultipleFields[n].id+i-1; if (len == fieldLen+1 && isdigit(name[fieldLen])) {
return &foundField; index = name[fieldLen] - '1';
}
else if (len == fieldLen+2 && isdigit(name[fieldLen]) && isdigit(name[fieldLen+1])) {
index = 10 * (name[fieldLen] - '0') + (name[fieldLen+1] - '1');
}
else {
continue;
}
if (index < luaMultipleFields[n].count) {
field.id = luaMultipleFields[n].id;
if (flags & FIND_FIELD_DESC) {
snprintf(field.desc, sizeof(field.desc)-1, luaMultipleFields[n].desc, index+1);
field.desc[sizeof(field.desc)-1] = '\0';
}
else {
field.desc[0] = '\0';
}
return true;
} }
} }
} }
return 0; // not found return false; // not found
} }
/** // get a detailed info about particular field
Return filed data for a given field id
Note: function is non-reentrant,
it uses global variable foundField for return value
*/
const LuaField * luaFindFieldById(int id)
{
for(int n = 0; n < (int)DIM(luaFields); ++n) {
if (id == luaFields[n].id) {
return &luaFields[n];
}
}
//search in multiples
for(int n = 0; n < (int)DIM(luaMultipleFields); ++n) {
if ((id >= 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
static int luaGetFieldInfo(lua_State *L) static int luaGetFieldInfo(lua_State *L)
{ {
const LuaField * field; const char * what = luaL_checkstring(L, 1);
if (lua_isnumber(L, 1)) { LuaField field;
int id = luaL_checkinteger(L, 1); bool found = luaFindFieldByName(what, field, FIND_FIELD_DESC);
// TRACE("luaGetFieldInfo(): int %d", id); if (found) {
field = luaFindFieldById(id);
}
else {
const char * what = luaL_checkstring(L, 1);
// TRACE("luaGetFieldInfo(): str %s", what);
field = luaFindFieldByName(what);
}
if (field) {
lua_newtable(L); lua_newtable(L);
lua_pushtableinteger(L, "id", field->id); lua_pushtableinteger(L, "id", field.id);
lua_pushstring(L, "name"); lua_pushstring(L, field->name); lua_settable(L, -3); lua_pushtablestring(L, "name", what);
lua_pushstring(L, "desc"); lua_pushstring(L, field->desc); lua_settable(L, -3); lua_pushtablestring(L, "desc", field.desc);
return 1; return 1;
} }
return 0; return 0;
@@ -272,12 +242,12 @@ static int luaGetValue(lua_State *L)
src = luaL_checkinteger(L, 1); src = luaL_checkinteger(L, 1);
} }
else { else {
// convert from filed name to its id // convert from field name to its id
const char *name = luaL_checkstring(L, 1); const char *name = luaL_checkstring(L, 1);
const LuaField * field = luaFindFieldByName(name); LuaField field;
if (field) { bool found = luaFindFieldByName(name, field);
// TRACE("luaGetValue(): %d", field->id); if (found) {
src = field->id; src = field.id;
} }
} }
if (src >= EXTRA_FIRST ) { if (src >= EXTRA_FIRST ) {
@@ -396,10 +366,11 @@ static int luaLcdDrawChannel(lua_State *L)
channel = luaL_checkinteger(L, 3); channel = luaL_checkinteger(L, 3);
} }
else { else {
const char *what = luaL_checkstring(L, 3); const char * what = luaL_checkstring(L, 3);
const LuaField * field = luaFindFieldByName(what); LuaField field;
if (field) { bool found = luaFindFieldByName(what, field);
channel = field->id; if (found) {
channel = field.id;
} }
} }
int att = luaL_checkinteger(L, 4); 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, "switch", CFN_SWITCH(cfn));
lua_pushtableinteger(L, "func", CFN_FUNC(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) { 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 { else {
lua_pushtableinteger(L, "value", cfn->all.val); lua_pushtableinteger(L, "value", cfn->all.val);
@@ -1263,7 +1234,6 @@ void luaInit()
lua_register(L, "getVersion", luaGetVersion); lua_register(L, "getVersion", luaGetVersion);
lua_register(L, "getGeneralSettings", luaGetGeneralSettings); lua_register(L, "getGeneralSettings", luaGetGeneralSettings);
lua_register(L, "getValue", luaGetValue); lua_register(L, "getValue", luaGetValue);
lua_register(L, "getFields", luaGetFields);
lua_register(L, "getFieldInfo", luaGetFieldInfo); lua_register(L, "getFieldInfo", luaGetFieldInfo);
lua_register(L, "playFile", luaPlayFile); lua_register(L, "playFile", luaPlayFile);
+5 -5
View File
@@ -1459,7 +1459,7 @@ enum MixSources {
MIXSRC_NONE, MIXSRC_NONE,
#if defined(PCBTARANIS) #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_LAST_INPUT = MIXSRC_FIRST_INPUT+MAX_INPUTS-1,
MIXSRC_FIRST_LUA, MIXSRC_FIRST_LUA,
@@ -1540,18 +1540,18 @@ enum MixSources {
MIXSRC_TRN, MIXSRC_TRN,
#endif #endif
MIXSRC_FIRST_LOGICAL_SWITCH, 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_SW9 = MIXSRC_SW1 + 8,
MIXSRC_SWA, MIXSRC_SWA,
MIXSRC_SWB, MIXSRC_SWB,
MIXSRC_SWC, MIXSRC_SWC,
MIXSRC_LAST_LOGICAL_SWITCH = MIXSRC_FIRST_LOGICAL_SWITCH+NUM_LOGICAL_SWITCH-1, 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_LAST_TRAINER = MIXSRC_FIRST_TRAINER+NUM_TRAINER-1,
MIXSRC_FIRST_CH, 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_CH2,
MIXSRC_CH3, MIXSRC_CH3,
MIXSRC_CH4, MIXSRC_CH4,
@@ -1569,7 +1569,7 @@ enum MixSources {
MIXSRC_CH16, MIXSRC_CH16,
MIXSRC_LAST_CH = MIXSRC_CH1+NUM_CHNOUT-1, 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_LAST_GVAR = MIXSRC_GVAR1+MAX_GVARS-1,
MIXSRC_FIRST_TELEM, MIXSRC_FIRST_TELEM,
+10 -13
View File
@@ -42,15 +42,13 @@ def LEXP_TELEMETRY(name, description):
checkName(name) checkName(name)
exports.append( ("MIXSRC_FIRST_TELEM-1+"+CONSTANT_VALUE, name, description) ) exports.append( ("MIXSRC_FIRST_TELEM-1+"+CONSTANT_VALUE, name, description) )
def LEXP_MULTIPLE(nameFormat, descriptionFormat, firstValue, numOfValues): def LEXP_MULTIPLE(nameFormat, descriptionFormat, valuesCount):
# print "LEXP_MULTIPLE %s, %s, %s, %s" % (nameFormat, descriptionFormat, firstValue, numOfValues) # print "LEXP_MULTIPLE %s, %s, %s" % (nameFormat, descriptionFormat, valuesCount)
first = int(firstValue) for v in range(valuesCount):
last = first + int(numOfValues) - 1 name = nameFormat + str(v)
for v in range(first, last+1):
name = nameFormat % v
# print name # print name
checkName(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"): def LEXP_EXTRA(name, description, value, condition = "true"):
# print "LEXP_EXTRA %s, %s, %s, %s" % (name, description, value, condition) # print "LEXP_EXTRA %s, %s, %s, %s" % (name, description, value, condition)
@@ -93,6 +91,7 @@ while True:
# warning = True # warning = True
# continue # continue
# dups_cnst.append(CONSTANT_VALUE) # dups_cnst.append(CONSTANT_VALUE)
# print cmd
eval(cmd) eval(cmd)
except: except:
print "ERROR: problem with the definition: %s" % line 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()) out.write("//This file was generated by luaexport.py script on %s\n\n\n" % time.asctime())
header = """ header = """
struct LuaField { struct LuaSingleField {
uint16_t id; uint16_t id;
const char * name; const char * name;
const char * desc; const char * desc;
//const uint8_t attr;
}; };
struct LuaMultipleField { struct LuaMultipleField {
uint16_t id; uint16_t id;
const char * name; const char * name;
const char * desc; const char * desc;
uint8_t start; uint8_t count;
uint8_t end;
}; };
""" """
@@ -127,7 +124,7 @@ out.write(header)
out.write(""" out.write("""
// The list of Lua fields // The list of Lua fields
// this aray is alphabetically sorted by the second field (name) // 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 exports.sort(key = lambda x: x[1]) #sort by name
data = [" {%s, \"%s\", \"%s\"}" % export for export in exports] 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 // The list of Lua fields that have a range of values
const LuaMultipleField luaMultipleFields[] = { 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".join(data))
out.write("\n};\n\n") out.write("\n};\n\n")
print "Generated %d items in luaMultipleFields[]" % len(exports_multiple) print "Generated %d items in luaMultipleFields[]" % len(exports_multiple)