2021-06-25 21:15:55 -07:00
|
|
|
/*
|
|
|
|
* @file logicdata_csv_reader.cpp
|
|
|
|
*
|
|
|
|
* @date Jun 26, 2021
|
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2021
|
|
|
|
*/
|
|
|
|
|
2021-08-03 19:05:01 -07:00
|
|
|
#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-07-01 14:29:31 -07:00
|
|
|
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;
|
2021-07-01 14:29:31 -07:00
|
|
|
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 = ð->engine;
|
|
|
|
EXPAND_Engine
|
|
|
|
|
|
|
|
const char s[2] = ",";
|
|
|
|
char *line = buffer;
|
|
|
|
|
|
|
|
char *timeStampstr = trim(strtok(line, s));
|
2021-07-21 20:16:44 -07:00
|
|
|
|
|
|
|
bool newState[TRIGGER_INPUT_PIN_COUNT];
|
2021-07-13 12:27:27 -07:00
|
|
|
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-13 12:04:30 -07:00
|
|
|
}
|
|
|
|
|
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-07-13 12:27:27 -07:00
|
|
|
}
|
|
|
|
|
2021-06-25 21:33:28 -07:00
|
|
|
double timeStamp = std::stod(timeStampstr);
|
|
|
|
|
2021-07-01 14:29:31 -07:00
|
|
|
timeStamp += m_timestampOffset;
|
|
|
|
|
2021-06-25 21:33:28 -07:00
|
|
|
eth->setTimeAndInvokeEventsUs(1'000'000 * timeStamp);
|
2021-07-13 15:02:29 -07:00
|
|
|
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-28 07:06:22 -07:00
|
|
|
|
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!!!
|
2021-06-28 07:06:22 -07:00
|
|
|
hwHandleShaftSignal(index, newState[index], nowNt PASS_ENGINE_PARAMETER_SUFFIX);
|
2021-06-25 21:33:28 -07:00
|
|
|
|
|
|
|
currentState[index] = newState[index];
|
|
|
|
}
|
2021-07-13 12:27:27 -07:00
|
|
|
|
2021-07-13 15:02:29 -07:00
|
|
|
for (size_t vvtIndex = 0; vvtIndex < m_vvtCount ; vvtIndex++) {
|
2021-07-13 12:27:27 -07:00
|
|
|
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 PASS_ENGINE_PARAMETER_SUFFIX);
|
2021-07-13 12:27:27 -07:00
|
|
|
|
2021-07-13 15:02:29 -07:00
|
|
|
currentVvtState[vvtIndex] = newVvtState[vvtIndex];
|
2021-07-13 12:27:27 -07:00
|
|
|
|
|
|
|
}
|
2021-06-25 21:33:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void CsvReader::readLine(EngineTestHelper *eth) {
|
|
|
|
if (!haveMore())
|
|
|
|
return;
|
|
|
|
processLine(eth);
|
|
|
|
}
|