From 0d08aa612d283a0220794738be18f549ed7d8fe4 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Sun, 8 Dec 2024 21:04:43 +0000 Subject: Change to async --- Cargo.toml | 1 + src/lib.rs | 50 +++++++++++++++++++++++++++++++++----------------- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fc22f5f..70c97dc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/lib.rs b/src/lib.rs index 2f8d64b..2cedba1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 = 2..=15; @@ -136,7 +137,7 @@ where result.map_err(|e| Error::ChipSelectError(e)) } -fn transfer( +async fn transfer( 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 { /// 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>; + fn read_thermocouple_raw( + &mut self, + chip_select: &mut CS, + ) -> impl Future>>; /// 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>; + ) -> impl Future>>; /// 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>; + fn read_all_raw( + &mut self, + chip_select: &mut CS, + ) -> impl Future>>; /// 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>; + ) -> impl Future>>; } impl Max31855 for SPI @@ -221,9 +228,12 @@ where SPI: SpiDevice, { /// 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> { + async fn read_thermocouple_raw( + &mut self, + chip_select: &mut CS, + ) -> Result> { 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> { 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> { + async fn read_all_raw( + &mut self, + chip_select: &mut CS, + ) -> Result> { 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> { - self.read_all_raw(chip_select).map(|r| r.convert(unit)) + self.read_all_raw(chip_select) + .await + .map(|r| r.convert(unit)) } } -- cgit v1.2.3-54-g00ecf