-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
69 lines (52 loc) · 1.93 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(Serial* com_, QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
com = com_;
ui->setupUi(this);
getSensorsFromFile();
t = new QTimer();
connect(t, SIGNAL(timeout()), this, SLOT(onUpdateTick()));
t->setInterval(250);
t->start();
tickCounter = 0;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onUpdateTick() {
tickCounter++;
if(tickCounter >= ui->interval_time->value()/0.250 && ui->activate_checkbox->isChecked()) {
tickCounter = 0;
update();
}
}
void MainWindow::update() {
int sensor_id = rand() % sensors_stuff.size();
int value_id = rand() % sensors_stuff[sensor_id];
int value = (rand() % (abs(ui->max_value->value() - ui->min_value->value()))) + std::min(ui->min_value->value(), ui->max_value->value());
QString fake_trame = "@$" + QString("%1").arg(sensor_id, 2, 10, QChar('0')) + "$" + QString("%1").arg(value_id, 2, 10, QChar('0')) + "$" + QString("%1").arg(value, 8, 10, QChar('0')) + "$00$";
com->write(fake_trame);
ui->statusBar->showMessage("Trame envoyée: " + fake_trame);
}
void MainWindow::getSensorsFromFile() {
QXmlStreamReader reader;
QFile* cptConfig = new QFile("conf/cplist.xml");
cptConfig->open(QIODevice::ReadOnly);
reader.setDevice(cptConfig);
reader.readNext();
int nvalues = 0;
while(!reader.atEnd()) {
if((reader.name() == "sensor")&&(reader.attributes().value("name").toString() != "")) {
sensors_stuff.push_back(0);
}
if((reader.name() == "value")&&(reader.attributes().value("name").toString() != "")) {
sensors_stuff.last()++;
nvalues++;
}
reader.readNext();
}
ui->number_of_sensors->setText(QString::number(sensors_stuff.size()) + " capteurs");
ui->number_of_values->setText(QString::number(nvalues) + " valeurs");
}