-
Notifications
You must be signed in to change notification settings - Fork 1
/
sg35.cpp
226 lines (197 loc) · 5.05 KB
/
sg35.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
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
#include "pxt.h"
using namespace pxt;
//#include "Arduino.h"
#include "sg35.h"
uint16_t makeWord(uint8_t b1, uint8_t b2)
{
return (b1 << 8) | b2;
}
PMS::PMS()
{
//this->_stream = &stream;
}
void PMS::begin()
{
_stream->begin(9600);
for (uint8_t i = 0; i < 4; i++)
{
_stream->pinMode(i, 1);
_stream->digitalWrite(i, 1);
}
}
// Standby mode. For low power consumption and prolong the life of the sensor.
void PMS::sleep()
{
uint8_t command[] = {0x42, 0x4D, 0xE4, 0x00, 0x00, 0x01, 0x73};
_stream->write(command, sizeof(command));
}
// Operating mode. Stable data should be got at least 30 seconds after the sensor wakeup from the sleep mode because of the fan's performance.
void PMS::wakeUp()
{
uint8_t command[] = {0x42, 0x4D, 0xE4, 0x00, 0x01, 0x01, 0x74};
_stream->write(command, sizeof(command));
}
// Active mode. Default mode after power up. In this mode sensor would send serial data to the host automatically.
void PMS::activeMode()
{
uint8_t command[] = {0x42, 0x4D, 0xE1, 0x00, 0x01, 0x01, 0x71};
_stream->write(command, sizeof(command));
_mode = MODE_ACTIVE;
}
// Passive mode. In this mode sensor would send serial data to the host only for request.
void PMS::passiveMode()
{
uint8_t command[] = {0x42, 0x4D, 0xE1, 0x00, 0x00, 0x01, 0x70};
_stream->write(command, sizeof(command));
_mode = MODE_PASSIVE;
}
// Request read in Passive Mode.
void PMS::requestRead()
{
if (_mode == MODE_PASSIVE)
{
uint8_t command[] = {0x42, 0x4D, 0xE2, 0x00, 0x00, 0x01, 0x71};
_stream->write(command, sizeof(command));
}
}
// Non-blocking function for parse response.
bool PMS::read(DATA &data)
{
_data = &data;
loop();
return _status == STATUS_OK;
}
// Blocking function for parse response. Default timeout is 1s.
bool PMS::readUntil(DATA &data, uint16_t timeout)
{
_data = &data;
uint32_t start = system_timer_current_time();
do
{
loop();
if (_status == STATUS_OK)
break;
} while (system_timer_current_time() - start < timeout);
//_stream->flush();
return _status == STATUS_OK;
}
void PMS::loop()
{
_status = STATUS_WAITING;
if (_stream->available())
{
uint8_t ch = _stream->read();
switch (_index)
{
case 0:
if (ch != 0x42)
{
return;
}
_calculatedChecksum = ch;
break;
case 1:
if (ch != 0x4D)
{
_index = 0;
return;
}
_calculatedChecksum += ch;
break;
case 2:
_calculatedChecksum += ch;
_frameLen = ch << 8;
break;
case 3:
_frameLen |= ch;
// Unsupported sensor, different frame length, transmission error e.t.c.
if (_frameLen != 2 * 9 + 2 && _frameLen != 2 * 13 + 2)
{
_index = 0;
return;
}
_calculatedChecksum += ch;
break;
default:
if (_index == _frameLen + 2)
{
_checksum = ch << 8;
}
else if (_index == _frameLen + 2 + 1)
{
_checksum |= ch;
if (_calculatedChecksum == _checksum)
{
_status = STATUS_OK;
// Standard Particles, CF=1.
_data->PM_SP_UG_1_0 = makeWord(_payload[0], _payload[1]);
_data->PM_SP_UG_2_5 = makeWord(_payload[2], _payload[3]);
_data->PM_SP_UG_10_0 = makeWord(_payload[4], _payload[5]);
// Atmospheric Environment.
_data->PM_AE_UG_1_0 = makeWord(_payload[6], _payload[7]);
_data->PM_AE_UG_2_5 = makeWord(_payload[8], _payload[9]);
_data->PM_AE_UG_10_0 = makeWord(_payload[10], _payload[11]);
}
_index = 0;
return;
}
else
{
_calculatedChecksum += ch;
uint8_t payloadIndex = _index - 4;
// Payload is common to all sensors (first 2x6 bytes).
if (payloadIndex < sizeof(_payload))
{
_payload[payloadIndex] = ch;
}
}
break;
}
_index++;
}
}
namespace sg35
{
static PMS *pms = new PMS();
PMS::DATA data;
//%
bool read()
{
return pms->read(data);
}
//%
void begin()
{
pms->begin();
}
//%
void activeMode()
{
pms->activeMode();
}
//%
void passiveMode()
{
pms->passiveMode();
}
//%
uint16_t pm1()
{
return data.PM_AE_UG_1_0;
}
//%
uint16_t pm25()
{
return data.PM_AE_UG_2_5;
}
//%
uint16_t pm10()
{
return data.PM_AE_UG_10_0;
}
//%
void onDataReceived(Action body)
{
registerWithDal(0, 1, body);
}
} // namespace SG35