ZChar nightmare / take 2

This commit is contained in:
Raphael Coeffic
2021-06-09 09:24:48 +02:00
parent 25263b16bb
commit b0fb80b264
3 changed files with 49 additions and 41 deletions
+17 -22
View File
@@ -362,22 +362,20 @@ bool luaFindFieldByName(const char * name, LuaField & field, unsigned int flags)
// search in telemetry
field.desc[0] = '\0';
for (int i=0; i<MAX_TELEMETRY_SENSORS; i++) {
for (int i = 0; i < MAX_TELEMETRY_SENSORS; i++) {
if (isTelemetryFieldAvailable(i)) {
const char* sensorName = g_model.telemetrySensors[i].label;
if (!strncmp(sensorName, name, len)) {
if ((len <= TELEM_LABEL_LEN) && !strncmp(sensorName, name, len)) {
if (name[len] == '\0') {
field.id = MIXSRC_FIRST_TELEM + 3*i;
field.id = MIXSRC_FIRST_TELEM + 3 * i;
field.desc[0] = '\0';
return true;
}
else if (name[len] == '-' && name[len+1] == '\0') {
field.id = MIXSRC_FIRST_TELEM + 3*i + 1;
} else if (name[len] == '-' && name[len + 1] == '\0') {
field.id = MIXSRC_FIRST_TELEM + 3 * i + 1;
field.desc[0] = '\0';
return true;
}
else if (name[len] == '+' && name[len+1] == '\0') {
field.id = MIXSRC_FIRST_TELEM + 3*i + 2;
} else if (name[len] == '+' && name[len + 1] == '\0') {
field.id = MIXSRC_FIRST_TELEM + 3 * i + 2;
field.desc[0] = '\0';
return true;
}
@@ -1377,28 +1375,25 @@ static int luaSetTelemetryValue(lua_State * L)
uint32_t unit = luaL_optunsigned(L, 5, 0);
uint32_t prec = luaL_optunsigned(L, 6, 0);
char zname[4];
char name_buf[TELEM_LABEL_LEN]; // 4
const char* name = luaL_optstring(L, 7, NULL);
if (name != NULL && strlen(name) > 0) {
str2zchar(zname, name, 4);
}
else {
zname[0] = hex2zchar((id & 0xf000) >> 12);
zname[1] = hex2zchar((id & 0x0f00) >> 8);
zname[2] = hex2zchar((id & 0x00f0) >> 4);
zname[3] = hex2zchar((id & 0x000f) >> 0);
if (name == NULL || strlen(name) > 0) {
name_buf[0] = hex2char((id & 0xf000) >> 12);
name_buf[1] = hex2char((id & 0x0f00) >> 8);
name_buf[2] = hex2char((id & 0x00f0) >> 4);
name_buf[3] = hex2char((id & 0x000f) >> 0);
}
if (id | subId | instance) {
int index = setTelemetryValue(PROTOCOL_TELEMETRY_LUA, id, subId, instance, value, unit, prec);
int index = setTelemetryValue(PROTOCOL_TELEMETRY_LUA, id, subId, instance,
value, unit, prec);
if (index >= 0) {
TelemetrySensor &telemetrySensor = g_model.telemetrySensors[index];
telemetrySensor.id = id;
telemetrySensor.subId = subId;
telemetrySensor.instance = instance;
telemetrySensor.init(zname, unit, prec);
telemetrySensor.init(name ? name: name_buf, unit, prec);
lua_pushboolean(L, true);
}
else {
} else {
lua_pushboolean(L, false);
}
return 1;