Skip to content

Commit

Permalink
dlog bookmarks optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Mar 4, 2021
1 parent 2aa1ebc commit 76288f0
Show file tree
Hide file tree
Showing 12 changed files with 433 additions and 243 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"xmemory0": "cpp",
"initializer_list": "cpp",
"queue": "cpp",
"xlocnum": "cpp"
"xlocnum": "cpp",
"istream": "cpp"
},
"C_Cpp.errorSquiggles": "Disabled",
"cmake.configureOnOpen": true
Expand Down
57 changes: 6 additions & 51 deletions src/eez/dlog_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ void Writer::writeFileHeaderAndMetaFields(const Parameters &parameters) {
writeUint32Field(FIELD_ID_DATA_SIZE, 0);
m_dataSizeFieldOffset = m_bufferIndex - 4;

writeUint32Field(FIELD_ID_BOOKMARKS_SIZE, 0);
m_bookmarksSizeFieldOffset = m_bufferIndex - 4;

writeUint8Field(FIELD_ID_X_UNIT, parameters.xAxis.unit);
writeFloatField(FIELD_ID_X_STEP, parameters.xAxis.step);
writeUint8Field(FIELD_ID_X_SCALE_TYPE, parameters.xAxis.scaleType);
Expand Down Expand Up @@ -421,6 +424,9 @@ bool Reader::readRemainingFileHeaderAndMetaFields(Parameters &parameters) {
else if (fieldId == FIELD_ID_DATA_SIZE) {
parameters.dataSize = readUint32();
}
else if (fieldId == FIELD_ID_BOOKMARKS_SIZE) {
parameters.bookmarksSize = readUint32();
}
else if (fieldId == FIELD_ID_X_UNIT) {
parameters.xAxis.unit = (Unit)readUint8();
}
Expand Down Expand Up @@ -528,57 +534,6 @@ bool Reader::readRemainingFileHeaderAndMetaFields(Parameters &parameters) {
return !invalidHeader;
}

Bookmarks *Reader::readBookmarks(size_t srcBufferSize, uint8_t *destBuffer, size_t destBufferSize) {
auto bookmarks = (Bookmarks *)destBuffer;
auto bookmark = bookmarks->list;
uint8_t *texts = destBuffer + destBufferSize;

bookmarks->count = 0;

m_offset = 0;
while (m_offset < srcBufferSize) {
uint16_t fieldLength = readUint16();
if (fieldLength == 0) {
break;
}

if (m_offset - sizeof(uint16_t) + fieldLength > srcBufferSize) {
break;
}

uint8_t fieldId = readUint8();

uint16_t fieldDataLength = fieldLength - sizeof(uint16_t) - sizeof(uint8_t);

if (fieldId == FIELD_ID_BOOKMARK) {
texts -= fieldDataLength + 1;
if ((uint8_t *)(bookmark + 1) > texts) {
// TODO report that there is not enough room to store all the bookmarks
break;
}

bookmark->position = readUint32();
bookmark->text = (const char *)texts;

fieldDataLength -= 4;

auto p = texts;
for (int i = 0; i < fieldDataLength; i++) {
*p++ = readUint8();
}
*p = 0;

bookmarks->count++;
bookmark++;
} else {
// unknown field, skip
m_offset += fieldDataLength;
}
}

return bookmarks;
}

uint8_t Reader::readUint8() {
uint32_t i = m_offset;
m_offset += 1;
Expand Down
23 changes: 9 additions & 14 deletions src/eez/dlog_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ enum Fields {
// Default is 0.
FIELD_ID_DATA_CONTAINS_SAMPLE_VALIDITY_BIT = 4,

FIELD_ID_DATA_SIZE = 5, // number of data rows
FIELD_ID_DATA_SIZE = 5, // no. of data rows

FIELD_ID_BOOKMARKS_SIZE = 6, // no. of bookmarks

FIELD_ID_X_UNIT = 10,
FIELD_ID_X_STEP = 11,
Expand All @@ -97,8 +99,6 @@ enum Fields {

FIELD_ID_CHANNEL_MODULE_TYPE = 50,
FIELD_ID_CHANNEL_MODULE_REVISION = 51,

FIELD_ID_BOOKMARK = 62,
};

enum DataType {
Expand Down Expand Up @@ -127,6 +127,9 @@ enum DataType {
DATA_TYPE_DOUBLE_BE,
};

static const size_t MAX_BOOKMARK_TEXT_LEN = 25;
static const size_t NUM_BYTES_PER_BOOKMARK_IN_INDEX = 8;

struct Range {
float min;
float max;
Expand Down Expand Up @@ -184,20 +187,11 @@ struct Parameters {
float duration;

uint32_t dataSize;
uint32_t bookmarksSize;

void initYAxis(int yAxisIndex);
};

struct Bookmark {
uint32_t position;
const char *text;
};

struct Bookmarks {
uint32_t count;
Bookmark list[];
};

struct Writer {
public:
Writer(uint8_t *buffer, uint32_t bufferSize);
Expand All @@ -224,6 +218,7 @@ struct Writer {
uint32_t getDataOffset() { return m_dataOffset; }
uint32_t getFinishTimeFieldOffset() { return m_finalDurationFieldOffset; }
uint32_t getDataSizeFieldOffset() { return m_dataSizeFieldOffset; };
uint32_t getBookmarksSizeFieldOffset() { return m_bookmarksSizeFieldOffset; };
uint32_t getFileLength() { return m_fileLength; }
uint32_t getBitMask() { return m_bitMask; }

Expand All @@ -237,6 +232,7 @@ struct Writer {
uint32_t m_dataOffset = 0;
uint32_t m_finalDurationFieldOffset = 0;
uint32_t m_dataSizeFieldOffset = 0;
uint32_t m_bookmarksSizeFieldOffset = 0;

void writeUint8Field(uint8_t id, uint8_t value);
void writeUint8FieldWithIndex(uint8_t id, uint8_t value, uint8_t index);
Expand All @@ -257,7 +253,6 @@ struct Reader {

bool readFileHeaderAndMetaFields(Parameters &parameters, uint32_t &headerRemaining);
bool readRemainingFileHeaderAndMetaFields(Parameters &parameters); // call this only in case of VERSION2
Bookmarks *readBookmarks(size_t srcBufferSize, uint8_t *destBuffer, size_t destBufferSize);

uint16_t getVersion() { return m_version; }
uint32_t getDataOffset() { return m_dataOffset; }
Expand Down
22 changes: 6 additions & 16 deletions src/eez/gui/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -851,23 +851,13 @@ float ytDataGetPeriod(Cursor cursor, int16_t id) {
return value.getFloat();
}

void ytDataGetVisibleBookmarks(Cursor cursor, int16_t id, BookmarksSlice &bookmarksSlice) {
bookmarksSlice.start = 0;
bookmarksSlice.end = 0;
Value value(&bookmarksSlice, VALUE_TYPE_POINTER);
DATA_OPERATION_FUNCTION(id, DATA_OPERATION_YT_DATA_GET_VISIBLE_BOOKMARKS, cursor, value);
}

uint32_t ytDataGetBookmarkPosition(Cursor cursor, int16_t id, uint32_t bookmarkIndex) {
Value value(bookmarkIndex, VALUE_TYPE_UINT32);
DATA_OPERATION_FUNCTION(id, DATA_OPERATION_YT_DATA_GET_BOOKMARK_POSITION, cursor, value);
return value.getUInt32();
}

uint32_t ytDataGetSelectedBookmarkIndex(Cursor cursor, int16_t id) {
uint8_t *ytDataGetBookmarks(Cursor cursor, int16_t id) {
Value value;
DATA_OPERATION_FUNCTION(id, DATA_OPERATION_YT_DATA_GET_SELECTED_BOOKMARK_INDEX, cursor, value);
return value.getUInt32();
DATA_OPERATION_FUNCTION(id, DATA_OPERATION_YT_DATA_GET_BOOKMARKS, cursor, value);
if (value.getType() == VALUE_TYPE_POINTER) {
return (uint8_t *)value.getVoidPointer();
}
return nullptr;
}

bool ytDataIsCursorVisible(Cursor cursor, int16_t id) {
Expand Down
12 changes: 2 additions & 10 deletions src/eez/gui/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,7 @@ enum DataOperationEnum {
DATA_OPERATION_YT_DATA_GET_GET_VALUE_FUNC,
DATA_OPERATION_YT_DATA_GET_GRAPH_UPDATE_METHOD,
DATA_OPERATION_YT_DATA_GET_PERIOD,
DATA_OPERATION_YT_DATA_GET_VISIBLE_BOOKMARKS,
DATA_OPERATION_YT_DATA_GET_BOOKMARK_POSITION,
DATA_OPERATION_YT_DATA_GET_SELECTED_BOOKMARK_INDEX,
DATA_OPERATION_YT_DATA_GET_BOOKMARKS,
DATA_OPERATION_YT_DATA_IS_CURSOR_VISIBLE,
DATA_OPERATION_YT_DATA_GET_CURSOR_OFFSET,
DATA_OPERATION_YT_DATA_GET_CURSOR_X_VALUE,
Expand Down Expand Up @@ -470,13 +468,7 @@ void ytDataGetLabel(Cursor cursor, int16_t id, uint8_t valueIndex, char *text, i
Value::YtDataGetValueFunctionPointer ytDataGetGetValueFunc(Cursor cursor, int16_t id);
uint8_t ytDataGetGraphUpdateMethod(Cursor cursor, int16_t id);
float ytDataGetPeriod(Cursor cursor, int16_t id);
struct BookmarksSlice {
uint32_t start;
uint32_t end;
};
void ytDataGetVisibleBookmarks(Cursor cursor, int16_t id, BookmarksSlice &bookmarks);
uint32_t ytDataGetBookmarkPosition(Cursor cursor, int16_t id, uint32_t bookmarkIndex);
uint32_t ytDataGetSelectedBookmarkIndex(Cursor cursor, int16_t id);
uint8_t *ytDataGetBookmarks(Cursor cursor, int16_t id);
bool ytDataIsCursorVisible(Cursor cursor, int16_t id);
uint32_t ytDataGetCursorOffset(Cursor cursor, int16_t id);
Value ytDataGetCursorXValue(Cursor cursor, int16_t id);
Expand Down
33 changes: 20 additions & 13 deletions src/eez/gui/widgets/yt_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ struct YTGraphWidgetState {
uint32_t historyValuePosition;
uint8_t ytGraphUpdateMethod;
uint32_t cursorPosition;
uint8_t *bookmarks;
bool showLabels;
int8_t selectedValueIndex;
bool valueIsVisible[MAX_NUM_OF_Y_VALUES];
float valueDiv[MAX_NUM_OF_Y_VALUES];
float valueOffset[MAX_NUM_OF_Y_VALUES];
uint32_t selectedBookmarkIndex;
};

FixPointersFunctionType YT_GRAPH_fixPointers = nullptr;
Expand Down Expand Up @@ -246,6 +246,7 @@ struct YTGraphStaticDrawHelper {
int yMax;

uint32_t cursorPosition;
uint8_t *bookmarks;

Value::YtDataGetValueFunctionPointer ytDataGetValue;

Expand Down Expand Up @@ -440,17 +441,17 @@ struct YTGraphStaticDrawHelper {
}

// draw bookmarks
BookmarksSlice bookmarksSlice;
ytDataGetVisibleBookmarks(widgetCursor.cursor, widgetCursor.widget->data, bookmarksSlice);
uint32_t selectedBookmarkIndex = ytDataGetSelectedBookmarkIndex(widgetCursor.cursor, widgetCursor.widget->data);
for (uint32_t bookmarkIndex = bookmarksSlice.start; bookmarkIndex < bookmarksSlice.end; bookmarkIndex++) {
if (bookmarkIndex == selectedBookmarkIndex) {
display::setColor(COLOR_ID_SELECTED_BOOKMARK);
} else {
display::setColor(COLOR_ID_BOOKMARK);
if (bookmarks) {
for (int x = 0; x < widget->w; x++) {
if (bookmarks[x]) {
if (bookmarks[x] == 2) {
display::setColor(COLOR_ID_SELECTED_BOOKMARK);
} else {
display::setColor(COLOR_ID_BOOKMARK);
}
display::drawVLine(startX + x, widgetCursor.y, widget->h - 1);
}
}
uint32_t bookmarkPosition = ytDataGetBookmarkPosition(widgetCursor.cursor, widgetCursor.widget->data, bookmarkIndex);
display::drawVLine(startX + bookmarkPosition - currentHistoryValuePosition, widgetCursor.y, widget->h - 1);
}

// draw cursor
Expand Down Expand Up @@ -513,7 +514,7 @@ DrawFunctionType YT_GRAPH_draw = [](const WidgetCursor &widgetCursor) {
currentState->historyValuePosition = ytDataGetPosition(widgetCursor.cursor, widget->data);
currentState->ytGraphUpdateMethod = ytDataGetGraphUpdateMethod(widgetCursor.cursor, widget->data);
currentState->cursorPosition = currentState->historyValuePosition + ytDataGetCursorOffset(widgetCursor.cursor, widget->data);
currentState->selectedBookmarkIndex = ytDataGetSelectedBookmarkIndex(widgetCursor.cursor, widget->data);
currentState->bookmarks = ytDataGetBookmarks(widgetCursor.cursor, widget->data);

bool visibleValuesChanged = false;

Expand Down Expand Up @@ -552,12 +553,18 @@ DrawFunctionType YT_GRAPH_draw = [](const WidgetCursor &widgetCursor) {
currentState->selectedValueIndex != previousState->selectedValueIndex ||
visibleValuesChanged ||
previousHistoryValuePosition != currentState->historyValuePosition ||
(!previousState || previousState->numHistoryValues != currentState->numHistoryValues || previousState->cursorPosition != currentState->cursorPosition || previousState->selectedBookmarkIndex != currentState->selectedBookmarkIndex)
(
!previousState ||
previousState->numHistoryValues != currentState->numHistoryValues ||
previousState->cursorPosition != currentState->cursorPosition ||
previousState->bookmarks != currentState->bookmarks
)
) {
if (currentState->ytGraphUpdateMethod == YT_GRAPH_UPDATE_METHOD_STATIC) {
YTGraphStaticDrawHelper drawHelper(widgetCursor);

drawHelper.cursorPosition = currentState->cursorPosition;
drawHelper.bookmarks = currentState->bookmarks;
drawHelper.drawStatic(previousHistoryValuePosition, currentState->historyValuePosition, currentState->numHistoryValues, graphWidth, currentState->showLabels, currentState->selectedValueIndex);
} else {
const Style* style = getStyle(widget->style);
Expand Down
Loading

0 comments on commit 76288f0

Please sign in to comment.