Skip to content

Commit

Permalink
feat(cpn): add support for surface radios inc Radiomaster MT12 (#5381)
Browse files Browse the repository at this point in the history
  • Loading branch information
elecpower authored Sep 26, 2024
1 parent 2bd62c9 commit 149e1a2
Show file tree
Hide file tree
Showing 90 changed files with 3,301 additions and 1,942 deletions.
44 changes: 36 additions & 8 deletions companion/src/apppreferencesdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,27 @@
#include "joystickdialog.h"
#endif
#include "moduledata.h"
#include "compounditemmodels.h"
#include "filtereditemmodels.h"
#include "updates/updatefactories.h"
#include "updates/updateoptionsdialog.h"

#include <QAbstractItemModel>
constexpr char FIM_TEMPLATESETUP[] {"Template Setup"};

AppPreferencesDialog::AppPreferencesDialog(QWidget * parent, UpdateFactories * factories) :
QDialog(parent),
ui(new Ui::AppPreferencesDialog),
updateLock(false),
mainWinHasDirtyChild(false),
factories(factories)
factories(factories),
panelItemModels(nullptr)
{
ui->setupUi(this);
setWindowIcon(CompanionIcon("apppreferences.png"));
ui->tabWidget->setCurrentIndex(0);

panelItemModels = new FilteredItemModelFactory();
panelItemModels->registerItemModel(new FilteredItemModel(GeneralSettings::templateSetupItemModel()), FIM_TEMPLATESETUP);

initSettings();
connect(ui->boardCB, SIGNAL(currentIndexChanged(int)), this, SLOT(onBaseFirmwareChanged()));
connect(ui->opt_appDebugLog, &QCheckBox::toggled, this, &AppPreferencesDialog::toggleAppLogSettings);
Expand All @@ -65,6 +69,7 @@ AppPreferencesDialog::AppPreferencesDialog(QWidget * parent, UpdateFactories * f
AppPreferencesDialog::~AppPreferencesDialog()
{
delete ui;
delete panelItemModels;
}

void AppPreferencesDialog::setMainWinHasDirtyChild(bool value)
Expand Down Expand Up @@ -156,8 +161,8 @@ void AppPreferencesDialog::accept()

profile.defaultInternalModule(ui->defaultInternalModuleCB->currentData().toInt());
profile.externalModuleSize(ui->externalModuleSizeCB->currentData().toInt());
profile.channelOrder(ui->channelorderCB->currentIndex());
profile.defaultMode(ui->stickmodeCB->currentIndex());
profile.channelOrder(ui->channelorderCB->currentData().toInt());
profile.defaultMode(ui->stickmodeCB->currentData().toInt());
profile.burnFirmware(ui->burnFirmware->isChecked());
profile.sdPath(ui->sdPath->text());
profile.pBackupDir(ui->profilebackupPath->text());
Expand Down Expand Up @@ -305,8 +310,12 @@ void AppPreferencesDialog::initSettings()
ui->defaultInternalModuleCB->setCurrentIndex(ui->defaultInternalModuleCB->findData(profile.defaultInternalModule()));
ui->externalModuleSizeCB->setModel(Boards::externalModuleSizeItemModel());
ui->externalModuleSizeCB->setCurrentIndex(ui->externalModuleSizeCB->findData(profile.externalModuleSize()));
ui->channelorderCB->setCurrentIndex(profile.channelOrder());
ui->stickmodeCB->setCurrentIndex(profile.defaultMode());
panelItemModels->getItemModel(FIM_TEMPLATESETUP)->setFilterFlags(Boards::isAir() ? GeneralSettings::RadioTypeContextAir :
GeneralSettings::RadioTypeContextSurface);
ui->channelorderCB->setModel(panelItemModels->getItemModel(FIM_TEMPLATESETUP));
ui->channelorderCB->setCurrentIndex(ui->channelorderCB->findData(profile.channelOrder()));
ui->stickmodeCB->setModel(GeneralSettings::stickModeItemModel());
ui->stickmodeCB->setCurrentIndex(ui->stickmodeCB->findData(profile.defaultMode()));
ui->sdPath->setText(profile.sdPath());
if (!profile.pBackupDir().isEmpty()) {
if (QDir(profile.pBackupDir()).exists()) {
Expand All @@ -321,6 +330,11 @@ void AppPreferencesDialog::initSettings()
ui->pbackupEnable->setDisabled(true);
}

if (Boards::isSurface()) {
ui->stickmodeLabel->hide();
ui->stickmodeCB->hide();
}

ui->profileNameLE->setText(profile.name());

QString hwSettings;
Expand Down Expand Up @@ -649,7 +663,8 @@ bool AppPreferencesDialog::displayImage(const QString & fileName)
return false;

ui->imageLabel->setPixmap(makePixMap(image));
ui->imageLabel->setFixedSize(getCurrentFirmware()->getCapability(LcdWidth), getCurrentFirmware()->getCapability(LcdHeight));
ui->imageLabel->setFixedSize(Boards::getCapability(getCurrentBoard(), Board::LcdWidth),
Boards::getCapability(getCurrentBoard(), Board::LcdHeight));
return true;
}

Expand Down Expand Up @@ -690,6 +705,19 @@ void AppPreferencesDialog::onBaseFirmwareChanged()
profile.externalModuleSize(Boards::getDefaultExternalModuleSize(newfw->getBoard()));
ui->externalModuleSizeCB->setModel(Boards::externalModuleSizeItemModel());
ui->externalModuleSizeCB->setCurrentIndex(ui->externalModuleSizeCB->findData(profile.externalModuleSize()));

if (Boards::isSurface()) {
profile.defaultMode(1);
ui->stickmodeLabel->hide();
ui->stickmodeCB->hide();
profile.channelOrder(0);
}

ui->stickmodeCB->setCurrentIndex(ui->stickmodeCB->findData(profile.defaultMode()));
panelItemModels->getItemModel(FIM_TEMPLATESETUP)->setFilterFlags(Boards::isAir() ? GeneralSettings::RadioTypeContextAir :
GeneralSettings::RadioTypeContextSurface);
ui->channelorderCB->setCurrentIndex(ui->channelorderCB->findData(profile.channelOrder()));

}

Firmware *AppPreferencesDialog::getBaseFirmware() const
Expand Down
4 changes: 4 additions & 0 deletions companion/src/apppreferencesdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include <QCheckBox>
#include <QComboBox>

class FilteredItemModelFactory;

namespace Ui {
class AppPreferencesDialog;
}
Expand Down Expand Up @@ -95,6 +97,8 @@ class AppPreferencesDialog : public QDialog
QComboBox *cboReleaseChannel[MAX_COMPONENTS];
QPushButton *btnComponentOptions[MAX_COMPONENTS];

FilteredItemModelFactory *panelItemModels;

void loadUpdatesTab();

};
4 changes: 2 additions & 2 deletions companion/src/apppreferencesdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ Mode 4:
</widget>
</item>
<item row="15" column="0">
<widget class="QLabel" name="label_14">
<widget class="QLabel" name="stickmodeLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
Expand Down Expand Up @@ -676,7 +676,7 @@ Mode 4:
</widget>
</item>
<item row="16" column="0">
<widget class="QLabel" name="label_13">
<widget class="QLabel" name="channelorderLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
Expand Down
4 changes: 2 additions & 2 deletions companion/src/datamodels/compounditemmodels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ ThrottleSourceItemModel::ThrottleSourceItemModel(const GeneralSettings * const g
if (!modelData)
return;

setUpdateMask(IMUE_Timers | IMUE_Inputs | IMUE_TeleSensors);
setUpdateMask(IMUE_Channels);

for (int i = 0; i < modelData->thrTraceSrcCount(); i++) {
QStandardItem * modelItem = new QStandardItem();
Expand All @@ -407,7 +407,7 @@ ThrottleSourceItemModel::ThrottleSourceItemModel(const GeneralSettings * const g

void ThrottleSourceItemModel::setDynamicItemData(QStandardItem * item, const int value) const
{
item->setText(modelData->thrTraceSrcToString(value));
item->setText(modelData->thrTraceSrcToString(generalSettings, value));
item->setData(modelData->isThrTraceSrcAvailable(generalSettings, value), IMDR_Available);
}

Expand Down
87 changes: 81 additions & 6 deletions companion/src/firmwares/boardjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ BoardJson::BoardJson(Board::Type board, QString hwdefn) :
m_inputs(new InputsTable),
m_switches(new SwitchesTable),
m_trims(new TrimsTable),
m_keys(new KeysTable),
m_inputCnt({0, 0, 0, 0, 0, 0, 0, 0, 0}),
m_switchCnt({0, 0, 0})
{
Expand All @@ -79,10 +80,12 @@ BoardJson::~BoardJson()
delete m_inputs;
delete m_switches;
delete m_trims;
delete m_keys;
}

// static
void BoardJson::afterLoadFixups(Board::Type board, InputsTable * inputs, SwitchesTable * switches)
void BoardJson::afterLoadFixups(Board::Type board, InputsTable * inputs, SwitchesTable * switches,
KeysTable * keys, TrimsTable * trims)
{
// TODO json files do not contain gyro defs
// Radio cmake directive IMU is currently used
Expand Down Expand Up @@ -114,8 +117,8 @@ void BoardJson::afterLoadFixups(Board::Type board, InputsTable * inputs, Switche
}
}

// Flex switches are not listed in json file
int count = IS_RADIOMASTER_TX16S(board) ? 2 : 0;
// Flex switches are not listed in json file for these radios
int count = IS_RADIOMASTER_TX16S(board) || IS_RADIOMASTER_MT12(board) ? 2 : 0;

for (int i = 1; i <= count; i++) {
QString tag = QString("FL%1").arg(i);
Expand Down Expand Up @@ -164,6 +167,9 @@ const int BoardJson::getCapability(const Board::Capability capability) const
case Board::InputSwitches:
return m_inputCnt.switches;

case Board::Keys:
return m_keys->size();

case Board::MultiposPots:
// assumes every input has potential to be one
// index used for mapping 6 pos switches back to input
Expand Down Expand Up @@ -332,6 +338,14 @@ const int BoardJson::getInputSliderIndex(int index)
return -1;
}

const int BoardJson::getInputThrottleIndex()
{
if (getCapability(Board::Sticks) > 0)
return getInputTagOffset(m_inputs, Boards::getCapability(m_board, Board::Air) ? "RV" : "TH");

return -1;
}

const int BoardJson::getInputTypeOffset(Board::AnalogInputType type)
{
return getInputTypeOffset(m_inputs, type);
Expand Down Expand Up @@ -371,6 +385,42 @@ Board::InputInfo BoardJson::getInputInfo(const InputsTable * inputs, int index)
return info;
}

const int BoardJson::getKeyIndex(const QString key) const
{
return getKeyIndex(m_keys, key);
}

// static
int BoardJson::getKeyIndex(const KeysTable * keys, QString key)
{
for (int i = 0; i < (int)keys->size(); i++) {
if (keys->at(i).key.c_str() == key)
return i;
}

return -1;
}

const Board::KeyInfo BoardJson::getKeyInfo(int index) const
{
return getKeyInfo(m_keys, index);
}

// static
Board::KeyInfo BoardJson::getKeyInfo(const KeysTable * keys, int index)
{
Board::KeyInfo info;

if (index >= 0 && index < (int)keys->size()) {
KeyDefn defn = keys->at(index);
info.name = defn.name;
info.key = defn.key;
info.label = defn.label;
}

return info;
}

// static
int BoardJson::getNumericSuffix(const std::string str)
{
Expand Down Expand Up @@ -771,10 +821,10 @@ bool BoardJson::loadDefinition()
if (m_board == Board::BOARD_UNKNOWN)
return true;

if (!loadFile(m_board, m_hwdefn, m_inputs, m_switches, m_trims))
if (!loadFile(m_board, m_hwdefn, m_inputs, m_switches, m_keys, m_trims))
return false;

afterLoadFixups(m_board, m_inputs, m_switches);
afterLoadFixups(m_board, m_inputs, m_switches, m_keys, m_trims);

setInputCounts(m_inputs, m_inputCnt);
setSwitchCounts(m_switches, m_switchCnt);
Expand All @@ -795,6 +845,7 @@ bool BoardJson::loadDefinition()
"flex inputs:" << getCapability(Board::FlexInputs) <<
"input switches:" << getCapability(Board::InputSwitches) <<
"trims:" << getCapability(Board::NumTrims) <<
"keys:" << getCapability(Board::Keys) <<
"std switches:" << getCapability(Board::StandardSwitches) <<
"flex switches:" << getCapability(Board::FlexSwitches) <<
"func switches:" << getCapability(Board::FunctionSwitches) <<
Expand All @@ -805,7 +856,8 @@ bool BoardJson::loadDefinition()
}

// static
bool BoardJson::loadFile(Board::Type board, QString hwdefn, InputsTable * inputs, SwitchesTable * switches, TrimsTable * trims)
bool BoardJson::loadFile(Board::Type board, QString hwdefn, InputsTable * inputs, SwitchesTable * switches,
KeysTable * keys, TrimsTable * trims)
{
if (board == Board::BOARD_UNKNOWN) {
return false;
Expand Down Expand Up @@ -952,6 +1004,29 @@ bool BoardJson::loadFile(Board::Type board, QString hwdefn, InputsTable * inputs
}
}

if (obj.value("keys").isArray()) {
const QJsonArray &kys = obj.value("keys").toArray();

for (const QJsonValue &key : kys)
{
if (key.isObject()) {
const QJsonObject &o = key.toObject();
KeyDefn k;

if (!o.value("name").isUndefined()) {
k.name = o.value("name").toString().toStdString();
k.key = o.value("key").toString().toStdString();
k.label = o.value("label").toString().toStdString();
k.tag = k.name;
}

keys->insert(keys->end(), k);

// qDebug() << "name:" << k.name.c_str() << "key:" << k.key.c_str() << "label:" << k.label.c_str();
}
}
}

if (obj.value("trims").isArray()) {
const QJsonArray &trms = obj.value("trims").toArray();

Expand Down
27 changes: 25 additions & 2 deletions companion/src/firmwares/boardjson.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ class BoardJson

typedef std::vector<SwitchDefn> SwitchesTable;

struct KeyDefn {
std::string tag = "";
std::string name = "";
std::string key = "";
std::string label = "";
Board::LookupValueType cfgYaml = Board::LVT_TAG;
Board::LookupValueType refYaml = Board::LVT_NAME;

KeyDefn() = default;
};

typedef std::vector<KeyDefn> KeysTable;

struct TrimDefn {
std::string tag = "";
std::string name = "";
Expand Down Expand Up @@ -106,6 +119,7 @@ class BoardJson
const int getInputSliderIndex(int index);
const QString getInputTag(int index) const;
const int getInputTagOffset(QString tag);
const int getInputThrottleIndex();
const int getInputTypeOffset(Board::AnalogInputType type);
const int getInputYamlIndex(const QString val, YamlLookupType ylt) const;
const QString getInputYamlName(int index, YamlLookupType ylt) const;
Expand All @@ -119,6 +133,9 @@ class BoardJson
const bool isInputStick(int index) const;
const bool isInputSwitch(int index) const;

const Board::KeyInfo getKeyInfo(int index) const;
const int getKeyIndex(const QString key) const;

const int getSwitchIndex(const QString val, Board::LookupValueType lvt) const;
const Board::SwitchInfo getSwitchInfo(int index) const;
const QString getSwitchName(int index) const;
Expand All @@ -144,6 +161,7 @@ class BoardJson
InputsTable *m_inputs;
SwitchesTable *m_switches;
TrimsTable *m_trims;
KeysTable *m_keys;

struct InputCounts {
unsigned int flexGyroAxes;
Expand All @@ -167,8 +185,10 @@ class BoardJson

SwitchCounts m_switchCnt;

static bool loadFile(Board::Type board, QString hwdefn, InputsTable * inputs, SwitchesTable * switches, TrimsTable * trims);
static void afterLoadFixups(Board::Type board, InputsTable * inputs, SwitchesTable * switches);
static bool loadFile(Board::Type board, QString hwdefn, InputsTable * inputs, SwitchesTable * switches,
KeysTable * keys, TrimsTable * trims);
static void afterLoadFixups(Board::Type board, InputsTable * inputs, SwitchesTable * switches,
KeysTable * keys, TrimsTable * trims);

static int getInputsCalibrated(const InputsTable * inputs);

Expand All @@ -179,6 +199,9 @@ class BoardJson
static int getInputTagOffset(const InputsTable * inputs, QString tag);
static int getInputTypeOffset(const InputsTable * inputs, Board::AnalogInputType type);

static int getKeyIndex(const KeysTable * keys, QString key);
static Board::KeyInfo getKeyInfo(const KeysTable * keys, int index);

static int getSwitchIndex(const SwitchesTable * switches, QString val, Board::LookupValueType lvt);
static Board::SwitchInfo getSwitchInfo(const SwitchesTable * switches, int index);
static QString getSwitchName(const SwitchesTable * switches, int index);
Expand Down
Loading

0 comments on commit 149e1a2

Please sign in to comment.