1
0
Fork 0
mirror of https://github.com/EdgeTX/edgetx.git synced 2025-07-23 08:15:13 +03:00

Implement setting sensor values from LUA (#3977)

* Implement setting sensor values from LUA

* Fix name of lua sensors
This commit is contained in:
Arne Schwabe 2016-11-02 12:48:11 +01:00 committed by Bertrand Songis
parent 2974af5018
commit 54a9393588
4 changed files with 61 additions and 5 deletions

View file

@ -875,6 +875,51 @@ static int luaDefaultStick(lua_State * L)
return 1;
}
/* luadoc
@function setTelemetryValue(name, id, subID, instance, value, unit, precision)
@param name (string) Name of the sensor if it does not yet exit (4 chars)
@param id Id of the sensor
@param subID subID of the sensor, usually 0
@param instance instance of the sensor
@param value of the sensor
@param precision the precision of the sensor, value is devided by 10^precision, e.g. value=1000, prec=2 => 10.00
@retval true, if the sensor was just added. In this case the value is ignored (subsequent call will set the value)
*/
static int luaSetTelemetryValue(lua_State * L)
{
const char* name = luaL_checkstring(L, 1);
char zname[4];
str2zchar(zname, name, 4);
uint16_t id = luaL_checkinteger(L, 2);
uint8_t subId = luaL_checkinteger(L, 3);
uint8_t instance = luaL_checkinteger(L, 4);
int32_t value = luaL_checkinteger(L, 5);
uint32_t unit = luaL_checkinteger(L, 6);
uint32_t prec = luaL_checkinteger(L, 7);
int index = setTelemetryValue(TELEM_PROTO_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);
lua_pushboolean(L, true);
} else {
lua_pushboolean(L, false);
}
return 1;
}
/*luadoc
@function defaultChannel(stick)
@ -925,6 +970,7 @@ const luaL_Reg opentxLib[] = {
#endif
{ "sportTelemetryPop", luaSportTelemetryPop },
{ "sportTelemetryPush", luaSportTelemetryPush },
{ "setTelemetryValue", luaSetTelemetryValue },
#if defined(CROSSFIRE)
{ "crossfireTelemetryPop", luaCrossfireTelemetryPop },
{ "crossfireTelemetryPush", luaCrossfireTelemetryPush },

View file

@ -431,7 +431,8 @@ enum TelemetryProtocol
TELEM_PROTO_FRSKY_D,
TELEM_PROTO_FRSKY_SPORT,
TELEM_PROTO_CROSSFIRE,
TELEM_PROTO_SPEKTRUM
TELEM_PROTO_SPEKTRUM,
TELEM_PROTO_LUA
};
enum TelemAnas {

View file

@ -107,7 +107,7 @@ PACK(struct CellValue
}
});
void setTelemetryValue(TelemetryProtocol protocol, uint16_t id, uint8_t subId, uint8_t instance, int32_t value, uint32_t unit, uint32_t prec);
int setTelemetryValue(TelemetryProtocol protocol, uint16_t id, uint8_t subId, uint8_t instance, int32_t value, uint32_t unit, uint32_t prec);
void delTelemetryIndex(uint8_t index);
int availableTelemetryIndex();
int lastUsedTelemetryIndex();

View file

@ -429,7 +429,7 @@ int lastUsedTelemetryIndex()
return -1;
}
void setTelemetryValue(TelemetryProtocol protocol, uint16_t id, uint8_t subId, uint8_t instance, int32_t value, uint32_t unit, uint32_t prec)
int setTelemetryValue(TelemetryProtocol protocol, uint16_t id, uint8_t subId, uint8_t instance, int32_t value, uint32_t unit, uint32_t prec)
{
bool available = false;
@ -443,7 +443,7 @@ void setTelemetryValue(TelemetryProtocol protocol, uint16_t id, uint8_t subId, u
}
if (available || !allowNewSensors) {
return;
return -1;
}
int index = availableTelemetryIndex();
@ -468,15 +468,24 @@ void setTelemetryValue(TelemetryProtocol protocol, uint16_t id, uint8_t subId, u
case TELEM_PROTO_SPEKTRUM:
spektrumSetDefault(index, id, subId, instance);
break;
#endif
#if defined(LUA)
case TELEM_PROTO_LUA:
// Sensor will be initialized by calling function
// This drops the first value
return index;
#endif
default:
return;
return index;
}
telemetryItems[index].setValue(g_model.telemetrySensors[index], value, unit, prec);
return index;
}
else {
POPUP_WARNING(STR_TELEMETRYFULL);
return -1;
}
}
void TelemetrySensor::init(const char * label, uint8_t unit, uint8_t prec)