1+ /* !
2+ * @file WipperSnapper_I2C_Driver_D6T1A.h
3+ *
4+ * Device driver for a D6T1A thermal 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+ * Copyright (c) Tyeth Gundry 2025 for Adafruit Industries.
11+ *
12+ * MIT license, all text here must be included in any redistribution.
13+ *
14+ */
15+
16+ #ifndef WipperSnapper_I2C_Driver_D6T1A_H
17+ #define WipperSnapper_I2C_Driver_D6T1A_H
18+
19+ #include < OmronD6T.h>
20+
21+ #include " WipperSnapper_I2C_Driver.h"
22+
23+ /* *************************************************************************/
24+ /* !
25+ @brief Class that provides a sensor driver for the D6T1A temperature
26+ and pressure sensor.
27+ */
28+ /* *************************************************************************/
29+ class WipperSnapper_I2C_Driver_D6T1A : public WipperSnapper_I2C_Driver {
30+ public:
31+ /* ******************************************************************************/
32+ /* !
33+ @brief Constructor for an D6T1A sensor.
34+ @param i2c
35+ The I2C interface.
36+ @param sensorAddress
37+ 7-bit device address.
38+ */
39+ /* ******************************************************************************/
40+ WipperSnapper_I2C_Driver_D6T1A (TwoWire *i2c, uint16_t sensorAddress)
41+ : WipperSnapper_I2C_Driver(i2c, sensorAddress) {
42+ _i2c = i2c;
43+ _sensorAddress = sensorAddress;
44+ _deviceTemp = NAN;
45+ _objectTemp = NAN;
46+ _lastRead = 0 ;
47+ }
48+
49+ /* ******************************************************************************/
50+ /* !
51+ @brief Destructor for an D6T1A sensor.
52+ */
53+ /* ******************************************************************************/
54+ ~WipperSnapper_I2C_Driver_D6T1A () { delete _d6t1a; }
55+
56+ /* ******************************************************************************/
57+ /* !
58+ @brief Initializes the D6T1A sensor and begins I2C.
59+ @returns True if initialized successfully, False otherwise.
60+ */
61+ /* ******************************************************************************/
62+ bool begin () {
63+ _d6t1a = new OmronD6T (OmronD6T::D6T_1A, _i2c);
64+ // attempt to initialize D6T1A
65+ if (!_d6t1a->begin (_sensorAddress))
66+ return false ;
67+ return true ;
68+ }
69+
70+ /* ******************************************************************************/
71+ /* !
72+ @brief Checks if sensor was read within last 1s, or is the first read.
73+ @returns True if the sensor was recently read, False otherwise.
74+ */
75+ /* ******************************************************************************/
76+ bool HasBeenReadInLast200ms () {
77+ return _lastRead != 0 && millis () - _lastRead < 200 ;
78+ }
79+
80+ /* ******************************************************************************/
81+ /* !
82+ @brief Reads the sensor.
83+ @returns True if the sensor was read successfully, False otherwise.
84+ */
85+ /* ******************************************************************************/
86+ bool ReadSensorData () {
87+ // dont read sensor more than once per 200ms
88+ if (HasBeenReadInLast200ms ()) {
89+ return true ;
90+ }
91+
92+ _d6t1a->read ();
93+ _deviceTemp = (float )_d6t1a->ambientTempC ();
94+ _objectTemp = (float )_d6t1a->objectTempC (0 , 0 );
95+ _lastRead = millis ();
96+ return true ;
97+ }
98+
99+ /* ******************************************************************************/
100+ /* !
101+ @brief Gets the D6T1A's current temperature.
102+ @param tempEvent
103+ Pointer to an Adafruit_Sensor event.
104+ @returns True if the temperature was obtained successfully, False
105+ otherwise.
106+ */
107+ /* ******************************************************************************/
108+ bool getEventAmbientTemp (sensors_event_t *tempEvent) {
109+ if (ReadSensorData () && _deviceTemp != NAN) {
110+ // if the sensor was read recently, return the cached temperature
111+ tempEvent->temperature = _deviceTemp;
112+ return true ;
113+ }
114+ return false ; // sensor not read recently, return false
115+ }
116+
117+ /* ******************************************************************************/
118+ /* !
119+ @brief Gets the D6T1A's object temperature.
120+ @param tempEvent
121+ Pointer to an Adafruit_Sensor event.
122+ @returns True if the temperature was obtained successfully, False
123+ otherwise.
124+ */
125+ /* ******************************************************************************/
126+ bool getEventObjectTemp (sensors_event_t *tempEvent) {
127+ if (ReadSensorData () && _objectTemp != NAN) {
128+ // if the sensor was read recently, return the cached temperature
129+ tempEvent->temperature = _objectTemp;
130+ return true ;
131+ }
132+ return false ; // sensor not read recently, return false
133+ }
134+
135+ protected:
136+ float _deviceTemp; // /< Device temperature in Celsius
137+ float _objectTemp; // /< Object temperature in Celsius
138+ uint32_t _lastRead; // /< Last time the sensor was read in milliseconds
139+ OmronD6T *_d6t1a = nullptr ; // /< D6T1A object
140+ };
141+
142+ #endif // WipperSnapper_I2C_Driver_D6T1A
0 commit comments