forked from FOME-Tech/fome-fw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logicdata_csv_reader.cpp
130 lines (105 loc) · 3.09 KB
/
logicdata_csv_reader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* @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");
m_triggerColumnIndeces = triggerColumnIndeces;
m_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) {
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[m_triggerColumnIndeces[i]] = triggerToken[0] == '1';
}
for (size_t i = 0; i < m_vvtCount; i++) {
char *vvtToken = trim(strtok(nullptr, s));
newVvtState[m_vvtColumnIndeces[i]] = vvtToken[0] == '1';
}
if (timeStampstr == nullptr) {
firmwareError(ObdCode::OBD_PCM_Processor_Fault, "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();
// todo: we invert VVT but we do not invert trigger input!!!
hwHandleShaftSignal(index, newTriggerState[index], nowNt);
currentState[index] = newTriggerState[index];
}
for (size_t vvtIndex = 0; vvtIndex < m_vvtCount ; vvtIndex++) {
if (currentVvtState[vvtIndex] == newVvtState[vvtIndex]) {
continue;
}
efitick_t nowNt = getTimeNowNt();
bool isRising = newVvtState[vvtIndex];
// 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(isRising, nowNt, bankIndex *2 + camIndex);
currentVvtState[vvtIndex] = newVvtState[vvtIndex];
}
}
void CsvReader::readLine(EngineTestHelper *eth) {
if (!haveMore())
return;
processLine(eth);
}