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