-
Notifications
You must be signed in to change notification settings - Fork 2
/
decode.cpp
129 lines (111 loc) · 2.42 KB
/
decode.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
#include "ftcom.hpp"
#include <iostream>
#include <fstream>
using namespace std;
ifstream inFile;
bool simUpdate(const uint8_t pinState, const uint8_t cycles) {
return true;
}
int decide(double value, double mean) {
if (value > mean) {
return 1;
} else {
return 0;
}
}
void printData(FTCom& ftcom, int numBytesReq) {
uint8_t data[numBytesReq];
if (ftcom.getRxRole() == FTCom::MASTER) {
cout << "Master: ";
} else {
cout << "Slave : ";
}
int numBytes = ftcom.readData(data, numBytesReq);
for (int b = 0; b < numBytes; b++) {
cout << (int) *(data + b) << " ";
}
cout << endl;
}
int main(int argc, char** argv) {
if (argc <= 1 || argc >= 4) {
cout << "Syntax: " << endl;
cout << "\t" << argv[0] << " file" << endl;
cout << "\t" << argv[0] << " file OF" << endl;
return -1;
}
int OF = 2;
if (argc == 3) {
try {
OF = stoi(argv[2]);
} catch (...) {
cout << "Unable to parse OF" << endl;
return -1;
}
}
inFile.open(argv[1]);
if (!inFile.good()) {
cout << "Unable to read from " << argv[1] << endl;
return -1;
}
cout << "Reading " << argv[1] << " with OF of " << OF << endl;
FTCom ftcom(&simUpdate, FTCom::SLAVE, OF);
double time;
double value;
double mean;
{
bool firstRound = true;
double minValue;
double maxValue;
while (inFile >> time >> value) {
if (firstRound) {
minValue = value;
maxValue = value;
firstRound = false;
}
if (value > maxValue) {
maxValue = value;
}
if (value < minValue) {
minValue = value;
}
}
inFile.close();
mean = minValue + abs(maxValue - minValue) / 2;
cout << "Max: " << maxValue << " Min: " << minValue << endl;
cout << "Decision level: " << mean << endl;
}
{
bool firstRound = true;
int lastCnt;
int cnt = 0;
int lastState;
int state;
inFile.open(argv[1]);
while (inFile >> time >> value) {
if (firstRound) {
lastCnt = cnt;
lastState = decide(value, mean);
firstRound = false;
}
state = decide(value, mean);
if (lastState != state) {
int duration = cnt - lastCnt;
int bytesRead = ftcom.nextEdge(duration);
if (bytesRead > 0) {
printData(ftcom, bytesRead);
}
lastState = state;
lastCnt = cnt;
}
cnt++;
}
inFile.close();
// Simulate final toggle
int duration = cnt - lastCnt;
int bytesRead = ftcom.nextEdge(duration);
if (bytesRead > 0) {
printData(ftcom, bytesRead);
}
cout << "Errors: " << ftcom.getRxErrors() << endl;
}
}