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-12-01 10:35:44 -08:00
|
|
|
CsvReader::~CsvReader() {
|
|
|
|
if (fp) {
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-24 20:07:55 -07:00
|
|
|
void CsvReader::open(const char *fileName, const int* triggerColumnIndeces, const int *vvtColumnIndeces) {
|
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");
|
2022-09-24 19:55:54 -07:00
|
|
|
this->triggerColumnIndeces = triggerColumnIndeces;
|
2022-09-24 20:07:55 -07:00
|
|
|
this->vvtColumnIndeces = vvtColumnIndeces;
|
2021-06-25 21:33:28 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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++) {
|
2022-08-13 20:33:30 -07:00
|
|
|
char *triggerToken = trim(strtok(nullptr, s));
|
2021-12-01 10:35:44 -08:00
|
|
|
v[i] = std::stod(triggerToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
return timeStamp;
|
|
|
|
}
|
|
|
|
|
2021-06-25 21:33:28 -07:00
|
|
|
void CsvReader::processLine(EngineTestHelper *eth) {
|
|
|
|
Engine *engine = ð->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
|
|
|
|
2022-09-24 19:55:54 -07:00
|
|
|
bool newTriggerState[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++) {
|
2022-08-13 20:33:30 -07:00
|
|
|
char * triggerToken = trim(strtok(nullptr, s));
|
2022-09-24 19:55:54 -07:00
|
|
|
newTriggerState[triggerColumnIndeces[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++) {
|
2022-08-13 20:33:30 -07:00
|
|
|
char *vvtToken = trim(strtok(nullptr, s));
|
2022-09-24 20:07:55 -07:00
|
|
|
newVvtState[vvtColumnIndeces[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++) {
|
2022-09-24 19:55:54 -07:00
|
|
|
if (currentState[index] == newTriggerState[index]) {
|
2021-06-25 21:33:28 -07:00
|
|
|
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!!!
|
2022-09-24 19:55:54 -07:00
|
|
|
hwHandleShaftSignal(index, newTriggerState[index], nowNt);
|
2021-06-25 21:33:28 -07:00
|
|
|
|
2022-09-24 19:55:54 -07:00
|
|
|
currentState[index] = newTriggerState[index];
|
2021-06-25 21:33:28 -07:00
|
|
|
}
|
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();
|
2022-09-10 23:57:35 -07:00
|
|
|
TriggerValue event = newVvtState[vvtIndex] ^ engineConfiguration->invertCamVVTSignal ? TriggerValue::RISE : TriggerValue::FALL;
|
2021-07-21 20:24:23 -07:00
|
|
|
// todo: configurable selection of vvt mode - dual bank or dual cam single bank
|
2022-09-24 21:26:42 -07:00
|
|
|
int bankIndex;
|
|
|
|
int camIndex;
|
|
|
|
if (twoBanksSingleCamMode) {
|
|
|
|
bankIndex = vvtIndex;
|
|
|
|
camIndex = 0;
|
|
|
|
} else {
|
|
|
|
bankIndex = vvtIndex / 2;
|
|
|
|
camIndex = vvtIndex % 2;
|
|
|
|
}
|
2021-11-16 01:15:29 -08:00
|
|
|
hwHandleVvtCamSignal(event, nowNt, bankIndex *2 + camIndex);
|
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);
|
|
|
|
}
|