@@ -91,7 +91,7 @@ AssignedButtonAction::AssignedButtonAction(QObject* parent, const QString action
91
91
: QObject(parent)
92
92
, _action(action)
93
93
, _isPwmOverrideAction(false )
94
- , _rcChannel (0 )
94
+ , _pwmRcChannel (0 )
95
95
{
96
96
}
97
97
@@ -102,8 +102,8 @@ AssignedButtonAction::AssignedButtonAction(
102
102
, _isPwmOverrideAction(true )
103
103
, _loPwmValue(loPwmValue)
104
104
, _hiPwmValue(hiPwmValue)
105
- , _latchMode (latch)
106
- , _rcChannel (getRcChannelFromAction(action))
105
+ , _pwmLatchMode (latch)
106
+ , _pwmRcChannel (getRcChannelFromAction(action))
107
107
{
108
108
}
109
109
@@ -115,40 +115,20 @@ void AssignedButtonAction::sendPwm(Vehicle *vehicle, bool buttonDown)
115
115
}
116
116
117
117
uint16_t pwmValue = buttonDown ? _hiPwmValue : _loPwmValue;
118
- if (_latchMode ) {
119
- qCDebug (JoystickLog) << " Latch mode, current saved button state " << (_latchButtonDown ? " down" : " up" );
118
+ if (_pwmLatchMode ) {
119
+ qCDebug (JoystickLog) << " Latch mode, current saved button state " << (_pwmLatchButtonDown ? " down" : " up" );
120
120
121
121
if (buttonDown) {
122
- _latchButtonDown = !_latchButtonDown ;
123
- pwmValue = _latchButtonDown ? _hiPwmValue : _loPwmValue;
122
+ _pwmLatchButtonDown = !_pwmLatchButtonDown ;
123
+ pwmValue = _pwmLatchButtonDown ? _hiPwmValue : _loPwmValue;
124
124
qCDebug (JoystickLog) << " calculated new PWM Value " << pwmValue;
125
125
} else {
126
126
qCDebug (JoystickLog) << " since button up - exiting" ;
127
127
return ;
128
128
}
129
129
}
130
130
131
- vehicle->rcChannelOverride (_rcChannel, pwmValue);
132
- }
133
-
134
- uint8_t AssignedButtonAction::pwmChannel () const
135
- {
136
- return _rcChannel;
137
- };
138
-
139
- void AssignedButtonAction::pwmLatchMode (bool latch)
140
- {
141
- _latchMode = latch;
142
- }
143
-
144
- bool AssignedButtonAction::pwmLatchMode () const
145
- {
146
- return _latchMode;
147
- }
148
-
149
- bool AssignedButtonAction::isPwmOverrideAction () const
150
- {
151
- return _isPwmOverrideAction;
131
+ vehicle->rcChannelOverride (_pwmRcChannel, pwmValue);
152
132
}
153
133
154
134
uint8_t AssignedButtonAction::getRcChannelFromAction (const QString action)
@@ -180,7 +160,6 @@ Joystick::Joystick(const QString& name, int axisCount, int buttonCount, int hatC
180
160
, _totalButtonCount(_buttonCount+_hatButtonCount)
181
161
, _multiVehicleManager(multiVehicleManager)
182
162
{
183
- // qRegisterMetaType<QList<bool>*>("QList<bool>*");
184
163
_rgAxisValues = new int [static_cast <size_t >(_axisCount)];
185
164
_rgCalibration = new Calibration_t[static_cast <size_t >(_axisCount)];
186
165
_rgButtonValues = new uint8_t [static_cast <size_t >(_totalButtonCount)];
@@ -369,12 +348,13 @@ void Joystick::_loadSettings()
369
348
// Remap to stored TX mode in settings
370
349
_remapAxes (2 , _transmitterMode, _rgFunctionAxis);
371
350
372
- while (_pwmVisibilities.length () < _totalButtonCount) {
373
- _pwmVisibilities.push_back (false );
351
+ // Preparing pwm sections visibility map
352
+ while (_pwmSettingsVisibilities.length () < _totalButtonCount) {
353
+ _pwmSettingsVisibilities.push_back (false );
374
354
}
375
355
376
356
for (int button = 0 ; button < _totalButtonCount; button++) {
377
- _pwmVisibilities [button] = false ;
357
+ _pwmSettingsVisibilities [button] = false ;
378
358
QString action = settings.value (QString (_buttonActionNameKey).arg (button), QString ()).toString ();
379
359
if (!action.isEmpty () && action != _buttonActionNone) {
380
360
if (_buttonActionArray[button]) {
@@ -383,7 +363,7 @@ void Joystick::_loadSettings()
383
363
384
364
AssignedButtonAction *ap;
385
365
if (assignableActionIsPwm (action)) {
386
- _pwmVisibilities [button] = true ;
366
+ _pwmSettingsVisibilities [button] = true ;
387
367
int lowPwm = settings.value (QString (_buttonActionLowPwmValueKey).arg (button), -1 ).toInt ();
388
368
int highPwm = settings.value (QString (_buttonActionHighPwmValueKey).arg (button), -1 ).toInt ();
389
369
bool latch = settings.value (QString (_buttonActionLatchPwmValueKey).arg (button), false ).toBool ();
@@ -393,13 +373,14 @@ void Joystick::_loadSettings()
393
373
} else {
394
374
ap = new AssignedButtonAction (this , action);
395
375
}
396
- ap->repeat (settings.value (QString (_buttonActionRepeatKey).arg (button), false ).toBool ());
397
-
398
376
_buttonActionArray[button] = ap;
399
377
_buttonActionArray[button]->buttonTime ().start ();
378
+
379
+ bool savedRepeatState = settings.value (QString (_buttonActionRepeatKey).arg (button), false ).toBool ();
380
+ setButtonRepeatIfAvailable (button, savedRepeatState);
381
+
400
382
qCDebug (JoystickLog) << " _loadSettings button:action" << button << _buttonActionArray[button]->action ()
401
383
<< _buttonActionArray[button]->repeat ();
402
-
403
384
}
404
385
}
405
386
if (badSettings) {
@@ -910,67 +891,87 @@ void Joystick::setButtonAction(int button, const QString& action)
910
891
return ;
911
892
}
912
893
qCWarning (JoystickLog) << " setButtonAction:" << button << action;
913
- QSettings settings;
914
- settings.beginGroup (_settingsGroup);
915
- settings.beginGroup (_name);
916
- if (action.isEmpty () || action == _buttonActionNone) {
917
- _pwmVisibilities[button] = false ;
918
- if (_buttonActionArray[button]) {
919
- // -- Clear from settings
920
- settings.remove (QString (_buttonActionNameKey).arg (button));
921
- settings.remove (QString (_buttonActionRepeatKey).arg (button));
922
- if (_buttonActionArray[button]->isPwmOverrideAction ()) {
923
- settings.remove (QString (_buttonActionHighPwmValueKey).arg (button));
924
- settings.remove (QString (_buttonActionLowPwmValueKey).arg (button));
925
- settings.remove (QString (_buttonActionLatchPwmValueKey).arg (button));
926
- }
894
+
895
+ if (action.isEmpty () || action == _buttonActionNone) {
896
+ _pwmSettingsVisibilities[button] = false ;
897
+ if (_buttonActionArray[button]) {
898
+ removeButtonSettings (button);
927
899
_buttonActionArray[button]->deleteLater ();
928
900
_buttonActionArray[button] = nullptr ;
929
901
}
930
902
} else {
931
903
bool isPwmAction = assignableActionIsPwm (action);
932
- _pwmVisibilities[button] = isPwmAction;
933
- qCDebug (JoystickLog) << " setButtonAction: isPwmAction" << isPwmAction;
934
- AssignedButtonAction *ap;
935
- // TODO(bzd) rethink this section
936
- if (!_buttonActionArray[button]) {
937
- if (isPwmAction) {
938
- ap = new AssignedButtonAction (this , action, 0 , 0 , false );
939
- } else {
940
- ap = new AssignedButtonAction (this , action);
941
- }
942
- _buttonActionArray[button] = ap;
904
+ _pwmSettingsVisibilities[button] = isPwmAction;
905
+ qCDebug (JoystickLog) << " setButtonAction: isPwmAction " << isPwmAction;
906
+
907
+ if (!_buttonActionArray[button]) {
908
+ _buttonActionArray[button] = isPwmAction ? new AssignedButtonAction (this , action, 1000 , 2000 , false )
909
+ : new AssignedButtonAction (this , action);
943
910
} else {
944
911
if (isPwmAction) {
945
- // TODO fix it better, maybe one aciton with PWM values in it
912
+ // PWM Actions are not reusable
946
913
_buttonActionArray[button]->deleteLater ();
947
- ap = new AssignedButtonAction (this , action, 0 , 0 , false );
948
- _buttonActionArray[button] = ap;
949
- }
950
- _buttonActionArray[button]->action (action);
951
- }
952
- // -- Make sure repeat is off if this action doesn't support repeats
953
- int idx = _findAssignableButtonAction (action);
954
- if (idx >= 0 ) {
955
- AssignableButtonAction* p = qobject_cast<AssignableButtonAction*>(_assignableButtonActions[idx]);
956
- if (!p->canRepeat ()) {
957
- _buttonActionArray[button]->repeat (false );
914
+ _buttonActionArray[button] = new AssignedButtonAction (this , action, 0 , 0 , false );
915
+ } else {
916
+ if (_buttonActionArray[button]->isPwmOverrideAction ()) {
917
+ _buttonActionArray[button]->deleteLater ();
918
+ _buttonActionArray[button] = new AssignedButtonAction (this , action);
919
+ } else {
920
+ _buttonActionArray[button]->action (action);
921
+ }
958
922
}
959
923
}
960
924
961
- // -- Save to settings
962
- settings.setValue (QString (_buttonActionNameKey).arg (button), _buttonActionArray[button]->action ());
963
- settings.setValue (QString (_buttonActionRepeatKey).arg (button), _buttonActionArray[button]->repeat ());
964
- if (isPwmAction) {
965
- settings.setValue (QString (_buttonActionLowPwmValueKey).arg (button), _buttonActionArray[button]->lowPwm ());
966
- settings.setValue (QString (_buttonActionHighPwmValueKey).arg (button), _buttonActionArray[button]->highPwm ());
967
- settings.setValue (QString (_buttonActionLatchPwmValueKey).arg (button), _buttonActionArray[button]->pwmLatchMode ());
968
- }
925
+ // -- Make sure repeat is off if this action doesn't support repeats
926
+ setButtonRepeatIfAvailable (button, false );
927
+ saveButtonSettings (button);
969
928
}
970
929
emit pwmVisibilitiesChanged ();
971
930
emit buttonActionsChanged ();
972
931
}
973
932
933
+ void Joystick::setButtonRepeatIfAvailable (int button, bool repeat)
934
+ {
935
+ int idx = _findAssignableButtonAction (_buttonActionArray[button]->action ());
936
+ if (idx >= 0 ) {
937
+ AssignableButtonAction *p = qobject_cast<AssignableButtonAction *>(_assignableButtonActions[idx]);
938
+ if (p->canRepeat ()) {
939
+ _buttonActionArray[button]->repeat (repeat);
940
+ return ;
941
+ }
942
+ }
943
+ _buttonActionArray[button]->repeat (false );
944
+ }
945
+
946
+ void Joystick::saveButtonSettings (int button)
947
+ {
948
+ QSettings settings;
949
+ settings.beginGroup (_settingsGroup);
950
+ settings.beginGroup (_name);
951
+ // -- Save to settings
952
+ settings.setValue (QString (_buttonActionNameKey).arg (button), _buttonActionArray[button]->action ());
953
+ settings.setValue (QString (_buttonActionRepeatKey).arg (button), _buttonActionArray[button]->repeat ());
954
+ if (_buttonActionArray[button]->isPwmOverrideAction ()) {
955
+ settings.setValue (QString (_buttonActionLowPwmValueKey).arg (button), _buttonActionArray[button]->lowPwm ());
956
+ settings.setValue (QString (_buttonActionHighPwmValueKey).arg (button), _buttonActionArray[button]->highPwm ());
957
+ settings.setValue (QString (_buttonActionLatchPwmValueKey).arg (button), _buttonActionArray[button]->pwmLatchMode ());
958
+ }
959
+ }
960
+
961
+ void Joystick::removeButtonSettings (int button)
962
+ {
963
+ QSettings settings;
964
+ settings.beginGroup (_settingsGroup);
965
+ settings.beginGroup (_name);
966
+ settings.remove (QString (_buttonActionNameKey).arg (button));
967
+ settings.remove (QString (_buttonActionRepeatKey).arg (button));
968
+ if (_buttonActionArray[button]->isPwmOverrideAction ()) {
969
+ settings.remove (QString (_buttonActionHighPwmValueKey).arg (button));
970
+ settings.remove (QString (_buttonActionLowPwmValueKey).arg (button));
971
+ settings.remove (QString (_buttonActionLatchPwmValueKey).arg (button));
972
+ }
973
+ }
974
+
974
975
QString Joystick::getButtonAction (int button)
975
976
{
976
977
if (_validButton (button)) {
@@ -981,41 +982,34 @@ QString Joystick::getButtonAction(int button)
981
982
return QString (_buttonActionNone);
982
983
}
983
984
984
- bool Joystick::assignableActionIsPwm (int button) {
985
- qDebug (JoystickLog) << " assignableActionIsPwm: " << button;
986
- if (_validButton (button)) {
987
- auto action = getButtonAction (button);
988
- qDebug (JoystickLog) << " action " << action;
989
- return action.contains (" PWM" );
990
- }
991
- return false ;
985
+ bool Joystick::assignableButtonActionIsPwm (int button) {
986
+ return (_validButton (button) && _buttonActionArray[button]) ? _buttonActionArray[button]->isPwmOverrideAction () : false ;
992
987
}
993
988
994
989
bool Joystick::assignableActionIsPwm (QString action) {
995
990
return action.contains (" PWM" );
996
991
}
997
992
998
993
void Joystick::setButtonPwm (int button, bool lowPwm, int value) {
999
- // TODO czy tutaj przychodzi z GUI i trzeba ustawic tez w _buttonActionArray[button]?
1000
994
qDebug (JoystickLog) << " setButtonPwm: " << button << (lowPwm ? " LOW " : " HIGH " ) << value;
1001
- if (_validButton (button)) {
1002
- if ( assignableActionIsPwm (button)) {
1003
- QSettings settings;
1004
- settings.beginGroup (_settingsGroup );
1005
- settings. beginGroup (_name);
1006
- if ( lowPwm) {
1007
- settings.setValue (QString (_buttonActionLowPwmValueKey).arg (button), value);
1008
- } else {
1009
- settings. setValue ( QString (_buttonActionHighPwmValueKey). arg ( button), value);
1010
- }
995
+ if (assignableButtonActionIsPwm (button)) {
996
+ QSettings settings;
997
+ settings. beginGroup (_settingsGroup) ;
998
+ settings.beginGroup (_name );
999
+ if (lowPwm) {
1000
+ _buttonActionArray[button]-> lowPwm (value);
1001
+ settings.setValue (QString (_buttonActionLowPwmValueKey).arg (button), value);
1002
+ } else {
1003
+ _buttonActionArray[ button]-> highPwm ( value);
1004
+ settings. setValue ( QString (_buttonActionHighPwmValueKey). arg (button), value);
1011
1005
}
1012
1006
}
1007
+
1013
1008
}
1014
1009
1015
1010
int Joystick::getButtonPwm (int button, bool lowPwm) {
1016
- qDebug (JoystickLog) << " getButtonPwm: " << button << (lowPwm ? " LOW " : " HIGH " );
1017
1011
if (_validButton (button)) {
1018
- if (assignableActionIsPwm (button)) {
1012
+ if (assignableButtonActionIsPwm (button)) {
1019
1013
QSettings settings;
1020
1014
settings.beginGroup (_settingsGroup);
1021
1015
settings.beginGroup (_name);
@@ -1035,12 +1029,9 @@ void Joystick::setButtonPwmLatch(int button, bool latch)
1035
1029
return ;
1036
1030
}
1037
1031
qCDebug (JoystickLog) << " PWM Latch mode for button " << button << (latch ? " enabled" : " disabled" );
1038
- // TODO check is really this class
1039
- auto *action = (_buttonActionArray[button]);
1032
+ auto action = _buttonActionArray[button];
1040
1033
action->pwmLatchMode (latch);
1041
1034
1042
- // TODO finish saving settings
1043
-
1044
1035
QSettings settings;
1045
1036
settings.beginGroup (_settingsGroup);
1046
1037
settings.beginGroup (_name);
@@ -1051,9 +1042,8 @@ bool Joystick::getButtonPwmLatch(int button) {
1051
1042
if (!_validButton (button) || !_buttonActionArray[button]) {
1052
1043
return false ;
1053
1044
}
1054
- // TODO check is really this class
1055
- auto *action = (_buttonActionArray[button]);
1056
- return action->pwmLatchMode ();
1045
+ auto action = (_buttonActionArray[button]);
1046
+ return _buttonActionArray[button]->isPwmOverrideAction () ? action->pwmLatchMode () : false ;
1057
1047
}
1058
1048
1059
1049
QStringList Joystick::buttonActions ()
0 commit comments