Skip to content

Commit 19b58a8

Browse files
committed
Add HDC302x
1 parent 57a0b89 commit 19b58a8

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

platformio.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ lib_deps =
3434
adafruit/Adafruit HTS221
3535
adafruit/Adafruit HTU21DF Library
3636
adafruit/Adafruit HTU31D Library
37+
adafruit/Adafruit HDC302x
3738
adafruit/Adafruit LTR390 Library
3839
adafruit/Adafruit LTR329 and LTR303
3940
adafruit/Adafruit PCT2075

src/components/i2c/controller.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ static const std::map<std::string, FnCreateI2CDriver> I2cFactory = {
128128
const char *driver_name) -> drvBase * {
129129
return new drvHtu31d(i2c, addr, mux_channel, driver_name);
130130
}},
131+
{"hdc302x",
132+
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
133+
const char *driver_name) -> drvBase * {
134+
return new drvHdc302x(i2c, addr, mux_channel, driver_name);
135+
}},
131136
{"ina219",
132137
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
133138
const char *driver_name) -> drvBase * {

src/components/i2c/controller.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "drivers/drvDps310.h"
3030
#include "drivers/drvDs2484.h"
3131
#include "drivers/drvEns160.h"
32+
#include "drivers/drvHdc302x.h"
3233
#include "drivers/drvHts221.h"
3334
#include "drivers/drvHtu31d.h"
3435
#include "drivers/drvHtu21d.h"
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*!
2+
* @file drvHdc302x.h
3+
*
4+
* Device driver for an HDC302X Humidity and Temperature sensor.
5+
*/
6+
7+
#ifndef DRV_HDC302X_H
8+
#define DRV_HDC302X_H
9+
10+
#include "drvBase.h"
11+
#include <Adafruit_HDC302x.h>
12+
13+
/**************************************************************************/
14+
/*!
15+
@brief Class that provides a sensor driver for the HDC302X humidity and
16+
temperature sensor. This implementation uses the 1 Hz data rate.
17+
*/
18+
/**************************************************************************/
19+
class drvHdc302x : public drvBase {
20+
21+
public:
22+
/*******************************************************************************/
23+
/*!
24+
@brief Constructor for an HDC302X 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+
drvHdc302x(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 HDC302X sensor.
44+
*/
45+
/*******************************************************************************/
46+
~drvHdc302x() { delete _hdc302x; }
47+
48+
/*******************************************************************************/
49+
/*!
50+
@brief Initializes the HDC302X sensor and begins I2C.
51+
@returns True if initialized successfully, False otherwise.
52+
53+
*/
54+
/*******************************************************************************/
55+
bool begin() {
56+
// attempt to initialize the HDC302X using the I2C interface
57+
_hdc302x = new Adafruit_HDC302x();
58+
if (!_hdc302x->begin(_address, _i2c))
59+
return false;
60+
61+
// set the HDC302X's data rate to 1 Hz lowest noise
62+
_hdc302x->setAutoMode(EXIT_AUTO_MODE);
63+
// discard first reading (It returned -45c for me once)
64+
_hdc302x->readTemperatureHumidityOnDemand(_temp, _humidity,
65+
TRIGGERMODE_LP0);
66+
return true;
67+
}
68+
69+
/*******************************************************************************/
70+
/*!
71+
@brief Reads the HDC302X's temperature and humidity data.
72+
@returns True if the data was read successfully, False otherwise.
73+
*/
74+
/*******************************************************************************/
75+
bool ReadSensorData() {
76+
uint16_t status = _hdc302x->readStatus();
77+
if (status & 0x0010) {
78+
WS_DEBUG_PRINTLN(F("Device Reset Detected"));
79+
return false;
80+
}
81+
82+
if (status & 0x0001) {
83+
WS_DEBUG_PRINTLN(
84+
F("Checksum Verification Fail (incorrect checksum received)"));
85+
return false;
86+
}
87+
88+
if (!_hdc302x->readTemperatureHumidityOnDemand(_temp, _humidity,
89+
TRIGGERMODE_LP0)) {
90+
WS_DEBUG_PRINTLN(F("Failed to read temperature and humidity."));
91+
return false;
92+
}
93+
return true;
94+
}
95+
96+
/*******************************************************************************/
97+
/*!
98+
@brief Gets the HDC302X's current temperature.
99+
@param tempEvent
100+
Pointer to an Adafruit_Sensor event.
101+
@returns True if the temperature was obtained successfully, False
102+
otherwise.
103+
*/
104+
/*******************************************************************************/
105+
bool getEventAmbientTemp(sensors_event_t *tempEvent) {
106+
if (ReadSensorData() == false)
107+
return false;
108+
tempEvent->temperature = _temp;
109+
return true;
110+
}
111+
112+
/*******************************************************************************/
113+
/*!
114+
@brief Gets the HDC302X's current humidity.
115+
@param humidEvent
116+
Pointer to an Adafruit_Sensor event.
117+
@returns True if the humidity was obtained successfully, False
118+
otherwise.
119+
*/
120+
/*******************************************************************************/
121+
bool getEventRelativeHumidity(sensors_event_t *humidEvent) {
122+
if (ReadSensorData() == false)
123+
return false;
124+
humidEvent->relative_humidity = _humidity;
125+
return true;
126+
}
127+
128+
protected:
129+
Adafruit_HDC302x *_hdc302x; ///< Pointer to an HDC302X object
130+
double _temp = 0.0; ///< Holds data for the HDC302X's temperature sensor
131+
double _humidity = 0.0; ///< Holds data for the HDC302X's humidity sensor
132+
};
133+
#endif // DRV_HDC302X_H

0 commit comments

Comments
 (0)