Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support GVs in calculated telemetry sources #2096

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 62 additions & 10 deletions companion/src/datamodels/compounditemmodels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,22 +481,74 @@ TelemetrySourceItemModel::TelemetrySourceItemModel(const GeneralSettings * const
if (!modelData)
return;

setUpdateMask(IMUE_TeleSensors | IMUE_Modules);
const int count = firmware->getCapability(Sensors);
setUpdateMask(IMUE_TeleSensors | IMUE_Modules | IMUE_GVars);

for (int i = -count; i <= count; ++i) {
addItems(SOURCE_TYPE_GVAR, -firmware->getCapability(Gvars));
addItems(SOURCE_TYPE_TELEMETRY, -firmware->getCapability(Sensors));
addItems(SOURCE_TYPE_NONE, 1);
addItems(SOURCE_TYPE_TELEMETRY, firmware->getCapability(Sensors));
addItems(SOURCE_TYPE_GVAR, firmware->getCapability(Gvars));
}

void TelemetrySourceItemModel::addItems(const RawSourceType & type, int count)
{
int flags = 0;

if (type == SOURCE_TYPE_GVAR)
flags = RawSource::GVarsGroup;

int i = (count < 0 ? count : 1);
count = (i < 0 ? 0 : count + i);

for ( ; i < count; ++i) {
QStandardItem * modelItem = new QStandardItem();
modelItem->setData(i, IMDR_Id);
modelItem->setData(i < 0 ? IMDG_Negative : i > 0 ? IMDG_Positive : IMDG_None, IMDR_Flags);
setDynamicItemData(modelItem, i);
int id = i;

if (type == SOURCE_TYPE_NONE)
id = 0;

if (type != SOURCE_TYPE_GVAR) {
flags = (i < 0 ? IMDG_Negative : i > 0 ? IMDG_Positive : IMDG_None);
}
else {
id = ((SENSOR_GVAR_START + 1) * (i < 0 ? -1 : 1)) - id;
}

modelItem->setData(id, IMDR_Id);
modelItem->setData((int)type, IMDR_Type);
modelItem->setData(flags , IMDR_Flags);
setDynamicItemData(modelItem);
appendRow(modelItem);
}
}

void TelemetrySourceItemModel::setDynamicItemData(QStandardItem * item, const int value) const
void TelemetrySourceItemModel::setDynamicItemData(QStandardItem * item) const
{
item->setText(SensorData::sourceToString(modelData, value));
item->setData(SensorData::isSourceAvailable(modelData, value), IMDR_Available);
const int type = item->data(IMDR_Type).toInt();
int id = item->data(IMDR_Id).toInt();

switch (type) {
case SOURCE_TYPE_NONE:
{
const RawSource src = RawSource(id);
item->setText(src.toString());
item->setData(true, IMDR_Available);
break;
}
case SOURCE_TYPE_TELEMETRY:
item->setText(SensorData::sourceToString(modelData, id));
item->setData(SensorData::isSourceAvailable(modelData, id), IMDR_Available);
break;
case SOURCE_TYPE_GVAR:
{
int idx = SENSOR_GVAR_START - abs(id);
item->setText((id < 0 ? "-" : "") % modelData->gvarData[idx].nameToString(idx));
item->setData(true, IMDR_Available);
break;
}
default:
break;
}
}

void TelemetrySourceItemModel::update(const int event)
Expand All @@ -505,7 +557,7 @@ void TelemetrySourceItemModel::update(const int event)
emit aboutToBeUpdated();

for (int i = 0; i < rowCount(); ++i) {
setDynamicItemData(item(i), item(i)->data(IMDR_Id).toInt());
setDynamicItemData(item(i));
}

emit updateComplete();
Expand Down
3 changes: 2 additions & 1 deletion companion/src/datamodels/compounditemmodels.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ class TelemetrySourceItemModel: public AbstractDynamicItemModel
virtual void update(const int event = IMUE_SystemRefresh) override;

protected:
virtual void setDynamicItemData(QStandardItem * item, const int value) const;
virtual void setDynamicItemData(QStandardItem * item) const;
void addItems(const RawSourceType & type, int count);
};

class CurveRefTypeItemModel : public AbstractStaticItemModel
Expand Down
1 change: 1 addition & 0 deletions companion/src/firmwares/sensordata.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <QtCore>

constexpr int SENSOR_LABEL_LEN { 4 };
constexpr int SENSOR_GVAR_START { 127 };

