forked from Qt-Widgets/vehicle-can-daq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
188 lines (147 loc) · 5.54 KB
/
main.cpp
File metadata and controls
188 lines (147 loc) · 5.54 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <QGuiApplication>
#include <QCursor>
#include <QQmlApplicationEngine>
#include <QQmlComponent>
#include <QJsonArray>
#include <QCanBus>
#include <QCanBusDevice>
#include <QGeoPositionInfoSource> // <-- Geo Positioning
#include "canframeid.h"
#include "e46canbusframe.h"
#include "can_utilities.h"
#include "track.h"
#include <iostream>
namespace {
// Globals.
QObject *object;
QCanBusDevice *device;
canUtils::canDeviceTypes canDeviceType = canUtils::E46_DEVICE;
}
// Reads JSON string from file into a document object.
QJsonDocument readJson(const QString &filePath);
// Creates Qt JSON object from document object.
QJsonObject toJsonObject(const QJsonDocument &jsonDoc, const QString &trackName);
// Sets the filters to the CAN bus device.
QCanBusDevice::Filter setCanFilter(const unsigned short &id);
// Selects the appropriate frame mapping function for the CAN bus device in use.
void selectFrameMap();
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
// Hide mouse curser.
QGuiApplication::setOverrideCursor(QCursor(Qt::BlankCursor));
// Load gauge UI.
QQmlEngine engine;
QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/main.qml")));
object = component.create();
/***************************** Lap Timing and Geolocation functionality *****************************/
// Read JSON document from file path.
QJsonDocument jsonDoc = readJson(QCoreApplication::applicationDirPath() + QDir::separator() + "tracks.json");
// Populate track list for combobox selection in GUI.
QObject *comboBoxTracks = object->findChild<QObject*>("cboTracks");
if (comboBoxTracks)
{
QStringList list = jsonDoc.object().keys();
comboBoxTracks->setProperty("model", list);
comboBoxTracks->setProperty("currentIndex", -1);
}
QString selectedTrack = comboBoxTracks->property("currentText").toString();
// Create C++ JSON Object with item specified from the document.
QJsonObject jsonObj = toJsonObject(jsonDoc, selectedTrack);
// Create Track object containing all relevant racetrack information.
Track track(jsonObj);
if(track.trackFound()) {
std::cout << "Track Found" << std::endl;
std::cout << track.toString() << std::endl;
}
else {
std::cout << "Track NOT Found" << std::endl;
std::cout << track.toString() << std::endl;
}
/*QGeoPositionInfoSource *source = QGeoPositionInfoSource::createDefaultSource(0);
if(source)
source->minimumUpdateInterval();
QGeoPositionInfo gpi = source->lastKnownPosition();
QGeoCoordinate gc = gpi.coordinate();
std::cout << gc.toString().toStdString() << std::endl;
std::cout << source->availableSources();*/
/************************************** CAN Bus functionality ***************************************/
if(QCanBus::instance()->plugins().contains("socketcan"))
{
// Create CAN bus device and connect to can0 via SocketCAN plugin.
device = QCanBus::instance()->createDevice("socketcan", "can0");
device->connectDevice();
// Set filters for needed data frames from the CAN bus device.
if(device->state() == QCanBusDevice::ConnectedState)
{
object->setProperty("connStatus", "Connected");
// Apply filters to CAN Bus device.
QList<QCanBusDevice::Filter> filterList;
filterList.append(setCanFilter(E46_ENGINE_RPM));
//filterList.append(setCanFilter(E46_VEHICLE_SPEED));
filterList.append(setCanFilter(E46_FUEL_LEVEL));
filterList.append(setCanFilter(E46_COOLANT_TEMP));
filterList.append(setCanFilter(E46_OIL_TEMP));
device->setConfigurationParameter(QCanBusDevice::RawFilterKey, QVariant::fromValue(filterList));
// Connect framesRecieved signal to slot function for reading frames.
QObject::connect(device, &QCanBusDevice::framesReceived, selectFrameMap);
}
}
return app.exec();
}
QJsonDocument readJson(const QString &filePath) {
// Read JSON file to QString object.
QJsonDocument jsonDoc;
QString contents;
QFile file(filePath);
if(file.exists()) {
file.open(QIODevice::ReadOnly | QIODevice::Text);
contents = file.readAll();
file.close();
}
else {
jsonDoc = QJsonDocument();
return jsonDoc;
}
// Create JSON document from QString.
jsonDoc = QJsonDocument::fromJson(contents.toUtf8());
return jsonDoc;
}
QJsonObject toJsonObject(const QJsonDocument &jsonDoc, const QString &trackName) {
// Create JSON object from specified item (name of track).
QJsonObject jsonObj;
if(!jsonDoc.isEmpty())
jsonObj = jsonDoc.object();
else {
jsonObj = QJsonObject();
return jsonObj;
}
if(jsonObj.contains(trackName)) {
jsonObj = jsonObj.value(trackName).toObject();
}
else {
jsonObj = QJsonObject();
}
return jsonObj;
}
QCanBusDevice::Filter setCanFilter(const unsigned short &id)
{
QCanBusDevice::Filter filter;
filter.frameId = id;
filter.frameIdMask = 0x7FFu; // Compare against all 11-bits of frame id.
filter.format = QCanBusDevice::Filter::MatchBaseFormat;
filter.type = QCanBusFrame::DataFrame;
return filter;
}
void selectFrameMap()
{
switch(canDeviceType)
{
case canUtils::OBD2_DEVICE:
canUtils::mapObd2Frames(object, device);
break;
case canUtils::E46_DEVICE:
canUtils::mapE46Frames(object, device);
break;
}
}