-
Notifications
You must be signed in to change notification settings - Fork 8
/
arduino.cpp
243 lines (175 loc) · 5.56 KB
/
arduino.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
#include "arduino.h"
#include "ui_arduino.h"
#include <QIcon>
#include <QtCore>
Arduino::Arduino(QWidget *parent) :
QDialog(parent),
ui(new Ui::Arduino)
{
ui->setupUi(this);
setWindowIcon(QIcon(":/icon"));
arduino = new QSerialPort(this);
baudRateList << "1200" << "2400" << "4800" << "9600" << "19200" << "38400" << "57600" << "115200";
ports = QSerialPortInfo::availablePorts();
QListIterator <QSerialPortInfo> iter(ports);
QListIterator <QString> baudIter(baudRateList);
while (iter.hasNext()){
ui->comportBox->addItem(iter.next().portName());
}
while (baudIter.hasNext()){
ui->baudrateBox->addItem(baudIter.next());
}
ui->baudrateBox->setCurrentText("9600");
connect(ui->comportBox, SIGNAL(currentTextChanged(QString)), this, SLOT(setPortName()));
connect(ui->baudrateBox, SIGNAL(currentTextChanged(QString)),this, SLOT(setBaudRate()));
connect(ui->connectButton, SIGNAL(clicked()), this, SLOT(connectArduino()));
connect(ui->forwardButton, SIGNAL(pressed()), this, SLOT(forward()));
connect(ui->backwardButton, SIGNAL(pressed()), this, SLOT(backward()));
connect(ui->leftButton, SIGNAL(pressed()), this, SLOT(left()));
connect(ui->rightButton, SIGNAL(pressed()), this, SLOT(right()));
connect(ui->stopButton, SIGNAL(clicked()), this, SLOT(stop()));
connect(ui->forwardButton, SIGNAL(released()), this, SLOT(stop()));
connect(ui->leftButton, SIGNAL(released()), this, SLOT(stop()));
connect(ui->rightButton, SIGNAL(released()), this, SLOT(stop()));
connect(ui->backwardButton, SIGNAL(released()), this, SLOT(stop()));
ui->statusLabel->setText("Connect BT Module");
arduino->setPortName(currentPort);
ui->speedLabel->setText("Running at default speed: 230");
}
void Arduino::forward()
{
arduino->write("W");
}
void Arduino::backward()
{
arduino->write("S");
}
void Arduino::left()
{
arduino->write("A");
}
void Arduino::right()
{
arduino->write("D");
}
void Arduino::stop()
{
arduino->write("q");
}
void Arduino::keyPressEvent(QKeyEvent *event)
{
//ui->statusLabel->setText( QString("You have pressed %1").arg(event->key()));
switch( event->key()){
case Qt::Key_W:
arduino->write("W");
break;
case Qt::Key_S:
arduino->write("S");
break;
case Qt::Key_A:
arduino->write("A");
break;
case Qt::Key_D:
arduino->write("D");
break;
case Qt::Key_H:
arduino->write("H");
break;
case Qt::Key_L:
arduino->write("L");
break;
default:
arduino->write("q");
break;
}
}
void Arduino::keyReleaseEvent(QKeyEvent *event)
{
if (!(event->isAutoRepeat()))
arduino->write("q");
}
void Arduino::setPortName()
{
currentPort = ui->comportBox->currentText();
arduino->setPortName(currentPort);
}
void Arduino::setBaudRate()
{
baudRate = (ui->baudrateBox->currentText()).toInt();
//Debugging Purpose
//QMessageBox::information(this, "Hello", ui->baudrateBox->currentText());
}
void Arduino::connectArduino()
{
arduino->close();
if (arduino->open(QIODevice::ReadWrite)){
arduino->setBaudRate(baudRate);
// 8bit mode
arduino->setDataBits(QSerialPort::Data8);
arduino->setParity(QSerialPort::NoParity);
arduino->setStopBits(QSerialPort::OneStop);
// Skipping hw/sw control
arduino->setFlowControl(QSerialPort::NoFlowControl);
QMessageBox::information(this, "Connection Successful", "Successfully connected to " + currentPort);
ui->statusLabel->setText("Connection Established! Press W/A/S/D to control");
}
else {
QMessageBox::warning(this, "Failed", "Connection Failed");
}
}
Arduino::~Arduino()
{
arduino->close();
delete ui;
}
void Arduino::on_forwardButton_clicked()
{
if (!(arduino->isOpen())) QMessageBox::warning(this, "Connect the device", "Connect the device first!");
}
void Arduino::on_aboutButton_clicked()
{
QMessageBox::information(this, "About Me", "I am Manash\nStudying EEE @KUET, Bangladesh\nThis app was written in Qt C++\nHope you like it :)");
}
void Arduino::on_leftButton_clicked()
{
if (!(arduino->isOpen())) QMessageBox::warning(this, "Connect the device", "Connect the device first!");
}
void Arduino::on_rightButton_clicked()
{
if (!(arduino->isOpen())) QMessageBox::warning(this, "Connect the device", "Connect the device first!");
}
void Arduino::on_backwardButton_clicked()
{
if (!(arduino->isOpen())) QMessageBox::warning(this, "Connect the device", "Connect the device first!");
}
void Arduino::on_stopButton_clicked()
{
if (!(arduino->isOpen())) QMessageBox::warning(this, "Connect the device", "Connect the device first!");
}
void Arduino::on_highspeed_clicked()
{
if (!(arduino->isOpen())){
QMessageBox::warning(this, "Connect the device", "Connect the device first!");
} else {
arduino->write("H");
ui->speedLabel->setText("Running at High speed: 255");
}
}
void Arduino::on_mediumspeed_clicked()
{
if (!(arduino->isOpen())){
QMessageBox::warning(this, "Connect the device", "Connect the device first!");
} else {
arduino->write("M");
ui->speedLabel->setText("Running at Medium speed: 220");
}
}
void Arduino::on_lowspeed_clicked()
{
if (!(arduino->isOpen())){
QMessageBox::warning(this, "Connect the device", "Connect the device first!");
} else {
arduino->write("L");
ui->speedLabel->setText("Running at Low speed: 190");
}
}