-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAdafruit_LPS28.cpp
730 lines (663 loc) · 23.3 KB
/
Adafruit_LPS28.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
// For Adafruit_LPS28.cpp:
/*!
* @file Adafruit_LPS28.cpp
*
* @mainpage Adafruit LPS28 Pressure & Temperature Sensor library
*
* @section intro_sec Introduction
*
* This is a library for the LPS28 Pressure & Temperature Sensor
*
* Designed specifically to work with the Adafruit LPS28 Breakout
* ----> http://www.adafruit.com/products/6067
*
* These sensors use I2C to communicate, 2 pins are required to interface.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing products
* from Adafruit!
*
* @section author Author
*
* Written by Limor Fried/Ladyada for Adafruit Industries.
*
* @section license License
*
* MIT license, all text above must be included in any redistribution
*/
#include "Adafruit_LPS28.h"
/**
* @brief Main LPS28 driver class constructor
*
* @param sensor_id ID to differentiate multiple sensors in the same sketch.
* If not specified, defaults to 0x28. Temperature sensor will
* use this ID and pressure sensor will use sensor_id + 1
*/
Adafruit_LPS28::Adafruit_LPS28(int32_t sensor_id) {
_sensorid = sensor_id;
_sensorid_temp = sensor_id;
_sensorid_pressure = sensor_id + 1;
}
/**
* @brief Initializes the sensor with given parameters, tried reading
* and veriying the WHOAMI register
*
* @param theWire The Wire object to be used for I2C communication
* @param i2c_addr The I2C address of the sensor
* @return true If sensor initialization was successful
* @return false If sensor initialization failed
*/
bool Adafruit_LPS28::begin(TwoWire *theWire, uint8_t i2c_addr) {
i2c_dev = new Adafruit_I2CDevice(i2c_addr, theWire);
if (!i2c_dev->begin()) {
return false;
}
Adafruit_BusIO_Register whoami_reg(i2c_dev, LPS28_WHOAMI_REG, 1);
return (whoami_reg.read() == LPS28_WHOAMI_EXPECTED);
}
/**
* @brief Sets the pressure threshold for interrupt generation
*
* @param threshold The pressure threshold value to set
* @return true If threshold was set successfully
* @return false If threshold setting failed
*/
bool Adafruit_LPS28::setThresholdPressure(uint16_t threshold) {
Adafruit_BusIO_Register ths_p(i2c_dev, LPS28_THS_P, 2, LSBFIRST);
return ths_p.write(threshold);
}
/**
* @brief Gets the current pressure threshold value for the IRQ
*
* @return uint16_t The current pressure threshold value
*/
uint16_t Adafruit_LPS28::getThresholdPressure() {
Adafruit_BusIO_Register ths_p(i2c_dev, LPS28_THS_P, 2, LSBFIRST);
return ths_p.read();
}
/**
* @brief Sets the data output rate of the sensor (ODR)
*
* @param odr The desired output data rate from lps28_odr_t enum
* @return true If data rate was set successfully
* @return false If data rate setting failed
*/
bool Adafruit_LPS28::setDataRate(lps28_odr_t odr) {
Adafruit_BusIO_Register ctrl_reg1(i2c_dev, LPS28_CTRL_REG1, 1);
Adafruit_BusIO_RegisterBits odr_bits(&ctrl_reg1, 4, 3);
return odr_bits.write(odr);
}
/**
* @brief Gets the current data output rate setting (ODR)
*
* @return lps28_odr_t The current output data rate setting
*/
lps28_odr_t Adafruit_LPS28::getDataRate() {
Adafruit_BusIO_Register ctrl_reg1(i2c_dev, LPS28_CTRL_REG1, 1);
Adafruit_BusIO_RegisterBits odr_bits(&ctrl_reg1, 4, 3);
return (lps28_odr_t)odr_bits.read();
}
/**
* @brief Sets the number of pressure and temperature samples to average
*
* @param avg The averaging setting from lps28_avg_t enum
* @return true If averaging setting was set successfully
* @return false If averaging setting failed
*/
bool Adafruit_LPS28::setAveraging(lps28_avg_t avg) {
Adafruit_BusIO_Register ctrl_reg1(i2c_dev, LPS28_CTRL_REG1, 1);
Adafruit_BusIO_RegisterBits avg_bits(&ctrl_reg1, 3, 0);
return avg_bits.write(avg);
}
/**
* @brief Gets the current averaging setting
*
* @return lps28_avg_t The current averaging setting
*/
lps28_avg_t Adafruit_LPS28::getAveraging() {
Adafruit_BusIO_Register ctrl_reg1(i2c_dev, LPS28_CTRL_REG1, 1);
Adafruit_BusIO_RegisterBits avg_bits(&ctrl_reg1, 3, 0);
return (lps28_avg_t)avg_bits.read();
}
/**
* @brief Reboots the memory content of the sensor
*
* @return true If memory reboot was successful
* @return false If memory reboot failed
*/
bool Adafruit_LPS28::rebootMemory() {
Adafruit_BusIO_Register ctrl_reg2(i2c_dev, LPS28_CTRL_REG2, 1);
Adafruit_BusIO_RegisterBits boot_bit(&ctrl_reg2, 1, 7);
return boot_bit.write(true);
}
/**
* @brief Sets the full scale mode of the sensor
*
* @param mode true for FS_MODE=1 (1/2048 hPa/LSB), false for FS_MODE=0 (1/4096
* hPa/LSB)
* @return true If full scale mode was set successfully
* @return false If full scale mode setting failed
*/
bool Adafruit_LPS28::setFullScaleMode(bool mode) {
Adafruit_BusIO_Register ctrl_reg2(i2c_dev, LPS28_CTRL_REG2, 1);
Adafruit_BusIO_RegisterBits fs_mode_bit(&ctrl_reg2, 1, 6);
return fs_mode_bit.write(mode);
}
/**
* @brief Gets the current full scale mode setting
*
* @return true FS_MODE=1 (1/2048 hPa/LSB)
* @return false FS_MODE=0 (1/4096 hPa/LSB)
*/
bool Adafruit_LPS28::getFullScaleMode() {
Adafruit_BusIO_Register ctrl_reg2(i2c_dev, LPS28_CTRL_REG2, 1);
Adafruit_BusIO_RegisterBits fs_mode_bit(&ctrl_reg2, 1, 6);
return fs_mode_bit.read();
}
/**
* @brief Enables or disables the low-pass filter at ODR/9
*
* @param enable true to enable the low-pass filter, false to disable
* @return true If setting was applied successfully
* @return false If setting failed
*/
bool Adafruit_LPS28::setLowPassODR9(bool enable) {
Adafruit_BusIO_Register ctrl_reg2(i2c_dev, LPS28_CTRL_REG2, 1);
Adafruit_BusIO_RegisterBits lpfp_cfg_bit(&ctrl_reg2, 1, 5);
return lpfp_cfg_bit.write(enable);
}
/**
* @brief Performs a software reset of the sensor
*
* @return true If reset was successful
* @return false If reset failed
*/
bool Adafruit_LPS28::reset() {
Adafruit_BusIO_Register ctrl_reg2(i2c_dev, LPS28_CTRL_REG2, 1);
Adafruit_BusIO_RegisterBits swreset_bit(&ctrl_reg2, 1, 2);
return swreset_bit.write(true);
}
/**
* @brief Triggers a one-shot measurement
*
* @return true If one-shot measurement was triggered successfully
* @return false If trigger failed
*/
bool Adafruit_LPS28::triggerOneShot() {
Adafruit_BusIO_Register ctrl_reg2(i2c_dev, LPS28_CTRL_REG2, 1);
Adafruit_BusIO_RegisterBits oneshot_bit(&ctrl_reg2, 1, 0);
return oneshot_bit.write(true);
}
/**
* @brief Configures the interrupt pin settings
*
* @param polarity true for active-high, false for active-low
* @param openDrain true for open-drain output, false for push-pull
* @return true If interrupt pin configuration was successful
* @return false If configuration failed
*/
bool Adafruit_LPS28::setInterruptPin(bool polarity, bool openDrain) {
Adafruit_BusIO_Register ctrl_reg3(i2c_dev, LPS28_CTRL_REG3, 1);
Adafruit_BusIO_RegisterBits int_h_l_bit(&ctrl_reg3, 1, 3);
Adafruit_BusIO_RegisterBits pp_od_bit(&ctrl_reg3, 1, 1);
return int_h_l_bit.write(polarity) && pp_od_bit.write(openDrain);
}
/**
* @brief Enables or disables the SDA line internal pull-up resistor
*
* @param enable true to enable pull-up, false to disable
* @return true If pull-up setting was changed successfully
* @return false If setting change failed
*/
bool Adafruit_LPS28::setSDAPullup(bool enable) {
Adafruit_BusIO_Register if_ctrl(i2c_dev, LPS28_IF_CTRL, 1);
Adafruit_BusIO_RegisterBits sda_pu_bit(&if_ctrl, 1, 4);
return sda_pu_bit.write(enable);
}
/**
* @brief Enables or disables the INT pin internal pull-down resistor
*
* @param enable true to enable pull-down, false to disable
* @return true If pull-down setting was changed successfully
* @return false If setting change failed
*/
bool Adafruit_LPS28::setINTPulldown(bool enable) {
Adafruit_BusIO_Register if_ctrl(i2c_dev, LPS28_IF_CTRL, 1);
Adafruit_BusIO_RegisterBits int_pd_dis_bit(&if_ctrl, 1, 2);
return int_pd_dis_bit.write(!enable);
}
/**
* @brief Sets up automatic reference pressure mode
*
* @param enable true to enable auto reference pressure, false to disable
* @return true If auto reference pressure setting was changed successfully
* @return false If setting change failed
*/
bool Adafruit_LPS28::setAutoReferencePressure(bool enable) {
Adafruit_BusIO_Register interrupt_cfg(i2c_dev, LPS28_INTERRUPT_CFG, 1);
Adafruit_BusIO_RegisterBits autozero_bit(&interrupt_cfg, 1, 7);
return autozero_bit.write(enable);
}
/**
* @brief Gets the current auto reference pressure setting
*
* @return true If auto reference pressure is enabled
* @return false If auto reference pressure is disabled
*/
bool Adafruit_LPS28::getAutoReferencePressure() {
Adafruit_BusIO_Register interrupt_cfg(i2c_dev, LPS28_INTERRUPT_CFG, 1);
Adafruit_BusIO_RegisterBits autozero_bit(&interrupt_cfg, 1, 7);
return autozero_bit.read();
}
/**
* @brief Resets the auto reference pressure setting
*
* @return true If reset was successful
* @return false If reset failed
*/
bool Adafruit_LPS28::resetAutoReferencePressure() {
Adafruit_BusIO_Register interrupt_cfg(i2c_dev, LPS28_INTERRUPT_CFG, 1);
Adafruit_BusIO_RegisterBits reset_arp_bit(&interrupt_cfg, 1, 6);
return reset_arp_bit.write(true);
}
/**
* @brief Enables or disables the auto-zero function
*
* @param enable true to enable auto-zero, false to disable
* @return true If auto-zero setting was changed successfully
* @return false If setting change failed
*/
bool Adafruit_LPS28::setAutoZero(bool enable) {
Adafruit_BusIO_Register interrupt_cfg(i2c_dev, LPS28_INTERRUPT_CFG, 1);
Adafruit_BusIO_RegisterBits autozero_bit(&interrupt_cfg, 1, 5);
return autozero_bit.write(enable);
}
/**
* @brief Gets the current auto-zero setting
*
* @return true If auto-zero is enabled
* @return false If auto-zero is disabled
*/
bool Adafruit_LPS28::getAutoZero() {
Adafruit_BusIO_Register interrupt_cfg(i2c_dev, LPS28_INTERRUPT_CFG, 1);
Adafruit_BusIO_RegisterBits autozero_bit(&interrupt_cfg, 1, 5);
return autozero_bit.read();
}
/**
* @brief Resets the auto-zero function
*
* @return true If reset was successful
* @return false If reset failed
*/
bool Adafruit_LPS28::resetAutoZero() {
Adafruit_BusIO_Register interrupt_cfg(i2c_dev, LPS28_INTERRUPT_CFG, 1);
Adafruit_BusIO_RegisterBits reset_az_bit(&interrupt_cfg, 1, 4);
return reset_az_bit.write(true);
}
/**
* @brief Configures pressure interrupt settings
*
* @param low Enable low pressure interrupt
* @param high Enable high pressure interrupt
* @param latching Enable latching mode for interrupts
* @return true If pressure interrupt configuration was successful
* @return false If configuration failed
*/
bool Adafruit_LPS28::setPressureInterrupt(bool low, bool high, bool latching) {
Adafruit_BusIO_Register interrupt_cfg(i2c_dev, LPS28_INTERRUPT_CFG, 1);
Adafruit_BusIO_RegisterBits phe_bit(&interrupt_cfg, 1, 1);
Adafruit_BusIO_RegisterBits ple_bit(&interrupt_cfg, 1, 2);
Adafruit_BusIO_RegisterBits lir_bit(&interrupt_cfg, 1, 3);
return phe_bit.write(high) && ple_bit.write(low) && lir_bit.write(latching);
}
/**
* @brief Configures which conditions trigger the interrupt pin
*
* @param drdy Enable data ready interrupt
* @param drdy_pulse Enable pulsed mode for data ready interrupt
* @param int_enable Enable pressure threshold interrupt
* @param fifo_full Enable FIFO full interrupt
* @param fifo_watermark Enable FIFO watermark interrupt
* @param fifo_overrun Enable FIFO overrun interrupt
* @return true If interrupt pin configuration was successful
* @return false If configuration failed
*/
bool Adafruit_LPS28::setIntPinOutput(bool drdy, bool drdy_pulse,
bool int_enable, bool fifo_full,
bool fifo_watermark, bool fifo_overrun) {
Adafruit_BusIO_Register ctrl_reg4(i2c_dev, LPS28_CTRL_REG4, 1);
Adafruit_BusIO_RegisterBits drdy_pulse_bit(&ctrl_reg4, 1,
6); // Bit 6: DRDY_PLS
Adafruit_BusIO_RegisterBits drdy_bit(&ctrl_reg4, 1, 5); // Bit 5: DRDY
Adafruit_BusIO_RegisterBits int_enable_bit(&ctrl_reg4, 1, 4); // Bit 4: INT_EN
Adafruit_BusIO_RegisterBits fifo_full_bit(&ctrl_reg4, 1,
2); // Bit 2: INT_F_FULL
Adafruit_BusIO_RegisterBits fifo_watermark_bit(&ctrl_reg4, 1,
1); // Bit 1: INT_F_WTM
Adafruit_BusIO_RegisterBits fifo_overrun_bit(&ctrl_reg4, 1,
0); // Bit 0: INT_F_OVR
bool drdy_pulse_ok = drdy_pulse_bit.write(drdy_pulse);
bool drdy_ok = drdy_bit.write(drdy);
bool int_enable_ok = int_enable_bit.write(int_enable);
bool fifo_full_ok = fifo_full_bit.write(fifo_full);
bool fifo_watermark_ok = fifo_watermark_bit.write(fifo_watermark);
bool fifo_overrun_ok = fifo_overrun_bit.write(fifo_overrun);
return drdy_pulse_ok && drdy_ok && int_enable_ok && fifo_full_ok &&
fifo_watermark_ok && fifo_overrun_ok;
}
/**
* @brief Configures the FIFO operation mode
*
* @param stop_on_watermark Stop collecting data when watermark is reached
* @param mode FIFO operation mode from lps28_fifo_mode_t enum
* @return true If FIFO configuration was successful
* @return false If configuration failed
*/
bool Adafruit_LPS28::setFIFOmode(bool stop_on_watermark,
lps28_fifo_mode_t mode) {
Adafruit_BusIO_Register fifo_ctrl(i2c_dev, LPS28_FIFO_CTRL, 1);
Adafruit_BusIO_RegisterBits stop_on_wtm_bit(&fifo_ctrl, 1,
3); // Bit 3: STOP_ON_WTM
Adafruit_BusIO_RegisterBits f_mode_bits(&fifo_ctrl, 3,
0); // Bits 2:0 (F_MODE[2:0])
bool stop_ok = stop_on_wtm_bit.write(stop_on_watermark);
bool mode_ok = f_mode_bits.write(mode);
return stop_ok && mode_ok;
}
/**
* @brief Sets the FIFO watermark level
*
* @param wtm Watermark level (0-127)
* @return true If watermark was set successfully
* @return false If setting failed
*/
bool Adafruit_LPS28::setFIFOWatermark(uint8_t wtm) {
Adafruit_BusIO_Register fifo_wtm(i2c_dev, LPS28_FIFO_WTM, 1);
return fifo_wtm.write(wtm);
}
/**
* @brief Gets the current FIFO watermark level
*
* @return uint8_t Current watermark level (0-127)
*/
uint8_t Adafruit_LPS28::getFIFOWatermark() {
Adafruit_BusIO_Register fifo_wtm(i2c_dev, LPS28_FIFO_WTM, 1);
return fifo_wtm.read();
}
/**
* @brief Gets the current reference pressure value
*
* @return int16_t Current reference pressure value
*/
int16_t Adafruit_LPS28::getReferencePressure() {
Adafruit_BusIO_Register ref_p(i2c_dev, LPS28_REF_P, 2, LSBFIRST);
return ref_p.read();
}
/**
* @brief Sets the pressure offset value
*
* @param offset Pressure offset value to set
* @return true If offset was set successfully
* @return false If setting failed
*/
bool Adafruit_LPS28::setPressureOffset(int16_t offset) {
Adafruit_BusIO_Register rpds(i2c_dev, LPS28_RPDS, 2, LSBFIRST);
return rpds.write(offset);
}
/**
* @brief Gets the current pressure offset value
*
* @return int16_t Current pressure offset value
*/
int16_t Adafruit_LPS28::getPressureOffset() {
Adafruit_BusIO_Register rpds(i2c_dev, LPS28_RPDS, 2, LSBFIRST);
return rpds.read();
}
/**
* @brief Gets the source of the last interrupt
*
* @return uint8_t Interrupt source register value
* Bit 7: AUTOZERO
* Bit 6: RESET_ARP
* Bit 5: AUTOREFP
* Bit 4: RESET_AZ
* Bit 3: IA
* Bit 2: PL
* Bit 1: PH
* Bit 0: BOOT
*/
uint8_t Adafruit_LPS28::getIntSource() {
Adafruit_BusIO_Register int_source(i2c_dev, LPS28_INT_SOURCE, 1);
return int_source.read();
}
/**
* @brief Gets the number of unread samples in the FIFO
*
* @return uint8_t Number of unread FIFO samples
*/
uint8_t Adafruit_LPS28::getFIFOunreadSamples() {
Adafruit_BusIO_Register fifo_status1(i2c_dev, LPS28_FIFO_STATUS1, 1);
return fifo_status1.read();
}
/**
* @brief Gets the current sensor status
*
* @return uint8_t Status register value
* Bit 3: P_OR (Pressure data overrun)
* Bit 2: T_OR (Temperature data overrun)
* Bit 1: P_DA (Pressure data available)
* Bit 0: T_DA (Temperature data available)
*/
uint8_t Adafruit_LPS28::getStatus() {
Adafruit_BusIO_Register status_reg(i2c_dev, LPS28_STATUS, 1);
return status_reg.read();
}
/**
* @brief Gets the current pressure reading
*
* @return float Pressure reading in hPa (hectopascals)
* @note Resolution depends on FS_MODE:
* - FS_MODE = 0: 1 LSB = 1/4096 hPa
* - FS_MODE = 1: 1 LSB = 1/2048 hPa
*/
float Adafruit_LPS28::getPressure() {
// Read the 3-byte PRESS_OUT register
Adafruit_BusIO_Register pressure_out(i2c_dev, LPS28_PRESS_OUT, 3, LSBFIRST);
int32_t raw_pressure = pressure_out.read();
// Get full-scale mode using the getter
bool fs_mode = getFullScaleMode();
// Convert raw pressure value to hPa based on FS_MODE
float pressure_hpa;
if (fs_mode) {
pressure_hpa = raw_pressure / 2048.0; // FS_MODE = 1, 1 LSB = 1/2048 hPa
} else {
pressure_hpa = raw_pressure / 4096.0; // FS_MODE = 0, 1 LSB = 1/4096 hPa
}
return pressure_hpa;
}
/**
* @brief Gets the current temperature reading
*
* @return float Temperature in degrees Celsius
* @note Resolution is 0.01 degC per LSB
*/
float Adafruit_LPS28::getTemperature() {
// Read the 2-byte TEMP_OUT register (signed 16-bit, 2's complement)
Adafruit_BusIO_Register temp_out(i2c_dev, LPS28_TEMP_OUT, 2, LSBFIRST);
int16_t raw_temperature = (int16_t)temp_out.read();
// Convert raw temperature to degrees Celsius
float temperature_celsius = raw_temperature * 0.01; // 1 LSB = 0.01 deg C
return temperature_celsius;
}
/**
* @brief Gets the current FIFO status
*
* @return uint8_t FIFO status register value
* Bit 7-6: FIFO_FULL_IA (FIFO full interrupt active)
* Bit 5: FIFO_OVR_IA (FIFO overrun interrupt active)
* Bit 4: FIFO_WTM_IA (FIFO watermark interrupt active)
* Bit 3-0: Reserved
*/
uint8_t Adafruit_LPS28::getFIFOstatus() {
Adafruit_BusIO_Register fifo_status2(i2c_dev, LPS28_FIFO_STATUS2, 1);
return fifo_status2.read();
}
/**
* @brief Reads the next pressure value from the FIFO buffer
*
* @return float Pressure reading from FIFO in hPa (hectopascals)
* @note Resolution depends on FS_MODE:
* - FS_MODE = 0: 1 LSB = 1/4096 hPa
* - FS_MODE = 1: 1 LSB = 1/2048 hPa
*/
float Adafruit_LPS28::getFIFOpressure() {
// Read the 3-byte FIFO_DATA_OUT_PRESS_XL register
Adafruit_BusIO_Register fifo_pressure_out(
i2c_dev, LPS28_FIFO_DATA_OUT_PRESS_XL, 3, LSBFIRST);
int32_t raw_pressure = fifo_pressure_out.read();
// Get full-scale mode using the getter
bool fs_mode = getFullScaleMode();
// Convert raw pressure value to hPa based on FS_MODE
float pressure_hpa;
if (fs_mode) {
pressure_hpa = raw_pressure / 2048.0; // FS_MODE = 1, 1 LSB = 1/2048 hPa
} else {
pressure_hpa = raw_pressure / 4096.0; // FS_MODE = 0, 1 LSB = 1/4096 hPa
}
return pressure_hpa;
}
/******************** Sensor interface */
/**
* @brief Gets a pointer to the temperature sensor object
*
* @return Adafruit_Sensor* Pointer to an Adafruit_Sensor object for temperature
* readings
*/
Adafruit_Sensor *Adafruit_LPS28::getTemperatureSensor(void) {
if (!temp_sensor) {
temp_sensor = new Adafruit_LPS28_Temp(this);
}
return temp_sensor;
}
/**
* @brief Gets a pointer to the pressure sensor object
*
* @return Adafruit_Sensor* Pointer to an Adafruit_Sensor object for pressure
* readings
*/
Adafruit_Sensor *Adafruit_LPS28::getPressureSensor(void) {
if (!pressure_sensor) {
pressure_sensor = new Adafruit_LPS28_Pressure(this);
}
return pressure_sensor;
}
/**
* @brief Gets both pressure and temperature readings in unified sensor format
*
* @param pressure Pointer to pressure event structure to be filled
* @param temp Pointer to temperature event structure to be filled
* @return true if reading was successful
* @return false if reading failed
*/
bool Adafruit_LPS28::getEvent(sensors_event_t *pressure,
sensors_event_t *temp) {
uint32_t timestamp = millis();
// Read the sensor
float pressure_hPa = getPressure();
float temperature_C = getTemperature();
fillTempEvent(temp, timestamp);
fillPressureEvent(pressure, timestamp);
return true;
}
/**
* @brief Populates a temperature event structure with current readings
*
* @param temp Pointer to temperature event structure to be filled
* @param timestamp Current timestamp in milliseconds
*/
void Adafruit_LPS28::fillTempEvent(sensors_event_t *temp, uint32_t timestamp) {
memset(temp, 0, sizeof(sensors_event_t));
temp->version = sizeof(sensors_event_t);
temp->sensor_id = _sensorid_temp; // Use _sensorid_temp instead of _sensorID
temp->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
temp->timestamp = timestamp;
temp->temperature = getTemperature();
}
/**
* @brief Populates a pressure event structure with current readings
*
* @param pressure Pointer to pressure event structure to be filled
* @param timestamp Current timestamp in milliseconds
*/
void Adafruit_LPS28::fillPressureEvent(sensors_event_t *pressure,
uint32_t timestamp) {
memset(pressure, 0, sizeof(sensors_event_t));
pressure->version = sizeof(sensors_event_t);
pressure->sensor_id =
_sensorid_pressure; // Use _sensorid_pressure instead of _sensorID
pressure->type = SENSOR_TYPE_PRESSURE;
pressure->timestamp = timestamp;
pressure->pressure = getPressure();
}
/**
* @brief Gets the current temperature reading in unified sensor format
*
* @param event Pointer to sensor event structure to be filled with temperature
* data
* @return true Always returns true as reading cannot fail
*/
bool Adafruit_LPS28_Temp::getEvent(sensors_event_t *event) {
_theLPS28->fillTempEvent(event, millis());
return true;
}
/**
* @brief Gets the temperature sensor details
*
* Provides metadata about the temperature sensor including its
* range, resolution, and other capabilities.
*
* @param sensor Pointer to sensor metadata structure to be filled
*/
void Adafruit_LPS28_Temp::getSensor(sensor_t *sensor) {
memset(sensor, 0, sizeof(sensor_t));
strncpy(sensor->name, "LPS28", sizeof(sensor->name) - 1);
sensor->name[sizeof(sensor->name) - 1] = 0;
sensor->version = 1;
sensor->sensor_id = _sensorID;
sensor->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
sensor->min_delay = 0;
sensor->min_value = -40.0; // -40 degree C
sensor->max_value = 85.0; // 85 degree C
sensor->resolution = 0.01; // 0.01 degree C
}
/**
* @brief Gets the current pressure reading in unified sensor format
*
* @param event Pointer to sensor event structure to be filled with pressure
* data
* @return true Always returns true as reading cannot fail
*/
bool Adafruit_LPS28_Pressure::getEvent(sensors_event_t *event) {
_theLPS28->fillPressureEvent(event, millis());
return true;
}
/**
* @brief Gets the pressure sensor details
*
* Provides metadata about the pressure sensor including its
* range, resolution, and other capabilities.
*
* @param sensor Pointer to sensor metadata structure to be filled
*/
void Adafruit_LPS28_Pressure::getSensor(sensor_t *sensor) {
memset(sensor, 0, sizeof(sensor_t));
strncpy(sensor->name, "LPS28", sizeof(sensor->name) - 1);
sensor->name[sizeof(sensor->name) - 1] = 0;
sensor->version = 1;
sensor->sensor_id = _sensorID;
sensor->type = SENSOR_TYPE_PRESSURE;
sensor->min_delay = 0;
sensor->min_value = 260.0; // 260 hPa
sensor->max_value = 4060.0; // 4060 hPa
sensor->resolution = 0.0002; // Based on full-scale mode
}