@@ -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) {
@@ -918,67 +903,87 @@ void Joystick::setButtonAction(int button, const QString& action)
918
903
return ;
919
904
}
920
905
qCWarning (JoystickLog) << " setButtonAction:" << button << action;
921
- QSettings settings;
922
- settings.beginGroup (_settingsGroup);
923
- settings.beginGroup (_name);
924
- if (action.isEmpty () || action == _buttonActionNone) {
925
- _pwmVisibilities[button] = false ;
926
- if (_buttonActionArray[button]) {
927
- // -- Clear from settings
928
- settings.remove (QString (_buttonActionNameKey).arg (button));
929
- settings.remove (QString (_buttonActionRepeatKey).arg (button));
930
- if (_buttonActionArray[button]->isPwmOverrideAction ()) {
931
- settings.remove (QString (_buttonActionHighPwmValueKey).arg (button));
932
- settings.remove (QString (_buttonActionLowPwmValueKey).arg (button));
933
- settings.remove (QString (_buttonActionLatchPwmValueKey).arg (button));
934
- }
906
+
907
+ if (action.isEmpty () || action == _buttonActionNone) {
908
+ _pwmSettingsVisibilities[button] = false ;
909
+ if (_buttonActionArray[button]) {
910
+ removeButtonSettings (button);
935
911
_buttonActionArray[button]->deleteLater ();
936
912
_buttonActionArray[button] = nullptr ;
937
913
}
938
914
} else {
939
915
bool isPwmAction = assignableActionIsPwm (action);
940
- _pwmVisibilities[button] = isPwmAction;
941
- qCDebug (JoystickLog) << " setButtonAction: isPwmAction" << isPwmAction;
942
- AssignedButtonAction *ap;
943
- // TODO(bzd) rethink this section
944
- if (!_buttonActionArray[button]) {
945
- if (isPwmAction) {
946
- ap = new AssignedButtonAction (this , action, 0 , 0 , false );
947
- } else {
948
- ap = new AssignedButtonAction (this , action);
949
- }
950
- _buttonActionArray[button] = ap;
916
+ _pwmSettingsVisibilities[button] = isPwmAction;
917
+ qCDebug (JoystickLog) << " setButtonAction: isPwmAction " << isPwmAction;
918
+
919
+ if (!_buttonActionArray[button]) {
920
+ _buttonActionArray[button] = isPwmAction ? new AssignedButtonAction (this , action, 1000 , 2000 , false )
921
+ : new AssignedButtonAction (this , action);
951
922
} else {
952
923
if (isPwmAction) {
953
- // TODO fix it better, maybe one aciton with PWM values in it
924
+ // PWM Actions are not reusable
954
925
_buttonActionArray[button]->deleteLater ();
955
- ap = new AssignedButtonAction (this , action, 0 , 0 , false );
956
- _buttonActionArray[button] = ap;
957
- }
958
- _buttonActionArray[button]->action (action);
959
- }
960
- // -- Make sure repeat is off if this action doesn't support repeats
961
- int idx = _findAssignableButtonAction (action);
962
- if (idx >= 0 ) {
963
- AssignableButtonAction* p = qobject_cast<AssignableButtonAction*>(_assignableButtonActions[idx]);
964
- if (!p->canRepeat ()) {
965
- _buttonActionArray[button]->repeat (false );
926
+ _buttonActionArray[button] = new AssignedButtonAction (this , action, 0 , 0 , false );
927
+ } else {
928
+ if (_buttonActionArray[button]->isPwmOverrideAction ()) {
929
+ _buttonActionArray[button]->deleteLater ();
930
+ _buttonActionArray[button] = new AssignedButtonAction (this , action);
931
+ } else {
932
+ _buttonActionArray[button]->action (action);
933
+ }
966
934
}
967
935
}
968
936
969
- // -- Save to settings
970
- settings.setValue (QString (_buttonActionNameKey).arg (button), _buttonActionArray[button]->action ());
971
- settings.setValue (QString (_buttonActionRepeatKey).arg (button), _buttonActionArray[button]->repeat ());
972
- if (isPwmAction) {
973
- settings.setValue (QString (_buttonActionLowPwmValueKey).arg (button), _buttonActionArray[button]->lowPwm ());
974
- settings.setValue (QString (_buttonActionHighPwmValueKey).arg (button), _buttonActionArray[button]->highPwm ());
975
- settings.setValue (QString (_buttonActionLatchPwmValueKey).arg (button), _buttonActionArray[button]->pwmLatchMode ());
976
- }
937
+ // -- Make sure repeat is off if this action doesn't support repeats
938
+ setButtonRepeatIfAvailable (button, false );
939
+ saveButtonSettings (button);
977
940
}
978
941
emit pwmVisibilitiesChanged ();
979
942
emit buttonActionsChanged ();
980
943
}
981
944
945
+ void Joystick::setButtonRepeatIfAvailable (int button, bool repeat)
946
+ {
947
+ int idx = _findAssignableButtonAction (_buttonActionArray[button]->action ());
948
+ if (idx >= 0 ) {
949
+ AssignableButtonAction *p = qobject_cast<AssignableButtonAction *>(_assignableButtonActions[idx]);
950
+ if (p->canRepeat ()) {
951
+ _buttonActionArray[button]->repeat (repeat);
952
+ return ;
953
+ }
954
+ }
955
+ _buttonActionArray[button]->repeat (false );
956
+ }
957
+
958
+ void Joystick::saveButtonSettings (int button)
959
+ {
960
+ QSettings settings;
961
+ settings.beginGroup (_settingsGroup);
962
+ settings.beginGroup (_name);
963
+ // -- Save to settings
964
+ settings.setValue (QString (_buttonActionNameKey).arg (button), _buttonActionArray[button]->action ());
965
+ settings.setValue (QString (_buttonActionRepeatKey).arg (button), _buttonActionArray[button]->repeat ());
966
+ if (_buttonActionArray[button]->isPwmOverrideAction ()) {
967
+ settings.setValue (QString (_buttonActionLowPwmValueKey).arg (button), _buttonActionArray[button]->lowPwm ());
968
+ settings.setValue (QString (_buttonActionHighPwmValueKey).arg (button), _buttonActionArray[button]->highPwm ());
969
+ settings.setValue (QString (_buttonActionLatchPwmValueKey).arg (button), _buttonActionArray[button]->pwmLatchMode ());
970
+ }
971
+ }
972
+
973
+ void Joystick::removeButtonSettings (int button)
974
+ {
975
+ QSettings settings;
976
+ settings.beginGroup (_settingsGroup);
977
+ settings.beginGroup (_name);
978
+ settings.remove (QString (_buttonActionNameKey).arg (button));
979
+ settings.remove (QString (_buttonActionRepeatKey).arg (button));
980
+ if (_buttonActionArray[button]->isPwmOverrideAction ()) {
981
+ settings.remove (QString (_buttonActionHighPwmValueKey).arg (button));
982
+ settings.remove (QString (_buttonActionLowPwmValueKey).arg (button));
983
+ settings.remove (QString (_buttonActionLatchPwmValueKey).arg (button));
984
+ }
985
+ }
986
+
982
987
QString Joystick::getButtonAction (int button)
983
988
{
984
989
if (_validButton (button)) {
@@ -989,41 +994,34 @@ QString Joystick::getButtonAction(int button)
989
994
return QString (_buttonActionNone);
990
995
}
991
996
992
- bool Joystick::assignableActionIsPwm (int button) {
993
- qDebug (JoystickLog) << " assignableActionIsPwm: " << button;
994
- if (_validButton (button)) {
995
- auto action = getButtonAction (button);
996
- qDebug (JoystickLog) << " action " << action;
997
- return action.contains (" PWM" );
998
- }
999
- return false ;
997
+ bool Joystick::assignableButtonActionIsPwm (int button) {
998
+ return (_validButton (button) && _buttonActionArray[button]) ? _buttonActionArray[button]->isPwmOverrideAction () : false ;
1000
999
}
1001
1000
1002
1001
bool Joystick::assignableActionIsPwm (QString action) {
1003
1002
return action.contains (" PWM" );
1004
1003
}
1005
1004
1006
1005
void Joystick::setButtonPwm (int button, bool lowPwm, int value) {
1007
- // TODO czy tutaj przychodzi z GUI i trzeba ustawic tez w _buttonActionArray[button]?
1008
1006
qDebug (JoystickLog) << " setButtonPwm: " << button << (lowPwm ? " LOW " : " HIGH " ) << value;
1009
- if (_validButton (button)) {
1010
- if ( assignableActionIsPwm (button)) {
1011
- QSettings settings;
1012
- settings.beginGroup (_settingsGroup );
1013
- settings. beginGroup (_name);
1014
- if ( lowPwm) {
1015
- settings.setValue (QString (_buttonActionLowPwmValueKey).arg (button), value);
1016
- } else {
1017
- settings. setValue ( QString (_buttonActionHighPwmValueKey). arg ( button), value);
1018
- }
1007
+ if (assignableButtonActionIsPwm (button)) {
1008
+ QSettings settings;
1009
+ settings. beginGroup (_settingsGroup) ;
1010
+ settings.beginGroup (_name );
1011
+ if (lowPwm) {
1012
+ _buttonActionArray[button]-> lowPwm (value);
1013
+ settings.setValue (QString (_buttonActionLowPwmValueKey).arg (button), value);
1014
+ } else {
1015
+ _buttonActionArray[ button]-> highPwm ( value);
1016
+ settings. setValue ( QString (_buttonActionHighPwmValueKey). arg (button), value);
1019
1017
}
1020
1018
}
1019
+
1021
1020
}
1022
1021
1023
1022
int Joystick::getButtonPwm (int button, bool lowPwm) {
1024
- qDebug (JoystickLog) << " getButtonPwm: " << button << (lowPwm ? " LOW " : " HIGH " );
1025
1023
if (_validButton (button)) {
1026
- if (assignableActionIsPwm (button)) {
1024
+ if (assignableButtonActionIsPwm (button)) {
1027
1025
QSettings settings;
1028
1026
settings.beginGroup (_settingsGroup);
1029
1027
settings.beginGroup (_name);
@@ -1043,12 +1041,9 @@ void Joystick::setButtonPwmLatch(int button, bool latch)
1043
1041
return ;
1044
1042
}
1045
1043
qCDebug (JoystickLog) << " PWM Latch mode for button " << button << (latch ? " enabled" : " disabled" );
1046
- // TODO check is really this class
1047
- auto *action = (_buttonActionArray[button]);
1044
+ auto action = _buttonActionArray[button];
1048
1045
action->pwmLatchMode (latch);
1049
1046
1050
- // TODO finish saving settings
1051
-
1052
1047
QSettings settings;
1053
1048
settings.beginGroup (_settingsGroup);
1054
1049
settings.beginGroup (_name);
@@ -1059,9 +1054,8 @@ bool Joystick::getButtonPwmLatch(int button) {
1059
1054
if (!_validButton (button) || !_buttonActionArray[button]) {
1060
1055
return false ;
1061
1056
}
1062
- // TODO check is really this class
1063
- auto *action = (_buttonActionArray[button]);
1064
- return action->pwmLatchMode ();
1057
+ auto action = (_buttonActionArray[button]);
1058
+ return _buttonActionArray[button]->isPwmOverrideAction () ? action->pwmLatchMode () : false ;
1065
1059
}
1066
1060
1067
1061
QStringList Joystick::buttonActions ()
0 commit comments