1+ /* !
2+ * @file WipperSnapper_I2C_Driver_HDC302X.h
3+ *
4+ * Device driver for an HDC302X Humidity and Temperature sensor.
5+ */
6+
7+ #ifndef WipperSnapper_I2C_Driver_HDC302X_H
8+ #define WipperSnapper_I2C_Driver_HDC302X_H
9+
10+ #include " WipperSnapper_I2C_Driver.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 WipperSnapper_I2C_Driver_HDC302X : public WipperSnapper_I2C_Driver {
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+ */
30+ /* ******************************************************************************/
31+ WipperSnapper_I2C_Driver_HDC302X (TwoWire *i2c, uint16_t sensorAddress)
32+ : WipperSnapper_I2C_Driver(i2c, sensorAddress) {
33+ _i2c = i2c;
34+ _sensorAddress = sensorAddress;
35+ }
36+
37+ /* ******************************************************************************/
38+ /* !
39+ @brief Destructor for an HDC302X sensor.
40+ */
41+ /* ******************************************************************************/
42+ ~WipperSnapper_I2C_Driver_HDC302X () { delete _hdc302x; }
43+
44+ /* ******************************************************************************/
45+ /* !
46+ @brief Initializes the HDC302X sensor and begins I2C.
47+ @returns True if initialized successfully, False otherwise.
48+
49+ */
50+ /* ******************************************************************************/
51+ bool begin () {
52+ // attempt to initialize the HDC302X using the I2C interface
53+ _hdc302x = new Adafruit_HDC302x ();
54+ if (!_hdc302x->begin (_sensorAddress, _i2c))
55+ return false ;
56+
57+ // set the HDC302X's data rate to 1 Hz lowest noise
58+ _hdc302x->setAutoMode (EXIT_AUTO_MODE);
59+ // discard first reading (It returned -45c for me once)
60+ _hdc302x->readTemperatureHumidityOnDemand (_temp, _humidity,
61+ TRIGGERMODE_LP0);
62+ return true ;
63+ }
64+
65+ /* ******************************************************************************/
66+ /* !
67+ @brief Reads the HDC302X's temperature and humidity data.
68+ @returns True if the data was read successfully, False otherwise.
69+ */
70+ /* ******************************************************************************/
71+ bool readSensorData () {
72+ uint16_t status = _hdc302x->readStatus ();
73+ if (status & 0x0010 ) {
74+ WS_DEBUG_PRINTLN (F (" Device Reset Detected" ));
75+ return false ;
76+ }
77+
78+ if (status & 0x0001 ) {
79+ WS_DEBUG_PRINTLN (
80+ F (" Checksum Verification Fail (incorrect checksum received)" ));
81+ return false ;
82+ }
83+
84+ if (!_hdc302x->readTemperatureHumidityOnDemand (_temp, _humidity,
85+ TRIGGERMODE_LP0)) {
86+ WS_DEBUG_PRINTLN (F (" Failed to read temperature and humidity." ));
87+ return false ;
88+ }
89+ return true ;
90+ }
91+
92+ /* ******************************************************************************/
93+ /* !
94+ @brief Gets the HDC302X's current temperature.
95+ @param tempEvent
96+ Pointer to an Adafruit_Sensor event.
97+ @returns True if the temperature was obtained successfully, False
98+ otherwise.
99+ */
100+ /* ******************************************************************************/
101+ bool getEventAmbientTemp (sensors_event_t *tempEvent) {
102+ if (readSensorData () == false )
103+ return false ;
104+ tempEvent->temperature = _temp;
105+ return true ;
106+ }
107+
108+ /* ******************************************************************************/
109+ /* !
110+ @brief Gets the HDC302X's current humidity.
111+ @param humidEvent
112+ Pointer to an Adafruit_Sensor event.
113+ @returns True if the humidity was obtained successfully, False
114+ otherwise.
115+ */
116+ /* ******************************************************************************/
117+ bool getEventRelativeHumidity (sensors_event_t *humidEvent) {
118+ if (readSensorData () == false )
119+ return false ;
120+ humidEvent->relative_humidity = _humidity;
121+ return true ;
122+ }
123+
124+ protected:
125+ Adafruit_HDC302x *_hdc302x; // /< Pointer to an HDC302X object
126+ double _temp = 0.0 ; // /< Holds data for the HDC302X's temperature sensor
127+ double _humidity = 0.0 ; // /< Holds data for the HDC302X's humidity sensor
128+ };
129+
130+ #endif // WipperSnapper_I2C_Driver_HDC302X
0 commit comments