1+ /* !
2+ * @file WipperSnapper_I2C_Driver_DS2484.h
3+ *
4+ * Device driver the DS2484 I2C OneWire converter (hosting a DS18b20).
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 2024 for Adafruit Industries.
11+ *
12+ * MIT license, all text here must be included in any redistribution.
13+ *
14+ */
15+
16+ #ifndef WipperSnapper_I2C_Driver_DS2484_H
17+ #define WipperSnapper_I2C_Driver_DS2484_H
18+
19+ #define DS18B20_FAMILY_CODE 0x28
20+ #define DS18B20_CMD_CONVERT_T 0x44
21+ #define DS18B20_CMD_MATCH_ROM 0x55
22+ #define DS18B20_CMD_READ_SCRATCHPAD 0xBE
23+
24+ #include " WipperSnapper_I2C_Driver.h"
25+ #include < Adafruit_DS248x.h>
26+
27+ /* *************************************************************************/
28+ /* !
29+ @brief Class that provides a sensor driver for the DS2484 I2C OneWire
30+ converter hosting a DS18b20 temperature sensor.
31+ */
32+ /* *************************************************************************/
33+ class WipperSnapper_I2C_Driver_DS2484 : public WipperSnapper_I2C_Driver {
34+
35+ public:
36+ /* ******************************************************************************/
37+ /* !
38+ @brief Constructor for a DS2484 sensor.
39+ @param i2c
40+ The I2C interface.
41+ @param sensorAddress
42+ 7-bit device address.
43+ */
44+ /* ******************************************************************************/
45+ WipperSnapper_I2C_Driver_DS2484 (TwoWire *i2c,
46+ uint16_t sensorAddress = _DS2484_ADDRESS)
47+ : WipperSnapper_I2C_Driver(i2c, sensorAddress) {
48+ _i2c = i2c;
49+ _sensorAddress = sensorAddress;
50+ }
51+
52+ /* ******************************************************************************/
53+ /* !
54+ @brief Destructor for an DS2484 sensor.
55+ */
56+ /* ******************************************************************************/
57+ ~WipperSnapper_I2C_Driver_DS2484 () { delete _ds2484; }
58+
59+ /* ******************************************************************************/
60+ /* !
61+ @brief Initializes the DS2484 sensor and begins I2C.
62+ @returns True if initialized successfully, False otherwise.
63+ */
64+ /* ******************************************************************************/
65+ bool begin () {
66+ // initialize DS2484
67+ _ds2484 = new Adafruit_DS248x ();
68+ if (!_ds2484->begin (_i2c, (uint8_t )_sensorAddress)) {
69+ WS_DEBUG_PRINTLN (" Could not find DS2484" );
70+ return false ;
71+ }
72+
73+ // check bus is okay
74+ if (!_ds2484->OneWireReset ()) {
75+ WS_DEBUG_PRINTLN (" Failed to do a OneWire bus reset" );
76+ if (_ds2484->shortDetected ()) {
77+ WS_DEBUG_PRINTLN (" \t Short detected" );
78+ }
79+ if (!_ds2484->presencePulseDetected ()) {
80+ WS_DEBUG_PRINTLN (" \t No presense pulse" );
81+ }
82+ return false ;
83+ }
84+
85+ // locate first DS18B20
86+ bool found_device = false ;
87+ _ds2484->OneWireReset ();
88+ while (!found_device && _ds2484->OneWireSearch (_rom)) {
89+ if (_rom[0 ] == DS18B20_FAMILY_CODE) {
90+ found_device = true ;
91+ }
92+ }
93+
94+ if (!found_device) {
95+ WS_DEBUG_PRINTLN (" Could not find DS18B20 attached to DS2484" );
96+ return false ;
97+ }
98+
99+ WS_DEBUG_PRINTLN (" DS2484 found" );
100+ return true ;
101+ }
102+
103+ /* ******************************************************************************/
104+ /* !
105+ @brief Processes a temperature event.
106+ @param tempEvent
107+ Pointer to an Adafruit_Sensor event.
108+ @returns True if the temperature was obtained successfully, False
109+ otherwise.
110+ */
111+ bool processTemperatureEvent (sensors_event_t *tempEvent) {
112+ if (!_ds2484->OneWireReset ()) {
113+ return false ;
114+ }
115+ if (!_ds2484->presencePulseDetected ()) {
116+ tempEvent->temperature = NAN;
117+ return true ;
118+ }
119+
120+ _ds2484->OneWireWriteByte (DS18B20_CMD_MATCH_ROM); // Match ROM command
121+ for (int i = 0 ; i < 8 ; i++) {
122+ _ds2484->OneWireWriteByte (_rom[i]);
123+ }
124+
125+ // Start temperature conversion
126+ _ds2484->OneWireWriteByte (DS18B20_CMD_CONVERT_T); // Convert T command
127+ delay (750 ); // Wait for conversion (750ms for maximum precision)
128+
129+ // Read scratchpad
130+ _ds2484->OneWireReset ();
131+ _ds2484->OneWireWriteByte (DS18B20_CMD_MATCH_ROM); // Match ROM command
132+ for (int i = 0 ; i < 8 ; i++) {
133+ _ds2484->OneWireWriteByte (_rom[i]);
134+ }
135+ _ds2484->OneWireWriteByte (
136+ DS18B20_CMD_READ_SCRATCHPAD); // Read Scratchpad command
137+
138+ uint8_t data[9 ];
139+ for (int i = 0 ; i < 9 ; i++) {
140+ _ds2484->OneWireReadByte (&data[i]);
141+ }
142+
143+ // Calculate temperature
144+ int16_t raw = (data[1 ] << 8 ) | data[0 ];
145+ tempEvent->temperature = (float )raw / 16.0 ;
146+ return true ;
147+ }
148+
149+ /* ******************************************************************************/
150+ /* !
151+ @brief Gets the DS2484's current temperature.
152+ @param tempEvent
153+ Pointer to an Adafruit_Sensor event.
154+ @returns True if the temperature was obtained successfully, False
155+ otherwise.
156+ */
157+ /* ******************************************************************************/
158+ bool getEventAmbientTemp (sensors_event_t *tempEvent) {
159+ return processTemperatureEvent (tempEvent);
160+ }
161+
162+ protected:
163+ Adafruit_DS248x *_ds2484; // /< DS2484 driver object
164+ uint8_t _rom[8 ]; // /< DS18B20 ROM
165+ };
166+
167+ #endif // WipperSnapper_I2C_Driver_DS2484
0 commit comments