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
+ _lps28->setDataRate (LPS28_ODR_ONESHOT);
67
+ _lps28->setAveraging (LPS28_AVG_512);
68
+
69
+ return readSensor ();
70
+ }
71
+
72
+ /* ******************************************************************************/
73
+ /* !
74
+ @brief Reads the sensor and stores the data in the object.
75
+ @returns True if the sensor was read successfully, False otherwise.
76
+ */
77
+ /* ******************************************************************************/
78
+ bool readSensor () {
79
+ // grab one reading to seed the sensor
80
+ if (!_lps28->triggerOneShot ()) {
81
+ return false ;
82
+ }
83
+
84
+ // Wait (block up to 100ms) until data is ready
85
+ for (uint8_t i = 0 ; i < 100 ; i++) {
86
+ if (_lps28->getStatus () & LPS28_STATUS_PRESS_READY) {
87
+ if (_temp == NULL ) {
88
+ _temp = _lps28->getTemperatureSensor ();
89
+ }
90
+ if (_pressure == NULL ) {
91
+ _pressure = _lps28->getPressureSensor ();
92
+ }
93
+ return true ;
94
+ }
95
+ delay (1 );
96
+ }
97
+ return false ;
98
+ }
99
+
100
+ /* ******************************************************************************/
101
+ /* !
102
+ @brief Gets the LPS28DFW's current temperature.
103
+ @param tempEvent
104
+ Pointer to an Adafruit_Sensor event.
105
+ @returns True if the temperature was obtained successfully, False
106
+ otherwise.
107
+ */
108
+ /* ******************************************************************************/
109
+ bool getEventAmbientTemp (sensors_event_t *tempEvent) {
110
+ if (!readSensor ())
111
+ return false ;
112
+ _temp->getEvent (tempEvent);
113
+ return true ;
114
+ }
115
+
116
+ /* ******************************************************************************/
117
+ /* !
118
+ @brief Reads a pressure sensor and converts
119
+ the reading into the expected SI unit.
120
+ @param pressureEvent
121
+ Pointer to an Adafruit_Sensor event.
122
+ @returns True if the sensor event was obtained successfully, False
123
+ otherwise.
124
+ */
125
+ /* ******************************************************************************/
126
+ bool getEventPressure (sensors_event_t *pressureEvent) {
127
+ if (!readSensor ())
128
+ return false ;
129
+ _pressure->getEvent (pressureEvent);
130
+ return true ;
131
+ }
132
+
133
+ protected:
134
+ Adafruit_LPS28 *_lps28; // /< LPS28DFW object
135
+ Adafruit_Sensor *_temp =
136
+ NULL ; // /< Ptr to an adafruit_sensor representing the temperature
137
+ Adafruit_Sensor *_pressure =
138
+ NULL ; // /< Ptr to an adafruit_sensor representing the pressure
139
+ };
140
+
141
+ #endif // WipperSnapper_I2C_Driver_LPS28DFW
0 commit comments