Adafruit Library
WipperSnapper_I2C_Driver_DS2484.h
Go to the documentation of this file.
1 
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 
25 #include <Adafruit_DS248x.h>
26 
27 /**************************************************************************/
32 /**************************************************************************/
34 
35 public:
36  /*******************************************************************************/
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  /*******************************************************************************/
55  /*******************************************************************************/
57 
58  /*******************************************************************************/
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("\tShort detected");
77  }
78  if (!_ds2484->presencePulseDetected()) {
79  WS_DEBUG_PRINTLN("\tNo 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");
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  /*******************************************************************************/
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()) {
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  /*******************************************************************************/
166  /*******************************************************************************/
167  bool getEventAmbientTemp(sensors_event_t *tempEvent) {
168  return processTemperatureEvent(tempEvent);
169  }
170 
171 protected:
172  Adafruit_DS248x *_ds2484;
173  uint8_t _rom[8];
174 };
175 
176 #endif // WipperSnapper_I2C_Driver_DS2484
#define WS_DEBUG_PRINT(...)
Prints debug output.
Definition: Wippersnapper.h:49
bool begin()
Initializes the DS2484 sensor and begins I2C.
Definition: WipperSnapper_I2C_Driver_DS2484.h:64
WipperSnapper_I2C_Driver_DS2484(TwoWire *i2c, uint16_t sensorAddress)
Constructor for a DS2484 sensor.
Definition: WipperSnapper_I2C_Driver_DS2484.h:45
#define WS_DEBUG_PRINTHEX(...)
Prints debug output.
Definition: Wippersnapper.h:55
Class that provides a sensor driver for the DS2484 I2C OneWire converter hosting a DS18b20 temperatur...
Definition: WipperSnapper_I2C_Driver_DS2484.h:33
Adafruit_DS248x * _ds2484
DS2484 driver object.
Definition: WipperSnapper_I2C_Driver_DS2484.h:172
Base class for I2C Drivers.
Definition: WipperSnapper_I2C_Driver.h:30
uint8_t _rom[8]
DS18B20 ROM.
Definition: WipperSnapper_I2C_Driver_DS2484.h:173
uint16_t _sensorAddress
The I2C driver&#39;s unique I2C address.
Definition: WipperSnapper_I2C_Driver.h:1322
bool getEventAmbientTemp(sensors_event_t *tempEvent)
Gets the DS2484&#39;s current temperature.
Definition: WipperSnapper_I2C_Driver_DS2484.h:167
bool processTemperatureEvent(sensors_event_t *tempEvent)
Processes a temperature event.
Definition: WipperSnapper_I2C_Driver_DS2484.h:115
#define WS_DEBUG_PRINTLN(...)
Prints line from debug output.
Definition: Wippersnapper.h:52
#define DS18B20_FAMILY_CODE
DS18B20 family code.
Definition: WipperSnapper_I2C_Driver_DS2484.h:19
#define DS18B20_CMD_CONVERT_T
Convert T command.
Definition: WipperSnapper_I2C_Driver_DS2484.h:20
~WipperSnapper_I2C_Driver_DS2484()
Destructor for an DS2484 sensor.
Definition: WipperSnapper_I2C_Driver_DS2484.h:56
#define DS18B20_CMD_READ_SCRATCHPAD
Read Scratchpad command.
Definition: WipperSnapper_I2C_Driver_DS2484.h:22
TwoWire * _i2c
Pointer to the I2C driver&#39;s Wire object.
Definition: WipperSnapper_I2C_Driver.h:1321
#define DS18B20_CMD_MATCH_ROM
Match ROM command.
Definition: WipperSnapper_I2C_Driver_DS2484.h:21