27
27
28
28
#include " Adafruit_HTU21DF.h"
29
29
30
- #if defined(__AVR__)
31
- #include < util/delay.h>
32
- #endif
33
-
34
30
/* *
35
31
* Constructor for the HTU21DF driver.
36
32
*/
@@ -42,60 +38,64 @@ Adafruit_HTU21DF::Adafruit_HTU21DF() {
42
38
43
39
/* *
44
40
* Initialises the I2C transport, and configures the IC for normal operation.
45
- *
41
+ * @param theWire Pointer to TwoWire I2C object, uses &Wire by default
46
42
* @return true (1) if the device was successfully initialised, otherwise
47
43
* false (0).
48
44
*/
49
- boolean Adafruit_HTU21DF::begin (void ) {
50
- Wire.begin ();
45
+ bool Adafruit_HTU21DF::begin (TwoWire *theWire) {
46
+ if (i2c_dev) {
47
+ delete i2c_dev;
48
+ }
49
+ i2c_dev = new Adafruit_I2CDevice (HTU21DF_I2CADDR, theWire);
50
+
51
+ if (!i2c_dev->begin ()) {
52
+ return false ;
53
+ }
51
54
52
55
reset ();
53
56
54
- Wire.beginTransmission (HTU21DF_I2CADDR);
55
- Wire.write (HTU21DF_READREG);
56
- Wire.endTransmission ();
57
- Wire.requestFrom (HTU21DF_I2CADDR, 1 );
58
- return (Wire.read () == 0x2 ); // after reset should be 0x2
57
+ Adafruit_BusIO_Register reg =
58
+ Adafruit_BusIO_Register (i2c_dev, HTU21DF_READREG);
59
+
60
+ return (reg.read () == 0x2 ); // after reset should be 0x2
59
61
}
60
62
61
63
/* *
62
64
* Sends a 'reset' request to the HTU21DF, followed by a 15ms delay.
63
65
*/
64
66
void Adafruit_HTU21DF::reset (void ) {
65
- Wire. beginTransmission (HTU21DF_I2CADDR) ;
66
- Wire. write (HTU21DF_RESET );
67
- Wire. endTransmission ();
67
+ uint8_t cmd = HTU21DF_RESET ;
68
+ i2c_dev-> write (&cmd, 1 );
69
+
68
70
delay (15 );
69
71
}
70
72
71
73
/* *
72
74
* Performs a single temperature conversion in degrees Celsius.
73
75
*
74
76
* @return a single-precision (32-bit) float value indicating the measured
75
- * temperature in degrees Celsius.
77
+ * temperature in degrees Celsius or NAN on failure .
76
78
*/
77
79
float Adafruit_HTU21DF::readTemperature (void ) {
78
80
// OK lets ready!
79
- Wire.beginTransmission (HTU21DF_I2CADDR);
80
- Wire.write (HTU21DF_READTEMP);
81
- Wire.endTransmission ();
81
+ uint8_t cmd = HTU21DF_READTEMP;
82
+ if (!i2c_dev->write (&cmd, 1 )) {
83
+ return NAN;
84
+ }
82
85
83
86
delay (50 ); // add delay between request and actual read!
84
87
85
- uint8_t count = Wire.requestFrom (HTU21DF_I2CADDR, 3 );
86
-
87
- /* Make sure we got 3 bytes back. */
88
- if (count != 3 ) {
89
- return 0 .0f ;
88
+ uint8_t buf[3 ];
89
+ if (!i2c_dev->read (buf, 3 )) {
90
+ return NAN;
90
91
}
91
92
92
93
/* Read 16 bits of data, dropping the last two status bits. */
93
- uint16_t t = Wire. read () ;
94
+ uint16_t t = buf[ 0 ] ;
94
95
t <<= 8 ;
95
- t |= Wire. read () & 0b11111100 ;
96
+ t |= buf[ 1 ] & 0b11111100 ;
96
97
97
- uint8_t crc = Wire.read ();
98
- (void )crc;
98
+ // 3rd byte is the CRC
99
99
100
100
float temp = t;
101
101
temp *= 175 .72f ;
@@ -116,28 +116,25 @@ float Adafruit_HTU21DF::readTemperature(void) {
116
116
*/
117
117
float Adafruit_HTU21DF::readHumidity (void ) {
118
118
/* Prepare the I2C request. */
119
- Wire.beginTransmission (HTU21DF_I2CADDR);
120
- Wire.write (HTU21DF_READHUM);
121
- Wire.endTransmission ();
119
+ uint8_t cmd = HTU21DF_READHUM;
120
+ if (!i2c_dev->write (&cmd, 1 )) {
121
+ return NAN;
122
+ }
122
123
123
124
/* Wait a bit for the conversion to complete. */
124
125
delay (50 );
125
126
126
- /* Read the conversion results. */
127
- uint8_t count = Wire.requestFrom (HTU21DF_I2CADDR, 3 );
128
-
129
- /* Make sure we got 3 bytes back. */
130
- if (count != 3 ) {
131
- return 0 .0f ;
127
+ uint8_t buf[3 ];
128
+ if (!i2c_dev->read (buf, 3 )) {
129
+ return NAN;
132
130
}
133
131
134
132
/* Read 16 bits of data, dropping the last two status bits. */
135
- uint16_t h = Wire. read () ;
133
+ uint16_t h = buf[ 0 ] ;
136
134
h <<= 8 ;
137
- h |= Wire. read () & 0b11111100 ;
135
+ h |= buf[ 1 ] & 0b11111100 ;
138
136
139
- uint8_t crc = Wire.read ();
140
- (void )crc;
137
+ // 3rd byte is the CRC
141
138
142
139
float hum = h;
143
140
hum *= 125 .0f ;
0 commit comments