ms5637_rs/
lib.rs

1#![no_std]
2#![doc = include_str!("../README.md")]
3
4pub use ms56xx::{Error, I2cInterface, Measurement, OversamplingExtended as Oversampling};
5
6use ms56xx::{Ms56xx, Ms5637 as Ms5637Variant};
7
8/// MS5637 sensor driver.
9///
10/// Generic over the interface type (I2C only - MS5637 doesn't support SPI).
11pub struct Ms5637<INTERFACE> {
12    inner: Ms56xx<INTERFACE, Ms5637Variant>,
13}
14
15// Constructors for I2C
16impl<I2C> Ms5637<I2cInterface<I2C>> {
17    /// Create a new MS5637 sensor driver using I2C.
18    ///
19    /// The MS5637 has a fixed I2C address of 0x76.
20    ///
21    /// Note: You must call [`Self::init`] or [`Self::init_blocking`] before measurements.
22    pub fn new_i2c(i2c: I2C) -> Self {
23        Self {
24            inner: Ms56xx::new_i2c(i2c, false), // csb_high doesn't matter for MS5637
25        }
26    }
27
28    /// Create a new MS5637 sensor driver using I2C with an explicit address.
29    ///
30    /// This allows you to specify the exact I2C address. The MS5637 typically uses 0x76.
31    ///
32    /// Note: You must call [`Self::init`] or [`Self::init_blocking`] before measurements.
33    pub fn new_i2c_with_address(i2c: I2C, address: u8) -> Self {
34        Self {
35            inner: Ms56xx::new_i2c_with_address(i2c, address),
36        }
37    }
38
39    /// Get the configured I2C address of the sensor (always 0x76).
40    pub fn address(&self) -> u8 {
41        self.inner.address()
42    }
43
44    /// Release the underlying I2C bus.
45    pub fn destroy(self) -> I2C {
46        self.inner.destroy()
47    }
48}
49
50// Common methods for all interfaces
51impl<INTERFACE> Ms5637<INTERFACE> {
52    /// Returns true if the sensor has been initialized successfully.
53    pub fn is_initialized(&self) -> bool {
54        self.inner.is_initialized()
55    }
56}
57
58// Async methods for I2C
59impl<I2C: embedded_hal_async::i2c::I2c> Ms5637<I2cInterface<I2C>> {
60    /// Send reset command and wait for internal PROM reload (~3ms).
61    ///
62    /// # Errors
63    /// Returns an error if interface communication fails.
64    pub async fn reset(
65        &mut self,
66        delay: &mut impl embedded_hal_async::delay::DelayNs,
67    ) -> Result<(), I2C::Error> {
68        self.inner.reset(delay).await
69    }
70
71    /// Initialize the sensor by resetting it, reading PROM data and verifying the CRC.
72    ///
73    /// # Errors
74    /// Returns [`Error::BusError`] if interface communication fails, or [`Error::CrcMismatch`] if the
75    /// PROM CRC check fails.
76    pub async fn init(
77        &mut self,
78        delay: &mut impl embedded_hal_async::delay::DelayNs,
79    ) -> Result<(), Error<I2C::Error>> {
80        self.inner.init(delay).await
81    }
82
83    /// Carry out a complete measurement and conversion of pressure and temperature.
84    ///
85    /// # Errors
86    /// Returns [`Error::NotInitialized`] if the sensor has not been initialized yet, or
87    /// [`Error::BusError`] if interface communication fails.
88    pub async fn measure(
89        &mut self,
90        osr: Oversampling,
91        delay: &mut impl embedded_hal_async::delay::DelayNs,
92    ) -> Result<Measurement, Error<I2C::Error>> {
93        self.inner.measure(osr, delay).await
94    }
95}
96
97// Blocking methods for I2C
98impl<I2C: embedded_hal::i2c::I2c> Ms5637<I2cInterface<I2C>> {
99    /// Send reset command and wait for internal PROM reload (~3ms).
100    ///
101    /// This is the blocking version.
102    ///
103    /// # Errors
104    /// Returns an error if interface communication fails.
105    pub fn reset_blocking(
106        &mut self,
107        delay: &mut impl embedded_hal::delay::DelayNs,
108    ) -> Result<(), I2C::Error> {
109        self.inner.reset_blocking(delay)
110    }
111
112    /// Initialize the sensor by resetting it, reading PROM data and verifying the CRC.
113    ///
114    /// This is the blocking version.
115    ///
116    /// # Errors
117    /// Returns [`Error::BusError`] if interface communication fails, or [`Error::CrcMismatch`] if the
118    /// PROM CRC check fails.
119    pub fn init_blocking(
120        &mut self,
121        delay: &mut impl embedded_hal::delay::DelayNs,
122    ) -> Result<(), Error<I2C::Error>> {
123        self.inner.init_blocking(delay)
124    }
125
126    /// Carry out a complete measurement and conversion of pressure and temperature.
127    ///
128    /// This is the blocking version.
129    ///
130    /// # Errors
131    /// Returns [`Error::NotInitialized`] if the sensor has not been initialized yet, or
132    /// [`Error::BusError`] if interface communication fails.
133    pub fn measure_blocking(
134        &mut self,
135        osr: Oversampling,
136        delay: &mut impl embedded_hal::delay::DelayNs,
137    ) -> Result<Measurement, Error<I2C::Error>> {
138        self.inner.measure_blocking(osr, delay)
139    }
140}