Merge pull request #8259 from opentx/elecpower/cpn-tele-edit-prt

Companion sensors refactoring
This commit is contained in:
Bertrand Songis
2021-02-02 15:23:31 +01:00
committed by GitHub
21 changed files with 511 additions and 664 deletions
+1
View File
@@ -30,6 +30,7 @@ string(REPLACE ".cpp" ".h" firmwares_HDRS "${firmwares_SRCS}")
list(APPEND firmwares_HDRS
eepromimportexport.h
moduledata.h
helpersdata.h
)
set(firmwares_QT
+50
View File
@@ -0,0 +1,50 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include <QtCore>
class FieldRange
{
Q_DECLARE_TR_FUNCTIONS(FieldRange)
public:
FieldRange():
decimals(0),
min(0.0),
max(0.0),
step(1.0),
offset(0.0),
prefix(""),
unit("")
{
}
float getValue(int value) { return float(value) * step; }
int decimals;
double min;
double max;
double step;
double offset;
QString prefix;
QString unit;
};
+1 -1
View File
@@ -60,7 +60,7 @@ RawSourceRange RawSource::getRange(const ModelData * model, const GeneralSetting
result.min = -30000 * result.step;
result.max = +30000 * result.step;
result.decimals = sensor.prec;
result.unit = sensor.unitString();
result.unit = SensorData::unitToString(qr.quot);
break;
}
+306 -106
View File
@@ -33,12 +33,316 @@ void SensorData::updateUnit()
}
}
// TODO depreciated
QString SensorData::unitString() const
QString SensorData::nameToString(int index) const
{
return RadioData::getElementName(tr("TELE"), index + 1, label);
}
QString SensorData::getOrigin(const ModelData * model) const
{
if (type != TELEM_TYPE_CUSTOM || !id)
return QString();
const ModuleData & module = model->moduleData[moduleIdx];
if (module.isPxx2Module() && rxIdx <= 2 && module.access.receivers & (1 << rxIdx)) {
return QString(module.access.receiverName[rxIdx]);
}
else {
return QString();
}
}
bool SensorData::isEmpty() const
{
return (!isAvailable() && type == 0 && id == 0 && subid == 0 && instance == 0 && rxIdx == 0 && moduleIdx == 0 && unit == 0 && ratio == 0 && prec == 0 && offset == 0 && autoOffset == 0 && filter == 0 && onlyPositive == 0 && logs == 0);
}
QString SensorData::idToString() const
{
return idToString(id);
}
QString SensorData::typeToString() const
{
return typeToString(type);
}
QString SensorData::formulaToString() const
{
return formulaToString(formula);
}
QString SensorData::cellIndexToString() const
{
return cellIndexToString(index);
}
QString SensorData::unitToString() const
{
return unitToString(unit);
}
QString SensorData::precToString() const
{
return precToString(prec);
}
int SensorData::getMask() const
{
int mask = 0;
if (type == TELEM_TYPE_CALCULATED) {
if (formula < TELEM_FORMULA_CELL)
mask |= SENSOR_ISCONFIGURABLE | SENSOR_HAS_POSITIVE;
if (formula == TELEM_FORMULA_DIST)
mask |= SENSOR_HAS_GPS;
if (formula == TELEM_FORMULA_CELL)
mask |= SENSOR_HAS_CELLS;
if (formula == TELEM_FORMULA_CONSUMPTION)
mask |= SENSOR_HAS_CONSUMPTION;
if (formula <= TELEM_FORMULA_MULTIPLY)
mask |= SENSOR_HAS_SOURCES_12;
if (formula < TELEM_FORMULA_MULTIPLY)
mask |= SENSOR_HAS_SOURCES_34;
if (formula == TELEM_FORMULA_TOTALIZE)
mask |= SENSOR_HAS_TOTALIZE;
}
else {
if (unit < UNIT_FIRST_VIRTUAL)
mask |= (SENSOR_ISCONFIGURABLE | SENSOR_HAS_RATIO | SENSOR_HAS_POSITIVE);
}
if (mask & SENSOR_ISCONFIGURABLE && unit != UNIT_FAHRENHEIT)
mask |= SENSOR_HAS_PRECISION;
return mask;
}
constexpr char FMT_LABEL[] { "<b>%1:</b> " };
constexpr char FMT_VALUE[] { "%1 " };
constexpr char FMT_LABEL_VALUE[] { "<b>%1:</b> %2 " };
QString SensorData::paramsToString(const ModelData * model) const
{
if (!isAvailable())
return "";
QString str;
int mask = getMask();
if (type == TELEM_TYPE_CALCULATED) {
str.append(QString(FMT_LABEL_VALUE).arg(tr("Formula")).arg(formulaToString()));
}
else {
str.append(QString(FMT_LABEL_VALUE).arg(tr("Id")).arg(idToString()));
str.append(QString(FMT_LABEL_VALUE).arg(tr("Instance")).arg(instance));
}
if (mask & SENSOR_HAS_CELLS) {
str.append(QString(FMT_LABEL_VALUE).arg(tr("Sensor")).arg(sourceToString(model, source)));
str.append(QString(FMT_VALUE).arg(cellIndexToString()));
}
if (mask & SENSOR_HAS_SOURCES_12) {
str.append(QString(FMT_LABEL).arg(tr("Sources")));
for (int i = 0; i < 4; i++) {
if (i < 2 || mask & SENSOR_HAS_SOURCES_34) {
str.append(QString(FMT_VALUE).arg(sourceToString(model, sources[i])));
}
}
}
if (mask & SENSOR_HAS_CONSUMPTION || mask & SENSOR_HAS_TOTALIZE)
str.append(QString(FMT_LABEL_VALUE).arg(tr("Sensor")).arg(sourceToString(model, amps)));
if (mask & SENSOR_HAS_GPS) {
str.append(QString(FMT_LABEL_VALUE).arg(tr("GPS")).arg(sourceToString(model, gps)));
str.append(QString(FMT_LABEL_VALUE).arg(tr("Alt")).arg(sourceToString(model, alt)));
}
if (mask & SENSOR_ISCONFIGURABLE)
str.append(QString(FMT_LABEL_VALUE).arg(tr("Unit")).arg(unitToString()));
if (mask & SENSOR_HAS_PRECISION)
str.append(QString(FMT_LABEL_VALUE).arg(tr("Precision")).arg(precToString()));
if (mask & SENSOR_HAS_RATIO) {
if (unit != UNIT_RPMS) {
int precsn = prec == 0 ? 1 : pow(10, prec);
str.append(QString(FMT_LABEL_VALUE).arg(tr("Ratio")).arg((float)ratio / 10));
str.append(QString(FMT_LABEL_VALUE).arg(tr("Offset")).arg(QString::number((float)offset / precsn, 'f', prec)));
str.append(QString(FMT_LABEL_VALUE).arg(tr("Auto Offset")).arg(boolToString(autoOffset)));
}
else {
str.append(QString(FMT_LABEL_VALUE).arg(tr("Blades")).arg(ratio)); // TODO refactor to dedicated RPMS field
str.append(QString(FMT_LABEL_VALUE).arg(tr("Multiplier")).arg(offset)); // TODO refactor to dedicated RPMS field
}
}
if (mask & SENSOR_ISCONFIGURABLE)
str.append(QString(FMT_LABEL_VALUE).arg(tr("Filter")).arg(boolToString(filter)));
if (type == TELEM_TYPE_CALCULATED)
str.append(QString(FMT_LABEL_VALUE).arg(tr("Persist")).arg(boolToString(persistent)));
if (mask & SENSOR_HAS_POSITIVE)
str.append(QString(FMT_LABEL_VALUE).arg(tr("Positive")).arg(boolToString(onlyPositive)));
str.append(QString(FMT_LABEL_VALUE).arg(tr("Log")).arg(boolToString(logs)));
return str;
}
FieldRange SensorData::getRatioRange() const
{
FieldRange result;
if (unit == SensorData::UNIT_RPMS) {
result.decimals = 0;
result.max = 30000;
result.min = 1;
result.step = 1;
}
else {
result.decimals = 1;
result.max = 30000;
result.min = 0;
result.step = 0.1;
}
return result;
}
FieldRange SensorData::getOffsetRange() const
{
FieldRange result;
if (unit == SensorData::UNIT_RPMS) {
result.decimals = 0;
result.max = 30000;
result.min = 1;
result.step = 1;
}
else {
result.decimals = prec;
result.max = 30000.0f / powf(10.0f, prec);
result.min = -result.max;
result.step = pow(0.1, prec);
}
return result;
}
void SensorData::formulaChanged()
{
switch (formula) {
case TELEM_FORMULA_CELL:
prec = 2;
unit = UNIT_VOLTS;
break;
case TELEM_FORMULA_CONSUMPTION:
prec = 0;
unit = UNIT_MAH;
break;
case TELEM_FORMULA_DIST:
prec = 0;
unit = UNIT_METERS;
break;
}
}
void SensorData::unitChanged()
{
if (unit == UNIT_FAHRENHEIT)
prec = 0;
}
// static
QString SensorData::idToString(const int value)
{
return QString::number(value, 16).toUpper();
}
// static
QString SensorData::typeToString(const int value)
{
switch(value) {
case TELEM_TYPE_CUSTOM:
return tr("Custom");
case TELEM_TYPE_CALCULATED:
return tr("Calculated");
default:
return CPN_STR_UNKNOWN_ITEM;
}
}
// static
QString SensorData::formulaToString(const int value)
{
switch(value) {
case TELEM_FORMULA_ADD:
return tr("Add");
case TELEM_FORMULA_AVERAGE:
return tr("Average");
case TELEM_FORMULA_MIN:
return tr("Minimum");
case TELEM_FORMULA_MAX:
return tr("Maximum");
case TELEM_FORMULA_MULTIPLY:
return tr("Multiply");
case TELEM_FORMULA_TOTALIZE:
return tr("Totalize");
case TELEM_FORMULA_CELL:
return tr("Cell");
case TELEM_FORMULA_CONSUMPTION:
return tr("Consumption");
case TELEM_FORMULA_DIST:
return tr("Distance");
default:
return CPN_STR_UNKNOWN_ITEM;
}
}
// static
QString SensorData::cellIndexToString(const int value)
{
if (value == TELEM_CELL_INDEX_LOWEST)
return tr("Lowest");
if (value > TELEM_CELL_INDEX_LOWEST && value < TELEM_CELL_INDEX_HIGHEST)
return tr("Cell %1").arg(value - TELEM_CELL_INDEX_LOWEST);
if (value== TELEM_CELL_INDEX_HIGHEST)
return tr("Highest");
if (value == TELEM_CELL_INDEX_DELTA)
return tr("Delta");
return CPN_STR_UNKNOWN_ITEM;
}
// static
QString SensorData::precToString(const int value)
{
return QString("0.%1").arg(QString(value, '0'));
}
// static
QString SensorData::sourceToString(const ModelData * model, const int index, const bool sign)
{
if (model) {
const QString prfx = sign ? index < 0 ? "-" : "+" : "";
if (abs(index) > 0) {
const SensorData &sd = model->sensorData[abs(index) - 1];
if (sd.type == SensorData::TELEM_TYPE_CUSTOM)
return QString("%1%2 (%3)").arg(prfx).arg(sd.label).arg(sd.getOrigin(model));
else
return QString("%1%2").arg(prfx).arg(sd.label);
}
else
return CPN_STR_NONE_ITEM;
}
return "";
}
// static
QString SensorData::unitToString(const int value)
{
@@ -106,110 +410,6 @@ QString SensorData::unitToString(const int value)
}
}
QString SensorData::nameToString(int index) const
{
return RadioData::getElementName(tr("TELE"), index + 1, label);
}
QString SensorData::getOrigin(const ModelData * model) const
{
if (type != TELEM_TYPE_CUSTOM || !id)
return QString();
const ModuleData & module = model->moduleData[moduleIdx];
if (module.isPxx2Module() && rxIdx <= 2 && module.access.receivers & (1 << rxIdx)) {
return QString(module.access.receiverName[rxIdx]);
}
else {
return QString();
}
}
bool SensorData::isEmpty() const
{
return (!isAvailable() && type == 0 && id == 0 && subid == 0 && instance == 0 && rxIdx == 0 && moduleIdx == 0 && unit == 0 && ratio == 0 && prec == 0 && offset == 0 && autoOffset == 0 && filter == 0 && onlyPositive == 0 && logs == 0);
}
// static
QString SensorData::typeToString(const int value)
{
switch(value) {
case TELEM_TYPE_CUSTOM:
return tr("Custom");
case TELEM_TYPE_CALCULATED:
return tr("Calculated");
default:
return CPN_STR_UNKNOWN_ITEM;
}
}
// static
QString SensorData::formulaToString(const int value)
{
switch(value) {
case TELEM_FORMULA_ADD:
return tr("Add");
case TELEM_FORMULA_AVERAGE:
return tr("Average");
case TELEM_FORMULA_MIN:
return tr("Minimum");
case TELEM_FORMULA_MAX:
return tr("Maximum");
case TELEM_FORMULA_MULTIPLY:
return tr("Multiply");
case TELEM_FORMULA_TOTALIZE:
return tr("Totalize");
case TELEM_FORMULA_CELL:
return tr("Cell");
case TELEM_FORMULA_CONSUMPTION:
return tr("Consumption");
case TELEM_FORMULA_DIST:
return tr("Distance");
default:
return CPN_STR_UNKNOWN_ITEM;
}
}
// static
QString SensorData::cellIndexToString(const int value)
{
if (value == TELEM_CELL_INDEX_LOWEST)
return tr("Lowest");
if (value > TELEM_CELL_INDEX_LOWEST && value < TELEM_CELL_INDEX_HIGHEST)
return tr("Cell %1").arg(value - TELEM_CELL_INDEX_LOWEST);
if (value== TELEM_CELL_INDEX_HIGHEST)
return tr("Highest");
if (value == TELEM_CELL_INDEX_DELTA)
return tr("Delta");
return CPN_STR_UNKNOWN_ITEM;
}
// static
QString SensorData::precisionToString(const int value)
{
return QString("0.%1").arg(QString(value, '0'));
}
// static
QString SensorData::sourceToString(const ModelData * model, const int index, const bool sign)
{
if (model) {
const QString prfx = sign ? index < 0 ? "-" : "+" : "";
if (abs(index) > 0) {
const SensorData &sd = model->sensorData[abs(index) - 1];
if (sd.type == SensorData::TELEM_TYPE_CUSTOM)
return QString("%1%2 (%3)").arg(prfx).arg(sd.label).arg(sd.getOrigin(model));
else
return QString("%1%2").arg(prfx).arg(sd.label);
}
else
return CPN_STR_NONE_ITEM;
}
return "";
}
// static
bool SensorData::isSourceAvailable(const ModelData * model, const int index)
{
+32 -8
View File
@@ -18,19 +18,32 @@
* GNU General Public License for more details.
*/
#ifndef SENSORDATA_H
#define SENSORDATA_H
#pragma once
#include "helpersdata.h"
#include <QtCore>
constexpr int CPN_MAX_SENSORS = 60;
constexpr int SENSOR_LABEL_LEN = 4;
constexpr int CPN_MAX_SENSORS { 60 };
constexpr int SENSOR_LABEL_LEN { 4 };
constexpr int SENSOR_ISCONFIGURABLE { 1 << 1 };
constexpr int SENSOR_HAS_GPS { 1 << 2 };
constexpr int SENSOR_HAS_CELLS { 1 << 3 };
constexpr int SENSOR_HAS_CONSUMPTION { 1 << 4 };
constexpr int SENSOR_HAS_RATIO { 1 << 5 };
constexpr int SENSOR_HAS_TOTALIZE { 1 << 6 };
constexpr int SENSOR_HAS_SOURCES_12 { 1 << 7 };
constexpr int SENSOR_HAS_SOURCES_34 { 1 << 8 };
constexpr int SENSOR_HAS_POSITIVE { 1 << 9 };
constexpr int SENSOR_HAS_PRECISION { 1 << 10 };
class ModelData;
class AbstractStaticItemModel;
class PrecisionItemModel;
class SensorData {
Q_DECLARE_TR_FUNCTIONS(SensorData)
public:
@@ -167,19 +180,32 @@ class SensorData {
bool isAvailable() const { return strlen(label) > 0; }
void updateUnit();
QString unitString() const;
QString nameToString(int index) const;
QString getOrigin(const ModelData* model) const;
void clear() { memset(this, 0, sizeof(SensorData)); }
bool isEmpty() const;
QString idToString() const;
QString typeToString() const;
QString formulaToString() const;
QString cellIndexToString() const;
QString unitToString() const;
QString precToString() const;
int getMask() const;
QString paramsToString(const ModelData * model) const;
FieldRange getRatioRange() const;
FieldRange getOffsetRange() const;
void formulaChanged();
void unitChanged();
static QString sourceToString(const ModelData * model, const int index, const bool sign = false);
static bool isSourceAvailable(const ModelData * model, const int index);
static QString idToString(const int value);
static QString typeToString(const int value);
static QString formulaToString(const int value);
static QString cellIndexToString(const int value);
static QString unitToString(const int value);
static QString precisionToString(const int value);
static QString precToString(const int value);
static QString boolToString(const bool value) { return value ? tr("Y") : tr("N"); }
static AbstractStaticItemModel * typeItemModel();
static AbstractStaticItemModel * formulaItemModel();
@@ -187,5 +213,3 @@ class SensorData {
static AbstractStaticItemModel * unitItemModel();
static PrecisionItemModel * precisionItemModel();
};
#endif // SENSORDATA_H