-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmhz19.cpp
More file actions
94 lines (83 loc) · 1.84 KB
/
mhz19.cpp
File metadata and controls
94 lines (83 loc) · 1.84 KB
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
#include <stdint.h>
#include <stdbool.h>
#define CMD_SIZE 9
typedef enum {
START_BYTE,
COMMAND,
DATA,
CHECK
} state_t;
/**
Prepares a command buffer to send to an mhz19.
@param data tx data
@param buffer the tx buffer to fill
@param size the size of the tx buffer
@return number of bytes in buffer
*/
int prepare_tx(uint8_t cmd, const uint8_t *data, uint8_t buffer[], int size)
{
if (size < CMD_SIZE) {
return 0;
}
// create command buffer
buffer[0] = 0xFF;
buffer[1] = 0x01;
buffer[2] = cmd;
for (int i = 3; i < 8; i++) {
buffer[i] = *data++;
}
// calculate checksum
uint8_t check = 0;
for (int i = 0; i < 8; i++) {
check += buffer[i];
}
buffer[8] = 255 - check;
return CMD_SIZE;
}
/**
Processes one received byte.
@param b the byte
@param cmd the command code
@param data the buffer to contain a received message
@return true if a full message was received, false otherwise
*/
bool process_rx(uint8_t b, uint8_t cmd, uint8_t data[])
{
static uint8_t check = 0;
static int idx = 0;
static int len = 0;
static state_t state = START_BYTE;
// update checksum
check += b;
switch (state) {
case START_BYTE:
if (b == 0xFF) {
check = 0;
state = COMMAND;
}
break;
case COMMAND:
if (b == cmd) {
idx = 0;
len = 6;
state = DATA;
} else {
state = START_BYTE;
process_rx(b, cmd, data);
}
break;
case DATA:
data[idx++] = b;
if (idx == len) {
state = CHECK;
}
break;
case CHECK:
state = START_BYTE;
return (check == 0);
default:
state = START_BYTE;
break;
}
return false;
}