diff options
-rw-r--r-- | src/lib.rs | 95 |
1 files changed, 17 insertions, 78 deletions
@@ -45,7 +45,6 @@ use bit_field::BitField; 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 @@ -63,11 +62,9 @@ const FAULT_NO_THERMOCOUPLE_BIT: usize = 0; /// Possible errors returned by this crate #[derive(Debug)] -pub enum Error<SpiE, CsE> { +pub enum Error<SpiE> { /// An error returned by a call to Transfer::transfer SpiError(SpiE), - /// An error returned by a call to OutputPin::{set_high, set_low} - ChipSelectError(CsE), /// The fault bit (16) was set in the response from the MAX31855 Fault, /// The SCV fault bit (2) was set in the response from the MAX31855 @@ -119,38 +116,11 @@ impl Reading { } } -enum CsState { - High, - Low, -} -use CsState::*; - -fn set_cs<CS, SpiE, CsE>(cs: &mut CS, state: CsState) -> Result<(), Error<SpiE, CsE>> -where - CS: OutputPin<Error = CsE>, -{ - let result = match state { - CsState::High => cs.set_high(), - CsState::Low => cs.set_low(), - }; - - result.map_err(|e| Error::ChipSelectError(e)) -} - -async fn transfer<CS, SPI, SpiE, CsE>( - spi: &mut SPI, - chip_select: &mut CS, - buffer: &mut [u8], -) -> Result<(), Error<SpiE, CsE>> +async fn transfer<SPI, SpiE>(spi: &mut SPI, buffer: &mut [u8]) -> Result<(), Error<SpiE>> where - CS: OutputPin<Error = CsE>, SPI: SpiDevice<u8, Error = SpiE>, { - set_cs(chip_select, Low)?; - - spi.read(buffer).await.map_err(|e| Error::SpiError(e))?; - - set_cs(chip_select, High) + spi.read(buffer).await.map_err(|e| Error::SpiError(e)) } fn bits_to_i16(bits: u16, len: usize, divisor: i16, shift: usize) -> i16 { @@ -197,43 +167,25 @@ pub struct FullResult { } /// Trait enabling using the MAX31855 -pub trait Max31855<SpiE, CsE, CS> { +pub trait Max31855<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, - ) -> impl Future<Output = Result<i16, Error<SpiE, CsE>>>; + fn read_thermocouple_raw(&mut self) -> impl Future<Output = Result<i16, Error<SpiE>>>; /// 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, - ) -> impl Future<Output = Result<f32, Error<SpiE, CsE>>>; + fn read_thermocouple(&mut self, unit: Unit) -> impl Future<Output = Result<f32, Error<SpiE>>>; /// 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, - ) -> impl Future<Output = Result<FullResultRaw, Error<SpiE, CsE>>>; + fn read_all_raw(&mut self) -> impl Future<Output = Result<FullResultRaw, Error<SpiE>>>; /// 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, - ) -> impl Future<Output = Result<FullResult, Error<SpiE, CsE>>>; + fn read_all(&mut self, unit: Unit) -> impl Future<Output = Result<FullResult, Error<SpiE>>>; } -impl<CS, SPI, SpiE, CsE> Max31855<SpiE, CsE, CS> for SPI +impl<SPI, SpiE> Max31855<SpiE> for SPI where - CS: OutputPin<Error = CsE>, 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 - async fn read_thermocouple_raw( - &mut self, - chip_select: &mut CS, - ) -> Result<i16, Error<SpiE, CsE>> { + async fn read_thermocouple_raw(&mut self) -> Result<i16, Error<SpiE>> { let mut buffer = [0; 2]; - transfer(self, chip_select, &mut buffer).await?; + transfer(self, &mut buffer).await?; if buffer[1].get_bit(FAULT_BIT) { Err(Error::Fault)? @@ -247,23 +199,16 @@ 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 - async fn read_thermocouple( - &mut self, - chip_select: &mut CS, - unit: Unit, - ) -> Result<f32, Error<SpiE, CsE>> { - self.read_thermocouple_raw(chip_select) + async fn read_thermocouple(&mut self, unit: Unit) -> Result<f32, Error<SpiE>> { + self.read_thermocouple_raw() .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 - async fn read_all_raw( - &mut self, - chip_select: &mut CS, - ) -> Result<FullResultRaw, Error<SpiE, CsE>> { + async fn read_all_raw(&mut self) -> Result<FullResultRaw, Error<SpiE>> { let mut buffer = [0; 4]; - transfer(self, chip_select, &mut buffer).await?; + transfer(self, &mut buffer).await?; let fault = buffer[1].get_bit(0); @@ -296,13 +241,7 @@ 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 - async fn read_all( - &mut self, - chip_select: &mut CS, - unit: Unit, - ) -> Result<FullResult, Error<SpiE, CsE>> { - self.read_all_raw(chip_select) - .await - .map(|r| r.convert(unit)) + async fn read_all(&mut self, unit: Unit) -> Result<FullResult, Error<SpiE>> { + self.read_all_raw().await.map(|r| r.convert(unit)) } } |