Skip to content

Commit

Permalink
DLOG viwer div and offset encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Jun 22, 2021
1 parent 5c9cdd5 commit 2ed6af3
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 73 deletions.
6 changes: 4 additions & 2 deletions src/eez/gui/widgets/yt_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ struct YTGraphStaticDrawHelper {
}

void drawGraph(uint32_t currentHistoryValuePosition, int startX, int endX, int vertDivisions) {
xLabels[m_valueIndex] = INT_MIN;
//xLabels[m_valueIndex] = INT_MIN;
yLabels[m_valueIndex] = INT_MIN;

if (ytDataDataValueIsVisible(widgetCursor.cursor, widget->data, m_valueIndex)) {
Expand All @@ -374,11 +374,13 @@ struct YTGraphStaticDrawHelper {
yPrevMax = yMax;

if (yMin != INT_MIN) {
xLabels[m_valueIndex] = x;
//xLabels[m_valueIndex] = x;
yLabels[m_valueIndex] = widgetCursor.y + yMin;
}
}
}

xLabels[m_valueIndex] = endX;
}

void drawLabel(font::Font &font, bool transparent) {
Expand Down
116 changes: 56 additions & 60 deletions src/eez/modules/psu/dlog_record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@

#include <eez/memory.h>

//uint32_t g_debugVar1;
//uint32_t g_debugVar2;
//uint32_t g_debugVar3;

#if defined(EEZ_PLATFORM_STM32)
extern RTC_HandleTypeDef hrtc;
Expand Down Expand Up @@ -142,44 +145,45 @@ bool getNextWriteBuffer(const uint8_t *&buffer, uint32_t &bufferSize, bool flush
buffer = nullptr;
bufferSize = 0;

// #if defined(EEZ_PLATFORM_STM32)
// taskENTER_CRITICAL();
// #endif
int32_t timeDiff = millis() - g_lastSavedBufferTickCount;
uint32_t indexDiff = g_writer.getBufferIndex() - g_lastSavedBufferIndex;
if (indexDiff > 0 && (flush || timeDiff >= CONF_DLOG_SYNC_FILE_TIME_MS || indexDiff >= CHUNK_SIZE)) {
bufferSize = MIN(indexDiff, CHUNK_SIZE);
buffer = g_saveBuffer;

int32_t i = g_writer.getBufferIndex() - (g_lastSavedBufferIndex + DLOG_RECORD_BUFFER_SIZE);

if (i <= 0) {
i = 0;
} else {
// #if defined(EEZ_PLATFORM_STM32)
// taskEXIT_CRITICAL();
// #endif
abortAfterBufferOverflowError(0);
return false;
}
#if defined(EEZ_PLATFORM_STM32)
taskENTER_CRITICAL();
#endif
int32_t timeDiff = millis() - g_lastSavedBufferTickCount;
uint32_t indexDiff = (g_writer.getBufferIndex() - g_lastSavedBufferIndex);

if (indexDiff > 0 && (flush || timeDiff >= CONF_DLOG_SYNC_FILE_TIME_MS || indexDiff >= CHUNK_SIZE)) {
bufferSize = MIN(indexDiff, CHUNK_SIZE);
buffer = g_saveBuffer;

int32_t i = g_writer.getBufferIndex() - (g_lastSavedBufferIndex + DLOG_RECORD_BUFFER_SIZE);

if (i <= 0) {
i = 0;
} else {
#if defined(EEZ_PLATFORM_STM32)
taskEXIT_CRITICAL();
#endif
abortAfterBufferOverflowError(0);
return false;
}

if ((uint32_t)i < bufferSize) {
uint32_t tail = (g_lastSavedBufferIndex + i) % DLOG_RECORD_BUFFER_SIZE;
uint32_t head = (g_lastSavedBufferIndex + bufferSize) % DLOG_RECORD_BUFFER_SIZE;
if (tail < head) {
memcpy(g_saveBuffer + i, DLOG_RECORD_BUFFER + tail, head - tail);
} else {
uint32_t n = DLOG_RECORD_BUFFER_SIZE - tail;
memcpy(g_saveBuffer, DLOG_RECORD_BUFFER + tail, n);
if (head > 0) {
memcpy(g_saveBuffer + n, DLOG_RECORD_BUFFER, head);
}
}
}
}
// #if defined(EEZ_PLATFORM_STM32)
// taskEXIT_CRITICAL();
// #endif
if ((uint32_t)i < bufferSize) {
uint32_t tail = (g_lastSavedBufferIndex + i) % DLOG_RECORD_BUFFER_SIZE;
uint32_t head = (g_lastSavedBufferIndex + bufferSize) % DLOG_RECORD_BUFFER_SIZE;
if (tail < head) {
memcpy(g_saveBuffer + i, DLOG_RECORD_BUFFER + tail, head - tail);
} else {
uint32_t n = DLOG_RECORD_BUFFER_SIZE - tail;
memcpy(g_saveBuffer, DLOG_RECORD_BUFFER + tail, n);
if (head > 0) {
memcpy(g_saveBuffer + n, DLOG_RECORD_BUFFER, head);
}
}
}
}
#if defined(EEZ_PLATFORM_STM32)
taskEXIT_CRITICAL();
#endif

return true;
}
Expand Down Expand Up @@ -209,30 +213,19 @@ void fileWrite(bool flush) {

int err = 0;

// File g_file;
// if (g_file.open(g_activeRecording.parameters.filePath, FILE_OPEN_APPEND | FILE_WRITE)) {
// if (g_file.seek(g_lastSavedBufferIndex)) {
size_t written = g_file.write(buffer, bufferSize);
//g_debugVar3 = g_lastSavedBufferIndex;
size_t written = g_file.write(buffer, bufferSize);
//g_debugVar3 = 0;

if (written != bufferSize) {
err = event_queue::EVENT_ERROR_DLOG_WRITE_ERROR;
}

// if (!g_file.close()) {
// err = event_queue::EVENT_ERROR_DLOG_WRITE_ERROR;
// }
if (written != bufferSize) {
err = event_queue::EVENT_ERROR_DLOG_WRITE_ERROR;
}

if (!err) {
g_file.sync();
g_lastSavedBufferIndex += bufferSize;
g_lastSavedBufferTickCount = millis();
}
// } else {
// err = event_queue::EVENT_ERROR_DLOG_SEEK_ERROR;
// }
// } else {
// err = event_queue::EVENT_ERROR_DLOG_FILE_REOPEN_ERROR;
// }
if (!err) {
g_file.sync();
g_lastSavedBufferIndex += bufferSize;
g_lastSavedBufferTickCount = millis();
}

if (err) {
//DebugTrace("write error\n");
Expand Down Expand Up @@ -366,7 +359,7 @@ static void log() {
++g_activeRecording.size;

g_nextTime = ++g_iSample * g_activeRecording.parameters.period;

if (g_nextTime > g_activeRecording.parameters.duration) {
stateTransition(EVENT_FINISH);
break;
Expand Down Expand Up @@ -833,12 +826,15 @@ void abortAfterMassStorageError() {
}

void reset() {
abort();
stateTransition(EVENT_RESET);
}

////////////////////////////////////////////////////////////////////////////////

void tick() {
//g_debugVar1 = g_writer.getBufferIndex();
//g_debugVar2 = g_lastSavedBufferIndex;
if (g_state == STATE_EXECUTING && g_nextTime <= g_activeRecording.parameters.duration && !g_inStateTransition) {
log();
}
Expand Down
124 changes: 113 additions & 11 deletions src/eez/modules/psu/dlog_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ static uint32_t g_yValuesScrollPosition = 0;
static Overlay g_multipleValuesOverlay;
static Overlay g_singleValueOverlay;

static Recording *g_cacheRecording;
static int g_cacheDlogValueIndex;
static float g_cacheMin;
static float g_cacheMax;

////////////////////////////////////////////////////////////////////////////////

static uint32_t getPosition(Recording& recording);
Expand Down Expand Up @@ -167,6 +172,8 @@ void loadSamples() {

if (g_loadedRowIndex < numRows) {
loadBlockElements();

g_cacheRecording = nullptr;
}
}

Expand Down Expand Up @@ -209,6 +216,11 @@ void stateManagment() {
}
}

auto newViewHeight = persist_conf::devConf.viewFlags.dlogViewLegendViewOption == persist_conf::DLOG_VIEW_LEGEND_VIEW_OPTION_DOCK && !persist_conf::devConf.viewFlags.dlogViewDrawerIsOpen ? 204 : 240;
if (newViewHeight != VIEW_HEIGHT) {
VIEW_HEIGHT = newViewHeight;
}

auto isExecuting = dlog_record::isExecuting();
if (!isExecuting && g_wasExecuting) {
if (psu::gui::isPageOnStack(PAGE_ID_DLOG_VIEW)) {
Expand Down Expand Up @@ -463,6 +475,37 @@ static float getDuration(Recording &recording) {
return recording.size > 0 ? (recording.size - 1) * recording.parameters.xAxis.step : 0;
}

////////////////////////////////////////////////////////////////////////////////

static void getRangeOfScreenData(Recording &recording, int dlogValueIndex, float &totalMin, float &totalMax) {
if (g_cacheRecording == &recording && g_cacheDlogValueIndex == dlogValueIndex) {
totalMin = g_cacheMin;
totalMax = g_cacheMax;
return;
}

totalMin = FLT_MAX;
totalMax = -FLT_MAX;
auto startPosition = getPosition(recording);
for (auto position = startPosition; position < startPosition + VIEW_WIDTH; position++) {
float max;
float min = recording.getValue(position, dlogValueIndex, &max);
if (min < totalMin) {
totalMin = min;
}
if (max > totalMax) {
totalMax = max;
}
}

g_cacheRecording = &recording;
g_cacheDlogValueIndex = dlogValueIndex;
g_cacheMin = totalMin;
g_cacheMax = totalMax;
}

////////////////////////////////////////////////////////////////////////////////

static void adjustXAxisOffset(Recording &recording) {
auto duration = getDuration(recording);
if (recording.xAxisOffset + VIEW_WIDTH * recording.parameters.period > duration) {
Expand All @@ -482,6 +525,19 @@ static Unit getYAxisUnit(Recording& recording, int dlogValueIndex) {
return recording.parameters.yAxisScaleType == dlog_file::SCALE_LINEAR ? recording.parameters.yAxes[dlogValueIndex].unit : UNIT_UNKNOWN;
}

float getYAxisRangeMax(Recording& recording, int dlogValueIndex) {
return recording.parameters.yAxes[dlogValueIndex].range.max;
}

float getYAxisResolution(Recording& recording, int dlogValueIndex) {
if (recording.parameters.yAxes[dlogValueIndex].unit == UNIT_AMPER) {
if (getYAxisRangeMax(recording, dlogValueIndex) <= 0.05f) {
return 0.00001f;
}
}
return 0.001f;
}

static uint32_t getPosition(Recording& recording) {
if (&recording == &dlog_record::g_activeRecording) {
return recording.size > (uint32_t)VIEW_WIDTH ? recording.size - VIEW_WIDTH : 0;
Expand Down Expand Up @@ -2480,6 +2536,13 @@ void guessStepValues(StepValues *stepValues, Unit unit) {
void data_dlog_value_div(DataOperationEnum operation, Cursor cursor, Value &value) {
auto &recording = dlog_view::getRecording();

float minValue;
float maxValue;
getRangeOfScreenData(recording, cursor, minValue, maxValue);

float minDiv = (maxValue - minValue) / 100;
float maxDiv = (maxValue - minValue) * 3;

if (operation == DATA_OPERATION_GET) {
bool focused = g_focusCursor == cursor && g_focusDataId == DATA_ID_DLOG_VISIBLE_VALUE_DIV;
if (focused && g_focusEditValue.getType() != VALUE_TYPE_NONE) {
Expand All @@ -2495,11 +2558,18 @@ void data_dlog_value_div(DataOperationEnum operation, Cursor cursor, Value &valu
value = Value(recording.dlogValues[cursor].div, getYAxisUnit(recording, cursor));
}
} else if (operation == DATA_OPERATION_GET_MIN) {
value = Value(1E-6f, getYAxisUnit(recording, cursor));
value = Value(minDiv, getYAxisUnit(recording, cursor));
} else if (operation == DATA_OPERATION_GET_MAX) {
value = Value(1E6f, getYAxisUnit(recording, cursor));
value = Value(maxDiv, getYAxisUnit(recording, cursor));
} else if (operation == DATA_OPERATION_SET) {
recording.dlogValues[cursor].offset = recording.dlogValues[cursor].offset * value.getFloat() / recording.dlogValues[cursor].div;
float mid = (minValue + maxValue) / 2;
float oldDiv = recording.dlogValues[cursor].div;
float newDiv = value.getFloat();
float oldOffset = recording.dlogValues[cursor].offset;

float newOffset = (mid + oldOffset) * newDiv / oldDiv - mid;

recording.dlogValues[cursor].offset = newOffset;
recording.dlogValues[cursor].div = value.getFloat();
} else if (operation == DATA_OPERATION_GET_NAME) {
value = "Div";
Expand All @@ -2509,8 +2579,14 @@ void data_dlog_value_div(DataOperationEnum operation, Cursor cursor, Value &valu
value = 0;
} else if (operation == DATA_OPERATION_GET_ENCODER_STEP_VALUES) {
StepValues *stepValues = value.getStepValues();
guessStepValues(stepValues, getYAxisUnit(recording, cursor));
stepValues->encoderSettings.mode = edit_mode_step::g_visibleValueDivEncoderMode;

stepValues->values = nullptr;
stepValues->count = 0;

stepValues->encoderSettings.range = 100 * minDiv;
stepValues->encoderSettings.step = minDiv;

stepValues->encoderSettings.mode = ENCODER_MODE_AUTO;
value = 1;
} else if (operation == DATA_OPERATION_SET_ENCODER_MODE) {
edit_mode_step::g_visibleValueDivEncoderMode = (EncoderMode)value.getInt();
Expand All @@ -2525,6 +2601,22 @@ void data_dlog_visible_value_div(DataOperationEnum operation, Cursor cursor, Val

void data_dlog_value_offset(DataOperationEnum operation, Cursor cursor, Value &value) {
auto &recording = dlog_view::getRecording();

float minValue;
float maxValue;
getRangeOfScreenData(recording, cursor, minValue, maxValue);

float minOffset = -recording.dlogValues[cursor].div * NUM_VERT_DIVISIONS / 2 - maxValue;
float maxOffset = recording.dlogValues[cursor].div * NUM_VERT_DIVISIONS / 2 - minValue;

if (recording.dlogValues[cursor].offset < minOffset) {
minOffset = recording.dlogValues[cursor].offset;
}

if (recording.dlogValues[cursor].offset > maxOffset) {
maxOffset = recording.dlogValues[cursor].offset;
}

if (operation == DATA_OPERATION_GET) {
bool focused = g_focusCursor == cursor && g_focusDataId == DATA_ID_DLOG_VISIBLE_VALUE_OFFSET;
if (focused && g_focusEditValue.getType() != VALUE_TYPE_NONE) {
Expand All @@ -2540,12 +2632,12 @@ void data_dlog_value_offset(DataOperationEnum operation, Cursor cursor, Value &v
value = Value(recording.dlogValues[cursor].offset, getYAxisUnit(recording, cursor));
}
} else if (operation == DATA_OPERATION_GET_MIN) {
value = Value(-1E6f, getYAxisUnit(recording, cursor));
value = Value(minOffset, getYAxisUnit(recording, cursor));
} else if (operation == DATA_OPERATION_GET_MAX) {
float rangeMin;
float rangeMax;
getRange(recording, cursor, rangeMin, rangeMax);
value = Value(1E6f, getYAxisUnit(recording, cursor));
value = Value(maxOffset, getYAxisUnit(recording, cursor));
} else if (operation == DATA_OPERATION_SET) {
recording.dlogValues[cursor].offset = value.getFloat();
} else if (operation == DATA_OPERATION_GET_NAME) {
Expand All @@ -2556,11 +2648,15 @@ void data_dlog_value_offset(DataOperationEnum operation, Cursor cursor, Value &v
value = 0;
} else if (operation == DATA_OPERATION_GET_ENCODER_STEP_VALUES) {
StepValues *stepValues = value.getStepValues();
guessStepValues(stepValues, getYAxisUnit(recording, cursor));
stepValues->encoderSettings.mode = edit_mode_step::g_visibleValueOffsetEncoderMode;

stepValues->values = nullptr;
stepValues->count = 0;

stepValues->encoderSettings.range = maxOffset - minOffset;
stepValues->encoderSettings.step = (maxOffset - minOffset) / VIEW_HEIGHT;

stepValues->encoderSettings.mode = ENCODER_MODE_AUTO;
value = 1;
} else if (operation == DATA_OPERATION_SET_ENCODER_MODE) {
edit_mode_step::g_visibleValueOffsetEncoderMode = (EncoderMode)value.getInt();
}
}

Expand Down Expand Up @@ -2756,6 +2852,12 @@ void action_dlog_view_select_options_tab() {
g_selectedTab = TAB_OPTIONS;
}

void data_dlog_has_bookmark(DataOperationEnum operation, Cursor cursor, Value &value) {
if (operation == DATA_OPERATION_GET) {
value = getRecording().parameters.bookmarksSize > 0;
}
}

void data_dlog_bookmarks_is_scrollbar_visible(DataOperationEnum operation, Cursor cursor, Value &value) {
if (operation == DATA_OPERATION_GET) {
value = &getRecording() == &g_dlogFile ? getRecording().parameters.bookmarksSize > BOOKMARKS_PER_PAGE : 0;
Expand Down

0 comments on commit 2ed6af3

Please sign in to comment.