-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
396 lines (325 loc) · 9.42 KB
/
main.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
#include "chargergui.h"
#include <QApplication>
#include <QThread>
#include <QComboBox>
#include <QObject>
#include <QCommandLinkButton>
#include <QDoubleSpinBox>
#include <QLabel>
#include <string>
#include <thread>
#include <exception>
#include "can_api.h"
#include "timer.h"
#define ELCON_WRITE_ADDR 0x1806E5F4
#define ELCON_READ_ADDR 0x18FF50E5
#define GREAT 1
#define PER GREAT
void chargeButtonPressed();
struct GuiItems
{
ChargerGui * gui;
CanInterface * can_if;
QComboBox * device_box;
QComboBox * baud_combobox;
QCommandLinkButton * connect_button;
QCommandLinkButton * refresh_button;
QCommandLinkButton * charge_button;
QDoubleSpinBox * request_current;
QDoubleSpinBox * target_voltage;
QLabel * voltage_label;
QLabel * current_label;
QLabel * timer_label;
QThread * reader_thread;
QThread * charger_thread;
Timer timer;
std::mutex can_mtx;
bool terminate = false;
void init(ChargerGui &w);
virtual ~GuiItems() {
if (can_if)
{
terminate = true;
gui = 0;
device_box = 0;
baud_combobox = 0;
connect_button = 0;
refresh_button = 0;
charge_button = 0;
request_current = 0;
target_voltage = 0;
voltage_label = 0;
current_label = 0;
timer = 0;
can_mtx.lock();
can_if->Close();
can_if = 0;
can_mtx.unlock();
dev_open = false;
charge_enable = false;
}
}
bool charge_enable = false;
bool dev_open = false;
} gui_items;
int getBaudRate(int baud_index)
{
int baud = 500000;
switch (baud_index)
{
// 1mbps
case 0:
baud = 1000000;
break;
// 500kbps
case 1:
baud = 500000;
break;
// 250kbps
case 2:
baud = 250000;
break;
// 125kbps
case 3:
baud = 125000;
break;
}
return baud;
}
/// @brief: Slot for handling when the baud rate is changed
/// Disconnects the CAN device and resets the GUI
void baudRateChanged(int idx)
{
CanInterface * can_if = gui_items.can_if;
can_if->setBaudRate(getBaudRate(idx));
}
/// @brief: thread that reads the CAN bus and displays the values to the GUI
/// if we get a message from the address we care about
void readTheCanBusYo()
{
while (!gui_items.terminate)
{
CanInterface * can_if = gui_items.can_if;
while (gui_items.dev_open)
{
if (gui_items.charge_enable)
{
uint64_t seconds = gui_items.timer.elapsedMs() / 1000;
int hour = seconds / 3600;
int minute = (seconds % 3600) / 60;
int second = seconds % 60;
QString minutes;
QString hours;
QString seconds_str;
hours.setNum(hour);
minutes.setNum(minute);
seconds_str.setNum(second);
if (hour < 10)
{
hours = "0" + hours;
}
if (minute < 10)
{
minutes = "0" + minutes;
}
if (second < 10)
{
seconds_str = "0" + seconds_str;
}
QString time = hours + ":" + minutes + ":" + seconds_str;
gui_items.timer_label->setText(time);
}
double actual_current = 0;
double actual_voltage = 0;
CanFrame rx_data;
gui_items.can_mtx.lock();
if (can_if)
{
rx_data = can_if->readCanData();
}
gui_items.can_mtx.unlock();
uint32_t id = rx_data.getId();
if (id == ELCON_READ_ADDR)
{
actual_voltage = ((double) ((rx_data.data[0] << 8)
| (rx_data.data[1]))) / 10;
actual_current = ((double) ((rx_data.data[2] << 8)
| (rx_data.data[3]))) / 10;
if (!gui_items.terminate)
{
gui_items.voltage_label->setText(QString().setNum(actual_voltage));
gui_items.current_label->setText(QString().setNum(actual_current));
}
}
}
}
}
/// @brief: Thread that sends the charge command
/// Will send charge enable = 1 when the start charging
/// button is clicked, 0 otherwise.
void elconsChargeTheCarYo()
{
CanInterface * can_if = gui_items.can_if;
uint8_t to_send[5];
while (!gui_items.terminate)
{
if (gui_items.charge_enable && gui_items.dev_open)
{
uint16_t target_voltage = 0;
uint16_t request_current = 0;
// don't access these pointers anymore pls
if (!gui_items.terminate)
{
request_current = gui_items.request_current->value() * 10;
target_voltage = gui_items.target_voltage->value() * 10;
}
to_send[0] = target_voltage >> 8;
to_send[1] = target_voltage;
to_send[2] = request_current >> 8;
to_send[3] = request_current;
to_send[4] = 0;
if (can_if)
{
can_if->writeCanData(ELCON_WRITE_ADDR, 5, to_send);
}
}
else if (gui_items.dev_open)
{
to_send[0] = 0;
to_send[1] = 0;
to_send[2] = 0;
to_send[3] = 0;
to_send[4] = 1;
if (can_if)
{
can_if->writeCanData(ELCON_WRITE_ADDR, 5, to_send);
}
}
// delay 1 second
QThread::currentThread()->msleep(1000);
}
}
/// @brief: slot for when the connect button is pressed
void connectButtonPressed()
{
CanInterface * can = gui_items.can_if;
if (!gui_items.dev_open)
{
int baud_idx = gui_items.baud_combobox->currentIndex();
int dev_index = gui_items.device_box->currentIndex();
int baud_rate = getBaudRate(baud_idx);
gui_items.can_mtx.lock();
can->Open(dev_index, baud_rate);
gui_items.can_mtx.unlock();
gui_items.dev_open = true;
gui_items.connect_button->setText("Disconnect");
gui_items.connect_button->setStyleSheet("color:rgb(170,0,0);");
gui_items.charge_button->setEnabled(true);
}
else
{
gui_items.connect_button->setText("Connect");
gui_items.dev_open = false;
gui_items.charge_enable = false;
gui_items.connect_button->setStyleSheet("color:rgb(0,85,0);");
gui_items.can_mtx.lock();
can->Close();
gui_items.can_mtx.unlock();
gui_items.charge_button->setEnabled(false);
// toggle back to start charging
if (gui_items.charge_button->text() == "Stop Charging")
{
chargeButtonPressed();
}
}
}
/// @brief: Slot that disconnects the CAN device if one is connected and the
/// device combobox changes indexes
void deviceIndexChanged(int idx)
{
if (gui_items.dev_open)
{
connectButtonPressed();
}
}
/// @brief: slot for when the charge_enable button is clicked
void chargeButtonPressed()
{
if (gui_items.charge_button->text() == "Start Charging")
{
gui_items.charge_enable = true;
gui_items.charge_button->setText("Stop Charging");
gui_items.charge_button->setStyleSheet("color:rgb(170,0,0);");
gui_items.timer.start();
}
else
{
gui_items.charge_button->setText("Start Charging");
gui_items.charge_enable = false;
gui_items.charge_button->setStyleSheet("color:rgb(0,85,0);");
}
}
/// @brief: Slot that updates the device list when the refresh button is clicked
void refreshButtonPressed()
{
auto dev_list = gui_items.can_if->getDevices();
QComboBox * dev_combobox = gui_items.device_box;
// insert the devices into the device list.
for (auto dev = dev_list.begin(); dev != dev_list.end(); dev++)
{
std::string name = *dev;
QString devname = name.c_str();
// don't add duplicates to the list
if (dev_combobox->findText(devname) == -1)
{
dev_combobox->addItem(name.c_str());
}
}
}
void GuiItems::init(ChargerGui &w)
{
gui_items.gui = &w;
baud_combobox = w.findChild<QComboBox *>("BaudRateBox");
device_box = w.findChild<QComboBox *>("CanDevices");
connect_button = w.findChild<QCommandLinkButton *>("CanConnectButton");
charge_button = w.findChild<QCommandLinkButton *>("ChargeButton");
refresh_button = w.findChild<QCommandLinkButton *>("RefreshButton");
// set the button text to green
connect_button->setStyleSheet("color:rgb(0,85,0);");
charge_button->setEnabled(false);
charger_thread = QThread::create(elconsChargeTheCarYo);
reader_thread = QThread::create(readTheCanBusYo);
// I guess findChild doesn't check more than 1 level of recursion
QWidget * subframe = w.findChild<QWidget *>("widget");
QFrame * timer_frame = w.findChild<QFrame *>("timerFrame");
timer_label = timer_frame->findChild<QLabel *>("ElapsedTimeLabel");
voltage_label = subframe->findChild<QLabel*>("VoltageLabel");
current_label = subframe->findChild<QLabel*>("CurrentLabel");
target_voltage = w.findChild<QDoubleSpinBox *>("TargetVoltageBox");
request_current = w.findChild<QDoubleSpinBox *>("TargetCurrentBox");
int baud = getBaudRate(baud_combobox->currentIndex());
can_if = NewCanDevice(baud);
QObject::connect(baud_combobox,
QOverload<int>::of(&QComboBox::currentIndexChanged),
baudRateChanged);
QObject::connect(connect_button, &QCommandLinkButton::clicked,
connectButtonPressed);
QObject::connect(charge_button, &QCommandLinkButton::clicked,
chargeButtonPressed);
QObject::connect(refresh_button, &QCommandLinkButton::clicked,
refreshButtonPressed);
QObject::connect(device_box,
QOverload<int>::of(&QComboBox::currentIndexChanged),
deviceIndexChanged);
refreshButtonPressed();
charger_thread->start();
reader_thread->start();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ChargerGui w;
gui_items.init(w);
w.show();
return a.exec();
}