-
Notifications
You must be signed in to change notification settings - Fork 6
/
MainWindow.cpp
566 lines (508 loc) · 19.9 KB
/
MainWindow.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
// QWalkingPad - Simple desktop application for controlling the Kingsmith WalkingPad over BLE
// Copyright (C) 2021 Dorian Rudolph
//
// This program 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.
//
// This program 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 "MainWindow.h"
#include <QMenuBar>
#include <QStatusBar>
#include <QLoggingCategory>
#include <QLabel>
#include <QTimer>
#include <QMessageBox>
#include <QFileDialog>
#include <QGroupBox>
#include <QRadioButton>
#include <QHBoxLayout>
#include "Protocol.h"
#include <QDateTime>
#include <QSlider>
#include "AbsoluteSliderStyle.h"
#include <QPushButton>
#include <QFontDatabase>
#include <QActionGroup>
MainWindow::MainWindow() {
setWindowTitle("QWalkingPad");
setupLayout();
setupMenu();
setupTimer();
startDiscovering();
}
void MainWindow::showEvent(QShowEvent *event) {
auto path = settings.getDataPath();
if(!path.isEmpty()) {
stats.load(path);
if (!stats.hasFile()) {
QMessageBox::warning(this, "Failed to load statistics", "Failed to parse file " + settings.getDataPath());
}
updateStatsLabel();
}
}
QSlider * MainWindow::makeSpeedSlider() {
auto slider = new QSlider(Qt::Orientation::Horizontal);
slider->setFixedHeight(25);
slider->setStyle(new AbsoluteSliderStyle(slider->style()));
slider->setRange(0, maxSpeed); // TODO handle different maximum speeds
slider->setTickInterval(10);
slider->setTickPosition(QSlider::TicksBelow);
slider->setTracking(false);
slider->setPageStep(5);
slider->setSingleStep(1);
return slider;
}
void MainWindow::setupLayout() {
centerWidget = new QWidget;
centerWidget->setEnabled(false);
auto vBox = new QVBoxLayout;
vBox->setAlignment(Qt::AlignTop);
centerWidget->setLayout(vBox);
setCentralWidget(centerWidget);
auto modeGroup = new QGroupBox("Mode");
vBox->addWidget(modeGroup);
auto hBox = new QHBoxLayout;
modeGroup->setLayout(hBox);
modeGroup->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
auto addModeButton = [&](const char *label, uint8_t mode, bool checked=false) {
auto button = new QRadioButton(label);
modeButtons[mode] = button;
hBox->addWidget(button);
button->setChecked(checked);
connect(button, &QRadioButton::clicked, this, [this, mode]{
setModeTime = QDateTime::currentMSecsSinceEpoch();
send(Pad::setMode(mode));
});
};
addModeButton("Sleep", Pad::MODE_SLEEP, true);
addModeButton("Manual", Pad::MODE_MANUAL);
addModeButton("Auto", Pad::MODE_AUTO);
auto speedGroup = new QGroupBox("Speed");
speedGroup->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
auto grid = new QGridLayout;
speedGroup->setLayout(grid);
vBox->addWidget(speedGroup);
speedLabel = new QLabel;
grid->addWidget(new QLabel("Current:"), 0, 0, Qt::AlignTop);
grid->addWidget(speedLabel, 0, 1, Qt::AlignTop);
speedSlider = makeSpeedSlider();
connect(speedSlider, &QSlider::valueChanged, this, [=](auto speed){
setSpeedTime = QDateTime::currentMSecsSinceEpoch();
setSpeedWidgets(speed);
send(Pad::setSpeed(speed));
});
connect(speedSlider, &QSlider::sliderMoved, this, &MainWindow::setSpeedWidgets);
grid->addWidget(speedSlider, 0, 2);
setSpeedWidgets(0);
startSpeedLabel = new QLabel;
grid->addWidget(new QLabel("Start:"), 1, 0, Qt::AlignTop);
grid->addWidget(startSpeedLabel, 1, 1, Qt::AlignTop);
startSpeedSlider = makeSpeedSlider();
grid->addWidget(startSpeedSlider, 1, 2);
connect(startSpeedSlider, &QSlider::valueChanged, this, [=](auto speed){
setStartSpeedWidgets(speed);
send(Pad::setStartSpeed(speed));
if (settings.getUnifiedSpeed()){
send(Pad::setSpeed(speed));
}
});
connect(startSpeedSlider, &QSlider::sliderMoved, this, &MainWindow::setStartSpeedWidgets);
setStartSpeedWidgets(20);
startButton = new QPushButton("Start");
connect(startButton, &QPushButton::pressed, this, [=](){ sendStart(); });
grid->addWidget(startButton, 2, 0, 1, 2);
auto statsGroup = new QGroupBox("Statistics");
statsGroup->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
vBox->addWidget(statsGroup);
auto statsLayout = new QGridLayout;
//statsLayout->setAlignment(Qt::AlignCenter);
statsGroup->setLayout(statsLayout);
QFont font("monospace", 20);
font.setFixedPitch(true);
statsLabel = new QLabel;
statsLabel->setFont(font);
statsLayout->addWidget(statsLabel, 0, 0, Qt::AlignCenter);
updateStatsLabel();
}
void MainWindow::setSpeedWidgets(int speed) {
speedSlider->setSliderPosition(speed);
speedLabel->setText(QString("%1 km/h").arg(speed/10., 0, 'f', 1));
}
void MainWindow::setStartSpeedWidgets(int speed) {
speed = std::max(speed, 5);
startSpeedSlider->setSliderPosition(speed);
startSpeedLabel->setText(QString("%1 km/h").arg(speed/10., 0, 'f', 1));
}
void MainWindow::setupMenu() {
menuBar()->setNativeMenuBar(false);
menuBar()->show();
connectMenu = menuBar()->addMenu("&Connect");
disconnectAction = connectMenu->addAction("&Disconnect");
connect(disconnectAction, &QAction::triggered, this, &MainWindow::disconnect);
disconnectAction->setDisabled(true);
scanAction = connectMenu->addAction("&Scan");
connect(scanAction, &QAction::triggered, this, &MainWindow::scan);
scanAction->setDisabled(true);
auto autoReconnectAction = connectMenu->addAction("&Reconnect Automatically");
autoReconnectAction->setCheckable(true);
autoReconnectAction->setChecked(settings.getAutoReconnect());
connect(autoReconnectAction, &QAction::triggered, [this](auto checked) {
settings.setAutoReconnect(checked);
});
connectMenu->addSeparator();
auto settingsMenu = menuBar()->addMenu("&Settings");
auto unifiedSpeed = settingsMenu->addAction("&Unified Speed");
unifiedSpeed->setCheckable(true);
unifiedSpeed->setChecked(settings.getUnifiedSpeed());
connect(unifiedSpeed, &QAction::triggered, [this](auto checked) {
settings.setUnifiedSpeed(checked);
});
settingsMenu->addAction(unifiedSpeed);
auto dataPath = settingsMenu->addAction("Set &Data Path (" + settings.getDataPath() + ")");
connect(dataPath, &QAction::triggered, [this, dataPath](){
QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::AnyFile);
dialog.setNameFilter("CSV (*.csv)");
dialog.setDefaultSuffix("csv");
dialog.setWindowTitle("Select CSV Data File Path");
auto dir = QFileInfo(settings.getDataPath()).absoluteDir();
if (dir.exists()) {
dialog.setDirectory(dir);
}
if (dialog.exec()){
auto selected = dialog.selectedFiles();
if (!selected.empty()) {
auto path = selected[0];
settings.setDataPath(path);
dataPath->setText("Set &Data Path (" + path + ")");
if(!path.isEmpty()) {
stats.changePath(path);
if (!stats.hasFile()) {
QMessageBox::warning(this, "Failed to load statistics", "Failed to parse file " + settings.getDataPath());
}
}
}
}
});
auto useSystemTheme = settingsMenu->addAction("Use S&ystem Theme");
useSystemTheme->setCheckable(true);
useSystemTheme->setChecked(settings.getUseSystemTheme());
connect(useSystemTheme, &QAction::triggered, [this](auto checked) {
settings.setUseSystemTheme(checked);
});
settingsMenu->addSection("Show Statistics");
auto statisticsGroup = new QActionGroup(this);
auto showStatistics = settings.getShowStatistics();
auto addStatisticsAction = [=](const char *label, int setting){
auto action = settingsMenu->addAction(label);
action->setCheckable(true);
statisticsGroup->addAction(action);
connect(action, &QAction::triggered, this, [=]() {
settings.setShowStatistics(setting);
updateStatsLabel();
});
if (setting == showStatistics) action->setChecked(true);
};
addStatisticsAction("&Today", Settings::TODAY);
addStatisticsAction("&All time", Settings::ALL_TIME);
addStatisticsAction("&Current run", Settings::CURRENT_RUN);
statusLabel = new QLabel("Disconnected", this);
statusBar()->addPermanentWidget(statusLabel);
messageLabel = new QLabel("Scanning...", this);
statusBar()->addWidget(messageLabel);
}
void MainWindow::setupTimer() {
tickTimer = new QTimer(this);
tickTimer->setInterval(1000);
connect(tickTimer, &QTimer::timeout, this, &MainWindow::tick);
tickTimer->start();
// It seems the WalkingPad ignores commands if they come in too quickly
// So we only send one command from the queue every 50 msec
sendTimer = new QTimer(this);
sendTimer->setInterval(50);
connect(sendTimer, &QTimer::timeout, this, &MainWindow::handleSend);
sendTimer->start();
}
void MainWindow::handleSend() {
if (state != CONNECTED) return;
if (!sendQueue.empty()) {
service->writeCharacteristic(writeChar, sendQueue.front());
//qDebug() << "SEND" << sendQueue.front().toHex();
sendQueue.pop_front();
}
}
void MainWindow::send(const QByteArray &msg){
sendQueue.push_back(msg);
}
void MainWindow::startDiscovering() {
deviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
deviceDiscoveryAgent->setLowEnergyDiscoveryTimeout(2000);
connect(deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &MainWindow::scanFinished);
connect(deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::canceled, this, &MainWindow::scanFinished);
connect(deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::errorOccurred, this, &MainWindow::scanError);
connect(deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &MainWindow::addDevice);
//TODO handle device updated
deviceDiscoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
}
void MainWindow::disconnect() {
state = DISCONNECTING;
bleController->disconnectFromDevice();
showMessage("Disconnecting...");
if (bleController->state() != QLowEnergyController::ClosingState){
disconnected();
}
}
void MainWindow::addDevice(const QBluetoothDeviceInfo &device) {
if (!(device.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration))
return;
qDebug() << "Found" << device.name() << device.address();
auto deviceAction = new QAction(device.name() + " (" + device.address().toString() + ")", this);
deviceAction->setData(devices.size());
deviceAction->setEnabled(connectionsEnabled);
connect(deviceAction, &QAction::triggered, this, [this](){
auto action = qobject_cast<QAction*>(sender());
selectedDevice = devices[action->data().toInt()];
connectDevice();
});
devices.append(device);
for (auto a : connectActions) {
connectMenu->removeAction(a);
}
connectActions.append(deviceAction);
std::sort(connectActions.begin(), connectActions.end(), [](const auto a, const auto b) {
return a->text() < b->text(); });
connectMenu->addActions(connectActions);
if (!autoConnected && settings.getAutoReconnect() && device.address().toString() == settings.getLastUUID()) {
autoConnected = true;
selectedDevice = devices.back();
connectDevice();
}
}
void MainWindow::scanFinished() {
if (state == DISCONNECTED){
showMessage("Scan finished");
}
scanAction->setDisabled(false);
}
void MainWindow::scanError(QBluetoothDeviceDiscoveryAgent::Error error) {
auto err = deviceDiscoveryAgent->errorString();
qCritical() << "Error while scanning for devices" << err;
showMessage("Scan error: " + err);
}
void MainWindow::scan() {
for (auto a : connectActions) {
connectMenu->removeAction(a);
delete a;
}
devices.clear();
connectActions.clear();
scanAction->setDisabled(true);
deviceDiscoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
}
void MainWindow::connectDevice() {
state = CONNECTING;
sendQueue.clear();
setConnectActionEnabled(false);
disconnectAction->setEnabled(true);
showMessage("Connecting to " + selectedDevice.name() + " ...");
if (!bleController) {
delete bleController;
}
service = nullptr;
bleController = QLowEnergyController::createCentral(selectedDevice, this);
connect(bleController, &QLowEnergyController::serviceDiscovered, this, &MainWindow::serviceDiscovered);
connect(bleController, &QLowEnergyController::discoveryFinished, this, &MainWindow::discoveryFinished);
connect(bleController, &QLowEnergyController::connected, this, &MainWindow::connected);
connect(bleController, &QLowEnergyController::disconnected, this, &MainWindow::disconnected);
connect(bleController, &QLowEnergyController::errorOccurred, this, &MainWindow::connectError);
bleController->connectToDevice();
}
void MainWindow::connected() {
qDebug("Connected");
settings.setLastUUID(selectedDevice.address().toString());
bleController->discoverServices();
setStatus("Connected (" + selectedDevice.name() + ")");
showMessage("");
}
void MainWindow::disconnected() {
qDebug("Disconnected");
setConnectActionEnabled(true);
setStatus("Disconnected");
bleController->disconnectFromDevice();
state = DISCONNECTED;
centerWidget->setEnabled(false);
disconnectAction->setEnabled(false);
sendQueue.clear();
showMessage("");
}
void MainWindow::serviceDiscovered(const QBluetoothUuid &gatt) {
qDebug() << "Service discovered" << gatt;
if (gatt.toUInt16() == 0xfe00) {
service = bleController->createServiceObject(gatt, bleController);
Q_ASSERT(service->state() == QLowEnergyService::RemoteService);
connect(service, &QLowEnergyService::stateChanged,this, &MainWindow::serviceStateChanged);
service->discoverDetails();
}
}
void MainWindow::serviceStateChanged(QLowEnergyService::ServiceState newState) {
if (newState != QLowEnergyService::RemoteServiceDiscovered) return;
connect(service, &QLowEnergyService::characteristicChanged, this, &MainWindow::characteristicChanged);
auto readChar = service->characteristic(QBluetoothUuid((quint16)0xfe01));
writeChar = service->characteristic(QBluetoothUuid((quint16)0xfe02));
if (!readChar.isValid() || !writeChar.isValid()) {
qCritical("Characteristics not found");
handleInvalidService();
}
auto notifyConfig = readChar.descriptor(QBluetoothUuid::DescriptorType::ClientCharacteristicConfiguration);
service->writeDescriptor(notifyConfig, QByteArray::fromHex("0100"));
state = CONNECTED;
centerWidget->setEnabled(true);
queriedParams = false;
retrievingRecords = false;
}
void MainWindow::sendStart() {
if (modeButtons[Pad::MODE_SLEEP]->isChecked())
send(Pad::setMode(Pad::MODE_MANUAL));
send(Pad::start());
relativeSetTime = QDateTime::currentMSecsSinceEpoch();
relativeSetSpeed = startSpeedSlider->sliderPosition();
}
void MainWindow::characteristicChanged(const QLowEnergyCharacteristic &c, const QByteArray &value){
if (c.uuid().toUInt16() != 0xfe01) return;
//qDebug() << "Characteristic Changed" << c.uuid() << value.toHex(' ');
using namespace Pad;
auto parsed = parseMessage(value);
if (auto info = std::get_if<Info>(&parsed)) {
//qDebug("Info: state %u, speed %u, mode %u, time %u, distance %u, steps %u", info->state, info->speed, info->mode, info->time, info->distance, info->steps);
auto time = QDateTime::currentMSecsSinceEpoch();
if (info->mode < 3 && (time - setModeTime) > 1000) {
modeButtons[info->mode]->setChecked(true);
}
if ((time - setSpeedTime) > 1000 && !speedSlider->isSliderDown()) {
if (!speedSlider->isSliderDown())
setSpeedWidgets(info->speed);
}
currentSpeed = info->speed;
currentData.distance = info->distance * 10;
currentData.duration = info->time;
currentData.steps = info->steps;
updateStatsLabel();
auto s = info->state;
startButton->setText(s == 0 || s == 5 ? "Start" : "Stop");
} else if (auto params = std::get_if<Params>(&parsed)) {
//qDebug("Params: goalType %u, goal %u, regulate %u, maxSpeed %u, startSpeed %u, startMode %u, sensitivity %u, display %x, lock %u, unit %u", params->goalType, params->goal, params->regulate, params->maxSpeed, params->startSpeed, params->startMode, params->sensitivity, params->display, params->lock, params->unit);
queriedParams = true; // only query params once
setStartSpeedWidgets(params->startSpeed);
maxSpeed = params->maxSpeed;
startSpeedSlider->setMaximum(maxSpeed);
speedSlider->setMaximum(maxSpeed);
} else if (auto record = std::get_if<Record>(&parsed)) {
//qDebug("Record: onTime %u, startTime %u, duration %u, distance %u, steps %u, remaining %u", record->onTime, record->startTime, record->duration, record->distance, record->steps, record->remainingRecords);
remainingRecords = record->remainingRecords;
if (record->duration) {
stats.addRecord(*record);
retrievingRecords = true;
retrievingRecordsTime = QDateTime::currentMSecsSinceEpoch();
send(syncRecord(remainingRecords));
} else {
retrievingRecords = false;
}
} else {
qWarning() << "Unknown Message" << value.toHex();
}
}
void MainWindow::discoveryFinished() {
qDebug() << "Discovery Finished";
if (!service) {
handleInvalidService();
}
}
void MainWindow::connectError(QLowEnergyController::Error error) {
auto err {bleController->errorString()};
qCritical() << "Error while connecting to " << selectedDevice.name() << err;
showMessage("Connection error: " + err);
QMessageBox::warning(this, "Connection Error", "Error while connecting: " + err + " ");
disconnected();
}
void MainWindow::showMessage(const QString &message) {
messageLabel->setText(message);
}
void MainWindow::setStatus(const QString &status) {
statusLabel->setText(status);
}
void MainWindow::tick() {
if (state != CONNECTED) return;
send(Pad::query());
if (!queriedParams)
send(Pad::queryParams());
if (retrievingRecords && (QDateTime::currentMSecsSinceEpoch() - retrievingRecordsTime > 5000))
retrievingRecords = false;
if (!retrievingRecords)
send(Pad::syncRecord(255));
}
void MainWindow::setConnectActionEnabled(bool enabled) {
connectionsEnabled = enabled;
for (auto a : connectActions) {
a->setEnabled(enabled);
}
}
void MainWindow::handleInvalidService() {
qCritical("No WalkingPad Service found");
QMessageBox::warning(this, "No WalkingPad Service", selectedDevice.name() + " does not appear to be a WalkingPad. Disconnecting...");
showMessage("No WalkingPad Service. Disconnecting...");
bleController->disconnectFromDevice();
}
void MainWindow::updateStatsLabel() {
auto dat = currentData;
auto addStats = settings.getShowStatistics();
if (addStats == Settings::ALL_TIME) {
dat += stats.allTime();
} else if (addStats == Settings::TODAY) {
dat += stats.today();
}
constexpr auto w = 8;
statsLabel->setText(QString("%1 km/h\n"
"%2 time\n"
"%3 steps\n"
"%4 km")
.arg(currentSpeed/10., w, 'f', 1)
.arg(QTime::fromMSecsSinceStartOfDay(dat.duration * 1000).toString("hh:mm:ss"), w)
.arg(dat.steps, w)
.arg(dat.distance/1000., w, 'f', 2));
}
void MainWindow::receivedMessage(int instanceId, QByteArray message) {
if (state != CONNECTED) return;
auto args = message.split(' ');
if (args.empty()) return;
auto cmd = args[0];
if (cmd == "start") {
sendStart();
} else {
if (args.size() < 2) return;
bool ok;
int val = args[1].toInt(&ok);
if (!ok) return;
if (cmd == "setSpeed" && val >= 0 && val <= maxSpeed) {
send(Pad::setSpeed(val));
} else if (cmd == "addSpeed") {
auto now = QDateTime::currentMSecsSinceEpoch();
if ((now - relativeSetTime) > 5000) {
relativeSetSpeed = currentSpeed;
}
auto speed = relativeSetSpeed + val;
if (speed >= 0 && speed <= maxSpeed) {
relativeSetSpeed = speed;
send(Pad::setSpeed(speed));
}
relativeSetTime = now;
}
}
}