Skip to content

Commit 8961871

Browse files
committed
dlog view
1 parent 4505315 commit 8961871

File tree

7 files changed

+125
-97
lines changed

7 files changed

+125
-97
lines changed

src/eez/main.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@
5151
#include <eez/modules/psu/serial_psu.h>
5252
#include <eez/modules/psu/sd_card.h>
5353

54-
volatile uint32_t g_debugVarBufferDiff;
55-
5654
////////////////////////////////////////////////////////////////////////////////
5755

5856
#if !defined(__EMSCRIPTEN__)

src/eez/modules/dib-mio168/dib-mio168.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@
5656

5757
#include <scpi/scpi.h>
5858

59-
volatile uint32_t g_debugVarLogInvalid;
60-
6159
using namespace eez::psu;
6260
using namespace eez::psu::gui;
6361
using namespace eez::gui;
@@ -1697,7 +1695,6 @@ struct Mio168Module : public Module {
16971695

16981696
void Command_DlogRecordingStart_FillRequest(Request &request) {
16991697
memcpy(&request.dlogRecordingStart, &dlogRecordingStart, sizeof(dlogRecordingStart));
1700-
g_debugVarLogInvalid = 0;
17011698
}
17021699

17031700
////////////////////////////////////////
@@ -1720,7 +1717,6 @@ struct Mio168Module : public Module {
17201717
}
17211718

17221719
while (dlogDataRecordIndex < dlogRecordingData.recordIndex) {
1723-
g_debugVarLogInvalid++;
17241720
dlog_record::logInvalid();
17251721
dlogDataRecordIndex++;
17261722
}

src/eez/modules/psu/dlog_record.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040

4141
#include <eez/memory.h>
4242

43-
volatile extern uint32_t g_debugVarBufferDiff;
44-
4543
namespace eez {
4644

4745
using namespace scpi;
@@ -143,7 +141,6 @@ bool getNextWriteBuffer(const uint8_t *&buffer, uint32_t &bufferSize, bool flush
143141
// #endif
144142
int32_t timeDiff = millis() - g_lastSavedBufferTickCount;
145143
uint32_t indexDiff = g_writer.getBufferIndex() - g_lastSavedBufferIndex;
146-
g_debugVarBufferDiff = indexDiff;
147144
if (indexDiff > 0 && (flush || timeDiff >= CONF_DLOG_SYNC_FILE_TIME_MS || indexDiff >= CHUNK_SIZE)) {
148145
bufferSize = MIN(indexDiff, CHUNK_SIZE);
149146
buffer = g_saveBuffer;

src/eez/modules/psu/dlog_view.cpp

+121-88
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ static Recording g_dlogFile;
6969
static uint32_t g_refreshCounter;
7070

7171
static uint8_t *g_rowDataStart = FILE_VIEW_BUFFER;
72-
static const uint32_t ROW_VALUES_BUFFER_SIZE = 65536;
72+
static const uint32_t ROW_DATA_BUFFER_SIZE = 65536;
7373

74-
static uint8_t *g_bookmarksDataStart = g_rowDataStart + ROW_VALUES_BUFFER_SIZE;
74+
static uint8_t *g_bookmarksDataStart = g_rowDataStart + ROW_DATA_BUFFER_SIZE;
7575
struct Bookmark {
7676
uint32_t position;
7777
const char *text;
@@ -98,6 +98,11 @@ static double g_loadScale;
9898
static uint32_t g_loadedRowIndex;
9999
static void loadBlockElements();
100100

101+
static bool g_fileIsOpen;
102+
File g_file;
103+
uint32_t g_cacheRowIndexStart;
104+
uint32_t g_cacheRowIndexEnd;
105+
101106
static bool g_isLoading;
102107
static bool g_refreshed;
103108
static bool g_wasExecuting;
@@ -125,41 +130,45 @@ static void loadVisibleBookmarks(uint32_t positionStart, uint32_t positionEnd);
125130

126131
void tick() {
127132
if (g_state == STATE_READY) {
128-
uint32_t rowIndexStart = dlog_view::getPosition(getRecording());
129-
uint32_t rowIndexEnd = rowIndexStart + VIEW_WIDTH;
130-
if (rowIndexStart > 0) {
131-
rowIndexStart = rowIndexStart - 1;
132-
}
133-
auto loadScale = g_dlogFile.xAxisDiv / g_dlogFile.xAxisDivMin;
134-
135-
uint32_t numRows = rowIndexEnd - rowIndexStart;
136-
137-
if (rowIndexStart != g_rowIndexStart || rowIndexEnd != g_rowIndexEnd || loadScale != g_loadScale) {
138-
uint32_t n = numRows * g_dlogFile.parameters.numYAxes;
139-
for (unsigned i = 0; i < n; i++) {
140-
g_blockElements[i].min = NAN;
141-
g_blockElements[i].max = NAN;
142-
}
143-
g_rowIndexStart = rowIndexStart;
144-
g_rowIndexEnd = rowIndexEnd;
145-
g_loadScale = loadScale;
146-
g_loadedRowIndex = 0;
147-
148-
auto positionStart = (uint32_t)floorf(rowIndexStart * loadScale);
149-
auto positionEnd = (uint32_t)ceilf(rowIndexEnd * loadScale);
150-
loadVisibleBookmarks(positionStart, positionEnd);
151-
}
152-
153-
if (g_loadedRowIndex < numRows) {
154-
loadBlockElements();
155-
}
133+
loadSamples();
156134

157135
if (g_bookmarksScrollPosition != g_loadedBookmarksScrollPosition) {
158136
loadBookmarks();
159137
}
160138
}
161139
}
162140

141+
void loadSamples() {
142+
uint32_t rowIndexStart = dlog_view::getPosition(getRecording());
143+
uint32_t rowIndexEnd = rowIndexStart + VIEW_WIDTH;
144+
if (rowIndexStart > 0) {
145+
rowIndexStart = rowIndexStart - 1;
146+
}
147+
auto loadScale = g_dlogFile.xAxisDiv / g_dlogFile.xAxisDivMin;
148+
149+
uint32_t numRows = rowIndexEnd - rowIndexStart;
150+
151+
if (rowIndexStart != g_rowIndexStart || rowIndexEnd != g_rowIndexEnd || loadScale != g_loadScale) {
152+
uint32_t n = numRows * g_dlogFile.parameters.numYAxes;
153+
for (unsigned i = 0; i < n; i++) {
154+
g_blockElements[i].min = NAN;
155+
g_blockElements[i].max = NAN;
156+
}
157+
g_rowIndexStart = rowIndexStart;
158+
g_rowIndexEnd = rowIndexEnd;
159+
g_loadScale = loadScale;
160+
g_loadedRowIndex = 0;
161+
162+
auto positionStart = (uint32_t)floorf(rowIndexStart * loadScale);
163+
auto positionEnd = (uint32_t)ceilf(rowIndexEnd * loadScale);
164+
loadVisibleBookmarks(positionStart, positionEnd);
165+
}
166+
167+
if (g_loadedRowIndex < numRows) {
168+
loadBlockElements();
169+
}
170+
}
171+
163172
State getState() {
164173
if (g_showLatest) {
165174
if (g_wasExecuting) {
@@ -202,80 +211,105 @@ void stateManagment() {
202211
}
203212
}
204213

214+
#define IS_VALID_SAMPLE(recording, rowData) !recording.parameters.dataContainsSampleValidityBit || *rowData & 0x80
215+
205216
bool isValidSample(Recording &recording, uint8_t *rowData) {
206-
return !recording.parameters.dataContainsSampleValidityBit|| rowData[0] & 0x80;
207-
}
217+
return IS_VALID_SAMPLE(recording, rowData);
218+
}
219+
220+
#define GET_SAMPLE(recording, rowData, columnIndex) \
221+
float value; \
222+
auto &yAxis = recording.parameters.yAxes[columnIndex]; \
223+
auto dataType = yAxis.dataType; \
224+
uint32_t columnDataIndex = recording.columnDataIndexes[columnIndex]; \
225+
auto columnData = rowData + columnDataIndex; \
226+
if (dataType == dlog_file::DATA_TYPE_BIT) { \
227+
value = *columnData & recording.columnBitMask[columnIndex] ? 1.0f : 0.0f; \
228+
} else if (dataType == dlog_file::DATA_TYPE_INT16_BE) { \
229+
auto iValue = int16_t((columnData[0] << 8) | columnData[1]); \
230+
value = float(yAxis.transformOffset + yAxis.transformScale * iValue); \
231+
} else if (dataType == dlog_file::DATA_TYPE_INT24_BE) { \
232+
auto iValue = ((int32_t)((columnData[0] << 24) | (columnData[1] << 16) | (columnData[2] << 8))) >> 8; \
233+
value = float(yAxis.transformOffset + yAxis.transformScale * iValue); \
234+
} else if (yAxis.dataType == dlog_file::DATA_TYPE_FLOAT) { \
235+
uint8_t *p = (uint8_t *)&value; \
236+
*p++ = *columnData++; \
237+
*p++ = *columnData++; \
238+
*p++ = *columnData++; \
239+
*p = *columnData; \
240+
} else { \
241+
assert(false); \
242+
value = NAN; \
243+
} \
208244

209245
float getSample(Recording &recording, uint8_t *rowData, unsigned columnIndex) {
210-
float value;
211-
auto &yAxis = recording.parameters.yAxes[columnIndex];
212-
auto dataType = yAxis.dataType;
213-
uint32_t columnDataIndex = recording.columnDataIndexes[columnIndex];
214-
auto columnData = rowData + columnDataIndex;
215-
if (dataType == dlog_file::DATA_TYPE_BIT) {
216-
value = *columnData & recording.columnBitMask[columnIndex] ? 1.0f : 0.0f;
217-
} else if (dataType == dlog_file::DATA_TYPE_INT16_BE) {
218-
auto iValue = int16_t((columnData[0] << 8) | columnData[1]);
219-
value = float(yAxis.transformOffset + yAxis.transformScale * iValue);
220-
} else if (dataType == dlog_file::DATA_TYPE_INT24_BE) {
221-
auto iValue = ((int32_t)((columnData[0] << 24) | (columnData[1] << 16) | (columnData[2] << 8))) >> 8;
222-
value = float(yAxis.transformOffset + yAxis.transformScale * iValue);
223-
} else if (yAxis.dataType == dlog_file::DATA_TYPE_FLOAT) {
224-
uint8_t *p = (uint8_t *)&value;
225-
*p++ = *columnData++;
226-
*p++ = *columnData++;
227-
*p++ = *columnData++;
228-
*p = *columnData;
229-
} else {
230-
assert(false);
231-
value = NAN;
232-
}
233-
return value;
246+
GET_SAMPLE(recording, rowData, columnIndex);
247+
return value;
234248
}
235249

236-
static void loadBlockElements() {
237-
File file;
238-
if (!file.open(g_filePath, FILE_OPEN_EXISTING | FILE_READ)) {
239-
return;
250+
static uint8_t *readMoreSamples(uint32_t rowIndex) {
251+
if (!g_fileIsOpen) {
252+
if (!g_file.open(g_filePath, FILE_OPEN_EXISTING | FILE_READ)) {
253+
return nullptr;
254+
}
255+
g_fileIsOpen = true;
240256
}
241257

258+
auto filePosition = g_dlogFile.dataOffset + rowIndex * g_dlogFile.numBytesPerRow;
259+
260+
if (!g_file.seek(filePosition)) {
261+
return nullptr;
262+
}
263+
242264
auto numSamplesPerValue = (unsigned)round(g_loadScale);
265+
auto remaining = g_file.size() - filePosition;
266+
auto maxRequiredPerView = (g_rowIndexEnd - g_rowIndexStart) * numSamplesPerValue * g_dlogFile.numBytesPerRow;
267+
uint32_t bytesToRead = MIN(MIN(ROW_DATA_BUFFER_SIZE, remaining), maxRequiredPerView);
268+
bytesToRead = (bytesToRead / g_dlogFile.numBytesPerRow) * g_dlogFile.numBytesPerRow;
269+
270+
uint32_t bytesRead = g_file.read(g_rowDataStart, bytesToRead);
271+
if (bytesRead != bytesToRead) {
272+
return nullptr;
273+
}
274+
275+
g_cacheRowIndexStart = rowIndex;
276+
g_cacheRowIndexEnd = rowIndex + bytesRead / g_dlogFile.numBytesPerRow;
277+
278+
return g_rowDataStart;
279+
}
280+
281+
static void loadBlockElements() {
282+
auto numSamplesPerBlock = (unsigned)round(g_loadScale);
243283

244284
uint32_t startTime = millis();
245285

246-
auto filePosition = g_dlogFile.dataOffset + (uint32_t)round(g_rowIndexStart * g_loadScale) * g_dlogFile.numBytesPerRow;
247-
uint8_t *rowDataStart = g_rowDataStart;
248-
uint8_t *rowDataEnd = g_rowDataStart;
286+
if (g_loadedRowIndex == 0) {
287+
g_cacheRowIndexStart = 0;
288+
g_cacheRowIndexEnd = 0;
289+
}
249290

250291
uint32_t n = g_rowIndexEnd - g_rowIndexStart;
251292
while (g_loadedRowIndex < n) {
252293
BlockElement *blockElementStart = g_blockElements + g_loadedRowIndex * g_dlogFile.parameters.numYAxes;
253294

254-
for (unsigned sampleIndex = 0; sampleIndex < numSamplesPerValue; sampleIndex++) {
255-
if (rowDataStart >= rowDataEnd) {
256-
if (!file.seek(filePosition)) {
257-
goto Exit;
258-
}
295+
auto rowIndex = (uint32_t)round((g_rowIndexStart + g_loadedRowIndex) * g_loadScale);
259296

260-
uint32_t bytesToRead = ((numSamplesPerValue - sampleIndex) + (n - (g_loadedRowIndex + 1)) * numSamplesPerValue) * g_dlogFile.numBytesPerRow;
261-
if (bytesToRead > ROW_VALUES_BUFFER_SIZE) {
262-
bytesToRead = ROW_VALUES_BUFFER_SIZE;
263-
}
297+
uint8_t *rowData = g_rowDataStart + (rowIndex - g_cacheRowIndexStart) * g_dlogFile.numBytesPerRow;
264298

265-
uint32_t bytesRead = file.read(g_rowDataStart, bytesToRead);
266-
if (bytesRead != bytesToRead) {
299+
for (unsigned blockSampleIndex = 0; blockSampleIndex < numSamplesPerBlock; blockSampleIndex++) {
300+
if (rowIndex < g_cacheRowIndexStart || rowIndex >= g_cacheRowIndexEnd) {
301+
rowData = readMoreSamples(rowIndex);
302+
if (!rowData) {
267303
goto Exit;
268304
}
269-
rowDataStart = g_rowDataStart;
270-
rowDataEnd = rowDataStart + bytesToRead;
271305
}
272306

273-
if (isValidSample(g_dlogFile, rowDataStart)) {
307+
if (IS_VALID_SAMPLE(g_dlogFile, rowData)) {
274308
BlockElement *blockElement = blockElementStart;
275309

276310
for (uint32_t columnIndex = 0; columnIndex < g_dlogFile.parameters.numYAxes; columnIndex++, blockElement++) {
277-
float value = getSample(g_dlogFile, rowDataStart, columnIndex);
278-
if (sampleIndex == 0) {
311+
GET_SAMPLE(g_dlogFile, rowData, columnIndex);
312+
if (blockSampleIndex == 0) {
279313
blockElement->min = blockElement->max = value;
280314
} else if (value < blockElement->min) {
281315
blockElement->min = value;
@@ -285,25 +319,23 @@ static void loadBlockElements() {
285319
}
286320
}
287321

288-
rowDataStart += g_dlogFile.numBytesPerRow;
289-
filePosition += g_dlogFile.numBytesPerRow;
322+
rowIndex++;
323+
rowData += g_dlogFile.numBytesPerRow;
290324
}
291325

292326
g_loadedRowIndex++;
293327
g_refreshed = true;
294328

295329
if (millis() - startTime > 50) {
296-
break;
330+
break;
297331
}
298-
299-
auto nextFilePosition = g_dlogFile.dataOffset + (uint32_t)round((g_rowIndexStart + g_loadedRowIndex) * g_loadScale) * g_dlogFile.numBytesPerRow;
300-
auto diff = nextFilePosition - filePosition;
301-
rowDataStart += diff;
302-
filePosition = nextFilePosition;
303332
}
304333

305334
Exit:
306-
file.close();
335+
if (g_fileIsOpen) {
336+
g_file.close();
337+
g_fileIsOpen = false;
338+
}
307339
}
308340

309341
static float getValue(uint32_t rowIndex, uint8_t columnIndex, float *max) {
@@ -341,6 +373,7 @@ static void adjustXAxisOffset(Recording &recording) {
341373
if (recording.xAxisOffset < 0) {
342374
recording.xAxisOffset = 0;
343375
}
376+
sendMessageToLowPriorityThread(THREAD_MESSAGE_DLOG_LOAD_SAMPLES);
344377
}
345378

346379
static Unit getXAxisUnit(Recording& recording) {

src/eez/modules/psu/dlog_view.h

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ bool openFile(const char *filePath, int *err = nullptr);
157157

158158
extern State getState();
159159

160+
void loadSamples();
160161
bool isValidSample(Recording &recording, uint8_t *rowData);
161162
float getSample(Recording &recording, uint8_t *rowData, unsigned columnIndex);
162163

src/eez/tasks.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ void lowPriorityThreadOneIter() {
307307
dlog_record::stateTransition(param);
308308
} else if (type == THREAD_MESSAGE_DLOG_SHOW_FILE) {
309309
dlog_view::openFile(nullptr);
310+
} else if (type == THREAD_MESSAGE_DLOG_LOAD_SAMPLES) {
311+
dlog_view::loadSamples();
310312
} else if (type == THREAD_MESSAGE_DLOG_LOAD_BOOKMARKS) {
311313
dlog_view::loadBookmarks();
312314
} else if (type == THREAD_MESSAGE_ABORT_DOWNLOADING) {

src/eez/tasks.h

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ enum LowPriorityThreadMessage {
8787
THREAD_MESSAGE_SD_DETECT_IRQ,
8888
THREAD_MESSAGE_DLOG_STATE_TRANSITION,
8989
THREAD_MESSAGE_DLOG_SHOW_FILE,
90+
THREAD_MESSAGE_DLOG_LOAD_SAMPLES,
9091
THREAD_MESSAGE_DLOG_LOAD_BOOKMARKS,
9192
THREAD_MESSAGE_ABORT_DOWNLOADING,
9293
THREAD_MESSAGE_SCREENSHOT,

0 commit comments

Comments
 (0)