Skip to main content

lps22hh_rs/
driver.rs

1use super::{
2    BusOperation, I2c, RegisterOperation, SensorOperation, SevenBitAddress, SpiDevice, bisync, i2c,
3    prelude::*, spi,
4};
5
6use core::fmt::Debug;
7use core::marker::PhantomData;
8
9/// Driver for LPS22HH sensor.
10///
11/// The struct takes a bus object to write to the registers.
12/// The bus is generalized over the BusOperation trait, allowing the use
13/// of I2C or SPI protocols; this also allows the user to implement sharing
14/// techniques to share the underlying bus.
15#[bisync]
16pub struct Lps22hh<B, S>
17where
18    B: BusOperation,
19    S: SensorState,
20{
21    /// The bus driver.
22    bus: B,
23    _state: PhantomData<S>,
24}
25
26/// Driver errors.
27#[derive(Debug)]
28#[bisync]
29pub enum Error<B> {
30    Bus(B),          // Error at the bus level
31    UnexpectedValue, // Unexpected value read from a register
32}
33
34#[bisync]
35impl<P> Lps22hh<i2c::I2cBus<P>, OnState>
36where
37    P: I2c,
38{
39    /// Constructor method for using the I2C bus.
40    pub fn new_i2c(i2c: P, address: I2CAddress) -> Self {
41        // Initialize the I2C bus with the COMPONENT address
42        let bus = i2c::I2cBus::new(i2c, address as SevenBitAddress);
43        Self {
44            bus,
45            _state: PhantomData,
46        }
47    }
48}
49
50#[bisync]
51impl<P> Lps22hh<spi::SpiBus<P>, OnState>
52where
53    P: SpiDevice,
54{
55    /// Constructor method for using the SPI bus.
56    pub fn new_spi(spi: P) -> Self {
57        // Initialize the SPI bus
58        let bus = spi::SpiBus::new(spi);
59        Self {
60            bus,
61            _state: PhantomData,
62        }
63    }
64}
65
66#[bisync]
67impl<B, S> Lps22hh<B, S>
68where
69    B: BusOperation,
70    S: SensorState,
71{
72    // build Lps22h instance from a generic bus that implements BusOperation
73    pub fn from_bus(bus: B) -> Self {
74        Self {
75            bus,
76            _state: PhantomData,
77        }
78    }
79}
80
81#[bisync]
82impl<B: BusOperation, S: SensorState> SensorOperation for Lps22hh<B, S> {
83    type Error = Error<B::Error>;
84
85    /// Read Register Data
86    ///
87    /// Reads multiple bytes from a specified register into a buffer by sending the register address and receiving the data.
88    async fn read_from_register(&mut self, reg: u8, buf: &mut [u8]) -> Result<(), Error<B::Error>> {
89        self.bus
90            .read_from_register(reg, buf)
91            .await
92            .map_err(Error::Bus)
93    }
94
95    /// Write Register Data
96    ///
97    /// Writes multiple bytes to a specified register, splitting the data into chunks if necessary to comply with bus limitations.
98    async fn write_to_register(&mut self, reg: u8, buf: &[u8]) -> Result<(), Error<B::Error>> {
99        self.bus
100            .write_to_register(reg, buf)
101            .await
102            .map_err(Error::Bus)
103    }
104}
105
106#[bisync]
107impl<B: BusOperation> Lps22hh<B, OnState> {
108    /// Reset Autozero Function
109    ///
110    /// Sets the RESET_AZ bit in the interrupt configuration register to reset the Autozero function, clearing AUTOZERO and reference pressure registers.
111    pub async fn autozero_rst_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
112        let mut reg = InterruptCfg::read(self).await?;
113        reg.set_reset_az(val);
114        reg.write(self).await?;
115
116        Ok(())
117    }
118    /// Get Autozero Reset Status
119    ///
120    /// Reads the current state of the RESET_AZ bit from the interrupt configuration register.
121    pub async fn autozero_rst_get(&mut self) -> Result<u8, Error<B::Error>> {
122        let val: u8 = InterruptCfg::read(self).await.map(|reg| reg.reset_az())?;
123
124        Ok(val)
125    }
126    /// Enable Autozero Function
127    ///
128    /// Enables or disables the Autozero function. When enabled, the sensor uses the current pressure as a reference and stores it internally. The AUTOZERO bit clears automatically after the first measurement.
129    pub async fn autozero_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
130        let mut reg = InterruptCfg::read(self).await?;
131        reg.set_autozero(val);
132        reg.write(self).await?;
133
134        Ok(())
135    }
136    /// Get Autozero Enable Status
137    ///
138    /// Retrieves the current state of the Autozero enable bit.
139    pub async fn autozero_get(&mut self) -> Result<u8, Error<B::Error>> {
140        let val: u8 = InterruptCfg::read(self).await.map(|reg| reg.autozero())?;
141
142        Ok(val)
143    }
144    /// Reset AutoRefP Function
145    ///
146    /// Sets the RESET_ARP bit to reset the AutoRefP function, clearing the AutoRefP interrupt reference.
147    pub async fn pressure_snap_rst_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
148        let mut reg = InterruptCfg::read(self).await?;
149        reg.set_reset_arp(val);
150        reg.write(self).await?;
151
152        Ok(())
153    }
154    /// Get AutoRefP Reset Status
155    ///
156    /// Reads the current state of the RESET_ARP bit from the interrupt configuration register.
157    pub async fn pressure_snap_rst_get(&mut self) -> Result<u8, Error<B::Error>> {
158        let val: u8 = InterruptCfg::read(self).await.map(|reg| reg.reset_arp())?;
159
160        Ok(val)
161    }
162    /// Enable AutoRefP Function
163    ///
164    /// Enables or disables the AutoRefP function. When enabled, differential pressure is
165    /// used for interrupt generation without modifying the pressure output registers.
166    pub async fn pressure_snap_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
167        let mut reg = InterruptCfg::read(self).await?;
168        reg.set_autorefp(val);
169        reg.write(self).await?;
170
171        Ok(())
172    }
173    /// Get AutoRefP Enable Status
174    ///
175    /// Retrieves the current state of the AutoRefP enable bit.
176    pub async fn pressure_snap_get(&mut self) -> Result<u8, Error<B::Error>> {
177        let val: u8 = InterruptCfg::read(self).await.map(|reg| reg.autorefp())?;
178
179        Ok(val)
180    }
181    /// Set Block Data Update (BDU)
182    ///
183    /// Controls whether output registers are updated continuously or only after both
184    /// MSB and LSB are read, ensuring data consistency.
185    pub async fn block_data_update_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
186        let mut reg = CtrlReg1::read(self).await?;
187        reg.set_bdu(val);
188        reg.write(self).await?;
189
190        Ok(())
191    }
192    /// Get Block Data Update (BDU) Status
193    ///
194    /// Reads the current setting of the block data update feature.
195    pub async fn block_data_update_get(&mut self) -> Result<u8, Error<B::Error>> {
196        let val: u8 = CtrlReg1::read(self).await.map(|reg| reg.bdu())?;
197
198        Ok(val)
199    }
200    /// Set Output Data Rate (ODR)
201    ///
202    /// Configures the sensor's output data rate and related modes such as
203    /// low-noise and one-shot.
204    pub async fn data_rate_set(&mut self, val: Odr) -> Result<(), Error<B::Error>> {
205        // Read the current values of the control registers
206        let mut ctrl_reg1 = CtrlReg1::read(self).await?;
207        let mut ctrl_reg2 = CtrlReg2::read(self).await?;
208
209        // Update the ODR field in the control register 1
210        ctrl_reg1.set_odr((val as u8) & 0x07);
211        ctrl_reg1.write(self).await?;
212
213        // Update the low noise and one shot fields in the control register 2
214        //let val_u8 = val as u8;
215        ctrl_reg2.set_low_noise_en((val as u8 & 0x10) >> 4);
216        ctrl_reg2.set_one_shot((val as u8 & 0x08) >> 3);
217        ctrl_reg2.write(self).await?;
218
219        Ok(())
220    }
221    /// Get Output Data Rate (ODR)
222    ///
223    /// Reads the current output data rate and mode settings from the sensor.
224    pub async fn data_rate_get(&mut self) -> Result<Odr, Error<B::Error>> {
225        let ctrl_reg1 = CtrlReg1::read(self).await?;
226        let ctrl_reg2 = CtrlReg2::read(self).await?;
227
228        let combined_value =
229            (ctrl_reg2.low_noise_en() << 4) + (ctrl_reg2.one_shot() << 3) + ctrl_reg1.odr();
230
231        let val = Odr::try_from(combined_value).unwrap_or_default();
232
233        Ok(val)
234    }
235    /// Set Reference Pressure
236    ///
237    /// Writes a 16-bit (2's complement reference pressure value used in
238    /// AUTOZERO or AUTOREFP modes).
239    pub async fn pressure_ref_set(&mut self, val: i16) -> Result<(), Error<B::Error>> {
240        let ref_p = RefP::new().with_ref_p(val);
241        ref_p.write(self).await
242    }
243    /// Get Reference Pressure
244    ///
245    /// Reads the 16-bit (2's complement) reference pressure value used in
246    /// AUTOZERO or AUTOREFP modes.
247    pub async fn pressure_ref_get(&mut self) -> Result<i16, Error<B::Error>> {
248        Ok(RefP::read(self).await?.ref_p())
249    }
250    /// Set Pressure Offset
251    ///
252    /// Writes a 16-bit pressure offset value used for one-point calibration (OPC)
253    /// after soldering.
254    pub async fn pressure_offset_set(&mut self, val: i16) -> Result<(), Error<B::Error>> {
255        Rpds::new().with_rpds(val).write(self).await
256    }
257    /// Get Pressure Offset
258    ///
259    /// Reads the 16-bit pressure offset value used for one-point calibration.
260    pub async fn pressure_offset_get(&mut self) -> Result<i16, Error<B::Error>> {
261        Rpds::read(self).await.map(|reg| reg.rpds())
262    }
263    /// Read All Interrupt and Status Flags
264    ///
265    /// Retrieves the current interrupt source, FIFO status, and general status flags from the sensor.
266    pub async fn all_sources_get(
267        &mut self,
268    ) -> Result<(IntSource, FifoStatus2, Status), Error<B::Error>> {
269        Ok((
270            IntSource::read(self).await?,
271            FifoStatusReg::read(self).await?.into(),
272            Status::read(self).await?,
273        ))
274    }
275    /// Read Status Register
276    ///
277    /// Reads the status register which indicates data availability and overrun conditions.
278    pub async fn status_reg_get(&mut self) -> Result<Status, Error<B::Error>> {
279        Status::read(self).await
280    }
281    /// Check Pressure Data Ready Flag
282    ///
283    /// Indicates if new pressure data is available to read.
284    pub async fn press_flag_data_ready_get(&mut self) -> Result<u8, Error<B::Error>> {
285        let val: u8 = Status::read(self).await.map(|reg| reg.p_da())?;
286
287        Ok(val)
288    }
289    /// Check Temperature Data Ready Flag
290    ///
291    /// Indicates if new temperature data is available to read.
292    pub async fn temp_flag_data_ready_get(&mut self) -> Result<u8, Error<B::Error>> {
293        let val: u8 = Status::read(self).await.map(|reg| reg.t_da())?;
294
295        Ok(val)
296    }
297    /// Read Raw Pressure Data
298    ///
299    /// Reads the 24-bit raw pressure output from the sensor registers.
300    pub async fn pressure_raw_get(&mut self) -> Result<u32, Error<B::Error>> {
301        let reg = PressOut::read(self).await?;
302        Ok(reg.pressure())
303    }
304    /// Read Raw Temperature Data
305    ///
306    /// Reads the 16-bit raw temperature output from the sensor registers.
307    pub async fn temperature_raw_get(&mut self) -> Result<i16, Error<B::Error>> {
308        let val = TempOut::read(self).await?;
309        Ok(val.temperature())
310    }
311    /// Read Raw Pressure Data from FIFO
312    ///
313    /// Reads the 24-bit raw pressure data stored in the FIFO buffer.
314    pub async fn fifo_pressure_raw_get(&mut self) -> Result<u32, Error<B::Error>> {
315        let reg = FifoDataOutPress::read(self).await?;
316        Ok(reg.pressure())
317    }
318    /// Read Raw Temperature Data from FIFO
319    ///
320    /// Reads the 16-bit raw temperature data stored in the FIFO buffer.
321    pub async fn fifo_temperature_raw_get(&mut self) -> Result<i16, Error<B::Error>> {
322        let fifo_temp = FifoDataOutTemp::read(self).await?;
323        Ok(fifo_temp.temperature())
324    }
325    /// Read Device ID
326    ///
327    /// Reads the device identification register to verify sensor identity.
328    pub async fn device_id_get(&mut self) -> Result<u8, Error<B::Error>> {
329        WhoAmI::read(self).await.map(|reg| reg.who_am_i())
330    }
331    /// Perform Software Reset
332    ///
333    /// Sets the software reset bit to restore default values in user registers.
334    pub async fn reset_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
335        let mut reg = CtrlReg2::read(self).await?;
336        reg.set_swreset(val);
337        reg.write(self).await
338    }
339    /// Read Software Reset Status
340    ///
341    /// Reads the software reset bit to check if a reset is in progress or completed.
342    pub async fn reset_get(&mut self) -> Result<u8, Error<B::Error>> {
343        let val: u8 = CtrlReg2::read(self).await?.swreset();
344
345        Ok(val)
346    }
347    /// Enable or Disable Register Auto-Increment
348    ///
349    /// Controls whether the register address automatically increments during multi-byte serial interface accesses.
350    pub async fn auto_increment_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
351        let mut reg = CtrlReg2::read(self).await?;
352        reg.set_if_add_inc(val);
353        reg.write(self).await
354    }
355    /// Get Register Auto-Increment Status
356    ///
357    /// Reads the current setting of the register address auto-increment feature.
358    pub async fn auto_increment_get(&mut self) -> Result<u8, Error<B::Error>> {
359        let val: u8 = CtrlReg2::read(self).await?.if_add_inc();
360
361        Ok(val)
362    }
363    /// Reload Calibration Parameters
364    ///
365    /// Triggers a reboot of memory content to reload factory calibration parameters from internal flash.
366    pub async fn boot_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
367        let mut reg = CtrlReg2::read(self).await?;
368        reg.set_boot(val);
369        reg.write(self).await
370    }
371    /// Get Calibration Reload Status
372    ///
373    /// Reads the status of the boot bit indicating if calibration parameters are being reloaded.
374    pub async fn boot_get(&mut self) -> Result<u8, Error<B::Error>> {
375        let val: u8 = CtrlReg2::read(self).await?.boot();
376
377        Ok(val)
378    }
379    /// Configure Low-Pass Filter Bandwidth
380    ///
381    /// Selects the low-pass filter bandwidth to reduce noise on pressure data.
382    pub async fn lp_bandwidth_set(&mut self, val: LpfpCfg) -> Result<(), Error<B::Error>> {
383        let mut reg = CtrlReg1::read(self).await?;
384        reg.set_lpfp_cfg(val as u8);
385        reg.write(self).await
386    }
387    /// Get Low-Pass Filter Bandwidth Setting
388    ///
389    /// Reads the current low-pass filter bandwidth configuration.
390    pub async fn lp_bandwidth_get(&mut self) -> Result<LpfpCfg, Error<B::Error>> {
391        let reg = CtrlReg1::read(self).await?;
392
393        let val = LpfpCfg::try_from(reg.lpfp_cfg()).unwrap_or_default();
394
395        Ok(val)
396    }
397    /// Enable or Disable I2C Interface
398    ///
399    /// Controls whether the I2C interface is enabled or disabled.
400    pub async fn i2c_interface_set(&mut self, val: I2cMode) -> Result<(), Error<B::Error>> {
401        let mut reg = IfCtrl::read(self).await?;
402        reg.set_i2c_disable(val as u8);
403        reg.write(self).await
404    }
405    /// Get I2C Interface Status
406    ///
407    /// Reads whether the I2C interface is currently enabled or disabled.
408    pub async fn i2c_interface_get(&mut self) -> Result<I2cMode, Error<B::Error>> {
409        let reg = IfCtrl::read(self).await?;
410
411        let val = I2cMode::try_from(reg.i2c_disable()).unwrap_or_default();
412        Ok(val)
413    }
414    /// Configure MIPI I3C Interface and Interrupt Pin
415    ///
416    /// Enables or disables the MIPI I3C communication protocol and configures the interrupt pin.
417    pub async fn i3c_interface_set(&mut self, val: I3cMode) -> Result<(), Error<B::Error>> {
418        let mut reg = IfCtrl::read(self).await?;
419        reg.set_i3c_disable((val as u8) & 0x01);
420        reg.set_int_en_i3c(((val as u8) & 0x10) >> 4);
421        reg.write(self).await
422    }
423    /// Get MIPI I3C Interface and Interrupt Pin Status
424    ///
425    /// Reads the current configuration of the MIPI I3C interface and interrupt pin.
426    pub async fn i3c_interface_get(&mut self) -> Result<I3cMode, Error<B::Error>> {
427        let reg = IfCtrl::read(self).await?;
428        let reg_int_en_i3c = reg.int_en_i3c();
429        let reg_i3c_disable = reg.i3c_disable();
430        let val = (reg_int_en_i3c << 4) + reg_i3c_disable;
431        let val = I3cMode::try_from(val).unwrap_or_default();
432
433        Ok(val)
434    }
435    /// Enable or Disable Pull-Up on SDO Pin
436    ///
437    /// Controls the internal pull-up resistor connection on the SDO pin.
438    pub async fn sdo_sa0_mode_set(&mut self, val: PullUp) -> Result<(), Error<B::Error>> {
439        let mut reg = IfCtrl::read(self).await?;
440        reg.set_sdo_pu_en(val as u8);
441        reg.write(self).await
442    }
443    /// Get Pull-Up Status on SDO Pin
444    ///
445    /// Reads whether the internal pull-up resistor on the SDO pin is connected or disconnected.
446    pub async fn sdo_sa0_mode_get(&mut self) -> Result<PullUp, Error<B::Error>> {
447        let tmp: u8 = IfCtrl::read(self).await?.sdo_pu_en();
448        let val = PullUp::try_from(tmp).unwrap_or_default();
449        Ok(val)
450    }
451    /// Enable or Disable Pull-Up on SDA Pin
452    ///
453    /// Controls the internal pull-up resistor connection on the SDA pin.
454    pub async fn sda_mode_set(&mut self, val: PullUp) -> Result<(), Error<B::Error>> {
455        let mut reg = IfCtrl::read(self).await?;
456        reg.set_sda_pu_en(val as u8);
457        reg.write(self).await?;
458
459        Ok(())
460    }
461    /// Get Pull-Up Status on SDA Pin
462    ///
463    /// Reads whether the internal pull-up resistor on the SDA pin is connected or disconnected.
464    pub async fn sda_mode_get(&mut self) -> Result<PullUp, Error<B::Error>> {
465        let reg = IfCtrl::read(self).await?;
466
467        let val = PullUp::try_from(reg.sda_pu_en()).unwrap_or_default();
468        Ok(val)
469    }
470    /// Set SPI Interface Mode
471    ///
472    /// Selects between 4-wire and 3-wire SPI interface modes for communication.
473    pub async fn spi_mode_set(&mut self, val: Sim) -> Result<(), Error<B::Error>> {
474        let mut reg = CtrlReg1::read(self).await?;
475        reg.set_sim(val as u8);
476        reg.write(self).await?;
477
478        Ok(())
479    }
480    /// Get SPI Interface Mode
481    ///
482    /// Reads the current SPI interface mode setting (4-wire or 3-wire).
483    pub async fn spi_mode_get(&mut self) -> Result<Sim, Error<B::Error>> {
484        let reg = CtrlReg1::read(self).await?;
485
486        let val = Sim::try_from(reg.sim()).unwrap_or_default();
487        Ok(val)
488    }
489    /// Set Interrupt Request Latch Mode
490    ///
491    /// Configures whether interrupt requests are pulsed or latched.
492    pub async fn int_notification_set(&mut self, val: Lir) -> Result<(), Error<B::Error>> {
493        let mut reg = InterruptCfg::read(self).await?;
494        reg.set_lir(val as u8);
495        reg.write(self).await?;
496
497        Ok(())
498    }
499    /// Get Interrupt Request Latch Mode
500    ///
501    /// Reads the current interrupt request latch configuration.
502    pub async fn int_notification_get(&mut self) -> Result<Lir, Error<B::Error>> {
503        let reg = InterruptCfg::read(self).await?;
504
505        let val = Lir::try_from(reg.lir()).unwrap_or_default();
506        Ok(val)
507    }
508    /// Set Interrupt Pin Output Mode
509    ///
510    /// Selects push-pull or open-drain configuration for interrupt output pads.
511    pub async fn pin_mode_set(&mut self, val: PpOd) -> Result<(), Error<B::Error>> {
512        let mut reg = CtrlReg2::read(self).await?;
513        reg.set_pp_od(val as u8);
514        reg.write(self).await?;
515
516        Ok(())
517    }
518    /// Get Interrupt Pin Output Mode
519    ///
520    /// Reads the current push-pull or open-drain configuration of interrupt pads.
521    pub async fn pin_mode_get(&mut self) -> Result<PpOd, Error<B::Error>> {
522        let reg = CtrlReg2::read(self).await?;
523
524        let val = PpOd::try_from(reg.pp_od()).unwrap_or_default();
525        Ok(val)
526    }
527    /// Set Interrupt Active Level
528    ///
529    /// Configures whether interrupts are active-high or active-low.
530    pub async fn pin_polarity_set(&mut self, val: IntHL) -> Result<(), Error<B::Error>> {
531        let mut reg = CtrlReg2::read(self).await?;
532        reg.set_int_h_l(val as u8);
533        reg.write(self).await?;
534
535        Ok(())
536    }
537    /// Get Interrupt Active Level
538    ///
539    /// Reads the current interrupt active-high or active-low configuration.
540    pub async fn pin_polarity_get(&mut self) -> Result<IntHL, Error<B::Error>> {
541        let reg = CtrlReg2::read(self).await?;
542
543        let val = IntHL::try_from(reg.int_h_l()).unwrap_or_default();
544
545        Ok(val)
546    }
547    /// Configure Interrupt Signal Routing on INT1 Pin
548    ///
549    /// Routes various interrupt signals such as data-ready, FIFO watermark, FIFO overrun, and
550    /// FIFO full flags to the INT1 pin.
551    pub async fn pin_int_route_set(&mut self, val: PinIntRoute) -> Result<(), Error<B::Error>> {
552        let mut ctrl_reg3 = CtrlReg3::read(self).await?;
553
554        ctrl_reg3.set_drdy(val.drdy_pres as u8);
555        ctrl_reg3.set_int_f_wtm(val.fifo_th as u8);
556        ctrl_reg3.set_int_f_ovr(val.fifo_ovr as u8);
557        ctrl_reg3.set_int_f_full(val.fifo_full as u8);
558
559        ctrl_reg3.write(self).await?;
560
561        Ok(())
562    }
563    /// Get Interrupt Signal Routing on INT1 Pin
564    ///
565    /// Reads which interrupt signals are currently routed to the INT1 pin.
566    pub async fn pin_int_route_get(&mut self) -> Result<PinIntRoute, Error<B::Error>> {
567        let ctrl_reg3 = CtrlReg3::read(self).await?;
568
569        let val = PinIntRoute {
570            drdy_pres: ctrl_reg3.drdy() != 0,
571            fifo_th: ctrl_reg3.int_f_wtm() != 0,
572            fifo_ovr: ctrl_reg3.int_f_ovr() != 0,
573            fifo_full: ctrl_reg3.int_f_full() != 0,
574        };
575
576        Ok(val)
577    }
578    /// Enable Interrupt on Pressure Threshold Events
579    ///
580    /// Configures interrupt generation on pressure low/high threshold events.
581    /// Disables interrupt generation if no threshold is selected.
582    pub async fn int_on_threshold_set(&mut self, val: Pe) -> Result<(), Error<B::Error>> {
583        let mut reg = InterruptCfg::read(self).await?;
584        reg.set_pe(val as u8);
585
586        if (val as u8) == (Pe::NoThreshold as u8) {
587            reg.set_diff_en(0_u8);
588        } else {
589            reg.set_diff_en(1_u8);
590        }
591
592        reg.write(self).await?;
593
594        Ok(())
595    }
596    /// Get Interrupt on Pressure Threshold Status
597    ///
598    /// Reads the current configuration of interrupt generation on pressure threshold events.
599    pub async fn int_on_threshold_get(&mut self) -> Result<Pe, Error<B::Error>> {
600        let reg = InterruptCfg::read(self).await?;
601
602        let val = Pe::try_from(reg.pe()).unwrap_or_default();
603        Ok(val)
604    }
605    /// Set Pressure Interrupt Threshold
606    ///
607    /// Writes a 15-bit user-defined threshold value for pressure interrupt events.
608    /// The threshold value is scaled as threshold (hPa) × 16.
609    pub async fn int_threshold_set(&mut self, buff: u16) -> Result<(), Error<B::Error>> {
610        ThsP::from_bits(buff).write(self).await
611    }
612    /// Get Pressure Interrupt Threshold
613    ///
614    /// Reads the 15-bit user-defined threshold value for pressure interrupt events.
615    pub async fn int_threshold_get(&mut self) -> Result<u16, Error<B::Error>> {
616        let val = ThsP::read(self).await?;
617        Ok(val.ths())
618    }
619    /// Set FIFO Mode
620    ///
621    /// Selects the FIFO operating mode such as bypass, FIFO, continuous stream, or triggered modes.
622    pub async fn fifo_mode_set(&mut self, val: FMode) -> Result<(), Error<B::Error>> {
623        let mut reg = FifoCtrl::read(self).await?;
624        reg.set_f_mode(val as u8);
625        reg.write(self).await
626    }
627    /// Get FIFO Mode
628    ///
629    /// Reads the current FIFO operating mode.
630    pub async fn fifo_mode_get(&mut self) -> Result<FMode, Error<B::Error>> {
631        let reg = FifoCtrl::read(self).await?;
632        let val = FMode::try_from(reg.f_mode()).unwrap_or_default();
633
634        Ok(val)
635    }
636    /// Enable or Disable FIFO Stop on Watermark
637    ///
638    /// Configures whether FIFO stops filling when the watermark level is reached.
639    pub async fn fifo_stop_on_wtm_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
640        let mut reg = FifoCtrl::read(self).await?;
641        reg.set_stop_on_wtm(val);
642        reg.write(self).await?;
643
644        Ok(())
645    }
646    /// Get FIFO Stop on Watermark Status
647    ///
648    /// Reads whether FIFO stop on watermark is enabled.
649    pub async fn fifo_stop_on_wtm_get(&mut self) -> Result<u8, Error<B::Error>> {
650        let val: u8 = FifoCtrl::read(self).await.map(|reg| reg.stop_on_wtm())?;
651
652        Ok(val)
653    }
654    /// Set FIFO Watermark Level
655    ///
656    /// Sets the FIFO watermark level which triggers interrupts when reached.
657    pub async fn fifo_watermark_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
658        let mut reg = FifoWtm::read(self).await?;
659        reg.set_wtm(val);
660        reg.write(self).await?;
661
662        Ok(())
663    }
664    /// Get FIFO Watermark Level
665    ///
666    /// Reads the current FIFO watermark level.
667    pub async fn fifo_watermark_get(&mut self) -> Result<u8, Error<B::Error>> {
668        let val: u8 = FifoWtm::read(self).await.map(|reg| reg.wtm())?;
669
670        Ok(val)
671    }
672    /// Get FIFO Data Level
673    ///
674    /// Reads the number of unread samples currently stored in the FIFO buffer.
675    pub async fn fifo_data_level_get(&mut self) -> Result<u8, Error<B::Error>> {
676        let val = FifoStatusReg::read(self).await?.into_bits();
677        Ok(val.to_le_bytes()[0])
678    }
679    /// Get FIFO Status Flags
680    ///
681    /// Reads FIFO status flags including full, overrun, and watermark interrupt active flags.
682    pub async fn fifo_src_get(&mut self) -> Result<FifoStatus2, Error<B::Error>> {
683        let val = FifoStatusReg::read(self).await?;
684        Ok(val.into())
685    }
686    /// Get FIFO Full Flag
687    ///
688    /// Indicates if the FIFO buffer is completely full.
689    pub async fn fifo_full_flag_get(&mut self) -> Result<u8, Error<B::Error>> {
690        let val: u8 = FifoStatusReg::read(self)
691            .await
692            .map(|reg| reg.fifo_full_ia())?;
693
694        Ok(val)
695    }
696    /// Get FIFO Overrun Flag
697    ///
698    /// Indicates if FIFO data has been overwritten due to overrun
699    pub async fn fifo_ovr_flag_get(&mut self) -> Result<u8, Error<B::Error>> {
700        let val: u8 = FifoStatusReg::read(self)
701            .await
702            .map(|reg| reg.fifo_ovr_ia())?;
703
704        Ok(val)
705    }
706    /// Get FIFO Watermark Flag
707    ///
708    /// Indicates if the FIFO watermark level has been reached.
709    pub async fn fifo_wtm_flag_get(&mut self) -> Result<u8, Error<B::Error>> {
710        let val: u8 = FifoStatusReg::read(self)
711            .await
712            .map(|reg| reg.fifo_wtm_ia())?;
713
714        Ok(val)
715    }
716}
717
718#[bisync]
719pub fn from_lsb_to_celsius(lsb: i16) -> f32 {
720    (lsb as f32) / 100.0
721}
722
723#[bisync]
724pub fn from_lsb_to_hpa(lsb: u32) -> f32 {
725    (lsb as f32) / 4096.0
726}
727
728/// I2CAddress - Available I2C addresses for the  sensor
729///
730/// Variants:
731/// - `AddressH` (0x5D): High address
732/// - `AddressL` (0x5C): Low address
733#[repr(u8)]
734#[bisync]
735pub enum I2CAddress {
736    AddressH = 0x5D,
737    AddressL = 0x5C,
738}
739
740/// Device ID for the  sensor
741#[bisync]
742pub const ID: u8 = 0xB3;