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