1
+ /* !
2
+ * @file WipperSnapper_I2C_Driver_SHT20.h
3
+ *
4
+ * Device driver for the SHT20 Temperature and Humidity Sensor
5
+ *
6
+ * Adafruit invests time and resources providing this open source code,
7
+ * please support Adafruit and open-source hardware by purchasing
8
+ * products from Adafruit!
9
+ *
10
+ * Adafruit Wippersnapper code Copyright (c) 2022 Adafruit Industries.
11
+ * SHT20 Driver code Copyright (c) DFRobot 2022.
12
+ *
13
+ * MIT license, all text here must be included in any redistribution.
14
+ *
15
+ */
16
+
17
+ #ifndef WipperSnapper_I2C_Driver_SHT20_H
18
+ #define WipperSnapper_I2C_Driver_SHT20_H
19
+
20
+ #include " WipperSnapper_I2C_Driver.h"
21
+ #include < DFRobot_SHT20.h>
22
+ #include < Wire.h>
23
+
24
+ /* *************************************************************************/
25
+ /* !
26
+ @brief Class that provides a driver interface for the SHT20 sensor.
27
+ */
28
+ /* *************************************************************************/
29
+ class WipperSnapper_I2C_Driver_SHT20 : public WipperSnapper_I2C_Driver {
30
+
31
+ public:
32
+ /* ******************************************************************************/
33
+ /* !
34
+ @brief Constructor for a SHT20 sensor.
35
+ @param i2c
36
+ The I2C interface.
37
+ @param sensorAddress
38
+ 7-bit device address.
39
+ */
40
+ /* ******************************************************************************/
41
+ WipperSnapper_I2C_Driver_SHT20 (TwoWire *i2c, uint16_t sensorAddress)
42
+ : WipperSnapper_I2C_Driver(i2c, sensorAddress) {
43
+ _i2c = i2c;
44
+ _sensorAddress = sensorAddress;
45
+ }
46
+
47
+ /* ******************************************************************************/
48
+ /* !
49
+ @brief Initializes the SHT20 sensor and begins I2C.
50
+ @returns True if initialized successfully, False otherwise.
51
+ */
52
+ /* ******************************************************************************/
53
+ bool begin () {
54
+ DFRobot_SHT20 sht20 (&Wire, SHT20_I2C_ADDR);
55
+
56
+ _SHT20 = new DFRobot_SHT20 (_i2c, _sensorAddress);
57
+ delay (100 );
58
+ return true ;
59
+ }
60
+
61
+ /* ******************************************************************************/
62
+ /* !
63
+ @brief Gets the SHT20'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
+ // populate temp and humidity objects with fresh data
72
+ tempEvent->temperature = _SHT20->readTemperature ();
73
+ // the driver returns a sketchy float if an error occurs, this could be caught
74
+ // here with a sensible range check and return false if not.
75
+ return true ;
76
+ }
77
+
78
+ /* ******************************************************************************/
79
+ /* !
80
+ @brief Gets the SHT20's current relative humidity reading.
81
+ @param humidEvent
82
+ Pointer to an Adafruit_Sensor event.
83
+ @returns True if the humidity was obtained successfully, False
84
+ otherwise.
85
+ */
86
+ /* ******************************************************************************/
87
+ bool getEventRelativeHumidity (sensors_event_t *humidEvent) {
88
+ // populate temp and humidity objects with fresh data
89
+ humidEvent->relative_humidity = _SHT20->readHumidity ();
90
+ // the driver returns a sketchy float if an error occurs, this could be caught
91
+ // here with a sensible range check and return false if not.
92
+ return true ;
93
+ }
94
+
95
+ protected:
96
+ DFRobot_SHT20 *_SHT20; // /< SHT20 object
97
+ };
98
+
99
+ #endif // WipperSnapper_I2C_Driver_SHT20
0 commit comments