forked from vedderb/vesc_tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preferences.cpp
executable file
·443 lines (388 loc) · 14.4 KB
/
preferences.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
Copyright 2016 - 2019 Benjamin Vedder [email protected]
This file is part of VESC Tool.
VESC Tool is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
VESC Tool is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "preferences.h"
#include "ui_preferences.h"
#include <QDebug>
#include <cmath>
#include <QFileDialog>
#include "utility.h"
Preferences::Preferences(QWidget *parent) :
QDialog(parent),
ui(new Ui::Preferences)
{
mVesc = nullptr;
#ifdef HAS_GAMEPAD
mGamepad = nullptr;
mUseGamepadControl = false;
#endif
mTimer = new QTimer(this);
mTimer->start(100);
connect(mTimer, SIGNAL(timeout()),
this, SLOT(timerSlot()));
ui->setupUi(this);
QString theme = Utility::getThemePath();
ui->pollRestoreButton->setIcon(QPixmap(theme + "icons/Restart-96.png"));
ui->pathScriptInputChooseButton->setIcon(QPixmap(theme + "icons/Open Folder-96.png"));
ui->pathRtLogChooseButton->setIcon(QPixmap(theme + "icons/Open Folder-96.png"));
ui->pathScriptOutputChooseButton->setIcon(QPixmap(theme + "icons/Open Folder-96.png"));
ui->jsConf1Button->setIcon(QPixmap(theme + "icons/Horizontal Settings Mixer-96.png"));
ui->jsConf2Button->setIcon(QPixmap(theme + "icons/Horizontal Settings Mixer-96.png"));
ui->jsConf3Button->setIcon(QPixmap(theme + "icons/Horizontal Settings Mixer-96.png"));
ui->jsConf4Button->setIcon(QPixmap(theme + "icons/Horizontal Settings Mixer-96.png"));
ui->jsConnectButton->setIcon(QPixmap(theme + "icons/Connected-96.png"));
ui->jsScanButton->setIcon(QPixmap(theme + "icons/Connected-96.png"));
ui->jsResetConfigButton->setIcon(QPixmap(theme + "icons/Restart-96.png"));
ui->uiScaleBox->setValue(mSettings.value("app_scale_factor", 1.0).toDouble());
ui->uiPlotWidthBox->setValue(mSettings.value("plot_line_width",4.0).toDouble());
ui->pathRtLogEdit->setText(mSettings.value("path_rt_log", "./log").toString());
ui->pathScriptInputEdit->setText(mSettings.value("path_script_input", "./log").toString());
ui->pathScriptOutputEdit->setText(mSettings.value("path_script_output", "./log").toString());
ui->pollRtDataBox->setValue(mSettings.value("poll_rate_rt_data", 50.0).toDouble());
ui->pollAppDataBox->setValue(mSettings.value("poll_rate_app_data", 20.0).toDouble());
ui->pollImuDataBox->setValue(mSettings.value("poll_rate_imu_data", 50.0).toDouble());
ui->pollBmsDataBox->setValue(mSettings.value("poll_rate_bms_data", 10.0).toDouble());
ui->darkModeBox->setChecked(Utility::isDarkMode());
#ifdef HAS_GAMEPAD
auto confAxis = [](QGamepad *gp, QGamepadManager::GamepadAxis axis) {
if (gp) {
QGamepadManager::instance()->configureAxis(gp->deviceId(), axis);
}
};
connect(ui->jsConf1Button, &QPushButton::clicked,
[=]() {confAxis(mGamepad, QGamepadManager::AxisLeftX);});
connect(ui->jsConf2Button, &QPushButton::clicked,
[=]() {confAxis(mGamepad, QGamepadManager::AxisLeftY);});
connect(ui->jsConf3Button, &QPushButton::clicked,
[=]() {confAxis(mGamepad, QGamepadManager::AxisRightX);});
connect(ui->jsConf4Button, &QPushButton::clicked,
[=]() {confAxis(mGamepad, QGamepadManager::AxisRightY);});
if (mSettings.contains("js_is_configured")) {
ui->jsConfigOkBox->setChecked(mSettings.value("js_is_configured").toBool());
}
if (mSettings.contains("js_is_inverted")) {
ui->jsInvertedBox->setChecked(mSettings.value("js_is_inverted").toBool());
}
if (mSettings.contains("js_is_bidirectional")) {
ui->jsBidirectionalBox->setChecked(mSettings.value("js_is_bidirectional").toBool());
}
if (mSettings.contains("js_axis")) {
ui->jseAxisBox->setCurrentIndex(mSettings.value("js_axis").toInt());
}
if (mSettings.contains("js_control_type")) {
ui->jsControlTypeBox->setCurrentIndex(mSettings.value("js_control_type").toInt());
}
if (mSettings.contains("js_current_min")) {
ui->jsCurrentMinBox->setValue(mSettings.value("js_current_min").toDouble());
}
if (mSettings.contains("js_current_max")) {
ui->jsCurrentMaxBox->setValue(mSettings.value("js_current_max").toDouble());
}
if (mSettings.contains("js_erpm_min")) {
ui->jsErpmMinBox->setValue(mSettings.value("js_erpm_min").toDouble());
}
if (mSettings.contains("js_erpm_max")) {
ui->jsErpmMaxBox->setValue(mSettings.value("js_erpm_max").toDouble());
}
if (mSettings.contains("js_range_min")) {
ui->jsMinBox->setValue(mSettings.value("js_range_min").toDouble());
}
if (mSettings.contains("js_range_max")) {
ui->jsMaxBox->setValue(mSettings.value("js_range_max").toDouble());
}
if (mSettings.contains("js_name")) {
ui->jsListBox->clear();
auto gamepads = QGamepadManager::instance()->connectedGamepads();
for (auto g: gamepads) {
auto name = QGamepadManager::instance()->gamepadName(g);
ui->jsListBox->addItem(name, g);
if (name == mSettings.value("js_name").toString()) {
if (mGamepad) {
mGamepad->deleteLater();
}
mGamepad = new QGamepad(g, this);
}
}
}
#endif
saveSettingsChanged();
}
Preferences::~Preferences()
{
#ifdef HAS_GAMEPAD
mSettings.setValue("js_is_configured", ui->jsConfigOkBox->isChecked());
mSettings.setValue("js_is_inverted", ui->jsInvertedBox->isChecked());
mSettings.setValue("js_is_bidirectional", ui->jsBidirectionalBox->isChecked());
mSettings.setValue("js_axis", ui->jseAxisBox->currentIndex());
mSettings.setValue("js_control_type", ui->jsControlTypeBox->currentIndex());
mSettings.setValue("js_current_min", ui->jsCurrentMinBox->value());
mSettings.setValue("js_current_max", ui->jsCurrentMaxBox->value());
mSettings.setValue("js_erpm_min", ui->jsErpmMinBox->value());
mSettings.setValue("js_erpm_max", ui->jsErpmMaxBox->value());
mSettings.setValue("js_range_min", ui->jsMinBox->value());
mSettings.setValue("js_range_max", ui->jsMaxBox->value());
if (mGamepad) {
mSettings.setValue("js_name", mGamepad->name());
}
#endif
delete ui;
}
VescInterface *Preferences::vesc() const
{
return mVesc;
}
void Preferences::setVesc(VescInterface *vesc)
{
mVesc = vesc;
if (mVesc) {
ui->loadQmlUiConnectBox->setChecked(mVesc->getLoadQmlUiOnConnect());
}
}
void Preferences::setUseGamepadControl(bool useControl)
{
#ifdef HAS_GAMEPAD
if (ui->jsConfigOkBox->isChecked()) {
if (mGamepad) {
mUseGamepadControl = useControl;
} else {
mVesc->emitMessageDialog("Gamepad Control",
"No recognized gamepad is connected.",
false, false);
}
} else if (mVesc) {
mVesc->emitMessageDialog("Gamepad Control",
"Gamepad control is not configured. Go to Settings->Gamepad to configure it.",
false, false);
}
#else
(void)useControl;
#endif
}
bool Preferences::isUsingGamepadControl()
{
#ifdef HAS_GAMEPAD
return mUseGamepadControl;
#else
return false;
#endif
}
void Preferences::closeEvent(QCloseEvent *event)
{
if (Utility::isDarkMode() != mLastIsDark) {
mVesc->emitMessageDialog("Theme Changed",
"Please restart VESC Tool for the theme changes to take effect.",
false, false);
}
if (!Utility::almostEqual(mLastScaling,
mSettings.value("app_scale_factor", 1.0).toDouble(), 0.001)) {
mVesc->emitMessageDialog("Scaling Changed",
"Please restart VESC Tool for the scaling change to take effect.",
false, false);
}
saveSettingsChanged();
event->accept();
}
void Preferences::timerSlot()
{
#ifdef HAS_GAMEPAD
if (mGamepad) {
ui->jsAxis1Bar->setValue(mGamepad->axisLeftX() * 1000.0);
ui->jsAxis2Bar->setValue(mGamepad->axisLeftY() * 1000.0);
ui->jsAxis3Bar->setValue(mGamepad->axisRightX() * 1000.0);
ui->jsAxis4Bar->setValue(mGamepad->axisRightY() * 1000.0);
double ax = 0.0;
if (ui->jseAxisBox->currentIndex() == 0) {
ax = mGamepad->axisLeftX() * 1000.0;
} else if (ui->jseAxisBox->currentIndex() == 1) {
ax = mGamepad->axisLeftY() * 1000.0;
} else if (ui->jseAxisBox->currentIndex() == 2) {
ax = mGamepad->axisRightX() * 1000.0;
} else if (ui->jseAxisBox->currentIndex() == 3) {
ax = mGamepad->axisRightY() * 1000.0;
}
if (ui->jsInvertedBox->isChecked()) {
ax = -ax;
}
double input = Utility::map(ax,
ui->jsMinBox->value(), ui->jsMaxBox->value(),
ui->jsBidirectionalBox->isChecked() ? -1.0 : 0.0, 1.0);
double range = 0.0;
int decimals = 2;
QString name = "Undefined";
QString unit = "";
int ctrlt = ui->jsControlTypeBox->currentIndex();
if (ctrlt == 0 || ctrlt == 1) {
range = input >= 0 ? fabs(ui->jsCurrentMaxBox->value()) : fabs(ui->jsCurrentMinBox->value());
input *= range;
name = "Current";
unit = " A";
if (mVesc && mUseGamepadControl) {
if (ctrlt == 0 || input > 0) {
mVesc->commands()->setCurrent(input);
} else {
mVesc->commands()->setCurrentBrake(input);
}
}
} else if (ctrlt == 2) {
range = 1.0;
input *= range;
name = "Duty";
unit = "";
if (mVesc && mUseGamepadControl) {
mVesc->commands()->setDutyCycle(input);
}
} else if (ctrlt == 3) {
range = input >= 0 ? fabs(ui->jsErpmMaxBox->value()) : fabs(ui->jsErpmMinBox->value());
input *= range;
name = "Speed";
unit = " ERPM";
decimals = 0;
if (mVesc && mUseGamepadControl) {
mVesc->commands()->setRpm(input);
}
} else if (ctrlt == 4) {
range = 360.0;
input *= range;
name = "Position";
unit = " Degrees";
decimals = 1;
if (mVesc && mUseGamepadControl) {
mVesc->commands()->setPos(input);
}
}
ui->jsDisp->setRange(range);
ui->jsDisp->setUnit(unit);
ui->jsDisp->setName(name);
ui->jsDisp->setVal(input);
ui->jsDisp->setDecimals(decimals);
if (!mGamepad->isConnected()) {
mGamepad->deleteLater();
mGamepad = nullptr;
}
}
#endif
}
void Preferences::on_uiScaleBox_valueChanged(double arg1)
{
mSettings.setValue("app_scale_factor", arg1);
}
void Preferences::on_uiPlotWidthBox_valueChanged(double arg1)
{
mSettings.setValue("plot_line_width", arg1);
}
void Preferences::on_jsScanButton_clicked()
{
#ifdef HAS_GAMEPAD
ui->jsListBox->clear();
auto gamepads = QGamepadManager::instance()->connectedGamepads();
for (auto g: gamepads) {
ui->jsListBox->addItem(QGamepadManager::instance()->gamepadName(g), g);
}
#endif
}
void Preferences::on_jsConnectButton_clicked()
{
#ifdef HAS_GAMEPAD
QVariant item = ui->jsListBox->currentData();
if (item.isValid()) {
if (mGamepad) {
mGamepad->deleteLater();
}
mGamepad = new QGamepad(item.toInt(), this);
}
#endif
}
void Preferences::on_jsResetConfigButton_clicked()
{
#ifdef HAS_GAMEPAD
if (mGamepad) {
QGamepadManager::instance()->resetConfiguration(mGamepad->deviceId());
}
#endif
}
void Preferences::on_loadQmlUiConnectBox_toggled(bool checked)
{
if (mVesc) {
mVesc->setLoadQmlUiOnConnect(checked);
}
}
void Preferences::on_pathRtLogChooseButton_clicked()
{
ui->pathRtLogEdit->setText(
QFileDialog::getExistingDirectory(this, "Choose RT log output directory"));
}
void Preferences::on_pathScriptInputChooseButton_clicked()
{
ui->pathScriptInputEdit->setText(
QFileDialog::getExistingDirectory(this, "Choose script input file directory"));
}
void Preferences::on_pathRtLogEdit_textChanged(const QString &arg1)
{
mSettings.setValue("path_rt_log", arg1);
mSettings.sync();
}
void Preferences::on_pathScriptInputEdit_textChanged(const QString &arg1)
{
mSettings.setValue("path_script_input", arg1);
mSettings.sync();
}
void Preferences::on_pathScriptOutputChooseButton_clicked()
{
ui->pathScriptOutputEdit->setText(
QFileDialog::getExistingDirectory(this, "Choose script output file directory"));
}
void Preferences::on_pathScriptOutputEdit_textChanged(const QString &arg1)
{
mSettings.setValue("path_script_output", arg1);
mSettings.sync();
}
void Preferences::on_pollRtDataBox_valueChanged(double arg1)
{
mSettings.setValue("poll_rate_rt_data", arg1);
mSettings.sync();
}
void Preferences::on_pollAppDataBox_valueChanged(double arg1)
{
mSettings.setValue("poll_rate_app_data", arg1);
mSettings.sync();
}
void Preferences::on_pollImuDataBox_valueChanged(double arg1)
{
mSettings.setValue("poll_rate_imu_data", arg1);
mSettings.sync();
}
void Preferences::on_pollBmsDataBox_valueChanged(double arg1)
{
mSettings.setValue("poll_rate_bms_data", arg1);
mSettings.sync();
}
void Preferences::on_pollRestoreButton_clicked()
{
ui->pollRtDataBox->setValue(50.0);
ui->pollAppDataBox->setValue(20.0);
ui->pollImuDataBox->setValue(50.0);
ui->pollBmsDataBox->setValue(10.0);
}
void Preferences::on_darkModeBox_toggled(bool checked)
{
Utility::setDarkMode(checked);
}
void Preferences::on_okButton_clicked(){
close();
}
void Preferences::saveSettingsChanged()
{
mLastScaling = mSettings.value("app_scale_factor", 1.0).toDouble();
mLastIsDark = Utility::isDarkMode();
}