constexpr int SENSOR_ISCONFIGURABLE { 1 << 1 };
constexpr int SENSOR_HAS_GPS { 1 << 2 };
Expand Down
1 change: 1 addition & 0 deletions radio/src/datastructs_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ PACK(struct TelemetrySensor {
void init(const char *label, uint8_t unit=UNIT_RAW, uint8_t prec=0);
void init(uint16_t id);
bool isAvailable() const;
bool isOfflineFresh() const;
int32_t getValue(int32_t value, uint8_t unit, uint8_t prec) const;
bool isConfigurable() const;
bool isPrecConfigurable() const;
Expand Down
45 changes: 38 additions & 7 deletions radio/src/gui/128x64/model_telemetry_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,46 @@ void menuModelSensor(event_t event)
{
drawStringWithIndex(0, y, STR_SOURCE, k-SENSOR_FIELD_PARAM1+1);
int8_t * source = &sensor->calc.sources[k-SENSOR_FIELD_PARAM1];
uint8_t delta = GV_GET_GV1_VALUE(-MAX_TELEMETRY_SENSORS, MAX_TELEMETRY_SENSORS);
if (attr) {
*source = checkIncDec(event, *source, -MAX_TELEMETRY_SENSORS, MAX_TELEMETRY_SENSORS, EE_MODEL|NO_INCDEC_MARKS, isSensorAvailable);
}
if (*source < 0) {
lcdDrawChar(SENSOR_2ND_COLUMN, y, '-', attr);
drawSource(lcdNextPos, y, MIXSRC_FIRST_TELEM+3*(-1-*source), attr);
#if defined(GVARS)
if (event == EVT_KEY_LONG(KEY_ENTER)) {
*source = (GV_IS_GV_VALUE(*source, -MAX_TELEMETRY_SENSORS, MAX_TELEMETRY_SENSORS) ? 0 : delta);
s_editMode = !s_editMode;
}
if(GV_IS_GV_VALUE(*source, -MAX_TELEMETRY_SENSORS, MAX_TELEMETRY_SENSORS)) {
int8_t idx = (int16_t)GV_INDEX_CALC_DELTA(*source, delta);
CHECK_INCDEC_MODELVAR(event, idx, -MAX_GVARS, MAX_GVARS-1);
if (idx < 0) {
*source = (int8_t)GV_CALC_VALUE_IDX_NEG(idx, delta);
}
else {
*source = (int8_t)GV_CALC_VALUE_IDX_POS(idx, delta);
}
} else {
#endif
*source = checkIncDec(event, *source, -MAX_TELEMETRY_SENSORS, MAX_TELEMETRY_SENSORS, EE_MODEL|NO_INCDEC_MARKS, isSensorAvailable);
}
#if defined(GVARS)
}
else {
drawSource(SENSOR_2ND_COLUMN, y, *source ? MIXSRC_FIRST_TELEM+3*(*source-1) : 0, attr);
if(GV_IS_GV_VALUE(*source, -MAX_TELEMETRY_SENSORS, MAX_TELEMETRY_SENSORS)) {
int8_t gvindex = GV_INDEX_CALC_DELTA(*source, delta);
if(gvindex<0 && sensor->formula == TELEM_FORMULA_MULTIPLY) {
lcdDrawChar(SENSOR_2ND_COLUMN, y, '/', attr);
drawGVarName(lcdNextPos, y, -gvindex-1, attr);
} else {
drawGVarName(SENSOR_2ND_COLUMN, y, gvindex, attr);
}
} else
#endif
{
if (*source < 0) {
lcdDrawChar(SENSOR_2ND_COLUMN, y, '-', attr);
drawSource(lcdNextPos, y, MIXSRC_FIRST_TELEM+3*(-1-*source), attr);
}
else {
drawSource(SENSOR_2ND_COLUMN, y, *source ? MIXSRC_FIRST_TELEM+3*(*source-1) : 0, attr);
}
}
break;
}
Expand Down
41 changes: 36 additions & 5 deletions radio/src/gui/212x64/model_telemetry_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,46 @@ void menuModelSensor(event_t event)
{
drawStringWithIndex(0, y, STR_SOURCE, k-SENSOR_FIELD_PARAM1+1);
int8_t * source = &sensor->calc.sources[k-SENSOR_FIELD_PARAM1];
uint8_t delta = GV_GET_GV1_VALUE(-MAX_TELEMETRY_SENSORS, MAX_TELEMETRY_SENSORS);
if (attr) {
#if defined(GVARS)
if (event == EVT_KEY_LONG(KEY_ENTER)) {
*source = (GV_IS_GV_VALUE(*source, -MAX_TELEMETRY_SENSORS, MAX_TELEMETRY_SENSORS) ? 0 : delta);
s_editMode = !s_editMode;
}
if(GV_IS_GV_VALUE(*source, -MAX_TELEMETRY_SENSORS, MAX_TELEMETRY_SENSORS)) {
int8_t idx = (int16_t)GV_INDEX_CALC_DELTA(*source, delta);
CHECK_INCDEC_MODELVAR(event, idx, -MAX_GVARS, MAX_GVARS-1);
if (idx < 0) {
*source = (int8_t)GV_CALC_VALUE_IDX_NEG(idx, delta);
}
else {
*source = (int8_t)GV_CALC_VALUE_IDX_POS(idx, delta);
}
} else {
#endif
*source = checkIncDec(event, *source, -MAX_TELEMETRY_SENSORS, MAX_TELEMETRY_SENSORS, EE_MODEL|NO_INCDEC_MARKS, isSensorAvailable);
}
if (*source < 0) {
lcdDrawChar(SENSOR_2ND_COLUMN, y, '-', attr);
drawSource(lcdNextPos, y, MIXSRC_FIRST_TELEM+3*(-1-*source), attr);
#if defined(GVARS)
}
else {
drawSource(SENSOR_2ND_COLUMN, y, *source ? MIXSRC_FIRST_TELEM+3*(*source-1) : 0, attr);
if(GV_IS_GV_VALUE(*source, -MAX_TELEMETRY_SENSORS, MAX_TELEMETRY_SENSORS)) {
int8_t gvindex = GV_INDEX_CALC_DELTA(*source, delta);
if(gvindex<0 && sensor->formula == TELEM_FORMULA_MULTIPLY) {
lcdDrawChar(SENSOR_2ND_COLUMN, y, '/', attr);
drawGVarName(lcdNextPos, y, -gvindex-1, attr);
} else {
drawGVarName(SENSOR_2ND_COLUMN, y, gvindex, attr);
}
} else
#endif
{
if (*source < 0) {
lcdDrawChar(SENSOR_2ND_COLUMN, y, '-', attr);
drawSource(lcdNextPos, y, MIXSRC_FIRST_TELEM+3*(-1-*source), attr);
}
else {
drawSource(SENSOR_2ND_COLUMN, y, *source ? MIXSRC_FIRST_TELEM+3*(*source-1) : 0, attr);
}
}
break;
}
Expand Down
Loading