diff options
author | Tomasz Kramkowski <tomasz@kramkow.ski> | 2024-12-08 21:04:43 +0000 |
---|---|---|
committer | Tomasz Kramkowski <tomasz@kramkow.ski> | 2024-12-08 21:04:43 +0000 |
commit | 0d08aa612d283a0220794738be18f549ed7d8fe4 (patch) | |
tree | 9162356f8d2433cd9eda6057c62ecc82ebe61c8b | |
parent | 19783d6b8c6bf8a8c065e75a5c0aeeebb23a84e9 (diff) | |
download | max31855-async-0d08aa612d283a0220794738be18f549ed7d8fe4.tar.gz max31855-async-0d08aa612d283a0220794738be18f549ed7d8fe4.tar.xz max31855-async-0d08aa612d283a0220794738be18f549ed7d8fe4.zip |
Change to async
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/lib.rs | 50 |
2 files changed, 34 insertions, 17 deletions
@@ -15,3 +15,4 @@ homepage = "https://github.com/cs2dsb/max31855.rs" [dependencies] embedded-hal = "1.0.0" bit_field = "0.10.0" +embedded-hal-async = "1.0.0" @@ -28,13 +28,13 @@ //! ); //! //! // Full 32-bit read, result contains both thermocouple and internal temperatures -//! match spi.read_all(&mut cs_pin, Unit::Celsius) { +//! match spi.read_all(&mut cs_pin, Unit::Celsius).await { //! Ok(v) => info!("Ok: {:?}", v), //! Err(e) => info!("Err: {:?}", e), //! } //! //! // Just thermocouple 16-bit read -//! match spi.read_thermocouple(&mut cs_pin, Unit::Celsius) { +//! match spi.read_thermocouple(&mut cs_pin, Unit::Celsius).await { //! Ok(v) => info!("Ok: {:?}", v), //! Err(e) => info!("Err: {:?}", e), //! } @@ -44,8 +44,9 @@ #![deny(warnings, missing_docs)] use bit_field::BitField; -use core::ops::RangeInclusive; -use embedded_hal::{spi::SpiDevice, digital::OutputPin}; +use core::{future::Future, ops::RangeInclusive}; +use embedded_hal::digital::OutputPin; +use embedded_hal_async::spi::SpiDevice; /// The bits that represent the thermocouple value when reading the first u16 from the sensor const THERMOCOUPLE_BITS: RangeInclusive<usize> = 2..=15; @@ -136,7 +137,7 @@ where result.map_err(|e| Error::ChipSelectError(e)) } -fn transfer<CS, SPI, SpiE, CsE>( +async fn transfer<CS, SPI, SpiE, CsE>( spi: &mut SPI, chip_select: &mut CS, buffer: &mut [u8], @@ -147,7 +148,7 @@ where { set_cs(chip_select, Low)?; - spi.read(buffer).map_err(|e| Error::SpiError(e))?; + spi.read(buffer).await.map_err(|e| Error::SpiError(e))?; set_cs(chip_select, High) } @@ -198,21 +199,27 @@ pub struct FullResult { /// Trait enabling using the MAX31855 pub trait Max31855<SpiE, CsE, CS> { /// Reads the thermocouple temperature and leave it as a raw ADC count. Checks if there is a fault but doesn't detect what kind of fault it is - fn read_thermocouple_raw(&mut self, chip_select: &mut CS) -> Result<i16, Error<SpiE, CsE>>; + fn read_thermocouple_raw( + &mut self, + chip_select: &mut CS, + ) -> impl Future<Output = Result<i16, Error<SpiE, CsE>>>; /// Reads the thermocouple temperature and converts it into degrees in the provided unit. Checks if there is a fault but doesn't detect what kind of fault it is fn read_thermocouple( &mut self, chip_select: &mut CS, unit: Unit, - ) -> Result<f32, Error<SpiE, CsE>>; + ) -> impl Future<Output = Result<f32, Error<SpiE, CsE>>>; /// Reads both the thermocouple and the internal temperatures, leaving them as raw ADC counts and resolves faults to one of vcc short, ground short or missing thermocouple - fn read_all_raw(&mut self, chip_select: &mut CS) -> Result<FullResultRaw, Error<SpiE, CsE>>; + fn read_all_raw( + &mut self, + chip_select: &mut CS, + ) -> impl Future<Output = Result<FullResultRaw, Error<SpiE, CsE>>>; /// Reads both the thermocouple and the internal temperatures, converts them into degrees in the provided unit and resolves faults to one of vcc short, ground short or missing thermocouple fn read_all( &mut self, chip_select: &mut CS, unit: Unit, - ) -> Result<FullResult, Error<SpiE, CsE>>; + ) -> impl Future<Output = Result<FullResult, Error<SpiE, CsE>>>; } impl<CS, SPI, SpiE, CsE> Max31855<SpiE, CsE, CS> for SPI @@ -221,9 +228,12 @@ where SPI: SpiDevice<u8, Error = SpiE>, { /// Reads the thermocouple temperature and leave it as a raw ADC count. Checks if there is a fault but doesn't detect what kind of fault it is - fn read_thermocouple_raw(&mut self, chip_select: &mut CS) -> Result<i16, Error<SpiE, CsE>> { + async fn read_thermocouple_raw( + &mut self, + chip_select: &mut CS, + ) -> Result<i16, Error<SpiE, CsE>> { let mut buffer = [0; 2]; - transfer(self, chip_select, &mut buffer)?; + transfer(self, chip_select, &mut buffer).await?; if buffer[1].get_bit(FAULT_BIT) { Err(Error::Fault)? @@ -237,19 +247,23 @@ where } /// Reads the thermocouple temperature and converts it into degrees in the provided unit. Checks if there is a fault but doesn't detect what kind of fault it is - fn read_thermocouple( + async fn read_thermocouple( &mut self, chip_select: &mut CS, unit: Unit, ) -> Result<f32, Error<SpiE, CsE>> { self.read_thermocouple_raw(chip_select) + .await .map(|r| unit.convert(Reading::Thermocouple.convert(r))) } /// Reads both the thermocouple and the internal temperatures, leaving them as raw ADC counts and resolves faults to one of vcc short, ground short or missing thermocouple - fn read_all_raw(&mut self, chip_select: &mut CS) -> Result<FullResultRaw, Error<SpiE, CsE>> { + async fn read_all_raw( + &mut self, + chip_select: &mut CS, + ) -> Result<FullResultRaw, Error<SpiE, CsE>> { let mut buffer = [0; 4]; - transfer(self, chip_select, &mut buffer)?; + transfer(self, chip_select, &mut buffer).await?; let fault = buffer[1].get_bit(0); @@ -282,11 +296,13 @@ where } /// Reads both the thermocouple and the internal temperatures, converts them into degrees in the provided unit and resolves faults to one of vcc short, ground short or missing thermocouple - fn read_all( + async fn read_all( &mut self, chip_select: &mut CS, unit: Unit, ) -> Result<FullResult, Error<SpiE, CsE>> { - self.read_all_raw(chip_select).map(|r| r.convert(unit)) + self.read_all_raw(chip_select) + .await + .map(|r| r.convert(unit)) } } |