diff --git a/library.properties b/library.properties index 5f4733745..cba6cec10 100644 --- a/library.properties +++ b/library.properties @@ -7,4 +7,4 @@ paragraph=Arduino application for Adafruit.io WipperSnapper category=Communication url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino architectures=* -depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit INA219, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork +depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit INA219, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit DS248x, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork diff --git a/platformio.ini b/platformio.ini index cc3c9e829..7a3bc6d24 100644 --- a/platformio.ini +++ b/platformio.ini @@ -28,6 +28,7 @@ lib_deps = adafruit/Adafruit BMP280 Library adafruit/Adafruit BMP3XX Library adafruit/Adafruit DPS310 + adafruit/Adafruit DS248x adafruit/Adafruit INA219 adafruit/Adafruit HTS221 adafruit/Adafruit HTU21DF Library diff --git a/src/components/i2c/WipperSnapper_I2C.cpp b/src/components/i2c/WipperSnapper_I2C.cpp index 3a25088fa..a89fc182b 100644 --- a/src/components/i2c/WipperSnapper_I2C.cpp +++ b/src/components/i2c/WipperSnapper_I2C.cpp @@ -296,6 +296,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice( _dps310->configureDriver(msgDeviceInitReq); drivers.push_back(_dps310); WS_DEBUG_PRINTLN("DPS310 Initialized Successfully!"); + } else if (strcmp("ds2484", msgDeviceInitReq->i2c_device_name) == 0) { + _ds2484 = new WipperSnapper_I2C_Driver_DS2484(this->_i2c, i2cAddress); + if (!_ds2484->begin()) { + WS_DEBUG_PRINTLN("ERROR: Failed to initialize DS2484!"); + _busStatusResponse = + wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL; + return false; + } + _ds2484->configureDriver(msgDeviceInitReq); + drivers.push_back(_ds2484); + WS_DEBUG_PRINTLN("DS2484 Initialized Successfully!"); } else if (strcmp("ens160", msgDeviceInitReq->i2c_device_name) == 0) { _ens160 = new WipperSnapper_I2C_Driver_ENS160(this->_i2c, i2cAddress); if (!_ens160->begin()) { diff --git a/src/components/i2c/WipperSnapper_I2C.h b/src/components/i2c/WipperSnapper_I2C.h index d9b1a2fa5..008397258 100644 --- a/src/components/i2c/WipperSnapper_I2C.h +++ b/src/components/i2c/WipperSnapper_I2C.h @@ -28,6 +28,7 @@ #include "drivers/WipperSnapper_I2C_Driver_BMP280.h" #include "drivers/WipperSnapper_I2C_Driver_BMP3XX.h" #include "drivers/WipperSnapper_I2C_Driver_DPS310.h" +#include "drivers/WipperSnapper_I2C_Driver_DS2484.h" #include "drivers/WipperSnapper_I2C_Driver_ENS160.h" #include "drivers/WipperSnapper_I2C_Driver_HTS221.h" #include "drivers/WipperSnapper_I2C_Driver_HTU21D.h" @@ -132,6 +133,7 @@ class WipperSnapper_Component_I2C { // Sensor driver objects WipperSnapper_I2C_Driver_AHTX0 *_ahtx0 = nullptr; WipperSnapper_I2C_Driver_DPS310 *_dps310 = nullptr; + WipperSnapper_I2C_Driver_DS2484 *_ds2484 = nullptr; WipperSnapper_I2C_Driver_ENS160 *_ens160 = nullptr; WipperSnapper_I2C_Driver_SCD30 *_scd30 = nullptr; WipperSnapper_I2C_Driver_BH1750 *_bh1750 = nullptr; diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h new file mode 100644 index 000000000..ed276ab9d --- /dev/null +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h @@ -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 + +/**************************************************************************/ +/*! + @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 \ No newline at end of file