-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
101 lines (87 loc) · 2.79 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QMessageBox>
using namespace std;
using namespace myo;
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
hub = nullptr;
isRunning = false;
ui->widgetControlPanel->setEnabled(false);
dataCollector = new DataCollector(this);
connect(dataCollector, &DataCollector::connectionLost, [this] () {
isRunning = false;
updateButtons();
ui->widgetControlPanel->setEnabled(false);
ui->pushButtonConnect->setEnabled(true);
QMessageBox::warning(this, tr("Warning"), tr("Connection Lost..."));
});
connect(ui->actionAbout, &QAction::triggered, [this] () {
QMessageBox::about(this, QString("About"), tr("This Application is written by Celal Savur \n"
"For any feedback or suggestions please contact to [email protected] \n"
"Thank you for using this application..."));
});
connect(ui->actionAboutQt, SIGNAL(triggered(bool)), qApp, SLOT(aboutQt()));
}
MainWindow::~MainWindow()
{
qDebug() << "~MainWindow()";
if(hub)
delete hub;
delete ui;
}
void MainWindow::on_pushButtonStartStreaming_clicked()
{
isRunning = true;
updateButtons();
while (isRunning) {
hub->runOnce(1);
qApp->processEvents();
}
qDebug() << "Stopped" ;
updateButtons();
}
void MainWindow::on_pushButtonStopStreaming_clicked()
{
isRunning = false;
updateButtons();
}
void MainWindow::closeEvent(QCloseEvent *event)
{
isRunning = false;
QMainWindow::closeEvent(event);
}
void MainWindow::on_pushButtonConnect_clicked()
{
bool connectionStatus = false;
try {
hub = new myo::Hub("edu.rit.mabl");
hub->setLockingPolicy(hub->lockingPolicyNone);
std::cout << "Attempting to find a Myo..." << std::endl;
qApp->processEvents();
myo::Myo* myo = hub->waitForMyo(10000);
if (!myo) {
throw std::runtime_error("Unable to find a Myo!");
}
connectionStatus = true;
myo->setStreamEmg(myo::Myo::streamEmgEnabled);
hub->addListener(dataCollector);
} catch (const std::exception& e) {
std::cout << "Error #" << e.what() << std::endl;
return;
}
// if connection is ok
if(connectionStatus) {
ui->widgetControlPanel->setEnabled(true);
ui->pushButtonConnect->setEnabled(false);
}
updateButtons();
}
void MainWindow::updateButtons()
{
ui->pushButtonStartStreaming->setEnabled(!isRunning);
ui->pushButtonStopStreaming->setEnabled(isRunning);
}