rusefi/firmware/development/sensor_chart.cpp

96 lines
2.0 KiB
C++
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
2015-09-13 12:02:21 -07:00
* @file sensor_chart.cpp
2015-07-10 06:01:56 -07:00
*
* @date Dec 20, 2013
2020-01-13 18:57:43 -08:00
* @author Andrey Belomutskiy, (c) 2012-2020
2015-07-10 06:01:56 -07:00
*/
#include "pch.h"
2022-09-07 12:56:45 -07:00
2019-07-05 17:03:32 -07:00
#include "sensor_chart.h"
2015-07-10 06:01:56 -07:00
2019-04-12 19:10:57 -07:00
#if EFI_SENSOR_CHART
2016-05-28 16:02:28 -07:00
#include "status_loop.h"
2015-07-10 06:01:56 -07:00
2019-04-12 19:10:57 -07:00
#if EFI_TEXT_LOGGING
2020-08-26 20:08:48 -07:00
static char LOGGING_BUFFER[SC_BUFFER_SIZE] CCM_OPTIONAL;
2018-09-16 21:00:19 -07:00
static Logging scLogging("analog chart", LOGGING_BUFFER, sizeof(LOGGING_BUFFER));
#endif /* EFI_TEXT_LOGGING */
2015-07-10 06:01:56 -07:00
2016-05-28 16:02:28 -07:00
static int initialized = false;
2015-07-10 06:01:56 -07:00
enum class ScState {
PreArm,
Armed,
Logging,
Full
};
static ScState state = ScState::PreArm;
static uint32_t lastRevCount = 0;
2015-07-10 06:01:56 -07:00
void scAddData(float angle, float value) {
2019-04-12 19:10:57 -07:00
#if EFI_TEXT_LOGGING
2015-07-10 06:01:56 -07:00
if (!initialized) {
return; // this is possible because of initialization sequence
}
// Don't log if we need a flush
if (state == ScState::Full) {
2015-07-10 06:01:56 -07:00
return;
}
auto currentRev = getRevolutionCounter();
if (state == ScState::PreArm) {
// nothing to do - we just need to grab the rev counter once so we can detect a change
state = ScState::Armed;
} else if (state == ScState::Armed) {
// If armed, wait for a NEW revolution to start
if (lastRevCount != currentRev) {
state = ScState::Logging;
// Reset logging and append header
scLogging.reset();
scLogging.appendPrintf(PROTOCOL_ANALOG_CHART LOG_DELIMITER);
}
} else if (state == ScState::Logging) {
// If running and the revolution idx changes, terminate logging and wait for flush
if (lastRevCount != currentRev) {
state = ScState::Full;
2015-07-10 06:01:56 -07:00
}
}
lastRevCount = currentRev;
if (state == ScState::Logging) {
if (scLogging.remainingSize() > 100) {
scLogging.appendPrintf( "%.2f|%.2f|", angle, value);
} else {
state = ScState::Full;
}
2015-07-10 06:01:56 -07:00
}
2018-09-16 21:00:19 -07:00
#endif /* EFI_TEXT_LOGGING */
2015-07-10 06:01:56 -07:00
}
void initSensorChart(void) {
2018-01-30 11:53:13 -08:00
#if EFI_SIMULATOR
printf("initSensorChart\n");
#endif
2015-07-10 06:01:56 -07:00
initialized = true;
}
void publishSensorChartIfFull() {
if (state != ScState::Full) {
return;
}
scLogging.appendPrintf(LOG_DELIMITER);
scheduleLogging(&scLogging);
state = ScState::Armed;
}
2015-09-13 09:01:42 -07:00
#endif /* EFI_SENSOR_CHART */