forked from bertrik/LoraWanPmSensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sds011_protocol.cpp
113 lines (104 loc) · 2.35 KB
/
sds011_protocol.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
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "sds011_protocol.h"
// magic header/footer bytes
#define MAGIC1 0xAA
#define MAGIC2 0xAB
/**
Initializes the measurement data state machine.
*/
SDS011Protocol::SDS011Protocol(void)
{
_state = HEAD;
_idx = 0;
_len = 0;
_sum = 0;
}
/**
Processes one byte in the measurement data state machine.
@param[in] b the byte
@return true if a full message was received
*/
bool SDS011Protocol::process_rx(uint8_t b, uint8_t rsp_id)
{
switch (_state) {
// wait for header byte
case HEAD:
if (b == MAGIC1) {
_state = COMMAND;
}
break;
// receive COMMAND byte
case COMMAND:
if (b == rsp_id) {
_sum = 0;
_idx = 0;
_len = 6;
_state = DATA;
} else {
_state = HEAD;
}
break;
// store data
case DATA:
_sum += b;
if (_idx < _len) {
_buf[_idx++] = b;
}
if (_idx == _len) {
_state = CHECK;
}
break;
// store checksum
case CHECK:
if (b == _sum) {
_state = TAIL;
} else {
_state = HEAD;
process_rx(b, rsp_id);
}
break;
// wait for tail byte
case TAIL:
_state = HEAD;
return (b == MAGIC2);
default:
_state = HEAD;
break;
}
return false;
}
/**
Creates a command buffer to send.
@param[out] buf the command buffer, should be at least 19 bytes
@param[in] cmd the command code
@param[in] cmd_len the length of the byte array
@param[in] cmd_data the command data byte array
@return the length of the command buffer, or 0 if no command could be constructed
*/
int SDS011Protocol::build_tx(uint8_t *buf, uint8_t cmd, size_t cmd_len, const uint8_t *cmd_data)
{
int idx = 0;
buf[idx++] = MAGIC1;
buf[idx++] = 0xB4;
buf[idx++] = cmd;
for (int i = 0; i < 12; i++) {
buf[idx++] = (i < cmd_len) ? cmd_data[i] : 0;
}
buf[idx++] = 0xFF;
buf[idx++] = 0xFF;
// calculate check
uint8_t sum = 0;
for (int i = 2; i < 17; i++) {
sum += buf[i];
}
buf[idx++] = sum;
buf[idx++] = MAGIC2;
return idx;
}
size_t SDS011Protocol::get_data(uint8_t *rsp)
{
memcpy(rsp, _buf, 6);
return 6;
}