Skip to content

Commit bb1e440

Browse files
committed
Adds HTU31D
Closes #721
1 parent c67ea20 commit bb1e440

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

src/components/i2c/controller.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ static const std::map<std::string, FnCreateI2CDriver> I2cFactory = {
123123
const char *driver_name) -> drvBase * {
124124
return new drvHtu21d(i2c, addr, mux_channel, driver_name);
125125
}},
126+
{"htu31d",
127+
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
128+
const char *driver_name) -> drvBase * {
129+
return new drvHtu31d(i2c, addr, mux_channel, driver_name);
130+
}},
126131
{"ina219",
127132
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
128133
const char *driver_name) -> drvBase * {

src/components/i2c/controller.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "drivers/drvDs2484.h"
3131
#include "drivers/drvEns160.h"
3232
#include "drivers/drvHts221.h"
33+
#include "drivers/drvHtu31d.h"
3334
#include "drivers/drvHtu21d.h"
3435
#include "drivers/drvIna219.h"
3536
#include "drivers/drvLc709203f.h"
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*!
2+
* @file drvHtu31d.h
3+
*
4+
* Device driver for an HTU31D Humidity and Temperature sensor.
5+
*/
6+
7+
#ifndef DRV_HTU31D_H
8+
#define DRV_HTU31D_H
9+
10+
#include "drvBase.h"
11+
#include <Adafruit_HTU31D.h>
12+
13+
/**************************************************************************/
14+
/*!
15+
@brief Class that provides a sensor driver for the HTU31D humidity and
16+
temperature sensor.
17+
*/
18+
/**************************************************************************/
19+
class drvHtu31d : public drvBase {
20+
21+
public:
22+
/*******************************************************************************/
23+
/*!
24+
@brief Constructor for an HTU31D sensor.
25+
@param i2c
26+
The I2C interface.
27+
@param sensorAddress
28+
7-bit device address.
29+
@param mux_channel
30+
The I2C multiplexer channel.
31+
@param driver_name
32+
The name of the driver.
33+
*/
34+
/*******************************************************************************/
35+
drvHtu31d(TwoWire *i2c, uint16_t sensorAddress, uint32_t mux_channel,
36+
const char *driver_name)
37+
: drvBase(i2c, sensorAddress, mux_channel, driver_name) {
38+
// Initialization handled by drvBase constructor
39+
}
40+
41+
/*******************************************************************************/
42+
/*!
43+
@brief Destructor for an HTU31D sensor.
44+
*/
45+
/*******************************************************************************/
46+
~drvHtu31d() { delete _htu31d; }
47+
48+
/*******************************************************************************/
49+
/*!
50+
@brief Initializes the HTU31D sensor and begins I2C.
51+
@returns True if initialized successfully, False otherwise.
52+
53+
*/
54+
/*******************************************************************************/
55+
bool begin() {
56+
// attempt to initialize the HTU31D using the I2C interface
57+
_htu31d = new Adafruit_HTU31D();
58+
return _htu31d->begin(_address, _i2c);
59+
}
60+
61+
/*******************************************************************************/
62+
/*!
63+
@brief Gets the HTU31D's current temperature.
64+
@param tempEvent
65+
Pointer to an Adafruit_Sensor event.
66+
@returns True if the temperature was obtained successfully, False
67+
otherwise.
68+
*/
69+
/*******************************************************************************/
70+
bool getEventAmbientTemp(sensors_event_t *tempEvent) {
71+
return _htu31d->getEvent(nullptr, tempEvent);
72+
}
73+
74+
/*******************************************************************************/
75+
/*!
76+
@brief Gets the HTU31D's current humidity.
77+
@param humidEvent
78+
Pointer to an Adafruit_Sensor event.
79+
@returns True if the humidity was obtained successfully, False
80+
otherwise.
81+
*/
82+
/*******************************************************************************/
83+
bool getEventRelativeHumidity(sensors_event_t *humidEvent) {
84+
return _htu31d->getEvent(humidEvent, nullptr);
85+
}
86+
87+
protected:
88+
Adafruit_HTU31D *_htu31d; ///< Pointer to an HTU31D object
89+
};
90+
#endif // DRV_HTU31D_H

0 commit comments

Comments
 (0)