|
| 1 | +/*! |
| 2 | + * @file WipperSnapper_I2C_Driver_LPS28DFW.h |
| 3 | + * |
| 4 | + * Device driver for a LPS28DFW precision pressure sensor breakout. |
| 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 2025 for Adafruit Industries. |
| 11 | + * |
| 12 | + * MIT license, all text here must be included in any redistribution. |
| 13 | + * |
| 14 | + */ |
| 15 | + |
| 16 | +#ifndef WipperSnapper_I2C_Driver_LPS28DFW_H |
| 17 | +#define WipperSnapper_I2C_Driver_LPS28DFW_H |
| 18 | + |
| 19 | +#include "WipperSnapper_I2C_Driver.h" |
| 20 | +#include <Adafruit_LPS28.h> |
| 21 | + |
| 22 | +/**************************************************************************/ |
| 23 | +/*! |
| 24 | + @brief Class that provides a sensor driver for the LPS28DFW temperature |
| 25 | + and pressure sensor. |
| 26 | +*/ |
| 27 | +/**************************************************************************/ |
| 28 | +class WipperSnapper_I2C_Driver_LPS28DFW : public WipperSnapper_I2C_Driver { |
| 29 | + |
| 30 | +public: |
| 31 | + /*******************************************************************************/ |
| 32 | + /*! |
| 33 | + @brief Constructor for an LPS28DFW sensor. |
| 34 | + @param i2c |
| 35 | + The I2C interface. |
| 36 | + @param sensorAddress |
| 37 | + 7-bit device address. |
| 38 | + */ |
| 39 | + /*******************************************************************************/ |
| 40 | + WipperSnapper_I2C_Driver_LPS28DFW(TwoWire *i2c, uint16_t sensorAddress) |
| 41 | + : WipperSnapper_I2C_Driver(i2c, sensorAddress) { |
| 42 | + _i2c = i2c; |
| 43 | + _sensorAddress = sensorAddress; |
| 44 | + } |
| 45 | + |
| 46 | + /*******************************************************************************/ |
| 47 | + /*! |
| 48 | + @brief Destructor for an LPS28DFW sensor. |
| 49 | + */ |
| 50 | + /*******************************************************************************/ |
| 51 | + ~WipperSnapper_I2C_Driver_LPS28DFW() { delete _lps28; } |
| 52 | + |
| 53 | + /*******************************************************************************/ |
| 54 | + /*! |
| 55 | + @brief Initializes the LPS28DFW sensor and begins I2C. |
| 56 | + @returns True if initialized successfully, False otherwise. |
| 57 | + */ |
| 58 | + /*******************************************************************************/ |
| 59 | + bool begin() { |
| 60 | + _lps28 = new Adafruit_LPS28(); |
| 61 | + // attempt to initialize LPS28DFW |
| 62 | + if (!_lps28->begin(_i2c, _sensorAddress)) |
| 63 | + return false; |
| 64 | + |
| 65 | + // Set up sample rate and filter initialization |
| 66 | + if (!_lps28->setDataRate(LPS28_ODR_ONESHOT)) { |
| 67 | + WS_DEBUG_PRINTLN("Failed to set data rate"); |
| 68 | + return false; |
| 69 | + } |
| 70 | + if (!_lps28->setAveraging(LPS28_AVG_512)) { |
| 71 | + WS_DEBUG_PRINTLN("Failed to set averaging"); |
| 72 | + return false; |
| 73 | + } |
| 74 | + if (!_lps28->setFullScaleMode(true)) { |
| 75 | + WS_DEBUG_PRINTLN("Failed to set 4060hPa max mode"); |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + return true; |
| 80 | + } |
| 81 | + |
| 82 | + /*******************************************************************************/ |
| 83 | + /*! |
| 84 | + @brief Reads the sensor and stores the data in the object. |
| 85 | + @returns True if the sensor was read successfully, False otherwise. |
| 86 | + */ |
| 87 | + /*******************************************************************************/ |
| 88 | + bool readSensor() { |
| 89 | + // grab one reading to seed the sensor |
| 90 | + if (!_lps28->triggerOneShot()) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + // Wait (block up to 100ms) until data is ready |
| 95 | + for (uint8_t i = 0; i < 100; i++) { |
| 96 | + if (_lps28->getStatus() & LPS28_STATUS_PRESS_READY) { |
| 97 | + if (_temp == NULL) { |
| 98 | + _temp = _lps28->getTemperatureSensor(); |
| 99 | + } |
| 100 | + if (_pressure == NULL) { |
| 101 | + _pressure = _lps28->getPressureSensor(); |
| 102 | + } |
| 103 | + return true; |
| 104 | + } |
| 105 | + delay(1); |
| 106 | + } |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + /*******************************************************************************/ |
| 111 | + /*! |
| 112 | + @brief Gets the LPS28DFW's current temperature. |
| 113 | + @param tempEvent |
| 114 | + Pointer to an Adafruit_Sensor event. |
| 115 | + @returns True if the temperature was obtained successfully, False |
| 116 | + otherwise. |
| 117 | + */ |
| 118 | + /*******************************************************************************/ |
| 119 | + bool getEventAmbientTemp(sensors_event_t *tempEvent) { |
| 120 | + if (!readSensor()) |
| 121 | + return false; |
| 122 | + _temp->getEvent(tempEvent); |
| 123 | + return true; |
| 124 | + } |
| 125 | + |
| 126 | + /*******************************************************************************/ |
| 127 | + /*! |
| 128 | + @brief Reads a pressure sensor and converts |
| 129 | + the reading into the expected SI unit. |
| 130 | + @param pressureEvent |
| 131 | + Pointer to an Adafruit_Sensor event. |
| 132 | + @returns True if the sensor event was obtained successfully, False |
| 133 | + otherwise. |
| 134 | + */ |
| 135 | + /*******************************************************************************/ |
| 136 | + bool getEventPressure(sensors_event_t *pressureEvent) { |
| 137 | + if (!readSensor()) |
| 138 | + return false; |
| 139 | + _pressure->getEvent(pressureEvent); |
| 140 | + return true; |
| 141 | + } |
| 142 | + |
| 143 | +protected: |
| 144 | + Adafruit_LPS28 *_lps28 = nullptr; ///< LPS28DFW object |
| 145 | + Adafruit_Sensor *_temp = |
| 146 | + NULL; ///< Ptr to an adafruit_sensor representing the temperature |
| 147 | + Adafruit_Sensor *_pressure = |
| 148 | + NULL; ///< Ptr to an adafruit_sensor representing the pressure |
| 149 | +}; |
| 150 | + |
| 151 | +#endif // WipperSnapper_I2C_Driver_LPS28DFW |
0 commit comments