-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSensorBase.cpp
57 lines (49 loc) · 1.16 KB
/
SensorBase.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
#include "SensorBase.h"
bool SensorBase::m_debug = false;
byte SensorBase::UpdateCRC(byte res, uint8_t val) {
for (int i = 0; i < 8; i++) {
uint8_t tmp = (uint8_t)((res ^ val) & 0x80);
res <<= 1;
if (0 != tmp) {
res ^= 0x31;
}
val <<= 1;
}
return res;
}
byte SensorBase::CalculateCRC(byte *data, byte len) {
byte res = 0;
for (int j = 0; j < len; j++) {
uint8_t val = data[j];
res = UpdateCRC(res, val);
}
return res;
}
void SensorBase::SetDebugMode(boolean mode) {
m_debug = mode;
}
void SensorBase::DisplayFrame(unsigned long &lastMillis, char *device, bool fIsValid, byte *data, byte frameLength) {
unsigned long now = millis();
char div[16];
if (lastMillis == 0) {
lastMillis = now;
}
sprintf(div, "%06ld ", (unsigned long)(now - lastMillis));
Serial.print(div);
lastMillis = now;
// Show the raw data bytes
Serial.print(device);
Serial.print(" [");
for (int i = 0; i < frameLength; i++) {
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.print("]");
// Check CRC
if (!fIsValid) {
Serial.print(" CRC:WRONG");
}
else {
Serial.print(" CRC:OK");
}
}