ms5607_rs/lib.rs
1#![no_std]
2#![doc = include_str!("../README.md")]
3
4pub use ms56xx::{
5 Error, I2cInterface, Measurement, OversamplingStandard as Oversampling, SpiInterface,
6};
7
8use ms56xx::{Ms56xx, Ms5607 as Ms5607Variant};
9
10/// MS5607 sensor driver.
11///
12/// Generic over the interface type (I2C or SPI).
13pub struct Ms5607<INTERFACE> {
14 inner: Ms56xx<INTERFACE, Ms5607Variant>,
15}
16
17// Constructors for I2C
18impl<I2C> Ms5607<I2cInterface<I2C>> {
19 /// Create a new MS5607 sensor driver using I2C with address based on CSB pin state.
20 /// - `csb_high` = true => 0x76
21 /// - `csb_high` = false => 0x77
22 ///
23 /// Note: You must call [`Self::init`] or [`Self::init_blocking`] before measurements.
24 pub fn new_i2c(i2c: I2C, csb_high: bool) -> Self {
25 Self {
26 inner: Ms56xx::new_i2c(i2c, csb_high),
27 }
28 }
29
30 /// Create a new MS5607 sensor driver using I2C with an explicit address.
31 ///
32 /// This allows you to specify the exact I2C address (typically 0x76 or 0x77).
33 ///
34 /// Note: You must call [`Self::init`] or [`Self::init_blocking`] before measurements.
35 pub fn new_i2c_with_address(i2c: I2C, address: u8) -> Self {
36 Self {
37 inner: Ms56xx::new_i2c_with_address(i2c, address),
38 }
39 }
40
41 /// Get the configured I2C address of the sensor.
42 pub fn address(&self) -> u8 {
43 self.inner.address()
44 }
45
46 /// Release the underlying I2C bus.
47 pub fn destroy(self) -> I2C {
48 self.inner.destroy()
49 }
50}
51
52// Constructors for SPI
53impl<SPI> Ms5607<SpiInterface<SPI>> {
54 /// Create a new MS5607 sensor driver using SPI.
55 ///
56 /// Note: CS (chip select) must be handled by the provided `SpiDevice`.
57 /// You must call [`Self::init`] or [`Self::init_blocking`] before measurements.
58 pub fn new_spi(spi: SPI) -> Self {
59 Self {
60 inner: Ms56xx::new_spi(spi),
61 }
62 }
63
64 /// Release the underlying SPI device.
65 pub fn destroy(self) -> SPI {
66 self.inner.destroy()
67 }
68}
69
70// Common methods for all interfaces
71impl<INTERFACE> Ms5607<INTERFACE> {
72 /// Returns true if the sensor has been initialized successfully.
73 pub fn is_initialized(&self) -> bool {
74 self.inner.is_initialized()
75 }
76}
77
78// Async methods for I2C
79impl<I2C: embedded_hal_async::i2c::I2c> Ms5607<I2cInterface<I2C>> {
80 /// Send reset command and wait for internal PROM reload (~3ms).
81 ///
82 /// # Errors
83 /// Returns an error if interface communication fails.
84 pub async fn reset(
85 &mut self,
86 delay: &mut impl embedded_hal_async::delay::DelayNs,
87 ) -> Result<(), I2C::Error> {
88 self.inner.reset(delay).await
89 }
90
91 /// Initialize the sensor by resetting it, reading PROM data and verifying the CRC.
92 ///
93 /// # Errors
94 /// Returns [`Error::BusError`] if interface communication fails, or [`Error::CrcMismatch`] if the
95 /// PROM CRC check fails.
96 pub async fn init(
97 &mut self,
98 delay: &mut impl embedded_hal_async::delay::DelayNs,
99 ) -> Result<(), Error<I2C::Error>> {
100 self.inner.init(delay).await
101 }
102
103 /// Carry out a complete measurement and conversion of pressure and temperature.
104 ///
105 /// # Errors
106 /// Returns [`Error::NotInitialized`] if the sensor has not been initialized yet, or
107 /// [`Error::BusError`] if interface communication fails.
108 pub async fn measure(
109 &mut self,
110 osr: Oversampling,
111 delay: &mut impl embedded_hal_async::delay::DelayNs,
112 ) -> Result<Measurement, Error<I2C::Error>> {
113 self.inner.measure(osr, delay).await
114 }
115}
116
117// Blocking methods for I2C
118impl<I2C: embedded_hal::i2c::I2c> Ms5607<I2cInterface<I2C>> {
119 /// Send reset command and wait for internal PROM reload (~3ms).
120 ///
121 /// This is the blocking version.
122 ///
123 /// # Errors
124 /// Returns an error if interface communication fails.
125 pub fn reset_blocking(
126 &mut self,
127 delay: &mut impl embedded_hal::delay::DelayNs,
128 ) -> Result<(), I2C::Error> {
129 self.inner.reset_blocking(delay)
130 }
131
132 /// Initialize the sensor by resetting it, reading PROM data and verifying the CRC.
133 ///
134 /// This is the blocking version.
135 ///
136 /// # Errors
137 /// Returns [`Error::BusError`] if interface communication fails, or [`Error::CrcMismatch`] if the
138 /// PROM CRC check fails.
139 pub fn init_blocking(
140 &mut self,
141 delay: &mut impl embedded_hal::delay::DelayNs,
142 ) -> Result<(), Error<I2C::Error>> {
143 self.inner.init_blocking(delay)
144 }
145
146 /// Carry out a complete measurement and conversion of pressure and temperature.
147 ///
148 /// This is the blocking version.
149 ///
150 /// # Errors
151 /// Returns [`Error::NotInitialized`] if the sensor has not been initialized yet, or
152 /// [`Error::BusError`] if interface communication fails.
153 pub fn measure_blocking(
154 &mut self,
155 osr: Oversampling,
156 delay: &mut impl embedded_hal::delay::DelayNs,
157 ) -> Result<Measurement, Error<I2C::Error>> {
158 self.inner.measure_blocking(osr, delay)
159 }
160}
161
162// Async methods for SPI
163impl<SPI: embedded_hal_async::spi::SpiDevice> Ms5607<SpiInterface<SPI>> {
164 /// Send reset command and wait for internal PROM reload (~3ms).
165 ///
166 /// # Errors
167 /// Returns an error if interface communication fails.
168 pub async fn reset(
169 &mut self,
170 delay: &mut impl embedded_hal_async::delay::DelayNs,
171 ) -> Result<(), SPI::Error> {
172 self.inner.reset(delay).await
173 }
174
175 /// Initialize the sensor by resetting it, reading PROM data and verifying the CRC.
176 ///
177 /// # Errors
178 /// Returns [`Error::BusError`] if interface communication fails, or [`Error::CrcMismatch`] if the
179 /// PROM CRC check fails.
180 pub async fn init(
181 &mut self,
182 delay: &mut impl embedded_hal_async::delay::DelayNs,
183 ) -> Result<(), Error<SPI::Error>> {
184 self.inner.init(delay).await
185 }
186
187 /// Carry out a complete measurement and conversion of pressure and temperature.
188 ///
189 /// # Errors
190 /// Returns [`Error::NotInitialized`] if the sensor has not been initialized yet, or
191 /// [`Error::BusError`] if interface communication fails.
192 pub async fn measure(
193 &mut self,
194 osr: Oversampling,
195 delay: &mut impl embedded_hal_async::delay::DelayNs,
196 ) -> Result<Measurement, Error<SPI::Error>> {
197 self.inner.measure(osr, delay).await
198 }
199}
200
201// Blocking methods for SPI
202impl<SPI: embedded_hal::spi::SpiDevice> Ms5607<SpiInterface<SPI>> {
203 /// Send reset command and wait for internal PROM reload (~3ms).
204 ///
205 /// This is the blocking version.
206 ///
207 /// # Errors
208 /// Returns an error if interface communication fails.
209 pub fn reset_blocking(
210 &mut self,
211 delay: &mut impl embedded_hal::delay::DelayNs,
212 ) -> Result<(), SPI::Error> {
213 self.inner.reset_blocking(delay)
214 }
215
216 /// Initialize the sensor by resetting it, reading PROM data and verifying the CRC.
217 ///
218 /// This is the blocking version.
219 ///
220 /// # Errors
221 /// Returns [`Error::BusError`] if interface communication fails, or [`Error::CrcMismatch`] if the
222 /// PROM CRC check fails.
223 pub fn init_blocking(
224 &mut self,
225 delay: &mut impl embedded_hal::delay::DelayNs,
226 ) -> Result<(), Error<SPI::Error>> {
227 self.inner.init_blocking(delay)
228 }
229
230 /// Carry out a complete measurement and conversion of pressure and temperature.
231 ///
232 /// This is the blocking version.
233 ///
234 /// # Errors
235 /// Returns [`Error::NotInitialized`] if the sensor has not been initialized yet, or
236 /// [`Error::BusError`] if interface communication fails.
237 pub fn measure_blocking(
238 &mut self,
239 osr: Oversampling,
240 delay: &mut impl embedded_hal::delay::DelayNs,
241 ) -> Result<Measurement, Error<SPI::Error>> {
242 self.inner.measure_blocking(osr, delay)
243 }
244}