1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-19 06:15:10 +03:00
This commit is contained in:
bsongis 2014-07-21 22:25:55 +02:00
parent 4d5962b15b
commit 45abfb872f
3 changed files with 135 additions and 2 deletions

View file

@ -198,12 +198,14 @@ void TelemetryAnalog::on_CalibSB_editingFinished()
} }
if (alarm2value<calib) { if (alarm2value<calib) {
alarm2value=calib; alarm2value=calib;
} else if (alarm2value>(ratio+calib)) { }
else if (alarm2value>(ratio+calib)) {
alarm2value=ratio+calib; alarm2value=ratio+calib;
} }
analog.alarms[0].value=round(((alarm1value-calib)*255)/ratio); analog.alarms[0].value=round(((alarm1value-calib)*255)/ratio);
analog.alarms[1].value=round(((alarm2value-calib)*255)/ratio); analog.alarms[1].value=round(((alarm2value-calib)*255)/ratio);
} else { }
else {
analog.offset=0; analog.offset=0;
analog.alarms[0].value=0; analog.alarms[0].value=0;
analog.alarms[1].value=0; analog.alarms[1].value=0;

View file

@ -1051,6 +1051,120 @@ static int luaModelSetGlobalVariable(lua_State *L)
return 0; return 0;
} }
static int luaModelGetTelemetryChannel(lua_State *L)
{
int idx = luaL_checkunsigned(L, 1);
if (idx < MAX_FRSKY_A_CHANNELS) {
FrSkyChannelData & channel = g_model.frsky.channels[idx];
lua_newtable(L);
double range = applyChannelRatio(idx, 255-channel.offset);
double offset = applyChannelRatio(idx, 0);
double alarm1 = applyChannelRatio(idx, channel.alarms_value[0]);
double alarm2 = applyChannelRatio(idx, channel.alarms_value[1]);
if (ANA_CHANNEL_UNIT(idx) >= UNIT_RAW) {
range /= 10;
offset /= 10;
alarm1 /= 10;
alarm2 /= 10;
}
else {
range /= 100;
offset /= 100;
alarm1 /= 100;
alarm2 /= 100;
}
lua_pushtablenumber(L, "range", range);
lua_pushtablenumber(L, "offset", offset);
lua_pushtablenumber(L, "alarm1", alarm1);
lua_pushtablenumber(L, "alarm2", alarm2);
lua_pushtableinteger(L, "unit", channel.type);
}
else {
lua_pushnil(L);
}
return 1;
}
int findmult(float value, float base)
{
int vvalue = value*10;
int vbase = base*10;
vvalue--;
int mult = 0;
for (int i=8; i>=0; i--) {
if (vvalue/vbase >= (1<<i)) {
mult = i+1;
break;
}
}
return mult;
}
static int luaModelSetTelemetryChannel(lua_State *L)
{
int idx = luaL_checkunsigned(L, 1);
if (idx < MAX_FRSKY_A_CHANNELS) {
FrSkyChannelData & channel = g_model.frsky.channels[idx];
double range = applyChannelRatio(idx, 255-channel.offset);
double offset = applyChannelRatio(idx, 0);
double alarm1 = applyChannelRatio(idx, channel.alarms_value[0]);
double alarm2 = applyChannelRatio(idx, channel.alarms_value[1]);
if (ANA_CHANNEL_UNIT(idx) >= UNIT_RAW) {
range /= 10;
offset /= 10;
alarm1 /= 10;
alarm2 /= 10;
}
else {
range /= 100;
offset /= 100;
alarm1 /= 100;
alarm2 /= 100;
}
luaL_checktype(L, -1, LUA_TTABLE);
for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
luaL_checktype(L, -2, LUA_TSTRING); // key is string
const char * key = luaL_checkstring(L, -2);
if (!strcmp(key, "unit")) {
channel.type = luaL_checkinteger(L, -1);
}
else if (!strcmp(key, "range")) {
range = luaL_checknumber(L, -1);
double value = range;
if (ANA_CHANNEL_UNIT(idx) < UNIT_RAW) {
value *= 10;
}
channel.multiplier = findmult(value, 255);
channel.ratio = (int)(round(value)) / (1<<channel.multiplier);
}
else if (!strcmp(key, "offset")) {
offset = luaL_checknumber(L, -1);
}
else if (!strcmp(key, "alarm1")) {
alarm1 = luaL_checknumber(L, -1);
}
else if (!strcmp(key, "alarm2")) {
alarm2 = luaL_checknumber(L, -1);
}
}
if (range > 0) {
channel.offset = round((offset * 255) / range);
channel.alarms_value[0] = limit<int>(0, round((alarm1-offset)*255/range), 255);
channel.alarms_value[1] = limit<int>(0, round((alarm2-offset)*255/range), 255);
}
else {
channel.offset = 0;;
channel.alarms_value[0] = 0;
channel.alarms_value[1] = 0;
}
eeDirty(EE_MODEL);
}
return 0;
}
static int luaPopupInput(lua_State *L) static int luaPopupInput(lua_State *L)
{ {
uint8_t event = luaL_checkinteger(L, 2); uint8_t event = luaL_checkinteger(L, 2);
@ -1190,6 +1304,8 @@ static const luaL_Reg modelLib[] = {
{ "setOutput", luaModelSetOutput }, { "setOutput", luaModelSetOutput },
{ "getGlobalVariable", luaModelGetGlobalVariable }, { "getGlobalVariable", luaModelGetGlobalVariable },
{ "setGlobalVariable", luaModelSetGlobalVariable }, { "setGlobalVariable", luaModelSetGlobalVariable },
{ "getTelemetryChannel", luaModelGetTelemetryChannel },
{ "setTelemetryChannel", luaModelSetTelemetryChannel },
{ NULL, NULL } /* sentinel */ { NULL, NULL } /* sentinel */
}; };

View file

@ -60,4 +60,19 @@ TEST(Lua, testSetModelId)
EXPECT_EQ(g_model.header.modelId, 2); EXPECT_EQ(g_model.header.modelId, 2);
} }
TEST(Lua, testSetTelemetryChannel)
{
luaExecStr("channel = model.getTelemetryChannel(0)");
luaExecStr("channel.range = 100.0");
luaExecStr("channel.offset = -10.0");
luaExecStr("channel.alarm1 = 60");
luaExecStr("channel.alarm2 = 50");
luaExecStr("model.setTelemetryChannel(0, channel)");
EXPECT_EQ(g_model.frsky.channels[0].multiplier, 2);
EXPECT_EQ(g_model.frsky.channels[0].ratio, 250);
EXPECT_EQ(g_model.frsky.channels[0].offset, -26);
EXPECT_EQ(g_model.frsky.channels[0].alarms_value[0], 179);
EXPECT_EQ(g_model.frsky.channels[0].alarms_value[1], 153);
}
#endif #endif