rusefi/unit_tests/logicdata_csv_reader.cpp

120 lines
2.7 KiB
C++
Raw Normal View History

2021-06-25 21:15:55 -07:00
/*
* @file logicdata_csv_reader.cpp
*
* @date Jun 26, 2021
* @author Andrey Belomutskiy, (c) 2012-2021
*/
#include "pch.h"
2021-06-25 21:33:28 -07:00
#include "logicdata_csv_reader.h"
static char* trim(char *str) {
while (str != nullptr && str[0] == ' ') {
str++;
}
return str;
}
2021-12-01 10:35:44 -08:00
CsvReader::~CsvReader() {
if (fp) {
fclose(fp);
}
}
void CsvReader::open(const char *fileName, const int* columnIndeces) {
2021-06-26 19:07:26 -07:00
printf("Reading from %s\r\n", fileName);
2021-06-25 21:33:28 -07:00
fp = fopen(fileName, "r");
this->columnIndeces = columnIndeces;
ASSERT_TRUE(fp != nullptr);
}
bool CsvReader::haveMore() {
bool result = fgets(buffer, sizeof(buffer), fp) != nullptr;
m_lineIndex++;
if (m_lineIndex == 0) {
2021-06-25 21:33:28 -07:00
// skip header
return haveMore();
}
return result;
}
2021-12-01 10:35:44 -08:00
double CsvReader::readTimestampAndValues(double *v) {
const char s[2] = ",";
char *line = buffer;
char *timeStampstr = trim(strtok(line, s));
double timeStamp = std::stod(timeStampstr);
for (size_t i = 0; i < m_triggerCount; i++) {
char *triggerToken = trim(strtok(NULL, s));
v[i] = std::stod(triggerToken);
}
return timeStamp;
}
2021-06-25 21:33:28 -07:00
void CsvReader::processLine(EngineTestHelper *eth) {
Engine *engine = &eth->engine;
const char s[2] = ",";
2021-12-07 14:00:42 -08:00
char *timeStampstr = trim(strtok(buffer, s));
2021-07-21 20:16:44 -07:00
bool newState[TRIGGER_INPUT_PIN_COUNT];
bool newVvtState[CAM_INPUTS_COUNT];
2021-06-25 22:31:50 -07:00
2021-07-21 20:16:44 -07:00
for (size_t i = 0;i<m_triggerCount;i++) {
char * triggerToken = trim(strtok(NULL, s));
newState[columnIndeces[i]] = triggerToken[0] == '1';
}
2021-07-21 20:16:44 -07:00
for (size_t i = 0;i<m_vvtCount;i++) {
char *vvtToken = trim(strtok(NULL, s));
newVvtState[i] = vvtToken[0] == '1';
2021-06-25 22:31:50 -07:00
}
2021-06-25 21:33:28 -07:00
2021-07-21 20:16:44 -07:00
if (timeStampstr == nullptr) {
firmwareError(OBD_PCM_Processor_Fault, "End of File");
return;
}
2021-06-25 21:33:28 -07:00
double timeStamp = std::stod(timeStampstr);
timeStamp += m_timestampOffset;
2021-06-25 21:33:28 -07:00
eth->setTimeAndInvokeEventsUs(1'000'000 * timeStamp);
for (size_t index = 0; index < m_triggerCount; index++) {
2021-06-25 21:33:28 -07:00
if (currentState[index] == newState[index]) {
continue;
}
2021-06-25 21:33:28 -07:00
efitick_t nowNt = getTimeNowNt();
2021-07-21 20:08:56 -07:00
// todo: we invert VVT but we do not invert trigger input!!!
hwHandleShaftSignal(index, newState[index], nowNt);
2021-06-25 21:33:28 -07:00
currentState[index] = newState[index];
}
for (size_t vvtIndex = 0; vvtIndex < m_vvtCount ; vvtIndex++) {
if (currentVvtState[vvtIndex] == newVvtState[vvtIndex]) {
continue;
}
efitick_t nowNt = getTimeNowNt();
2021-07-21 20:08:56 -07:00
trigger_value_e event = newVvtState[vvtIndex] ^ engineConfiguration->invertCamVVTSignal ? TV_RISE : TV_FALL;
2021-07-21 20:24:23 -07:00
// todo: configurable selection of vvt mode - dual bank or dual cam single bank
int bankIndex = vvtIndex;
int camIndex = 0;
hwHandleVvtCamSignal(event, nowNt, bankIndex *2 + camIndex);
currentVvtState[vvtIndex] = newVvtState[vvtIndex];
}
2021-06-25 21:33:28 -07:00
}
void CsvReader::readLine(EngineTestHelper *eth) {
if (!haveMore())
return;
processLine(eth);
}