feat(lua): Add getSourceValue (#2316)

* Moved getFieldInfo and getValue up right after related functions

* Modified getValue to return a valid status, so Lua can see if it got zero because of invalid source id

* Added new getSourceValue Lua function per Issue #781
Also added alias getSourceInfo for getFieldInfo

* Updated according to comment https://github.com/EdgeTX/edgetx/pull/2175#issuecomment-1245605114

* Replaced isFresh() by !isOld() as requested in https://github.com/EdgeTX/edgetx/pull/2316#issuecomment-1246759370

* Clean up `lua_pushtablenil` - tables cannot hold nil values

* Changed isFresh() to !isOld() ... AGAIN

* Return both `!isOld()` and `isFresh()`

* Return nil for all values if the item does not exist

* Return 0 instead of pushing three nil values

* Update documentation
This commit is contained in:
Jesper Frickmann
2022-09-28 09:42:16 +10:00
committed by GitHub
parent 73861554ec
commit 83fbf1a7f8
4 changed files with 267 additions and 128 deletions
+14 -3
View File
@@ -297,10 +297,11 @@ int16_t applyLimits(uint8_t channel, int32_t value)
}
// TODO same naming convention than the drawSource
getvalue_t getValue(mixsrc_t i)
// *valid added to return status to Lua for invalid sources
getvalue_t getValue(mixsrc_t i, bool* valid)
{
if (i == MIXSRC_NONE) {
if (valid != nullptr) *valid = false;
return 0;
}
else if (i <= MIXSRC_LAST_INPUT) {
@@ -312,6 +313,7 @@ getvalue_t getValue(mixsrc_t i)
div_t qr = div(i-MIXSRC_FIRST_LUA, MAX_SCRIPT_OUTPUTS);
return scriptInputsOutputs[qr.quot].outputs[qr.rem].value;
#else
if (valid != nullptr) *valid = false;
return 0;
#endif
}
@@ -344,6 +346,7 @@ getvalue_t getValue(mixsrc_t i)
#if defined(HELI)
return cyc_anas[i - MIXSRC_CYC1];
#else
if (valid != nullptr) *valid = false;
return 0;
#endif
}
@@ -361,6 +364,7 @@ getvalue_t getValue(mixsrc_t i)
return (switchState(3*sw) ? -1024 : (IS_CONFIG_3POS(sw) && switchState(3*sw+1) ? 0 : 1024));
}
else {
if (valid != nullptr) *valid = false;
return 0;
}
}
@@ -374,6 +378,7 @@ getvalue_t getValue(mixsrc_t i)
return (switchState(3*sw) ? -1024 : (IS_CONFIG_3POS(sw) && switchState(3*sw+1) ? 0 : 1024));
}
else {
if (valid != nullptr) *valid = false;
return 0;
}
}
@@ -406,6 +411,7 @@ getvalue_t getValue(mixsrc_t i)
#if defined(GVARS)
return GVAR_VALUE(i - MIXSRC_GVAR1, getGVarFlightMode(mixerCurrentFlightMode, i - MIXSRC_GVAR1));
#else
if (valid != nullptr) *valid = false;
return 0;
#endif
}
@@ -418,6 +424,7 @@ getvalue_t getValue(mixsrc_t i)
#if defined(RTCLOCK)
return (g_rtcTime % SECS_PER_DAY) / 60; // number of minutes from midnight
#else
if (valid != nullptr) *valid = false;
return 0;
#endif
}
@@ -427,6 +434,7 @@ getvalue_t getValue(mixsrc_t i)
else if (i <= MIXSRC_LAST_TELEM) {
if (IS_FAI_FORBIDDEN(i)) {
if (valid != nullptr) *valid = false;
return 0;
}
i -= MIXSRC_FIRST_TELEM;
@@ -441,7 +449,10 @@ getvalue_t getValue(mixsrc_t i)
return telemetryItem.value;
}
}
else return 0;
else {
if (valid != nullptr) *valid = false;
return 0;
}
}
void evalInputs(uint8_t mode)