-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSDS021.cpp
More file actions
246 lines (182 loc) · 4.44 KB
/
Copy pathSDS021.cpp
File metadata and controls
246 lines (182 loc) · 4.44 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "SDS021.h"
SDS021::SDS021()
{
ID_ = 0xFFFF;
PM2_5_ = 0.0;
PM10_ = 0.0;
Mode_ = false;
State_ = true;
Interval_ = 0;
Firmware_ = { 15, 7, 10 };
}
SDS021::~SDS021() {}
int SDS021::ID()
{
return ID_;
}
void SDS021::ID(int id)
{
// Compiles the message
byte* buffer = MakeMessage(EAction::Id, true, ID_);
buffer[13] = id >> 8;
buffer[14] = id & 0xFF;
WriteMessage(buffer);
// Waits for output buffer to clear and response to arrive
Serial.flush();
delay(kInputDelay_);
// Checks input cache for response
Update();
}
bool SDS021::PassiveMode()
{
return Mode_;
}
void SDS021::PassiveMode(bool passive)
{
// Compiles the message
byte* buffer = MakeMessage(EAction::Mode, true, ID_);
buffer[4] = passive;
WriteMessage(buffer);
// Waits for output buffer to clear and response to arrive
Serial.flush();
delay(kInputDelay_);
// Checks input cache for response
Update();
}
bool SDS021::Awake()
{
return State_;
}
void SDS021::Awake(bool working)
{
// Compiles the message
byte* buffer = MakeMessage(EAction::State, true, ID_);
buffer[4] = working;
WriteMessage(buffer);
// Waits for output buffer to clear and response to arrive
Serial.flush();
delay(kInputDelay_);
// Checks input cache for response
Update();
}
byte SDS021::Interval()
{
return Interval_;
}
void SDS021::Interval(byte minutes)
{
// Clamps minutes between 0-30
if (minutes > 30)
minutes = 30;
// Compiles the message
byte* buffer = MakeMessage(EAction::Interval, true, ID_);
buffer[4] = minutes;
WriteMessage(buffer);
// Waits for output buffer to clear and response to arrive
Serial.flush();
delay(kInputDelay_);
// Checks input cache for response
Update();
}
float SDS021::PM2_5()
{
return PM2_5_;
}
float SDS021::PM10()
{
return PM10_;
}
void SDS021::Query()
{
byte* buffer = MakeMessage(EAction::Query, false, ID_);
WriteMessage(buffer);
// Waits for output buffer to clear and response to arrive
Serial.flush();
delay(kInputDelay_);
// Checks input cache for response
Update();
}
bool SDS021::Update()
{
bool updated = false;
byte buffer[kInputLength_];
// Check Serial input buffer for a message
while (Serial.available() >= kInputLength_)
{
// Clear the local buffer
for (int i = 0; i < kInputLength_; i++)
buffer[i] = 0;
// Crawl through Serial input buffer for a start byte
while (Serial.peek() != (byte)EMessage::Head)
Serial.read();
// Read the next message
Serial.readBytes(buffer, kInputLength_);
// Update local values if message is valid
if (CheckSum(buffer))
{
ECommandId command_id = (ECommandId)buffer[1];
// Parse data frame
if (command_id == ECommandId::Data)
{
PM2_5_ = (float)CrunchBytes(buffer[3], buffer[2]) / 10;
PM10_ = (float)CrunchBytes(buffer[5], buffer[4]) / 10;
}
// Parse reply frame
else if (command_id == ECommandId::Reply)
{
EAction action = (EAction)buffer[2];
if (action == EAction::Mode)
Mode_ = buffer[4];
else if (action == EAction::State)
State_ = buffer[4];
else if (action == EAction::Interval)
Interval_ = buffer[4];
else if (action == EAction::Version)
Firmware_ = { buffer[3], buffer[4], buffer[5] };
}
// Update device ID
//ID_ = CrunchBytes(buffer[6], buffer[7]);
updated = true;
}
}
return updated;
}
byte* SDS021::MakeMessage(EAction action, bool set, int address)
{
// Creates a new byte array of zeroes
byte* buffer = new byte[kOutputLength_] { 0 };
// Sets common message values
buffer[0] = (byte)EMessage::Head;
buffer[1] = (byte)ECommandId::Command;
buffer[2] = (byte)action;
buffer[3] = (byte)set;
buffer[15] = address >> 8;
buffer[16] = address & 0xFF;
buffer[kOutputLength_ - 1] = (byte)EMessage::Tail;
return buffer;
}
void SDS021::WriteMessage(byte* buffer)
{
// Calculates and stores checksum in output buffer
buffer[17] = calcCheckSum(buffer, 2, 17);
Serial.write(buffer, kOutputLength_);
// Waits for software serial to finish sending message
delay(20); // (19 bytes * 8 bits * 0.104 ms = 15.808 ms minimum)
delete[] buffer;
}
byte SDS021::calcCheckSum(byte* buffer, int start_idx, int stop_idx)
{
int chk = 0;
for (int i = start_idx; i < stop_idx; i++)
chk += buffer[i];
chk &= 0xFF;
return chk;
}
bool SDS021::CheckSum(byte* buffer, int start_idx, int stop_idx)
{
return calcCheckSum(buffer, start_idx, stop_idx) == buffer[stop_idx];
}
int SDS021::CrunchBytes(byte high_byte, byte low_byte)
{
return ((int)high_byte << 8) + (int)low_byte;
}