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
|
#include "dumps.h"
#include <QDebug>
#include <QDir>
#include <QElapsedTimer>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <iostream>
QList<Pixels> openDump(
const QString &dumpPath)
{
QString dirToRead{dumpPath};
qDebug() << "trying to open dump path:" << dirToRead;
if (dirToRead.isEmpty()) {
qDebug() << "dumpPath not specified. looking into" << dumpsRoot;
QDir dumpsRootDir{dumpsRoot};
const auto filter = QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable;
// there is no battery in my rpi5 for now
const auto sort = QDir::Name;
const auto entries = dumpsRootDir.entryList(filter, sort);
if (entries.isEmpty()) {
qWarning() << "dumps root" << dumpsRoot << "contains no dumps. "
<< "specify existing dump path";
return {};
}
dirToRead = dumpsRoot + "/" + entries.last();
}
QDir dumpDir{dirToRead};
// const QStringList nameFilters{"*.bin"};
const QStringList nameFilters{};
const auto filter = QDir::Files;
const auto sort = QDir::Name;
auto filenames = dumpDir.entryList(nameFilters, filter, sort);
if (filenames.isEmpty()) {
qDebug() << "no filenames found in" << dumpDir.path();
return {};
}
qDebug() << "create results array" << filenames.size();
auto resultOptionals = QScopedPointer(
new QList<std::optional<Pixels>>(filenames.size()));
QElapsedTimer t;
t.start();
qDebug() << "open real files";
std::cout << "here" << std::endl;
std::transform(
std::execution::par_unseq,
filenames.begin(),
filenames.end(),
resultOptionals->begin(),
[dirToRead](const auto &filename) {
// std::cout << '.';
// auto rawProfile = openRawProfile(dirToRead + "/" + filename);
auto rawProfile = Pixels::load(dirToRead + "/" + filename);
return rawProfile;
});
filenames.clear();
filenames.squeeze();
qDebug() << Q_FUNC_INFO << "open raw profiles: elapsed (ms)" << t.elapsed();
// std::cout << std::endl;
std::remove_if(std::execution::par,
resultOptionals->begin(),
resultOptionals->end(),
[](auto &a) { return !a.has_value(); });
QList<Pixels> result(resultOptionals->size());
std::transform(std::execution::par,
std::make_move_iterator(resultOptionals->begin()),
std::make_move_iterator(resultOptionals->end()),
result.begin(),
[](auto &p) { return p.value(); });
qDebug() << Q_FUNC_INFO << "elapsed (ms)" << t.elapsed();
return result;
}
std::optional<Pixels> openRawProfile(
const QString &filePath)
{
QFile f{filePath};
if (!f.open(QFile::ReadOnly)) {
qWarning() << "cannot open file for reading:" << f.fileName();
qWarning() << "error string:" << f.errorString();
return {};
}
// TODO: rewrite to remove manual serialization/deserialization
const auto json = QJsonDocument::fromJson(f.readAll()).object();
const auto jsonCounters = json["counters"].toObject();
Pixels result;
result.counters.timestampUs = jsonCounters["timestampUs"].toInteger();
result.counters.measurementCounter = jsonCounters["measurementCounter"]
.toInteger();
result.counters.encoderPosition = jsonCounters["encoderPosition"].toInteger();
const auto jsonPixels = json["pixels"].toArray();
// TODO: check json pixels count
#ifdef FIRST_COLUMN_ONLY
result.pixels[0] = jsonPixels.at(0).toDouble();
#else
std::transform(
// std::execution::par_unseq,
jsonPixels.constBegin(),
jsonPixels.constEnd(),
result.pixels.begin(),
[](const auto &jsonPixel) { return jsonPixel.toDouble(); });
// for (size_t i = 0; i < jsonPixels.count() && i < result.pixels.size();
// ++i)
// {
// result.pixels[i] = jsonPixels[i].toDouble();
// }
#endif
return result;
}
|