Merge pull request #8397 from opentx/elecpower/cpn-timerdata-refactor

Timers refactoring and model setup code enhancements
This commit is contained in:
Bertrand Songis
2021-04-05 10:41:32 +02:00
committed by GitHub
31 changed files with 802 additions and 415 deletions
+2 -1
View File
@@ -23,11 +23,13 @@ set(firmwares_SRCS
rawswitch.cpp
sensordata.cpp
telem_data.cpp
timerdata.cpp
afhds3.cpp
ersky9x/ersky9xeeprom.cpp
ersky9x/ersky9xinterface.cpp
opentx/opentxeeprom.cpp
opentx/opentxinterface.cpp
datahelpers.cpp
)
string(REPLACE ".cpp" ".h" firmwares_HDRS "${firmwares_SRCS}")
@@ -35,7 +37,6 @@ string(REPLACE ".cpp" ".h" firmwares_HDRS "${firmwares_SRCS}")
list(APPEND firmwares_HDRS
eepromimportexport.h
moduledata.h
helpersdata.h
)
set(firmwares_QT
+70
View File
@@ -0,0 +1,70 @@
/*
* 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.
*/
#include "datahelpers.h"
QString DataHelpers::boolToString(const bool value, const BoolFormat format)
{
static const char *strings[] = {
QT_TRANSLATE_NOOP("DataHelpers", "Disabled"),
QT_TRANSLATE_NOOP("DataHelpers", "Enabled"),
QT_TRANSLATE_NOOP("DataHelpers", "OFF"),
QT_TRANSLATE_NOOP("DataHelpers", "ON"),
QT_TRANSLATE_NOOP("DataHelpers", "False"),
QT_TRANSLATE_NOOP("DataHelpers", "True"),
QT_TRANSLATE_NOOP("DataHelpers", "N"),
QT_TRANSLATE_NOOP("DataHelpers", "Y"),
QT_TRANSLATE_NOOP("DataHelpers", "No"),
QT_TRANSLATE_NOOP("DataHelpers", "Yes")
};
return QCoreApplication::translate("DataHelpers", strings[format * 2 + value]);
}
QString DataHelpers::getElementName(const QString & prefix, const unsigned int index, const char * name, const bool padding)
{
QString result = prefix;
result.append(padding ? QString("%1").arg(index, 2, 10, QChar('0')) : QString("%1").arg(index));
if (name && QString(name).trimmed().length() > 0)
result.append(":" + QString(name).trimmed());
return result;
}
QString DataHelpers::timeToString(const int value, const unsigned int mask)
{
bool negative = value < 0 ? true : false;
int val = abs(value);
QString result = QString("%1").arg(negative ? "-" : ((mask & TIMESTR_MASK_PADSIGN) ? " " : ""));
if (mask & TIMESTR_MASK_HRSMINS) {
int hours = val / 3600;
if (hours > 0 || (mask & TIMESTR_MASK_ZEROHRS)) {
val -= hours * 3600;
result.append(QString("%1:").arg(hours, 2, 10, QLatin1Char('0')));
}
}
int minutes = val / 60;
int seconds = val % 60;
result.append(QString("%1:%2").arg(minutes, 2, 10, QLatin1Char('0')).arg(seconds, 2, 10, QLatin1Char('0')));
return result;
}
@@ -24,8 +24,6 @@
class FieldRange
{
Q_DECLARE_TR_FUNCTIONS(FieldRange)
public:
FieldRange():
decimals(0),
@@ -48,3 +46,24 @@ class FieldRange
QString prefix;
QString unit;
};
constexpr unsigned int TIMESTR_MASK_HRSMINS { 1 << 1 };
constexpr unsigned int TIMESTR_MASK_ZEROHRS { 1 << 2 };
constexpr unsigned int TIMESTR_MASK_PADSIGN { 1 << 3 };
namespace DataHelpers
{
enum BoolFormat {
BOOL_FMT_ENABLEDISABLE,
BOOL_FMT_ONOFF,
BOOL_FMT_TRUEFALSE,
BOOL_FMT_YN,
BOOL_FMT_YESNO
};
QString boolToString(const bool value, const BoolFormat format);
QString getElementName(const QString & prefix, const unsigned int index, const char * name = 0, const bool padding = false);
QString timeToString(const int value, const unsigned int mask);
}
+5 -26
View File
@@ -27,31 +27,6 @@
#include "helpers.h"
#include "adjustmentreference.h"
/*
* TimerData
*/
void TimerData::convert(RadioDataConversionState & cstate)
{
cstate.setComponent(tr("TMR"), 1);
cstate.setSubComp(tr("Timer %1").arg(cstate.subCompIdx + 1));
mode.convert(cstate);
}
bool TimerData::isEmpty()
{
return (mode == RawSwitch(SWITCH_TYPE_TIMER_MODE, 0) && name[0] == '\0' && minuteBeep == 0 && countdownBeep == COUNTDOWN_SILENT && val == 0 && persistent == 0 /*&& pvalue == 0*/);
}
QString TimerData::nameToString(int index) const
{
return RadioData::getElementName(tr("TMR", "as in Timer"), index + 1, name);
}
/*
* ModelData
*/
ModelData::ModelData()
{
clear();
@@ -1381,7 +1356,6 @@ void ModelData::sortMixes()
void ModelData::updateResetParam(CustomFunctionData * cfd)
{
if (cfd->func != FuncReset)
return;
@@ -1392,6 +1366,11 @@ void ModelData::updateResetParam(CustomFunctionData * cfd)
switch (updRefInfo.type)
{
case REF_UPD_TYPE_TIMER:
if (cfd->param < 0 || cfd->param > 2)
return;
idxAdj = -2; // reverse earlier offset required for rawsource
break;
case REF_UPD_TYPE_SENSOR:
idxAdj = 5/*3 Timers + Flight + Telemetery*/ + firmware->getCapability(RotaryEncoders);
if (cfd->param < idxAdj || cfd->param > (idxAdj + firmware->getCapability(Sensors)))
+1 -29
View File
@@ -33,6 +33,7 @@
#include "output_data.h"
#include "sensordata.h"
#include "telem_data.h"
#include "timerdata.h"
#include <QtCore>
@@ -55,35 +56,6 @@ class RSSIAlarmData {
}
};
#define TIMER_NAME_LEN 8
class TimerData {
Q_DECLARE_TR_FUNCTIONS(TimerData)
public:
enum CountDownMode {
COUNTDOWN_SILENT,
COUNTDOWN_BEEPS,
COUNTDOWN_VOICE,
COUNTDOWN_HAPTIC
};
TimerData() { clear(); }
RawSwitch mode;
char name[TIMER_NAME_LEN+1];
bool minuteBeep;
unsigned int countdownBeep;
unsigned int val;
unsigned int persistent;
int countdownStart;
unsigned int direction;
int pvalue;
void clear() { memset(reinterpret_cast<void *>(this), 0, sizeof(TimerData)); mode = RawSwitch(SWITCH_TYPE_TIMER_MODE, 0); }
void convert(RadioDataConversionState & cstate);
bool isEmpty();
bool isModeOff() { return mode == RawSwitch(SWITCH_TYPE_TIMER_MODE, 0); }
QString nameToString(int index) const;
};
#define CPN_MAX_SCRIPTS 9
#define CPN_MAX_SCRIPT_INPUTS 10
class ScriptData {
-17
View File
@@ -22,23 +22,6 @@
#include "radiodataconversionstate.h"
#include "eeprominterface.h"
// static
QString RadioData::getElementName(const QString & prefix, unsigned int index, const char * name, bool padding)
{
QString result = prefix;
if (padding)
result += QString("%1").arg(index, 2, 10, QChar('0'));
else
result += QString("%1").arg(index);
if (name) {
QString trimmed = QString(name).trimmed();
if (trimmed.length() > 0) {
result += ":" + QString(name).trimmed();
}
}
return result;
}
RadioData::RadioData()
{
models.resize(getCurrentFirmware()->getCapability(Models));
+6 -5
View File
@@ -1,9 +1,10 @@
#ifndef _RADIODATA_H_
#define _RADIODATA_H_
#pragma once
#include "generalsettings.h"
#include "modeldata.h"
#include "datahelpers.h" // required for getElementName
#include <QtCore>
class RadioDataConversionState;
@@ -32,10 +33,10 @@ class RadioData {
void fixModelFilenames();
QString getNextModelFilename();
static QString getElementName(const QString & prefix, unsigned int index, const char * name = 0, bool padding = false);
// leave here until all calls repointed
static QString getElementName(const QString & prefix, unsigned int index, const char * name = 0, bool padding = false)
{ return DataHelpers::getElementName(prefix, index, name, padding); }
protected:
void fixModelFilename(unsigned int index);
};
#endif // _RADIODATA_H_
+6 -8
View File
@@ -19,8 +19,6 @@
*/
#include "sensordata.h"
#include "radiodata.h"
#include "modeldata.h"
#include "eeprominterface.h"
#include "compounditemmodels.h"
@@ -35,7 +33,7 @@ void SensorData::updateUnit()
QString SensorData::nameToString(int index) const
{
return RadioData::getElementName(tr("TELE"), index + 1, label);
return DataHelpers::getElementName(tr("TELE"), index + 1, label);
}
QString SensorData::getOrigin(const ModelData * model) const
@@ -171,7 +169,7 @@ QString SensorData::paramsToString(const ModelData * model) const
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)));
str.append(QString(FMT_LABEL_VALUE).arg(tr("Auto Offset")).arg(DataHelpers::boolToString(autoOffset, DataHelpers::BOOL_FMT_YN)));
}
else {
str.append(QString(FMT_LABEL_VALUE).arg(tr("Blades")).arg(ratio)); // TODO refactor to dedicated RPMS field
@@ -180,15 +178,15 @@ QString SensorData::paramsToString(const ModelData * model) const
}
if (mask & SENSOR_ISCONFIGURABLE)
str.append(QString(FMT_LABEL_VALUE).arg(tr("Filter")).arg(boolToString(filter)));
str.append(QString(FMT_LABEL_VALUE).arg(tr("Filter")).arg(DataHelpers::boolToString(filter, DataHelpers::BOOL_FMT_YN)));
if (type == TELEM_TYPE_CALCULATED)
str.append(QString(FMT_LABEL_VALUE).arg(tr("Persist")).arg(boolToString(persistent)));
str.append(QString(FMT_LABEL_VALUE).arg(tr("Persist")).arg(DataHelpers::boolToString(persistent, DataHelpers::BOOL_FMT_YN)));
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("Positive")).arg(DataHelpers::boolToString(onlyPositive, DataHelpers::BOOL_FMT_YN)));
str.append(QString(FMT_LABEL_VALUE).arg(tr("Log")).arg(boolToString(logs)));
str.append(QString(FMT_LABEL_VALUE).arg(tr("Log")).arg(DataHelpers::boolToString(logs, DataHelpers::BOOL_FMT_YN)));
return str;
}
+1 -2
View File
@@ -20,7 +20,7 @@
#pragma once
#include "helpersdata.h"
#include "datahelpers.h"
#include <QtCore>
@@ -205,7 +205,6 @@ class SensorData {
static QString cellIndexToString(const int value);
static QString unitToString(const int value);
static QString precToString(const int value);
static QString boolToString(const bool value) { return value ? tr("Y") : tr("N"); }
static bool isRssiSensorAvailable(const ModelData * model, const int value);
static QString rssiSensorToString(const ModelData * model, const int value);
+188
View File
@@ -0,0 +1,188 @@
/*
* 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.
*/
#include "timerdata.h"
#include "radiodataconversionstate.h"
#include "compounditemmodels.h"
void TimerData::convert(RadioDataConversionState & cstate)
{
cstate.setComponent(tr("TMR"), 1);
cstate.setSubComp(tr("Timer %1").arg(cstate.subCompIdx + 1));
mode.convert(cstate);
}
void TimerData::clear()
{
memset(reinterpret_cast<void *>(this), 0, sizeof(TimerData));
mode = RawSwitch(SWITCH_TYPE_TIMER_MODE, 0);
}
bool TimerData::isEmpty()
{
return (mode == RawSwitch(SWITCH_TYPE_TIMER_MODE, 0) && name[0] == '\0' && minuteBeep == 0 && countdownBeep == COUNTDOWNBEEP_SILENT && val == 0 && persistent == 0 /*&& pvalue == 0*/);
}
bool TimerData::isModeOff()
{
return mode == RawSwitch(SWITCH_TYPE_TIMER_MODE, 0);
}
QString TimerData::nameToString(int index) const
{
return DataHelpers::getElementName(tr("TMR", "as in Timer"), index + 1, name);
}
QString TimerData::countdownBeepToString() const
{
return countdownBeepToString(countdownBeep);
}
QString TimerData::countdownStartToString() const
{
if (countdownBeep == COUNTDOWNBEEP_SILENT)
return "";
else
return countdownStartToString(countdownStart);
}
QString TimerData::persistentToString(const bool verbose) const
{
return persistentToString(persistent, verbose);
}
QString TimerData::pvalueToString() const
{
return pvalueToString(pvalue);
}
QString TimerData::valToString() const
{
return valToString(val);
}
void TimerData::countdownBeepChanged()
{
if (countdownBeep == COUNTDOWNBEEP_SILENT)
countdownStart = 0;
}
// static
QString TimerData::countdownBeepToString(const int value)
{
switch(value) {
case COUNTDOWNBEEP_SILENT:
return tr("Silent");
case COUNTDOWNBEEP_BEEPS:
return tr("Beeps");
case COUNTDOWNBEEP_VOICE:
return tr("Voice");
case COUNTDOWNBEEP_HAPTIC:
return tr("Haptic");
default:
return CPN_STR_UNKNOWN_ITEM;
}
}
// static
QString TimerData::countdownStartToString(const int value)
{
switch(value) {
case COUNTDOWNSTART_5:
return tr("5s");
case COUNTDOWNSTART_10:
return tr("10s");
case COUNTDOWNSTART_20:
return tr("20s");
case COUNTDOWNSTART_30:
return tr("30s");
default:
return CPN_STR_UNKNOWN_ITEM;
}
}
// static
QString TimerData::persistentToString(const int value, const bool verbose)
{
switch(value) {
case PERSISTENT_NOT:
return verbose ? tr("Not persistent") : tr("NOT");
case PERSISTENT_FLIGHT:
return verbose ? tr("Persistent (flight)") : tr("Flight");
case PERSISTENT_MANUALRESET:
return verbose ? tr("Persistent (manual reset)") : tr("Manual reset");
default:
return CPN_STR_UNKNOWN_ITEM;
}
}
// static
QString TimerData::pvalueToString(const int value)
{
return DataHelpers::timeToString(value, TIMESTR_MASK_HRSMINS | TIMESTR_MASK_ZEROHRS);
}
// static
QString TimerData::valToString(const int value)
{
return DataHelpers::timeToString(value, TIMESTR_MASK_HRSMINS | TIMESTR_MASK_ZEROHRS);
}
// static
AbstractStaticItemModel * TimerData::countdownBeepItemModel()
{
AbstractStaticItemModel * mdl = new AbstractStaticItemModel();
mdl->setName(AIM_TIMER_COUNTDOWNBEEP);
for (int i = 0; i < COUNTDOWNBEEP_COUNT; i++) {
mdl->appendToItemList(countdownBeepToString(i), i);
}
mdl->loadItemList();
return mdl;
}
// static
AbstractStaticItemModel * TimerData::countdownStartItemModel()
{
AbstractStaticItemModel * mdl = new AbstractStaticItemModel();
mdl->setName(AIM_TIMER_COUNTDOWNSTART);
for (int i = COUNTDOWNSTART_LAST; i >= COUNTDOWNSTART_FIRST; i--) {
mdl->appendToItemList(countdownStartToString(i), i);
}
mdl->loadItemList();
return mdl;
}
// static
AbstractStaticItemModel * TimerData::persistentItemModel()
{
AbstractStaticItemModel * mdl = new AbstractStaticItemModel();
mdl->setName(AIM_TIMER_PERSISTENT);
for (int i = 0; i < PERSISTENT_COUNT; i++) {
mdl->appendToItemList(persistentToString(i), i);
}
mdl->loadItemList();
return mdl;
}
+98
View File
@@ -0,0 +1,98 @@
/*
* 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 "rawswitch.h"
#include "datahelpers.h"
#include <QtCore>
class RadioDataConversionState;
class AbstractStaticItemModel;
constexpr char AIM_TIMER_COUNTDOWNBEEP[] {"timerdata.countdownBeep"};
constexpr char AIM_TIMER_COUNTDOWNSTART[] {"timerdata.countdownStart"};
constexpr char AIM_TIMER_PERSISTENT[] {"timerdata.persistent"};
constexpr int TIMER_NAME_LEN {8};
class TimerData {
Q_DECLARE_TR_FUNCTIONS(TimerData)
public:
enum CountDownBeepType {
COUNTDOWNBEEP_SILENT,
COUNTDOWNBEEP_BEEPS,
COUNTDOWNBEEP_VOICE,
COUNTDOWNBEEP_HAPTIC,
COUNTDOWNBEEP_COUNT
};
enum CountDownStart {
COUNTDOWNSTART_30 = -2,
COUNTDOWNSTART_FIRST = COUNTDOWNSTART_30,
COUNTDOWNSTART_20,
COUNTDOWNSTART_10,
COUNTDOWNSTART_5,
COUNTDOWNSTART_LAST = COUNTDOWNSTART_5
};
enum PersistentType {
PERSISTENT_NOT,
PERSISTENT_FLIGHT,
PERSISTENT_MANUALRESET,
PERSISTENT_COUNT
};
TimerData() { clear(); }
RawSwitch mode;
char name[TIMER_NAME_LEN + 1];
bool minuteBeep;
unsigned int countdownBeep;
unsigned int val;
unsigned int persistent;
int countdownStart;
unsigned int direction;
int pvalue;
void convert(RadioDataConversionState & cstate);
void clear();
bool isEmpty();
bool isModeOff();
QString nameToString(int index) const;
QString countdownBeepToString() const;
QString countdownStartToString() const;
QString persistentToString(const bool verbose = true) const;
QString pvalueToString() const;
QString valToString() const;
void countdownBeepChanged();
static QString countdownBeepToString(const int value);
static QString countdownStartToString(const int value);
static QString persistentToString(const int value, const bool verbose = true);
static QString pvalueToString(const int value);
static QString valToString(const int value);
static AbstractStaticItemModel * countdownBeepItemModel();
static AbstractStaticItemModel * countdownStartItemModel();
static AbstractStaticItemModel * persistentItemModel();
};