feat(radio,cpn): Updates for new joystick system (#3400)

* Reduce RAM usage for advanced joystick system on radio:
- Don't allocate memory blocks unless joystick mode is being used.
Support multiple radio joystick configurations in Companion:
- Save configured joystick settings by joystick name in Companion settings.
- Save last joystick name used in each profile.
- Reload joystick settings by name when profile is changed.

* Only show configuration for used axes and buttons in Companion.
Send actual number of configured joystick buttons from radio in USB packet.

* Fix issue where USB joystick not recognised if no buttons configured.

* Fix compiler warnings.

* Remove debugging.

* Fix crash in Companion.

* Change channel 8 to type 'dial', workaround for Linux mapping issue.

---------

Co-authored-by: Phil Mitchell <phil.a.mitchell@gmail.com>
This commit is contained in:
philmoz
2023-04-21 14:27:41 +10:00
committed by GitHub
co-authored by Phil Mitchell
parent 62a40f1c47
commit e1fe7fce89
10 changed files with 537 additions and 294 deletions
+128 -1
View File
@@ -266,6 +266,36 @@ bool JButtonData::existsOnDisk()
return (m_settings.value(settingsPath() % button_idx_key(), -1).toInt() > -1);
}
NamedJStickData::NamedJStickData() : CompStoreObj(), index(-1)
{
CompStoreObj::addObjectMapping(propertyGroup(), this);
}
bool NamedJStickData::existsOnDisk()
{
return (m_settings.value(settingsPath() % stick_axe_key(), -1).toInt() > -1);
}
NamedJButtonData::NamedJButtonData() : CompStoreObj(), index(-1)
{
CompStoreObj::addObjectMapping(propertyGroup(), this);
}
bool NamedJButtonData::existsOnDisk()
{
return (m_settings.value(settingsPath() % button_idx_key(), -1).toInt() > -1);
}
NamedJSData::NamedJSData() : CompStoreObj(), index(-1)
{
CompStoreObj::addObjectMapping(propertyGroup(), this);
}
bool NamedJSData::existsOnDisk()
{
return (m_settings.value(settingsPath() % jsName(), -1).toInt() > -1);
}
// ** Profile class********************
@@ -411,6 +441,13 @@ AppData::AppData() :
joystick[i].setIndex(i);
for (int i = 0; i < MAX_JS_BUTTONS; i++)
jsButton[i].setIndex(i);
for (int i = 0; i < MAX_NAMED_JOYSTICKS; i++) {
namedJS[i].setIndex(i);
for (int a = 0; a < MAX_JS_AXES; a += 1)
namedJS[i].joystick[a].setIndex(a, i);
for (int b = 0; b < MAX_JS_BUTTONS; b += 1)
namedJS[i].jsButton[b].setIndex(b, i);
}
// Configure the updates
for (int i = 0; i < MAX_COMPONENTS; i++) {
@@ -421,6 +458,74 @@ AppData::AppData() :
}
}
void AppData::saveNamedJS(int i)
{
namedJS[i].jsName(currentProfile().jsName());
for (int a = 0; a < MAX_JS_AXES; a += 1) {
namedJS[i].joystick[a].stick_axe(joystick[a].stick_axe());
namedJS[i].joystick[a].stick_max(joystick[a].stick_max());
namedJS[i].joystick[a].stick_med(joystick[a].stick_med());
namedJS[i].joystick[a].stick_min(joystick[a].stick_min());
namedJS[i].joystick[a].stick_min(joystick[a].stick_min());
}
for (int b = 0; b < MAX_JS_BUTTONS; b += 1) {
namedJS[i].jsButton[b].button_idx(jsButton[b].button_idx());
}
namedJS[i].jsLastUsed(time(NULL));
}
void AppData::saveNamedJS()
{
for (int i = 0; i < MAX_NAMED_JOYSTICKS; i += 1) {
if (namedJS[i].jsName() == currentProfile().jsName()) {
saveNamedJS(i);
return;
}
}
for (int i = 0; i < MAX_NAMED_JOYSTICKS; i += 1) {
if (namedJS[i].jsName() == "") {
saveNamedJS(i);
return;
}
}
unsigned int oldestTime = namedJS[0].jsLastUsed();
int oldestN = 0;
for (int i = 1; i < MAX_NAMED_JOYSTICKS; i += 1) {
if (namedJS[i].jsLastUsed() < oldestTime) {
oldestTime = namedJS[i].jsLastUsed();
oldestN = i;
}
}
saveNamedJS(oldestN);
}
void AppData::loadNamedJS(int i)
{
for (int a = 0; a < MAX_JS_AXES; a += 1) {
joystick[a].stick_axe(namedJS[i].joystick[a].stick_axe());
joystick[a].stick_max(namedJS[i].joystick[a].stick_max());
joystick[a].stick_med(namedJS[i].joystick[a].stick_med());
joystick[a].stick_min(namedJS[i].joystick[a].stick_min());
joystick[a].stick_min(namedJS[i].joystick[a].stick_min());
}
for (int b = 0; b < MAX_JS_BUTTONS; b += 1) {
jsButton[b].button_idx(namedJS[i].jsButton[b].button_idx());
}
namedJS[i].jsLastUsed(time(NULL));
}
void AppData::loadNamedJS()
{
for (int i = 0; i < MAX_NAMED_JOYSTICKS; i += 1) {
if (namedJS[i].jsName() == currentProfile().jsName()) {
loadNamedJS(i);
return;
}
}
}
static QString fmtHex(quint32 num)
{
return QString::number(num, 16).toUpper();
@@ -452,7 +557,14 @@ void AppData::initAll()
joystick[i].init();
for (int i = 0; i < MAX_JS_BUTTONS; i++)
jsButton[i].init();
// Initialize the updatess
for (int i = 0; i < MAX_NAMED_JOYSTICKS; i++) {
namedJS[i].init();
for (int a = 0; a < MAX_JS_AXES; a += 1)
namedJS[i].joystick[a].init();
for (int b = 0; b < MAX_JS_BUTTONS; b += 1)
namedJS[i].jsButton[b].init();
}
// Initialize the updates
for (int i = 0; i < MAX_COMPONENTS; i++) {
component[i].init();
for (int j = 0; j < MAX_COMPONENT_ASSETS; j++) {
@@ -471,6 +583,13 @@ void AppData::resetAllSettings()
joystick[i].resetAll();
for (int i = 0; i < MAX_JS_BUTTONS; i++)
jsButton[i].resetAll();
for (int i = 0; i < MAX_NAMED_JOYSTICKS; i++) {
namedJS[i].resetAll();
for (int a = 0; a < MAX_JS_AXES; a += 1)
namedJS[i].joystick[a].resetAll();
for (int b = 0; b < MAX_JS_BUTTONS; b += 1)
namedJS[i].jsButton[b].resetAll();
}
for (int i = 0; i < MAX_COMPONENTS; i++) {
component[i].resetAll();
for (int j = 0; j < MAX_COMPONENT_ASSETS; j++) {
@@ -489,6 +608,13 @@ void AppData::storeAllSettings()
joystick[i].storeAll();
for (int i = 0; i < MAX_JS_BUTTONS; i++)
jsButton[i].storeAll();
for (int i = 0; i < MAX_NAMED_JOYSTICKS; i++) {
namedJS[i].storeAll();
for (int a = 0; a < MAX_JS_AXES; a += 1)
namedJS[i].joystick[a].storeAll();
for (int b = 0; b < MAX_JS_BUTTONS; b += 1)
namedJS[i].jsButton[b].storeAll();
}
for (int i = 0; i < MAX_COMPONENTS; i++) {
component[i].storeAll();
for (int j = 0; j < MAX_COMPONENT_ASSETS; j++)
@@ -506,6 +632,7 @@ void AppData::sessionId(int index)
if (index < 0 || index >= MAX_PROFILES || index == m_sessionId)
return;
m_sessionId = index;
loadNamedJS();
emit sessionIdChanged(index);
emit currentProfileChanged();
}
+105 -2
View File
@@ -63,6 +63,7 @@
#define MAX_JS_BUTTONS 32
#define MAX_COMPONENTS 10
#define MAX_COMPONENT_ASSETS 5
#define MAX_NAMED_JOYSTICKS 10
// It important that these function names are consistent everywhere.
#define PROP_FSIG_INIT_IMPL _init()
@@ -331,12 +332,22 @@ class JStickData: public CompStoreObj
public slots:
bool existsOnDisk();
public:
void clear() {
stick_axe(-1);
stick_med(-32768);
stick_max(32767);
stick_med(0);
stick_inv(0);
}
protected:
explicit JStickData();
void setIndex(int idx) { index = idx; }
inline QString propertyGroup() const override { return QStringLiteral("JsCalibration"); }
inline QString settingsPath() const override { return QString("%1/%2/").arg(propertyGroup()).arg(index); }
friend class AppData;
friend class NamedJSData;
private:
PROPERTY(int, stick_axe, -1)
@@ -348,22 +359,100 @@ class JStickData: public CompStoreObj
int index;
};
//! \brief JButtonData class stores properties related to each joystick button (button number).
class JButtonData: public CompStoreObj
{
Q_OBJECT
public slots:
bool existsOnDisk();
public:
void clear() {
button_idx(-1);
}
protected:
explicit JButtonData();
void setIndex(int idx) { index = idx; }
inline QString propertyGroup() const override { return QStringLiteral("JsButton"); }
inline QString settingsPath() const override { return QString("%1/%2/").arg(propertyGroup()).arg(index); }
friend class AppData;
friend class NamedJSData;
private:
PROPERTY(int, button_idx, -1)
PROPERTY(int, button_inv, 0)
int index;
};
//! \brief NamedJStickData class stores properties related to each joystick axis (calibration/assignment/direction).
class NamedJStickData: public CompStoreObj
{
Q_OBJECT
public slots:
bool existsOnDisk();
protected:
explicit NamedJStickData();
void setIndex(int idx, int nmIdx) { index = idx; namedIdx = nmIdx; }
inline QString propertyGroup() const override { return QStringLiteral("NamedJSData/%1").arg(namedIdx); }
inline QString settingsPath() const override { return QString("%1/JsCalibration/%2/").arg(propertyGroup()).arg(index); }
friend class AppData;
friend class NamedJSData;
private:
PROPERTY(int, stick_axe, -1)
PROPERTY(int, stick_min, -32768)
PROPERTY(int, stick_med, 0)
PROPERTY(int, stick_max, 32767)
PROPERTY(int, stick_inv, 0)
int namedIdx;
int index;
};
//! \brief NamedJButtonData class stores properties related to each joystick button (button number).
class NamedJButtonData: public CompStoreObj
{
Q_OBJECT
public slots:
bool existsOnDisk();
protected:
explicit NamedJButtonData();
void setIndex(int idx, int nmIdx) { index = idx; namedIdx = nmIdx; }
inline QString propertyGroup() const override { return QStringLiteral("NamedJSData/%1").arg(namedIdx); }
inline QString settingsPath() const override { return QString("%1/JsButton/%2/").arg(propertyGroup()).arg(index); }
friend class AppData;
friend class NamedJSData;
private:
PROPERTY(int, button_idx, -1)
int namedIdx;
int index;
};
class NamedJSData: public CompStoreObj
{
Q_OBJECT
public slots:
bool existsOnDisk();
protected:
explicit NamedJSData();
void setIndex(int idx) { index = idx; }
inline QString propertyGroup() const override { return QStringLiteral("NamedJSData"); }
inline QString settingsPath() const override { return QString("%1/%2/").arg(propertyGroup()).arg(index); }
friend class AppData;
public:
NamedJStickData joystick[MAX_JS_AXES];
NamedJButtonData jsButton[MAX_JS_BUTTONS];
private:
PROPERTYSTRD(jsName, "")
PROPERTY(unsigned int, jsLastUsed, 0)
int index;
};
@@ -432,6 +521,8 @@ class Profile: public CompStoreObj
PROPERTY4(int, txCurrentCalibration, "currentCalib", 0)
PROPERTY4(int, txVoltageCalibration, "VbatCalib", 0)
PROPERTYSTRD(jsName, "")
int index;
static const QStringList fwVarsList() //! for resetFwVariables()... TODO: make this go away
@@ -615,9 +706,19 @@ class AppData: public CompStoreObj
Profile profile[MAX_PROFILES];
JStickData joystick[MAX_JS_AXES];
JButtonData jsButton[MAX_JS_BUTTONS];
NamedJSData namedJS[MAX_NAMED_JOYSTICKS];
FwRevision fwRev;
ComponentData component[MAX_COMPONENTS];
void clearJSData() {
for (int i = 0; i < MAX_JS_AXES; i += 1)
joystick[i].clear();
for (int i = 0; i < MAX_JS_BUTTONS; i += 1)
jsButton[i].clear();
}
void saveNamedJS();
void loadNamedJS();
signals:
void currentProfileChanged();
@@ -625,6 +726,9 @@ class AppData: public CompStoreObj
inline QString propertyGroup() const override { return QStringLiteral("General"); }
inline QString settingsPath() const override { return QString(); }
void saveNamedJS(int i);
void loadNamedJS(int i);
private:
PROPERTY_META(int, sessionId, 0) // currently active radio profile ID, NOT saved to persistent storage -- Initialize before profileId!
@@ -679,7 +783,6 @@ class AppData: public CompStoreObj
PROPERTY4(int, embedSplashes, "embedded_splashes", 0)
PROPERTY4(int, fwServerFails, "fwserver", 0)
PROPERTY4(int, iconSize, "icon_size", 2)
PROPERTY4(int, jsCtrl, "js_ctrl", 0)
PROPERTY4(int, historySize, "history_size", 10)
PROPERTY (int, generalEditTab, 0)
PROPERTY (int, theme, 1)