Skip to content

Commit

Permalink
Merge pull request #21 from adafruit/busio
Browse files Browse the repository at this point in the history
busioify + doxyclang
  • Loading branch information
ladyada authored Dec 14, 2020
2 parents 3fe1d4d + 7b93d2f commit b0567fe
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 49 deletions.
79 changes: 38 additions & 41 deletions Adafruit_HTU21DF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@

#include "Adafruit_HTU21DF.h"

#if defined(__AVR__)
#include <util/delay.h>
#endif

/**
* Constructor for the HTU21DF driver.
*/
Expand All @@ -42,60 +38,64 @@ Adafruit_HTU21DF::Adafruit_HTU21DF() {

/**
* Initialises the I2C transport, and configures the IC for normal operation.
*
* @param theWire Pointer to TwoWire I2C object, uses &Wire by default
* @return true (1) if the device was successfully initialised, otherwise
* false (0).
*/
boolean Adafruit_HTU21DF::begin(void) {
Wire.begin();
bool Adafruit_HTU21DF::begin(TwoWire *theWire) {
if (i2c_dev) {
delete i2c_dev;
}
i2c_dev = new Adafruit_I2CDevice(HTU21DF_I2CADDR, theWire);

if (!i2c_dev->begin()) {
return false;
}

reset();

Wire.beginTransmission(HTU21DF_I2CADDR);
Wire.write(HTU21DF_READREG);
Wire.endTransmission();
Wire.requestFrom(HTU21DF_I2CADDR, 1);
return (Wire.read() == 0x2); // after reset should be 0x2
Adafruit_BusIO_Register reg =
Adafruit_BusIO_Register(i2c_dev, HTU21DF_READREG);

return (reg.read() == 0x2); // after reset should be 0x2
}

/**
* Sends a 'reset' request to the HTU21DF, followed by a 15ms delay.
*/
void Adafruit_HTU21DF::reset(void) {
Wire.beginTransmission(HTU21DF_I2CADDR);
Wire.write(HTU21DF_RESET);
Wire.endTransmission();
uint8_t cmd = HTU21DF_RESET;
i2c_dev->write(&cmd, 1);

delay(15);
}

/**
* Performs a single temperature conversion in degrees Celsius.
*
* @return a single-precision (32-bit) float value indicating the measured
* temperature in degrees Celsius.
* temperature in degrees Celsius or NAN on failure.
*/
float Adafruit_HTU21DF::readTemperature(void) {
// OK lets ready!
Wire.beginTransmission(HTU21DF_I2CADDR);
Wire.write(HTU21DF_READTEMP);
Wire.endTransmission();
uint8_t cmd = HTU21DF_READTEMP;
if (!i2c_dev->write(&cmd, 1)) {
return NAN;
}

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

uint8_t count = Wire.requestFrom(HTU21DF_I2CADDR, 3);

/* Make sure we got 3 bytes back. */
if (count != 3) {
return 0.0f;
uint8_t buf[3];
if (!i2c_dev->read(buf, 3)) {
return NAN;
}

/* Read 16 bits of data, dropping the last two status bits. */
uint16_t t = Wire.read();
uint16_t t = buf[0];
t <<= 8;
t |= Wire.read() & 0b11111100;
t |= buf[1] & 0b11111100;

uint8_t crc = Wire.read();
(void)crc;
// 3rd byte is the CRC

float temp = t;
temp *= 175.72f;
Expand All @@ -116,28 +116,25 @@ float Adafruit_HTU21DF::readTemperature(void) {
*/
float Adafruit_HTU21DF::readHumidity(void) {
/* Prepare the I2C request. */
Wire.beginTransmission(HTU21DF_I2CADDR);
Wire.write(HTU21DF_READHUM);
Wire.endTransmission();
uint8_t cmd = HTU21DF_READHUM;
if (!i2c_dev->write(&cmd, 1)) {
return NAN;
}

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

/* Read the conversion results. */
uint8_t count = Wire.requestFrom(HTU21DF_I2CADDR, 3);

/* Make sure we got 3 bytes back. */
if (count != 3) {
return 0.0f;
uint8_t buf[3];
if (!i2c_dev->read(buf, 3)) {
return NAN;
}

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

uint8_t crc = Wire.read();
(void)crc;
// 3rd byte is the CRC

float hum = h;
hum *= 125.0f;
Expand Down
12 changes: 4 additions & 8 deletions Adafruit_HTU21DF.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
#ifndef _ADAFRUIT_HTU21DF_H
#define _ADAFRUIT_HTU21DF_H

#if (ARDUINO >= 100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "Wire.h"
#include <Adafruit_BusIO_Register.h>
#include <Adafruit_I2CDevice.h>

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

boolean begin(void);
bool begin(TwoWire *theWire = &Wire);
float readTemperature(void);
float readHumidity(void);
void reset(void);

private:
boolean readData(void);
Adafruit_I2CDevice *i2c_dev = NULL; ///< Pointer to I2C bus interface
float _last_humidity, _last_temp;
};

Expand Down
1 change: 1 addition & 0 deletions library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ paragraph=Arduino library for the HTU21D-F sensors in the Adafruit shop
category=Sensors
url=https://github.com/adafruit/Adafruit_HTU21DF_Library
architectures=*
depends=Adafruit Unified Sensor, Adafruit BusIO

0 comments on commit b0567fe

Please sign in to comment.