-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsh500Parser.cpp
131 lines (115 loc) · 3.03 KB
/
Ash500Parser.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
#include <Ash500Parser.h>
#include <stdlib.h>
bool isParityOk(uint8_t byte, bool parity)
{
bool calculatedParity = false;
for (uint8_t i = 0; i < 8; i++)
{
if ((byte >> i) & 0x01)
{
calculatedParity = !calculatedParity;
}
}
return (calculatedParity == parity);
}
uint32_t Ash500Parser::decodeManchester(void* destination, const void* source, uint32_t bytes)
{
uint8_t* dst = reinterpret_cast<uint8_t*>(destination);
const uint8_t* src = reinterpret_cast<const uint8_t*>(source);
for (uint8_t byte = 0; byte < bytes; byte += 2)
{
uint8_t result = 0;
for (uint8_t bit = 0; bit < 4; bit++)
{
uint8_t bitPair = ((src[byte] >> (bit * 2)) & 0x03);
if (bitPair == 0x01)
{
result |= (1 << (bit + 4));
}
else if (bitPair == 0x02)
{
// ok = 0
}
else {
return byte;
}
}
for (uint8_t bit = 0; bit < 4; bit++)
{
uint8_t duo = ((src[byte + 1] >> (bit * 2)) & 0x03);
if (duo == 0x01)
{
result |= (1 << (bit));
}
else if (duo == 0x02)
{
// ok = 0
}
else {
return byte + 1;
}
}
dst[byte / 2] = result;
}
return bytes;
}
void Ash500Parser::parseData(const void* data)
{
m_error = NoError;
m_serial = 0;
m_temperature = 0;
m_humidity = 0;
uint8_t* raw = (uint8_t*)data;
uint8_t packet[9];
uint8_t parities[2];
packet[0] = raw[0];
packet[1] = (raw[1] << 1) | (raw[2] >> 7);
packet[2] = (raw[2] << 2) | (raw[3] >> 6);
packet[3] = (raw[3] << 3) | (raw[4] >> 5);
packet[4] = (raw[4] << 4) | (raw[5] >> 4);
packet[5] = (raw[5] << 5) | (raw[6] >> 3);
packet[6] = (raw[6] << 6) | (raw[7] >> 2);
packet[7] = (raw[7] << 7) | (raw[8] >> 1);
packet[8] = raw[9];
parities[0] =
(raw[1] & 128)
| (raw[2] & 64)
| (raw[3] & 32)
| (raw[4] & 16)
| (raw[5] & 8)
| (raw[6] & 4)
| (raw[7] & 2)
| (raw[8] & 1);
parities[1] = (raw[10] & 128);
for (int byte = 0; byte < 8; byte++)
{
bool parity = (((parities[0] >> (7 - byte)) & 0x01) == 0x01);
if ((!isParityOk(packet[byte], parity)))
{
m_error = ParityError;
return;
}
}
uint8_t decoded[9];
decoded[0] = packet[0] ^ 0x89;
for (int i = 1; i < 9; i++)
{
decoded[i] = (packet[i-1]+0x24) ^ packet[i];
}
m_serial = decoded[4];
m_humidity = decoded[7];
union {
int16_t temperature;
uint8_t rawBytes[2];
};
rawBytes[0] = decoded[6];
rawBytes[1] = decoded[5] & 0x7F;
m_temperature = temperature;
}
Ash500Parser::Ash500Parser(const void* rawData)
{
if (rawData != NULL)
{
parseData(rawData);
}
}