From ad480a790a3ac310e3825d4017b754d4d71859b5 Mon Sep 17 00:00:00 2001 From: tyeth Date: Fri, 1 Sep 2023 17:25:58 +0100 Subject: [PATCH] Add LPS22HB + LPS25HB Pressure sensors --- library.properties | 2 +- platformio.ini | 1 + src/components/i2c/WipperSnapper_I2C.cpp | 22 ++++ src/components/i2c/WipperSnapper_I2C.h | 4 + .../WipperSnapper_I2C_Driver_LPS22HB.h | 113 ++++++++++++++++++ .../WipperSnapper_I2C_Driver_LPS25HB.h | 113 ++++++++++++++++++ 6 files changed, 254 insertions(+), 1 deletion(-) create mode 100644 src/components/i2c/drivers/WipperSnapper_I2C_Driver_LPS22HB.h create mode 100644 src/components/i2c/drivers/WipperSnapper_I2C_Driver_LPS25HB.h diff --git a/library.properties b/library.properties index 42e48a56d..91fdb5706 100644 --- a/library.properties +++ b/library.properties @@ -7,4 +7,4 @@ paragraph=Arduino client for Adafruit.io WipperSnapper category=Communication url=https://github.com/adafruit/Adafruit_IO_Arduino architectures=* -depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit INA219, 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, 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 PM25 AQI Sensor, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit PCT2075, hp_BH1750 +depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit INA219, 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, 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 PM25 AQI Sensor, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit PCT2075, hp_BH1750 diff --git a/platformio.ini b/platformio.ini index cfef0e620..90452cf3e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -44,6 +44,7 @@ lib_deps = adafruit/Adafruit PM25 AQI Sensor adafruit/Adafruit VEML7700 Library adafruit/Adafruit LC709203F + adafruit/Adafruit LPS2X adafruit/Adafruit LPS35HW adafruit/Adafruit seesaw Library adafruit/Adafruit BME680 Library diff --git a/src/components/i2c/WipperSnapper_I2C.cpp b/src/components/i2c/WipperSnapper_I2C.cpp index ff576b006..7e05645a7 100644 --- a/src/components/i2c/WipperSnapper_I2C.cpp +++ b/src/components/i2c/WipperSnapper_I2C.cpp @@ -522,6 +522,28 @@ bool WipperSnapper_Component_I2C::initI2CDevice( _lc->configureDriver(msgDeviceInitReq); drivers.push_back(_lc); WS_DEBUG_PRINTLN("LC709203F Sensor Initialized Successfully!"); + } else if (strcmp("lps22hb", msgDeviceInitReq->i2c_device_name) == 0) { + _lps22hb = new WipperSnapper_I2C_Driver_LPS22HB(this->_i2c, i2cAddress); + if (!_lps22hb->begin()) { + WS_DEBUG_PRINTLN("ERROR: Failed to initialize LPS22HB Sensor!"); + _busStatusResponse = + wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL; + return false; + } + _lps22hb->configureDriver(msgDeviceInitReq); + drivers.push_back(_lps22hb); + WS_DEBUG_PRINTLN("LPS22HB Sensor Initialized Successfully!"); + } else if (strcmp("lps25hb", msgDeviceInitReq->i2c_device_name) == 0) { + _lps25hb = new WipperSnapper_I2C_Driver_LPS25HB(this->_i2c, i2cAddress); + if (!_lps25hb->begin()) { + WS_DEBUG_PRINTLN("ERROR: Failed to initialize LPS25HB Sensor!"); + _busStatusResponse = + wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL; + return false; + } + _lps25hb->configureDriver(msgDeviceInitReq); + drivers.push_back(_lps25hb); + WS_DEBUG_PRINTLN("LPS25HB Sensor Initialized Successfully!"); } else if ((strcmp("lps33hw", msgDeviceInitReq->i2c_device_name) == 0) || (strcmp("lps35hw", msgDeviceInitReq->i2c_device_name)) == 0) { _lps3xhw = new WipperSnapper_I2C_Driver_LPS3XHW(this->_i2c, i2cAddress); diff --git a/src/components/i2c/WipperSnapper_I2C.h b/src/components/i2c/WipperSnapper_I2C.h index 0a55601f8..fb007336a 100644 --- a/src/components/i2c/WipperSnapper_I2C.h +++ b/src/components/i2c/WipperSnapper_I2C.h @@ -31,6 +31,8 @@ #include "drivers/WipperSnapper_I2C_Driver_HTS221.h" #include "drivers/WipperSnapper_I2C_Driver_INA219.h" #include "drivers/WipperSnapper_I2C_Driver_LC709203F.h" +#include "drivers/WipperSnapper_I2C_Driver_LPS22HB.h" +#include "drivers/WipperSnapper_I2C_Driver_LPS25HB.h" #include "drivers/WipperSnapper_I2C_Driver_LPS3XHW.h" #include "drivers/WipperSnapper_I2C_Driver_MAX17048.h" #include "drivers/WipperSnapper_I2C_Driver_MCP9808.h" @@ -127,6 +129,8 @@ class WipperSnapper_Component_I2C { WipperSnapper_I2C_Driver_SHT3X *_sht3x = nullptr; WipperSnapper_I2C_Driver_SHTC3 *_shtc3 = nullptr; WipperSnapper_I2C_Driver_LC709203F *_lc = nullptr; + WipperSnapper_I2C_Driver_LPS22HB *_lps22hb = nullptr; + WipperSnapper_I2C_Driver_LPS25HB *_lps25hb = nullptr; WipperSnapper_I2C_Driver_LPS3XHW *_lps3xhw = nullptr; WipperSnapper_I2C_Driver_STEMMA_Soil_Sensor *_ss = nullptr; WipperSnapper_I2C_Driver_VL53L0X *_vl53l0x = nullptr; diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_LPS22HB.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_LPS22HB.h new file mode 100644 index 000000000..bb5858669 --- /dev/null +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_LPS22HB.h @@ -0,0 +1,113 @@ +/*! + * @file WipperSnapper_I2C_Driver_LPS22HB.h + * + * Device driver for a LPS22HB precision pressure sensor breakout. + * + * 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 2023 for Adafruit Industries. + * + * MIT license, all text here must be included in any redistribution. + * + */ + +#ifndef WipperSnapper_I2C_Driver_LPS22HB_H +#define WipperSnapper_I2C_Driver_LPS22HB_H + +#include "WipperSnapper_I2C_Driver.h" +#include + +/**************************************************************************/ +/*! + @brief Class that provides a sensor driver for the LPS22HB temperature + and pressure sensor. +*/ +/**************************************************************************/ +class WipperSnapper_I2C_Driver_LPS22HB : public WipperSnapper_I2C_Driver { + +public: + /*******************************************************************************/ + /*! + @brief Constructor for an LPS22HB sensor. + @param i2c + The I2C interface. + @param sensorAddress + 7-bit device address. + */ + /*******************************************************************************/ + WipperSnapper_I2C_Driver_LPS22HB(TwoWire *i2c, uint16_t sensorAddress) + : WipperSnapper_I2C_Driver(i2c, sensorAddress) { + _i2c = i2c; + _sensorAddress = sensorAddress; + } + + /*******************************************************************************/ + /*! + @brief Destructor for an LPS22HB sensor. + */ + /*******************************************************************************/ + ~WipperSnapper_I2C_Driver_LPS22HB() { delete _lps22; } + + /*******************************************************************************/ + /*! + @brief Initializes the LPS22HB sensor and begins I2C. + @returns True if initialized successfully, False otherwise. + */ + /*******************************************************************************/ + bool begin() { + _lps22 = new Adafruit_LPS22(); + // attempt to initialize LPS22HB + if (!_lps22->begin_I2C(_sensorAddress, _i2c)) + return false; + + // Set up sample rate and filter initialization + _lps22->setDataRate(LPS22_RATE_ONE_SHOT); + _temp = _lps22->getTemperatureSensor(); + _pressure = _lps22->getPressureSensor(); + return true; + } + + /*******************************************************************************/ + /*! + @brief Gets the LPS22HB'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) { + if (_temp == NULL) + return false; + _temp->getEvent(tempEvent); + return true; + } + + /*******************************************************************************/ + /*! + @brief Reads a pressure sensor and converts + the reading into the expected SI unit. + @param pressureEvent + Pointer to an Adafruit_Sensor event. + @returns True if the sensor event was obtained successfully, False + otherwise. + */ + /*******************************************************************************/ + bool getEventPressure(sensors_event_t *pressureEvent) { + if (_pressure == NULL) + return false; + _pressure->getEvent(pressureEvent); + return true; + } + +protected: + Adafruit_LPS22 *_lps22; ///< LPS22HB object + Adafruit_Sensor *_temp = + NULL; ///< Ptr to an adafruit_sensor representing the temperature + Adafruit_Sensor *_pressure = + NULL; ///< Ptr to an adafruit_sensor representing the pressure +}; + +#endif // WipperSnapper_I2C_Driver_LPS22HB \ No newline at end of file diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_LPS25HB.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_LPS25HB.h new file mode 100644 index 000000000..cdcc173f7 --- /dev/null +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_LPS25HB.h @@ -0,0 +1,113 @@ +/*! + * @file WipperSnapper_I2C_Driver_LPS25HB.h + * + * Device driver for a LPS25HB precision pressure sensor breakout. + * + * 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 2023 for Adafruit Industries. + * + * MIT license, all text here must be included in any redistribution. + * + */ + +#ifndef WipperSnapper_I2C_Driver_LPS25HB_H +#define WipperSnapper_I2C_Driver_LPS25HB_H + +#include "WipperSnapper_I2C_Driver.h" +#include + +/**************************************************************************/ +/*! + @brief Class that provides a sensor driver for the LPS25HB temperature + and pressure sensor. +*/ +/**************************************************************************/ +class WipperSnapper_I2C_Driver_LPS25HB : public WipperSnapper_I2C_Driver { + +public: + /*******************************************************************************/ + /*! + @brief Constructor for an LPS25HB sensor. + @param i2c + The I2C interface. + @param sensorAddress + 7-bit device address. + */ + /*******************************************************************************/ + WipperSnapper_I2C_Driver_LPS25HB(TwoWire *i2c, uint16_t sensorAddress) + : WipperSnapper_I2C_Driver(i2c, sensorAddress) { + _i2c = i2c; + _sensorAddress = sensorAddress; + } + + /*******************************************************************************/ + /*! + @brief Destructor for an LPS25HB sensor. + */ + /*******************************************************************************/ + ~WipperSnapper_I2C_Driver_LPS25HB() { delete _lps25; } + + /*******************************************************************************/ + /*! + @brief Initializes the LPS25HB sensor and begins I2C. + @returns True if initialized successfully, False otherwise. + */ + /*******************************************************************************/ + bool begin() { + _lps25 = new Adafruit_LPS25(); + // attempt to initialize LPS25HB + if (!_lps25->begin_I2C(_sensorAddress, _i2c)) + return false; + + // Set up sample rate and filter initialization + _lps25->setDataRate(LPS25_RATE_ONE_SHOT); + _temp = _lps25->getTemperatureSensor(); + _pressure = _lps25->getPressureSensor(); + return true; + } + + /*******************************************************************************/ + /*! + @brief Gets the LPS25HB'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) { + if (_temp == NULL) + return false; + _temp->getEvent(tempEvent); + return true; + } + + /*******************************************************************************/ + /*! + @brief Reads a pressure sensor and converts + the reading into the expected SI unit. + @param pressureEvent + Pointer to an Adafruit_Sensor event. + @returns True if the sensor event was obtained successfully, False + otherwise. + */ + /*******************************************************************************/ + bool getEventPressure(sensors_event_t *pressureEvent) { + if (_pressure == NULL) + return false; + _pressure->getEvent(pressureEvent); + return true; + } + +protected: + Adafruit_LPS25 *_lps25; ///< LPS25HB object + Adafruit_Sensor *_temp = + NULL; ///< Ptr to an adafruit_sensor representing the temperature + Adafruit_Sensor *_pressure = + NULL; ///< Ptr to an adafruit_sensor representing the pressure +}; + +#endif // WipperSnapper_I2C_Driver_LPS25HB \ No newline at end of file