-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEucInterface.cpp
146 lines (137 loc) · 7.7 KB
/
EucInterface.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "EucInterface.h"
Euc::Euc(Stream &ReceiverSerial, Stream &TransmitterSerial):
ReceiverSerial(ReceiverSerial), TransmitterSerial(TransmitterSerial) {
}
void Euc::setCallback(void (*eucLoopCallback)(float,float,float,float,float,float,bool)) {
eucLoop = eucLoopCallback;
}
void Euc::tick() {
Euc::RawData rawData = this->receiveRawData();
Euc::UsableData data = this->makeRawDataUsable(rawData);
this->eucLoop(data.voltage,data.speed,data.tempMileage,data.current,data.temperature,data.mileage,data.dataIsNew);
}
Euc::UsableData Euc::makeRawDataUsable(Euc::RawData eucRawData) {
Euc::UsableData eucUsableData;
eucUsableData.voltage = (float)((eucRawData.voltage[0] << 8) | eucRawData.voltage[1])/100; // volts
eucUsableData.speed = (float)((eucRawData.speed[0] << 8) | eucRawData.speed[1])/100*3.6; // kilometers per hour
eucUsableData.tempMileage = (float)((uint32_t)eucRawData.tempMileage[0]<<24 | (uint32_t)eucRawData.tempMileage[1]<<16 | (uint32_t)eucRawData.tempMileage[2]<<8 | (uint32_t)eucRawData.tempMileage[3])/1000.00; // kilometers
eucUsableData.current = (float)((eucRawData.current[0] << 8) | eucRawData.current[1])/100; // amps
eucUsableData.temperature = (float)((eucRawData.temperature[0] << 8) | eucRawData.temperature[1])/340+36.53; // Degrees Celsius
eucUsableData.mileage = (float)((uint32_t)eucRawData.mileage[0]<<24 | (uint32_t)eucRawData.mileage[1]<<16 | (uint32_t)eucRawData.mileage[2]<<8 | (uint32_t)eucRawData.mileage[3])/1000.00; // kilometers
eucUsableData.dataIsNew = eucRawData.dataIsNew;
return eucUsableData;
}
Euc::RawData Euc::receiveRawData() {
static unsigned int curPos = 0; // stores the current position the packet parser
static Euc::RawData eucRawDataReceived;
static Euc::RawData eucRawDataReceivedValid;
// TODO: there has to be a better way than this
static unsigned int hppEnd = sizeof Euc::RawData().headerPrimaryPacket;
static unsigned int hppSize = sizeof Euc::RawData().headerPrimaryPacket;
static unsigned int voltageEnd = sizeof Euc::RawData().headerPrimaryPacket+sizeof Euc::RawData().voltage;
static unsigned int voltageSize = sizeof Euc::RawData().voltage;
static unsigned int speedEnd = sizeof Euc::RawData().headerPrimaryPacket+sizeof Euc::RawData().voltage+sizeof Euc::RawData().speed;
static unsigned int speedSize = sizeof Euc::RawData().speed;
static unsigned int tempMileageEnd = sizeof Euc::RawData().headerPrimaryPacket+sizeof Euc::RawData().voltage+sizeof Euc::RawData().speed+sizeof Euc::RawData().tempMileage;
static unsigned int tempMileageSize = sizeof Euc::RawData().tempMileage;
static unsigned int currentEnd = sizeof Euc::RawData().headerPrimaryPacket+sizeof Euc::RawData().voltage+sizeof Euc::RawData().speed+sizeof Euc::RawData().tempMileage+sizeof Euc::RawData().current;
static unsigned int currentSize = sizeof Euc::RawData().current;
static unsigned int temperatureEnd = sizeof Euc::RawData().headerPrimaryPacket+sizeof Euc::RawData().voltage+sizeof Euc::RawData().speed+sizeof Euc::RawData().tempMileage+sizeof Euc::RawData().current+sizeof Euc::RawData().temperature;
static unsigned int temperatureSize = sizeof Euc::RawData().temperature;
static unsigned int unknownDataEnd = sizeof Euc::RawData().headerPrimaryPacket+sizeof Euc::RawData().voltage+sizeof Euc::RawData().speed+sizeof Euc::RawData().tempMileage+sizeof Euc::RawData().current+sizeof Euc::RawData().temperature+sizeof Euc::RawData().unknownData;
static unsigned int unknownDataSize = sizeof Euc::RawData().unknownData;
static unsigned int hspEnd = sizeof Euc::RawData().headerPrimaryPacket+sizeof Euc::RawData().voltage+sizeof Euc::RawData().speed+sizeof Euc::RawData().tempMileage+sizeof Euc::RawData().current+sizeof Euc::RawData().temperature+sizeof Euc::RawData().unknownData+sizeof Euc::RawData().headerSecondaryPacket;
static unsigned int hspSize = sizeof Euc::RawData().headerSecondaryPacket;
static unsigned int mileageEnd = sizeof Euc::RawData().headerPrimaryPacket+sizeof Euc::RawData().voltage+sizeof Euc::RawData().speed+sizeof Euc::RawData().tempMileage+sizeof Euc::RawData().current+sizeof Euc::RawData().temperature+sizeof Euc::RawData().unknownData+sizeof Euc::RawData().headerSecondaryPacket+sizeof Euc::RawData().mileage;
static unsigned int mileageSize = sizeof Euc::RawData().mileage;
static unsigned int endEnd = sizeof Euc::RawData().headerPrimaryPacket+sizeof Euc::RawData().voltage+sizeof Euc::RawData().speed+sizeof Euc::RawData().tempMileage+sizeof Euc::RawData().current+sizeof Euc::RawData().temperature+sizeof Euc::RawData().unknownData+sizeof Euc::RawData().headerSecondaryPacket+sizeof Euc::RawData().mileage+sizeof Euc::RawData().end-1;
static unsigned int endSize = sizeof Euc::RawData().end;
eucRawDataReceivedValid.dataIsNew = false;
if (ReceiverSerial.available() > 0) { // if a new byte has been received
unsigned char currentByte = ReceiverSerial.read(); // read the incoming byte
if (curPos < hppEnd) { // expected byte of the primary header was received
if (currentByte == eucRawDataReceived.headerPrimaryPacket[curPos]) {
curPos++;
} else {
curPos = 0;
}
} else if (curPos < voltageEnd) {
eucRawDataReceived.voltage[curPos-(voltageEnd-voltageSize)] = currentByte;
curPos++;
} else if (curPos < speedEnd) {
eucRawDataReceived.speed[curPos-(speedEnd-speedSize)] = currentByte;
curPos++;
} else if (curPos < tempMileageEnd) {
eucRawDataReceived.tempMileage[curPos-(tempMileageEnd-tempMileageSize)] = currentByte;
curPos++;
} else if (curPos < currentEnd) {
eucRawDataReceived.current[curPos-(currentEnd-currentSize)] = currentByte;
curPos++;
} else if (curPos < temperatureEnd) {
eucRawDataReceived.temperature[curPos-(temperatureEnd-temperatureSize)] = currentByte;
curPos++;
} else if (curPos < unknownDataEnd) {
eucRawDataReceived.unknownData[curPos-(unknownDataEnd-unknownDataSize)] = currentByte;
curPos++;
} else if (curPos < hspEnd) { // expected byte of the secondary header was received
if (currentByte == eucRawDataReceived.headerSecondaryPacket[curPos-(hspEnd-hspSize)]) {
curPos++;
} else {
curPos = 0;
}
} else if (curPos < mileageEnd) {
eucRawDataReceived.mileage[curPos-(mileageEnd-mileageSize)] = currentByte;
curPos++;
} else if (curPos < endEnd) { // expected byte of the end was received
if (currentByte == eucRawDataReceived.end[curPos-(endEnd-endSize)]) {
curPos++;
} else {
curPos = 0;
};
} else if (curPos == endEnd) { // all data received
eucRawDataReceivedValid = eucRawDataReceived;
eucRawDataReceivedValid.dataIsNew = true;
curPos = 0;
} else { // not sure if necessary
curPos = 0;
}
}
return eucRawDataReceivedValid;
}
void Euc::beep() {
this->TransmitterSerial.println("b");
}
void Euc::maddenMode() {
this->TransmitterSerial.println("h");
}
void Euc::comfortMode() {
this->TransmitterSerial.println("f");
}
void Euc::softMode() {
this->TransmitterSerial.println("s");
}
void Euc::calibrateAlignment() {
// TODO: check if delays are necessary
this->TransmitterSerial.println(",");
this->TransmitterSerial.println("c");
this->TransmitterSerial.println("y");
this->TransmitterSerial.println("c");
this->TransmitterSerial.println("y");
this->TransmitterSerial.println("cy");
this->TransmitterSerial.println("y");
}
void Euc::disableLevel1Alarm() {
this->TransmitterSerial.println("u");
}
void Euc::disableLevel2Alarm() {
this->TransmitterSerial.println("i");
}
void Euc::enableAlarms() {
this->TransmitterSerial.println("o");
}
void Euc::enable6kmhTiltback() {
this->TransmitterSerial.println("O");
}
void Euc::disable6kmhTiltback() {
this->TransmitterSerial.println("I");
}