Skip to content

Commit b0567fe

Browse files
authored
Merge pull request #21 from adafruit/busio
busioify + doxyclang
2 parents 3fe1d4d + 7b93d2f commit b0567fe

File tree

3 files changed

+43
-49
lines changed

3 files changed

+43
-49
lines changed

Adafruit_HTU21DF.cpp

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@
2727

2828
#include "Adafruit_HTU21DF.h"
2929

30-
#if defined(__AVR__)
31-
#include <util/delay.h>
32-
#endif
33-
3430
/**
3531
* Constructor for the HTU21DF driver.
3632
*/
@@ -42,60 +38,64 @@ Adafruit_HTU21DF::Adafruit_HTU21DF() {
4238

4339
/**
4440
* Initialises the I2C transport, and configures the IC for normal operation.
45-
*
41+
* @param theWire Pointer to TwoWire I2C object, uses &Wire by default
4642
* @return true (1) if the device was successfully initialised, otherwise
4743
* false (0).
4844
*/
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+
}
5154

5255
reset();
5356

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
5961
}
6062

6163
/**
6264
* Sends a 'reset' request to the HTU21DF, followed by a 15ms delay.
6365
*/
6466
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+
6870
delay(15);
6971
}
7072

7173
/**
7274
* Performs a single temperature conversion in degrees Celsius.
7375
*
7476
* @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.
7678
*/
7779
float Adafruit_HTU21DF::readTemperature(void) {
7880
// 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+
}
8285

8386
delay(50); // add delay between request and actual read!
8487

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;
9091
}
9192

9293
/* Read 16 bits of data, dropping the last two status bits. */
93-
uint16_t t = Wire.read();
94+
uint16_t t = buf[0];
9495
t <<= 8;
95-
t |= Wire.read() & 0b11111100;
96+
t |= buf[1] & 0b11111100;
9697

97-
uint8_t crc = Wire.read();
98-
(void)crc;
98+
// 3rd byte is the CRC
9999

100100
float temp = t;
101101
temp *= 175.72f;
@@ -116,28 +116,25 @@ float Adafruit_HTU21DF::readTemperature(void) {
116116
*/
117117
float Adafruit_HTU21DF::readHumidity(void) {
118118
/* 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+
}
122123

123124
/* Wait a bit for the conversion to complete. */
124125
delay(50);
125126

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;
132130
}
133131

134132
/* Read 16 bits of data, dropping the last two status bits. */
135-
uint16_t h = Wire.read();
133+
uint16_t h = buf[0];
136134
h <<= 8;
137-
h |= Wire.read() & 0b11111100;
135+
h |= buf[1] & 0b11111100;
138136

139-
uint8_t crc = Wire.read();
140-
(void)crc;
137+
// 3rd byte is the CRC
141138

142139
float hum = h;
143140
hum *= 125.0f;

Adafruit_HTU21DF.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@
55
#ifndef _ADAFRUIT_HTU21DF_H
66
#define _ADAFRUIT_HTU21DF_H
77

8-
#if (ARDUINO >= 100)
9-
#include "Arduino.h"
10-
#else
11-
#include "WProgram.h"
12-
#endif
13-
#include "Wire.h"
8+
#include <Adafruit_BusIO_Register.h>
9+
#include <Adafruit_I2CDevice.h>
1410

1511
/** Default I2C address for the HTU21D. */
1612
#define HTU21DF_I2CADDR (0x40)
@@ -37,13 +33,13 @@ class Adafruit_HTU21DF {
3733
public:
3834
Adafruit_HTU21DF();
3935

40-
boolean begin(void);
36+
bool begin(TwoWire *theWire = &Wire);
4137
float readTemperature(void);
4238
float readHumidity(void);
4339
void reset(void);
4440

4541
private:
46-
boolean readData(void);
42+
Adafruit_I2CDevice *i2c_dev = NULL; ///< Pointer to I2C bus interface
4743
float _last_humidity, _last_temp;
4844
};
4945

library.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ paragraph=Arduino library for the HTU21D-F sensors in the Adafruit shop
77
category=Sensors
88
url=https://github.com/adafruit/Adafruit_HTU21DF_Library
99
architectures=*
10+
depends=Adafruit Unified Sensor, Adafruit BusIO

0 commit comments

Comments
 (0)