forked from adafruit/Adafruit_AM2320
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adafruit_AM2320.cpp
321 lines (294 loc) · 10.7 KB
/
Adafruit_AM2320.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*!
* @file Adafruit_AM2320.cpp
*
* @mainpage Adafruit AM2320 Temperature & Humidity Unified Sensor driver
*
* @section intro_sec Introduction
*
* This is the documentation for Adafruit's AM2320 driver for the
* Arduino platform. It is designed specifically to work with the
* Adafruit AM2320 breakout: https://www.adafruit.com/products/xxxx
*
* These sensors use I2C to communicate, 2 pins (SCL+SDA) are required
* to interface with the breakout.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* @section dependencies Dependencies
*
* This library depends on <a
* href="https://github.com/adafruit/Adafruit_Sensor"> Adafruit_Sensor</a> being
* present on your system. Please make sure you have installed the latest
* version before using this library.
*
* @section author Author
*
* Written by Limor Fried for Adafruit Industries.
*
* @section license License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#include "Adafruit_AM2320.h"
/**************************************************************************/
/*!
@brief Instantiates a new AM2320 class
@param theI2C Optional pointer to a TwoWire object that should be used for
I2C communication. Defaults to &Wire.
@param tempSensorId the id that should be used for the temperature sensor.
Defaults to -1
@param humiditySensorId the id that should be used for the humidity sensor.
Defaults to -1
*/
/**************************************************************************/
Adafruit_AM2320::Adafruit_AM2320(TwoWire *theI2C, int32_t tempSensorId,
int32_t humiditySensorId)
: _temp(this, tempSensorId), _humidity(this, humiditySensorId) {
if (i2c_dev)
delete i2c_dev;
i2c_dev = new Adafruit_I2CDevice(0x5C, theI2C); // fixed addr
}
/**************************************************************************/
/*!
@brief Setups the hardware
@return true
*/
/**************************************************************************/
bool Adafruit_AM2320::begin() {
if (!i2c_dev->begin(false))
return false;
// sensor presence check
i2c_dev->detected(); // wake up
delay(10); // wait 10 ms
return i2c_dev->detected(); // check if ACK
}
/**************************************************************************/
/*!
@brief read the temperature from the device
@return the temperature reading as a floating point value
*/
/**************************************************************************/
float Adafruit_AM2320::readTemperature() {
uint16_t t = readRegister16(AM2320_REG_TEMP_H);
float ft;
if (t == 0xFFFF)
return NAN;
// check sign bit - the temperature MSB is signed , bit 0-15 are magnitude
if (t & 0x8000) {
ft = -(int16_t)(t & 0x7fff);
} else {
ft = (int16_t)t;
}
return ft / 10.0;
}
/**************************************************************************/
/*!
@brief read the humidity from the device
@return the humidity reading as a floating point value
*/
/**************************************************************************/
float Adafruit_AM2320::readHumidity() {
uint16_t h = readRegister16(AM2320_REG_HUM_H);
if (h == 0xFFFF)
return NAN;
float fh = h;
return fh / 10.0;
}
/**************************************************************************/
/*!
@brief read 2 bytes from a hardware register
@param reg the register to read
@return the read value as a 2 byte unsigned integer
*/
/**************************************************************************/
uint16_t Adafruit_AM2320::readRegister16(uint8_t reg) {
uint8_t buffer[6] = {0, 0, 0, 0, 0, 0};
// wake up
i2c_dev->write(buffer, 1);
delay(10); // wait 10 ms
// send a command to read register
buffer[0] = AM2320_CMD_READREG;
buffer[1] = reg;
buffer[2] = 2; // 2 bytes
i2c_dev->write(buffer, 3);
delay(2); // wait 2 ms
// 2 bytes preamble, 2 bytes data, 2 bytes CRC
i2c_dev->read(buffer, 6);
if (buffer[0] != 0x03)
return 0xFFFF; // must be 0x03 modbus reply
if (buffer[1] != 2)
return 0xFFFF; // must be 2 bytes reply
uint16_t the_crc = buffer[5];
the_crc <<= 8;
the_crc |= buffer[4];
uint16_t calc_crc = crc16(buffer, 4); // preamble + data
// Serial.print("CRC: 0x"); Serial.println(calc_crc, HEX);
if (the_crc != calc_crc)
return 0xFFFF;
// All good!
uint16_t ret = buffer[2];
ret <<= 8;
ret |= buffer[3];
return ret;
}
/**************************************************************************/
/*!
@brief perfor a CRC check to verify data
@param buffer the pointer to the data to check
@param nbytes the number of bytes to calculate the CRC over
@return the calculated CRC
*/
/**************************************************************************/
uint16_t Adafruit_AM2320::crc16(uint8_t *buffer, uint8_t nbytes) {
uint16_t crc = 0xffff;
for (int i = 0; i < nbytes; i++) {
uint8_t b = buffer[i];
crc ^= b;
for (int x = 0; x < 8; x++) {
if (crc & 0x0001) {
crc >>= 1;
crc ^= 0xA001;
} else {
crc >>= 1;
}
}
}
return crc;
}
/**************************************************************************/
/*!
@brief set the name of the sensor
@param sensor a pointer to the sensor
*/
/**************************************************************************/
void Adafruit_AM2320::setName(sensor_t *sensor) {
strncpy(sensor->name, "AM2320", sizeof(sensor->name) - 1);
}
/**************************************************************************/
/*!
@brief set minimum delay to a fixed value of 2 seconds
@param sensor a pointer to the sensor
*/
/**************************************************************************/
void Adafruit_AM2320::setMinDelay(sensor_t *sensor) {
sensor->min_delay = 2000000L; // 2 seconds (in microseconds)
}
/**************************************************************************/
/*!
@brief create a temperature sensor instance
@param parent the pointer to the parent sensor
@param id the id value for the sensor
*/
/**************************************************************************/
Adafruit_AM2320::Temperature::Temperature(Adafruit_AM2320 *parent, int32_t id)
: _parent(parent), _id(id) {}
/**************************************************************************/
/*!
@brief read the temperature from the device and populate a sensor_event_t
with the value
@param event a pointer to the event to populate
@return true
*/
/**************************************************************************/
bool Adafruit_AM2320::Temperature::getEvent(sensors_event_t *event) {
// Clear event definition.
memset(event, 0, sizeof(sensors_event_t));
// Populate sensor reading values.
event->version = sizeof(sensors_event_t);
event->sensor_id = _id;
event->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
event->timestamp = millis();
event->temperature = _parent->readTemperature();
return true;
}
/**************************************************************************/
/*!
@brief populate a sensor_t with data for this sensor
@param sensor a pointer to the sensor_t to populate
*/
/**************************************************************************/
void Adafruit_AM2320::Temperature::getSensor(sensor_t *sensor) {
// Clear sensor definition.
memset(sensor, 0, sizeof(sensor_t));
// Set sensor name.
_parent->setName(sensor);
// Set version and ID
sensor->version = AM2320_SENSOR_VERSION;
sensor->sensor_id = _id;
// Set type and characteristics.
sensor->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
_parent->setMinDelay(sensor);
// This isn't documented, of course
sensor->max_value = 80.0F;
sensor->min_value = -20.0F;
sensor->resolution = 2.0F;
}
/**************************************************************************/
/*!
@brief create a humidity sensor instance
@param parent the pointer to the parent sensor
@param id the id value for the sensor
*/
/**************************************************************************/
Adafruit_AM2320::Humidity::Humidity(Adafruit_AM2320 *parent, int32_t id)
: _parent(parent), _id(id) {}
/**************************************************************************/
/*!
@brief read the humidity from the device and populate a sensor_event_t with
the value
@param event a pointer to the event to populate
@return true
*/
/**************************************************************************/
bool Adafruit_AM2320::Humidity::getEvent(sensors_event_t *event) {
// Clear event definition.
memset(event, 0, sizeof(sensors_event_t));
// Populate sensor reading values.
event->version = sizeof(sensors_event_t);
event->sensor_id = _id;
event->type = SENSOR_TYPE_RELATIVE_HUMIDITY;
event->timestamp = millis();
event->relative_humidity = _parent->readHumidity();
return true;
}
/**************************************************************************/
/*!
@brief populate a sensor_t with data for this sensor
@param sensor a pointer to the sensor_t to populate
*/
/**************************************************************************/
void Adafruit_AM2320::Humidity::getSensor(sensor_t *sensor) {
// Clear sensor definition.
memset(sensor, 0, sizeof(sensor_t));
// Set sensor name.
_parent->setName(sensor);
// Set version and ID
sensor->version = AM2320_SENSOR_VERSION;
sensor->sensor_id = _id;
// Set type and characteristics.
sensor->type = SENSOR_TYPE_RELATIVE_HUMIDITY;
_parent->setMinDelay(sensor);
// This isn't documented, of course
sensor->max_value = 100.0F;
sensor->min_value = 0.0F;
sensor->resolution = 1.0F;
}