mirror of https://github.com/rusefi/mpx-logic.git
useful code to chop
This commit is contained in:
parent
9ec9c0ba0e
commit
b293e0af14
|
@ -0,0 +1,141 @@
|
|||
/*
|
||||
* @file logicdata_csv_reader.cpp
|
||||
*
|
||||
* @date Jun 26, 2021
|
||||
* @author Andrey Belomutskiy, (c) 2012-2021
|
||||
*/
|
||||
|
||||
#include "pch.h"
|
||||
#include "logicdata_csv_reader.h"
|
||||
|
||||
static char* trim(char *str) {
|
||||
while (str != nullptr && str[0] == ' ') {
|
||||
str++;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
CsvReader::~CsvReader() {
|
||||
if (fp) {
|
||||
fclose(fp);
|
||||
}
|
||||
}
|
||||
|
||||
void CsvReader::open(const char *fileName, const int* triggerColumnIndeces, const int *vvtColumnIndeces) {
|
||||
printf("Reading from %s\r\n", fileName);
|
||||
fp = fopen(fileName, "r");
|
||||
this->triggerColumnIndeces = triggerColumnIndeces;
|
||||
this->vvtColumnIndeces = vvtColumnIndeces;
|
||||
ASSERT_TRUE(fp != nullptr);
|
||||
}
|
||||
|
||||
bool CsvReader::haveMore() {
|
||||
bool result = fgets(buffer, sizeof(buffer), fp) != nullptr;
|
||||
m_lineIndex++;
|
||||
if (m_lineIndex == 0) {
|
||||
// skip header
|
||||
return haveMore();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param values reference of values array to modify
|
||||
* @return timestamp of current line
|
||||
*/
|
||||
double CsvReader::readTimestampAndValues(double *values) {
|
||||
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(nullptr, s));
|
||||
values[i] = std::stod(triggerToken);
|
||||
}
|
||||
|
||||
return timeStamp;
|
||||
}
|
||||
|
||||
// todo: separate trigger handling from csv file processing
|
||||
void CsvReader::processLine(EngineTestHelper *eth) {
|
||||
Engine *engine = ð->engine;
|
||||
|
||||
const char s[2] = ",";
|
||||
char *timeStampstr = trim(strtok(buffer, s));
|
||||
|
||||
bool newTriggerState[TRIGGER_INPUT_PIN_COUNT];
|
||||
bool newVvtState[CAM_INPUTS_COUNT];
|
||||
|
||||
for (size_t i = 0;i<m_triggerCount;i++) {
|
||||
char * triggerToken = trim(strtok(nullptr, s));
|
||||
newTriggerState[triggerColumnIndeces[i]] = triggerToken[0] == '1';
|
||||
}
|
||||
|
||||
for (size_t i = 0;i<m_vvtCount;i++) {
|
||||
char *vvtToken = trim(strtok(nullptr, s));
|
||||
if (vvtToken == nullptr) {
|
||||
criticalError("Null token in [%s]", buffer);
|
||||
}
|
||||
bool state = vvtToken[0] == '1';
|
||||
newVvtState[vvtColumnIndeces[i]] = state;
|
||||
}
|
||||
|
||||
if (timeStampstr == nullptr) {
|
||||
criticalError("End of File");
|
||||
return;
|
||||
}
|
||||
|
||||
double timeStamp = std::stod(timeStampstr);
|
||||
|
||||
timeStamp += m_timestampOffset;
|
||||
|
||||
eth->setTimeAndInvokeEventsUs(1'000'000 * timeStamp);
|
||||
for (size_t index = 0; index < m_triggerCount; index++) {
|
||||
if (currentState[index] == newTriggerState[index]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
efitick_t nowNt = getTimeNowNt();
|
||||
bool state;
|
||||
if (index == 0) {
|
||||
state = newTriggerState[index] ^ flipOnRead ^ engineConfiguration->invertPrimaryTriggerSignal;
|
||||
} else {
|
||||
state = newTriggerState[index] ^ flipOnRead ^ engineConfiguration->invertSecondaryTriggerSignal;
|
||||
}
|
||||
hwHandleShaftSignal(index, state, nowNt);
|
||||
|
||||
currentState[index] = newTriggerState[index];
|
||||
}
|
||||
|
||||
for (size_t vvtIndex = 0; vvtIndex < m_vvtCount ; vvtIndex++) {
|
||||
if (currentVvtState[vvtIndex] == newVvtState[vvtIndex]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
efitick_t nowNt = getTimeNowNt();
|
||||
TriggerValue event = newVvtState[vvtIndex] ^ engineConfiguration->invertCamVVTSignal ? TriggerValue::RISE : TriggerValue::FALL;
|
||||
// todo: configurable selection of vvt mode - dual bank or dual cam single bank
|
||||
int bankIndex;
|
||||
int camIndex;
|
||||
if (twoBanksSingleCamMode) {
|
||||
bankIndex = vvtIndex;
|
||||
camIndex = 0;
|
||||
} else {
|
||||
bankIndex = vvtIndex / 2;
|
||||
camIndex = vvtIndex % 2;
|
||||
}
|
||||
hwHandleVvtCamSignal(event, nowNt, bankIndex *2 + camIndex);
|
||||
|
||||
currentVvtState[vvtIndex] = newVvtState[vvtIndex];
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void CsvReader::readLine(EngineTestHelper *eth) {
|
||||
if (!haveMore())
|
||||
return;
|
||||
processLine(eth);
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* @file logicdata_csv_reader.h
|
||||
*
|
||||
* @date Jun 26, 2021
|
||||
* @author Andrey Belomutskiy, (c) 2012-2021
|
||||
*/
|
||||
|
||||
const int NORMAL_ORDER[2] = {0, 1};
|
||||
|
||||
const int REVERSE_ORDER[2] = {1, 0};
|
||||
|
||||
|
||||
|
||||
class CsvReader {
|
||||
public:
|
||||
CsvReader(size_t triggerCount, size_t vvtCount) : CsvReader(triggerCount, vvtCount, 0.0) {}
|
||||
CsvReader(size_t triggerCount, size_t vvtCount, double timestampOffset)
|
||||
: m_triggerCount(triggerCount)
|
||||
, m_vvtCount(vvtCount)
|
||||
, m_timestampOffset(timestampOffset)
|
||||
{
|
||||
}
|
||||
~CsvReader();
|
||||
|
||||
bool twoBanksSingleCamMode = true;
|
||||
|
||||
void open(const char *fileName, const int* triggerColumnIndeces = NORMAL_ORDER, const int *vvtColumnIndeces = NORMAL_ORDER);
|
||||
bool haveMore();
|
||||
void processLine(EngineTestHelper *eth);
|
||||
void readLine(EngineTestHelper *eth);
|
||||
double readTimestampAndValues(double *v);
|
||||
|
||||
bool flipOnRead = false;
|
||||
|
||||
int lineIndex() const {
|
||||
return m_lineIndex;
|
||||
}
|
||||
|
||||
private:
|
||||
const size_t m_triggerCount;
|
||||
const size_t m_vvtCount;
|
||||
const double m_timestampOffset;
|
||||
|
||||
FILE *fp = nullptr;
|
||||
char buffer[255];
|
||||
|
||||
bool currentState[2] = {0, 0};
|
||||
bool currentVvtState[CAM_INPUTS_COUNT] = {0, 0};
|
||||
|
||||
int m_lineIndex = -1;
|
||||
|
||||
const int* triggerColumnIndeces;
|
||||
const int* vvtColumnIndeces;
|
||||
};
|
||||
|
Loading…
Reference in New Issue