-
Notifications
You must be signed in to change notification settings - Fork 53
Add DS2484 hosting DS18b20 temp sensor #603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ffdd6d0
Add DS2484 hosting DS18b20 temp sensor
tyeth a6da532
Add DS2484 to library.properties requirements
tyeth 7356c89
Remove default address for DS2484
tyeth 6c3b226
Document DS18b20 defines for DS2484
tyeth 38dfd1f
Add OneWireSearchReset in case device search is locked
tyeth be8f206
Add onewire debug info if no sensor found on bus
tyeth c0acc94
format document
tyeth 80326ce
DS2484: resolve feedback + failure messages
tyeth 868f17f
Merge remote-tracking branch 'upstream/main' into add-DS2484
tyeth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| /*! | ||
| * @file WipperSnapper_I2C_Driver_DS2484.h | ||
| * | ||
| * Device driver the DS2484 I2C OneWire converter (hosting a DS18b20). | ||
| * | ||
| * Adafruit invests time and resources providing this open source code, | ||
| * please support Adafruit and open-source hardware by purchasing | ||
| * products from Adafruit! | ||
| * | ||
| * Copyright (c) Tyeth Gundry 2024 for Adafruit Industries. | ||
| * | ||
| * MIT license, all text here must be included in any redistribution. | ||
| * | ||
| */ | ||
|
|
||
| #ifndef WipperSnapper_I2C_Driver_DS2484_H | ||
| #define WipperSnapper_I2C_Driver_DS2484_H | ||
|
|
||
| #define DS18B20_FAMILY_CODE 0x28 ///< DS18B20 family code | ||
| #define DS18B20_CMD_CONVERT_T 0x44 ///< Convert T command | ||
| #define DS18B20_CMD_MATCH_ROM 0x55 ///< Match ROM command | ||
| #define DS18B20_CMD_READ_SCRATCHPAD 0xBE ///< Read Scratchpad command | ||
|
|
||
| #include "WipperSnapper_I2C_Driver.h" | ||
| #include <Adafruit_DS248x.h> | ||
|
|
||
| /**************************************************************************/ | ||
| /*! | ||
| @brief Class that provides a sensor driver for the DS2484 I2C OneWire | ||
| converter hosting a DS18b20 temperature sensor. | ||
| */ | ||
| /**************************************************************************/ | ||
| class WipperSnapper_I2C_Driver_DS2484 : public WipperSnapper_I2C_Driver { | ||
|
|
||
| public: | ||
| /*******************************************************************************/ | ||
| /*! | ||
| @brief Constructor for a DS2484 sensor. | ||
| @param i2c | ||
| The I2C interface. | ||
| @param sensorAddress | ||
| 7-bit device address. | ||
| */ | ||
| /*******************************************************************************/ | ||
| WipperSnapper_I2C_Driver_DS2484(TwoWire *i2c, uint16_t sensorAddress) | ||
| : WipperSnapper_I2C_Driver(i2c, sensorAddress) { | ||
| _i2c = i2c; | ||
| _sensorAddress = sensorAddress; | ||
| } | ||
|
|
||
| /*******************************************************************************/ | ||
| /*! | ||
| @brief Destructor for an DS2484 sensor. | ||
| */ | ||
| /*******************************************************************************/ | ||
| ~WipperSnapper_I2C_Driver_DS2484() { delete _ds2484; } | ||
|
|
||
| /*******************************************************************************/ | ||
| /*! | ||
| @brief Initializes the DS2484 sensor and begins I2C. | ||
| @returns True if initialized successfully, False otherwise. | ||
| */ | ||
| /*******************************************************************************/ | ||
| bool begin() { | ||
| // initialize DS2484 | ||
| _ds2484 = new Adafruit_DS248x(); | ||
| if (!_ds2484->begin(_i2c, (uint8_t)_sensorAddress)) { | ||
| WS_DEBUG_PRINTLN("Could not find DS2484"); | ||
| return false; | ||
| } | ||
|
|
||
| // check bus is okay | ||
| if (!_ds2484->OneWireReset()) { | ||
| WS_DEBUG_PRINTLN("Failed to do a OneWire bus reset"); | ||
| if (_ds2484->shortDetected()) { | ||
| WS_DEBUG_PRINTLN("\tShort detected"); | ||
| } | ||
| if (!_ds2484->presencePulseDetected()) { | ||
| WS_DEBUG_PRINTLN("\tNo presense pulse"); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| // locate first DS18B20 | ||
| bool found_device = false; | ||
| _ds2484->OneWireReset(); | ||
| _ds2484->OneWireSearchReset(); | ||
| while (!found_device && _ds2484->OneWireSearch(_rom)) { | ||
| if (_rom[0] == DS18B20_FAMILY_CODE) { | ||
| found_device = true; | ||
| } else { | ||
| WS_DEBUG_PRINT("Found unwanted device with family code: 0x"); | ||
| WS_DEBUG_PRINTHEX(_rom[0]); | ||
| WS_DEBUG_PRINTLN(" expected 0x28"); // DS18B20_FAMILY_CODE | ||
| } | ||
| } | ||
|
|
||
| if (!found_device) { | ||
| WS_DEBUG_PRINTLN("Could not find DS18B20 attached to DS2484"); | ||
| return false; | ||
| } | ||
|
|
||
| WS_DEBUG_PRINTLN("DS2484 found"); | ||
| return true; | ||
| } | ||
|
|
||
| /*******************************************************************************/ | ||
| /*! | ||
| @brief Processes a temperature event. | ||
| @param tempEvent | ||
| Pointer to an Adafruit_Sensor event. | ||
| @returns True if the temperature was obtained successfully, False | ||
| otherwise. | ||
| */ | ||
| bool processTemperatureEvent(sensors_event_t *tempEvent) { | ||
| if (!_ds2484->OneWireReset()) { | ||
| WS_DEBUG_PRINTLN("Failed to do a OneWire bus reset"); | ||
| return false; | ||
| } | ||
| if (!_ds2484->presencePulseDetected()) { | ||
| tempEvent->temperature = NAN; | ||
| return true; | ||
| } | ||
|
|
||
| _ds2484->OneWireWriteByte(DS18B20_CMD_MATCH_ROM); // Match ROM command | ||
| for (int i = 0; i < 8; i++) { | ||
| _ds2484->OneWireWriteByte(_rom[i]); | ||
| } | ||
|
|
||
| // Start temperature conversion | ||
| _ds2484->OneWireWriteByte(DS18B20_CMD_CONVERT_T); // Convert T command | ||
| delay(750); // Wait for conversion (750ms for maximum precision) | ||
|
|
||
| // Read scratchpad | ||
| if (!_ds2484->OneWireReset()) { | ||
| WS_DEBUG_PRINTLN( | ||
| "Failed to do a OneWire bus reset after starting temp conversion"); | ||
| return false; | ||
| } | ||
| _ds2484->OneWireWriteByte(DS18B20_CMD_MATCH_ROM); // Match ROM command | ||
| for (int i = 0; i < 8; i++) { | ||
| _ds2484->OneWireWriteByte(_rom[i]); | ||
| } | ||
| _ds2484->OneWireWriteByte( | ||
| DS18B20_CMD_READ_SCRATCHPAD); // Read Scratchpad command | ||
|
|
||
| uint8_t data[9]; | ||
| for (int i = 0; i < sizeof(data) / sizeof(data[0]); i++) { | ||
| _ds2484->OneWireReadByte(&data[i]); | ||
| } | ||
|
|
||
| // Calculate temperature | ||
| int16_t raw = (data[1] << 8) | data[0]; | ||
| tempEvent->temperature = (float)raw / 16.0; | ||
| return true; | ||
| } | ||
|
|
||
| /*******************************************************************************/ | ||
| /*! | ||
| @brief Gets the DS2484's current temperature. | ||
| @param tempEvent | ||
| Pointer to an Adafruit_Sensor event. | ||
| @returns True if the temperature was obtained successfully, False | ||
| otherwise. | ||
| */ | ||
| /*******************************************************************************/ | ||
| bool getEventAmbientTemp(sensors_event_t *tempEvent) { | ||
| return processTemperatureEvent(tempEvent); | ||
| } | ||
|
|
||
| protected: | ||
| Adafruit_DS248x *_ds2484; ///< DS2484 driver object | ||
| uint8_t _rom[8]; ///< DS18B20 ROM | ||
| }; | ||
|
|
||
| #endif // WipperSnapper_I2C_Driver_DS2484 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.