-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
174 lines (144 loc) · 5.57 KB
/
Copy pathmain.cpp
File metadata and controls
174 lines (144 loc) · 5.57 KB
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
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QCoreApplication>
#include <QDebug>
#include <QMetaObject>
#include <QThread>
#include <QStringList>
#include <QUrl>
#include <csignal>
#include "hardware/PigpioCompat.h"
#include "hardware/MotorController.h"
#include "hardware/LedController.h"
#include "hardware/SpeedSensor.h"
#include "hardware/TempSensor.h"
#include "connectivity/BleServer.h"
#include "models/VehicleModel.h"
int main(int argc, char *argv[])
{
#ifndef DATN_RPI_HARDWARE
// The locally built host Qt has an XCB platform plugin but no Wayland
// plugin. Select XCB before QGuiApplication tries to load a platform.
if (qEnvironmentVariableIsEmpty("QT_QPA_PLATFORM"))
qputenv("QT_QPA_PLATFORM", "xcb");
#endif
int pi = pigpio_start(NULL, NULL);
if (pi < 0)
{ qCritical() << "pigpio_start failed with code" << pi
<< "- check that pigpiod is running and TCP port 8888 is reachable";
return -1;
}
#ifdef DATN_RPI_HARDWARE
qDebug() << "giao tiep duoc gpio";
#else
qDebug() << "[Desktop] GPIO/I2C simulation enabled";
#endif
QGuiApplication app(argc, argv);
QThread* motorThread = new QThread(&app);
MotorController* motor = new MotorController(pi);
motor->moveToThread(motorThread);
motorThread->start(QThread::TimeCriticalPriority);
LedController* lights = new LedController(pi, &app);
QThread* speedThread = new QThread(&app);
SpeedSensor* speed = new SpeedSensor(pi);
speed->moveToThread(speedThread);
QObject::connect(speedThread, &QThread::started,
speed, &SpeedSensor::start,
Qt::QueuedConnection);
speedThread->start(QThread::HighPriority);
QThread* bleThread = new QThread(&app);
BleServer* ble = new BleServer();
ble->moveToThread(bleThread);
QObject::connect(bleThread, &QThread::started,
ble, &BleServer::start,
Qt::QueuedConnection);
bleThread->start(QThread::HighPriority);
QThread* tempThread = new QThread(&app);
TempSensor* temp = new TempSensor(pi);
temp->moveToThread(tempThread);
QObject::connect(tempThread, &QThread::started,
temp, &TempSensor::start,
Qt::QueuedConnection);
tempThread->start(QThread::LowPriority);
VehicleModel vehicle;
QObject::connect(speed, &SpeedSensor::dataUpdated,
&vehicle, &VehicleModel::onSensorData,
Qt::QueuedConnection);
QObject::connect(temp, &TempSensor::dataUpdated,
&vehicle, &VehicleModel::onTemperature,
Qt::QueuedConnection);
QObject::connect(ble, &BleServer::connectionChanged,
&vehicle, &VehicleModel::onBleConnected,
Qt::QueuedConnection);
QObject::connect(ble, &BleServer::commandReceived,
motor, [motor](const QString& cmd) {
const QString command = cmd.trimmed().toUpper();
const QStringList parts = command.split(':', Qt::SkipEmptyParts);
const QString op = parts.isEmpty() ? command : parts.first();
const int speed = parts.size() > 1 ? parts.at(1).toInt() : MOTOR_DEFAULT_SPEED;
qDebug() << "[Motor] BLE CMD" << op << "speed:" << speed;
if (op == "F") {
motor->forward(speed);
// qDebug() << "tien";
}
else if (op == "B") {
motor->backward(speed);
}
else if (op == "L") {
motor->turnLeft(speed);
}
else if (op == "R") {
motor->turnRight(speed);
}
else if (op == "S") {
motor->stop();
}
}, Qt::QueuedConnection);
QObject::connect(ble, &BleServer::commandReceived,
lights, [lights](const QString& cmd) {
if (cmd == "TL") {
lights->toggleLeftSignal();
}
else if (cmd == "TR") {
lights->toggleRightSignal();
}
else if (cmd == "HAZ") {
lights->toggleHazard();
}
else if (cmd == "LAMP"){
lights->toggleHeadLight();
}
}, Qt::QueuedConnection);
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("rpiHardware",
#ifdef DATN_RPI_HARDWARE
true
#else
false
#endif
);
engine.rootContext()->setContextProperty("vehicle", &vehicle);
engine.rootContext()->setContextProperty("ledCtrl", lights);
const QUrl url(u"qrc:/DATN_dashboard_car/Main.qml"_qs);
QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed,
&app, []() { QCoreApplication::exit(-1); },
Qt::QueuedConnection);
engine.load(url);
int ret = app.exec();
QMetaObject::invokeMethod(motor, "stop", Qt::BlockingQueuedConnection);
lights->allOff();
QMetaObject::invokeMethod(speed, "stop", Qt::BlockingQueuedConnection);
QMetaObject::invokeMethod(ble, "stop", Qt::BlockingQueuedConnection);
QMetaObject::invokeMethod(temp, "stop", Qt::BlockingQueuedConnection);
motorThread->quit(); motorThread->wait();
speedThread->quit(); speedThread->wait();
bleThread->quit(); bleThread->wait();
tempThread->quit(); tempThread->wait();
delete motor;
delete speed;
delete ble;
delete temp;
pigpio_stop(pi);
return ret;
}