Skip to content

Commit 66a12de

Browse files
committed
Merge remote-tracking branch 'origin/main' into main
2 parents ce03d68 + 5154fe5 commit 66a12de

File tree

6 files changed

+84
-24
lines changed

6 files changed

+84
-24
lines changed

Info.plist

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
<key>UIRequiresFullScreen</key>
3737
<false/>
3838
<key>NSBluetoothAlwaysUsageDescription</key>
39-
<string>We use Bluetooth LE to connect data logger</string>
39+
<string>We use Bluetooth LE to connect data logger device</string>
40+
<key>NSLocalNetworkUsageDescription</key>
41+
<string>We use local network to send UDP packets</string>
4042
<key>AppleMagnifiedMode</key>
4143
<false/>
4244
</dict>

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Table of Contents
88
- [2. Using conan Package Manager](#2-using-conan-package-manager)
99
- [3. Instillation](#3-instillation)
1010
- [4. Usage](#4-usage)
11+
- [4.1. Streaming Data Over UDP](#41-streaming-data-over-udp)
12+
- [4.2. Writing to File](#42-writing-to-file)
1113

1214
## 1. Requirements
1315

@@ -53,4 +55,12 @@ And under `Developer and Designer Tools`, select:
5355

5456
1. Open Qt Creator (the IDE that comes with Qt), `File -> Open File or Project...` then select `DataLogger/` folder.
5557
2. The IDE should be able to automatically detect `CMakeLists.txt` file and open a configuration view, make sure you select the a configuration that looks like - `Desktop Qt 5.15.* <build type>` - `<build type>` could be `clang 64bit` (mac) or `msvc 64-bit` (windows).
56-
3. Clicking on the play button (bottom left) should run the program and open a GUI.
58+
3. Clicking on the play button (bottom left) should run the program and open a GUI.
59+
60+
### 4.1. Streaming Data Over UDP
61+
62+
After clicking `Stream Data` button, all the data will be streamed via `http://localhost:45454`. Clicking on the same button will stop streaming.
63+
64+
### 4.2. Writing to File
65+
66+
The default location is the Desktop and the file name is in the format of `DataLogger-<date and time in GMT>`. `Write to File` is a toggle button, clicking it once will write to file and clicking it again will stop it.

chart.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
//
44

55
#include "chart.h"
6-
#include <QDebug>
7-
#include <iostream>
86

97
using namespace std;
108

@@ -45,9 +43,6 @@ void Chart::startUpdating(const QList<QLineSeries *> &seriesList, const QVector<
4543

4644
void Chart::updateAllSeries() {
4745

48-
qInfo() << "updates called";
49-
qInfo() << tempData.length();
50-
5146
removeZeroRows(*matrix);
5247
RowVectorXd meanData = matrix->colwise().mean().normalized();
5348

@@ -59,8 +54,6 @@ void Chart::updateAllSeries() {
5954
m_data.append(data);
6055
}
6156

62-
qInfo() << m_data.size() << c_windowWidth << pointCounter/2;
63-
6457
for (auto &i : c_seriesList) {
6558
i->replace(m_data);
6659
}

logger.cpp

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,18 @@ void Logger::on_actionSettings_triggered() {
9292

9393
void Logger::on_serverButton_toggled(bool checked) {
9494
if (checked) {
95-
ui->serverButton->setText("Stop Server");
96-
emit emitServerStatusLabel("server running on 127.0.0.1:1000");
95+
udpSocket = new QUdpSocket(this);
96+
ui->serverButton->setText("Stop Streaming");
97+
emit emitServerStatusLabel("server running on 127.0.0.1:45454");
9798
} else {
98-
ui->serverButton->setText("Start Server");
99+
udpSocket->close();
100+
delete udpSocket;
101+
udpSocket = nullptr;
102+
ui->serverButton->setText("Stream Data");
99103
emit emitServerStatusLabel("");
100104
}
105+
106+
doStream = checked;
101107
}
102108

103109
void Logger::slotReboot() {
@@ -256,21 +262,21 @@ void Logger::updateWaveValue(const QLowEnergyCharacteristic &info, const QByteAr
256262
}
257263
hexValue = hexValue.trimmed();
258264

259-
QVector<qreal> values;
265+
QVector<double> realValues;
260266
auto hexValueArray = hexValue.split(" ");
261267
for (int i = 0; i < hexValueArray.length(); ++i) {
262268
auto nValue = -hexValueArray[i].toInt(nullptr,
263269
16); // negating value because this gives me positive value (according to c# code)
264-
auto vValue = static_cast<qreal>(((nValue * 5) / 65536.0));
265-
values.append(vValue);
270+
auto vValue = static_cast<double>(((nValue * 5.0) / 65536.0));
271+
realValues.append(vValue);
266272
}
267273

268274
// qInfo() << values;
269275
if (lineSeries.length() == 0) {
270276
qChart->legend()->hide();
271277
QPen red(Qt::yellow);
272278
red.setWidth(3);
273-
for (int i = 0; i < values.length(); ++i) {
279+
for (int i = 0; i < realValues.length(); ++i) {
274280
auto *series = new QLineSeries;
275281
series->setUseOpenGL(true);
276282
series->setPen(red);
@@ -293,9 +299,32 @@ void Logger::updateWaveValue(const QLowEnergyCharacteristic &info, const QByteAr
293299
}
294300

295301
// qreal x = qChart->plotArea().width() / axisX->tickCount();
296-
chart->startUpdating(lineSeries, values, qChart->plotArea().width(), frequencyCounter);
302+
chart->startUpdating(lineSeries, realValues, qChart->plotArea().width(), frequencyCounter);
297303
// qInfo() << qChart->plotArea().width();
298304
// qChart->scroll(x, 0);
305+
306+
// Converts QVector<double> to QByteArray
307+
QByteArray line;
308+
QDataStream stream(&line, QIODevice::WriteOnly);
309+
stream << realValues;
310+
311+
if (doStream) {
312+
udpSocket->writeDatagram(line, QHostAddress::Broadcast, 45454);
313+
}
314+
315+
// Writes to DataLogger-(datetime in GMT).csv
316+
if (isFileOpen) {
317+
QTextStream out(file);
318+
for (int i = 0; i < realValues.length(); ++i) {
319+
if (i < realValues.length()) {
320+
out << QString("%1,").arg(QString::number(realValues.value(i)));
321+
} else {
322+
out << QString("%1").arg(QString::number(realValues.value(i)));
323+
}
324+
}
325+
out << Qt::endl;
326+
file->flush();
327+
}
299328
}
300329

301330
void Logger::confirmedDescriptorWrite(const QLowEnergyDescriptor &info, const QByteArray &value) {
@@ -307,10 +336,6 @@ void Logger::confirmedDescriptorWrite(const QLowEnergyDescriptor &info, const QB
307336
}
308337
}
309338

310-
void Logger::on_saveToFileButton_clicked() {
311-
312-
}
313-
314339
void Logger::countFrequency() {
315340
frequencyCheckDone = true;
316341
qInfo() << "frequency is " << frequencyCounter;
@@ -335,3 +360,21 @@ void Logger::errorService(QLowEnergyService::ServiceError error) {
335360
qWarning() << "Service Error: " << error;
336361
}
337362

363+
364+
void Logger::on_saveToFileButton_toggled(bool checked) {
365+
if (checked) {
366+
ui->saveToFileButton->setText("Stop Writing");
367+
auto fileName = QDir(
368+
QDir::fromNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)) +
369+
QString("/DataLogger-%1.csv").arg(QDateTime::currentDateTimeUtc().toString()));
370+
file = new QFile(fileName.path());
371+
file->open(QIODevice::Append | QIODevice::Text);
372+
} else {
373+
ui->saveToFileButton->setText("Write to File");
374+
file->flush();
375+
file->close();
376+
}
377+
378+
isFileOpen = checked;
379+
}
380+

logger.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <QtCharts>
66
#include <qlowenergycontroller.h>
77
#include <widgets/statusbarindicator.h>
8+
#include <QUdpSocket>
89
#include "deviceinfo.h"
910
#include "serviceinfo.h"
1011
#include "chart.h"
@@ -48,7 +49,7 @@ private slots:
4849
void on_actionAbout_DataBlogger_triggered();
4950
void on_actionSettings_triggered();
5051
void on_serverButton_toggled(bool checked);
51-
void on_saveToFileButton_clicked();
52+
void on_saveToFileButton_toggled(bool checked);
5253

5354
void serviceStateChanged(QLowEnergyService::ServiceState newState);
5455
void updateWaveValue(const QLowEnergyCharacteristic &info, const QByteArray &value);
@@ -78,6 +79,14 @@ private slots:
7879
QValueAxis *axisY = nullptr;
7980
bool foundDataChannelService = false;
8081

82+
// For streaming data
83+
QUdpSocket *udpSocket = nullptr;
84+
bool doStream = false;
85+
86+
// For writing data to file
87+
bool isFileOpen = false;
88+
QFile *file = nullptr;
89+
8190
// get the stream frequency
8291
QTimer *timer = nullptr;
8392
quint32 frequencyCounter = 0;

logger.ui

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@
6868
<string/>
6969
</property>
7070
<property name="text">
71-
<string>Save to File</string>
71+
<string>Write to File</string>
72+
</property>
73+
<property name="checkable">
74+
<bool>true</bool>
7275
</property>
7376
</widget>
7477
</item>
@@ -78,7 +81,7 @@
7881
<string/>
7982
</property>
8083
<property name="text">
81-
<string>Start Server</string>
84+
<string>Stream Data</string>
8285
</property>
8386
<property name="checkable">
8487
<bool>true</bool>

0 commit comments

Comments
 (0)