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 // /< DS18B20 family code
20
+ #define DS18B20_CMD_CONVERT_T 0x44 // /< Convert T command
21
+ #define DS18B20_CMD_MATCH_ROM 0x55 // /< Match ROM command
22
+ #define DS18B20_CMD_READ_SCRATCHPAD 0xBE // /< Read Scratchpad command
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, uint16_t sensorAddress)
46
+ : WipperSnapper_I2C_Driver(i2c, sensorAddress) {
47
+ _i2c = i2c;
48
+ _sensorAddress = sensorAddress;
49
+ }
50
+
51
+ /* ******************************************************************************/
52
+ /* !
53
+ @brief Destructor for an DS2484 sensor.
54
+ */
55
+ /* ******************************************************************************/
56
+ ~WipperSnapper_I2C_Driver_DS2484 () { delete _ds2484; }
57
+
58
+ /* ******************************************************************************/
59
+ /* !
60
+ @brief Initializes the DS2484 sensor and begins I2C.
61
+ @returns True if initialized successfully, False otherwise.
62
+ */
63
+ /* ******************************************************************************/
64
+ bool begin () {
65
+ // initialize DS2484
66
+ _ds2484 = new Adafruit_DS248x ();
67
+ if (!_ds2484->begin (_i2c, (uint8_t )_sensorAddress)) {
68
+ WS_DEBUG_PRINTLN (" Could not find DS2484" );
69
+ return false ;
70
+ }
71
+
72
+ // check bus is okay
73
+ if (!_ds2484->OneWireReset ()) {
74
+ WS_DEBUG_PRINTLN (" Failed to do a OneWire bus reset" );
75
+ if (_ds2484->shortDetected ()) {
76
+ WS_DEBUG_PRINTLN (" \t Short detected" );
77
+ }
78
+ if (!_ds2484->presencePulseDetected ()) {
79
+ WS_DEBUG_PRINTLN (" \t No presense pulse" );
80
+ }
81
+ return false ;
82
+ }
83
+
84
+ // locate first DS18B20
85
+ bool found_device = false ;
86
+ _ds2484->OneWireReset ();
87
+ _ds2484->OneWireSearchReset ();
88
+ while (!found_device && _ds2484->OneWireSearch (_rom)) {
89
+ if (_rom[0 ] == DS18B20_FAMILY_CODE) {
90
+ found_device = true ;
91
+ } else {
92
+ WS_DEBUG_PRINT (" Found unwanted device with family code: 0x" );
93
+ WS_DEBUG_PRINTHEX (_rom[0 ]);
94
+ WS_DEBUG_PRINTLN (" expected 0x28" ); // DS18B20_FAMILY_CODE
95
+ }
96
+ }
97
+
98
+ if (!found_device) {
99
+ WS_DEBUG_PRINTLN (" Could not find DS18B20 attached to DS2484" );
100
+ return false ;
101
+ }
102
+
103
+ WS_DEBUG_PRINTLN (" DS2484 found" );
104
+ return true ;
105
+ }
106
+
107
+ /* ******************************************************************************/
108
+ /* !
109
+ @brief Processes a temperature event.
110
+ @param tempEvent
111
+ Pointer to an Adafruit_Sensor event.
112
+ @returns True if the temperature was obtained successfully, False
113
+ otherwise.
114
+ */
115
+ bool processTemperatureEvent (sensors_event_t *tempEvent) {
116
+ if (!_ds2484->OneWireReset ()) {
117
+ WS_DEBUG_PRINTLN (" Failed to do a OneWire bus reset" );
118
+ return false ;
119
+ }
120
+ if (!_ds2484->presencePulseDetected ()) {
121
+ tempEvent->temperature = NAN;
122
+ return true ;
123
+ }
124
+
125
+ _ds2484->OneWireWriteByte (DS18B20_CMD_MATCH_ROM); // Match ROM command
126
+ for (int i = 0 ; i < 8 ; i++) {
127
+ _ds2484->OneWireWriteByte (_rom[i]);
128
+ }
129
+
130
+ // Start temperature conversion
131
+ _ds2484->OneWireWriteByte (DS18B20_CMD_CONVERT_T); // Convert T command
132
+ delay (750 ); // Wait for conversion (750ms for maximum precision)
133
+
134
+ // Read scratchpad
135
+ if (!_ds2484->OneWireReset ()) {
136
+ WS_DEBUG_PRINTLN (
137
+ " Failed to do a OneWire bus reset after starting temp conversion" );
138
+ return false ;
139
+ }
140
+ _ds2484->OneWireWriteByte (DS18B20_CMD_MATCH_ROM); // Match ROM command
141
+ for (int i = 0 ; i < 8 ; i++) {
142
+ _ds2484->OneWireWriteByte (_rom[i]);
143
+ }
144
+ _ds2484->OneWireWriteByte (
145
+ DS18B20_CMD_READ_SCRATCHPAD); // Read Scratchpad command
146
+
147
+ uint8_t data[9 ];
148
+ for (int i = 0 ; i < sizeof (data) / sizeof (data[0 ]); i++) {
149
+ _ds2484->OneWireReadByte (&data[i]);
150
+ }
151
+
152
+ // Calculate temperature
153
+ int16_t raw = (data[1 ] << 8 ) | data[0 ];
154
+ tempEvent->temperature = (float )raw / 16.0 ;
155
+ return true ;
156
+ }
157
+
158
+ /* ******************************************************************************/
159
+ /* !
160
+ @brief Gets the DS2484's current temperature.
161
+ @param tempEvent
162
+ Pointer to an Adafruit_Sensor event.
163
+ @returns True if the temperature was obtained successfully, False
164
+ otherwise.
165
+ */
166
+ /* ******************************************************************************/
167
+ bool getEventAmbientTemp (sensors_event_t *tempEvent) {
168
+ return processTemperatureEvent (tempEvent);
169
+ }
170
+
171
+ protected:
172
+ Adafruit_DS248x *_ds2484; // /< DS2484 driver object
173
+ uint8_t _rom[8 ]; // /< DS18B20 ROM
174
+ };
175
+
176
+ #endif // WipperSnapper_I2C_Driver_DS2484
0 commit comments