rusefi/unit_tests/logicdata_csv_reader.cpp

75 lines
1.6 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
*/
2021-06-25 21:33:28 -07:00
#include "engine_test_helper.h"
#include "logicdata_csv_reader.h"
static char* trim(char *str) {
while (str != nullptr && str[0] == ' ') {
str++;
}
return str;
}
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;
}
void CsvReader::processLine(EngineTestHelper *eth) {
Engine *engine = &eth->engine;
EXPAND_Engine
const char s[2] = ",";
char *line = buffer;
char *timeStampstr = trim(strtok(line, s));
bool newState[2];
2021-06-25 22:31:50 -07:00
char *firstToken = trim(strtok(NULL, s));
char *secondToken = trim(strtok(NULL, s));
newState[columnIndeces[0]] = firstToken[0] == '1';
if (secondToken != nullptr && m_triggerCount > 1) {
2021-06-25 22:31:50 -07:00
newState[columnIndeces[1]] = secondToken[0] == '1';
}
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 (int 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();
hwHandleShaftSignal(index, newState[index], nowNt PASS_ENGINE_PARAMETER_SUFFIX);
2021-06-25 21:33:28 -07:00
currentState[index] = newState[index];
}
}
void CsvReader::readLine(EngineTestHelper *eth) {
if (!haveMore())
return;
processLine(eth);
}