summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tomasz@kramkow.ski>2024-12-08 19:11:48 +0000
committerTomasz Kramkowski <tomasz@kramkow.ski>2024-12-08 19:12:59 +0000
commit9bf7e6fc3b5f75575955791be1025b09b2150c35 (patch)
treee19624cc5fcb1fd5aad3c805a04947b17687312c
parent1e5f4caa9ef8e7823adf492211888a267e00040a (diff)
downloadmax31855-async-9bf7e6fc3b5f75575955791be1025b09b2150c35.tar.gz
max31855-async-9bf7e6fc3b5f75575955791be1025b09b2150c35.tar.xz
max31855-async-9bf7e6fc3b5f75575955791be1025b09b2150c35.zip
formatting
-rw-r--r--Cargo.toml4
-rw-r--r--src/lib.rs78
2 files changed, 46 insertions, 36 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 7871ca3..ed1847b 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,5 +13,5 @@ documentation = "https://docs.rs/max31855"
homepage = "https://github.com/cs2dsb/max31855.rs"
[dependencies]
-embedded-hal = { version = "0.2.3" }
-bit_field = "0.10.0" \ No newline at end of file
+embedded-hal = version = "0.2.3"
+bit_field = "0.10.0"
diff --git a/src/lib.rs b/src/lib.rs
index d88d273..2663345 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,5 +1,5 @@
//! # max31855
-//!
+//!
//! Driver for [MAX31855 thermocouple converter](https://www.maximintegrated.com/en/products/sensors/MAX31855.html) using traits from `embedded-hal`.
//!
//! ## Features
@@ -17,7 +17,7 @@
//! polarity: Polarity::IdleLow,
//! phase: Phase::CaptureOnFirstTransition
//! };
-//!
+//!
//! let mut spi = Spi::spi2(
//! device.SPI2,
//! (sck_pin, miso_pin, mosi_pin)
@@ -26,13 +26,13 @@
//! clocks,
//! &mut rcc.apb1
//! );
-//!
+//!
//! // Full 32-bit read, result contains both thermocouple and internal temperatures
//! match spi.read_all(&mut cs_pin, Unit::Celsius) {
//! Ok(v) => info!("Ok: {:?}", v),
//! Err(e) => info!("Err: {:?}", e),
//! }
-//!
+//!
//! // Just thermocouple 16-bit read
//! match spi.read_thermocouple(&mut cs_pin, Unit::Celsius) {
//! Ok(v) => info!("Ok: {:?}", v),
@@ -43,12 +43,9 @@
#![no_std]
#![deny(warnings, missing_docs)]
-use embedded_hal::{
- blocking::spi::Transfer,
- digital::v2::OutputPin,
-};
use bit_field::BitField;
use core::ops::RangeInclusive;
+use embedded_hal::{blocking::spi::Transfer, digital::v2::OutputPin};
/// The bits that represent the thermocouple value when reading the first u16 from the sensor
const THERMOCOUPLE_BITS: RangeInclusive<usize> = 2..=15;
@@ -129,7 +126,7 @@ use CsState::*;
fn set_cs<CS, SpiE, CsE>(cs: &mut CS, state: CsState) -> Result<(), Error<SpiE, CsE>>
where
- CS: OutputPin<Error = CsE>,
+ CS: OutputPin<Error = CsE>,
{
let result = match state {
CsState::High => cs.set_high(),
@@ -139,9 +136,13 @@ where
result.map_err(|e| Error::ChipSelectError(e))
}
-fn transfer<CS, SPI, SpiE, CsE>(spi: &mut SPI, chip_select: &mut CS, buffer: &mut [u8]) -> Result<(), Error<SpiE, CsE>>
+fn transfer<CS, SPI, SpiE, CsE>(
+ spi: &mut SPI,
+ chip_select: &mut CS,
+ buffer: &mut [u8],
+) -> Result<(), Error<SpiE, CsE>>
where
- CS: OutputPin<Error = CsE>,
+ CS: OutputPin<Error = CsE>,
SPI: Transfer<u8, Error = SpiE>,
{
set_cs(chip_select, Low)?;
@@ -171,8 +172,8 @@ pub struct FullResultRaw {
impl FullResultRaw {
/// Convert the raw ADC counts into degrees in the provided Unit
- pub fn convert(self, unit: Unit) -> FullResult {
- let thermocouple = unit.convert(Reading::Thermocouple.convert(self.thermocouple));
+ pub fn convert(self, unit: Unit) -> FullResult {
+ let thermocouple = unit.convert(Reading::Thermocouple.convert(self.thermocouple));
let internal = unit.convert(Reading::Internal.convert(self.internal));
FullResult {
@@ -199,16 +200,24 @@ 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>>;
/// 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>>;
+ fn read_thermocouple(
+ &mut self,
+ chip_select: &mut CS,
+ unit: Unit,
+ ) -> 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>>;
/// 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>>;
+ fn read_all(
+ &mut self,
+ chip_select: &mut CS,
+ unit: Unit,
+ ) -> Result<FullResult, Error<SpiE, CsE>>;
}
impl<CS, SPI, SpiE, CsE> Max31855<SpiE, CsE, CS> for SPI
where
- CS: OutputPin<Error = CsE>,
+ CS: OutputPin<Error = CsE>,
SPI: Transfer<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
@@ -220,18 +229,20 @@ where
Err(Error::Fault)?
}
- let raw = (buffer[0] as u16) << 8 |
- (buffer[1] as u16) << 0;
+ let raw = (buffer[0] as u16) << 8 | (buffer[1] as u16) << 0;
let thermocouple = bits_to_i16(raw.get_bits(THERMOCOUPLE_BITS), 14, 4, 2);
-
+
Ok(thermocouple)
}
/// 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>> {
- self
- .read_thermocouple_raw(chip_select)
+ fn read_thermocouple(
+ &mut self,
+ chip_select: &mut CS,
+ unit: Unit,
+ ) -> Result<f32, Error<SpiE, CsE>> {
+ self.read_thermocouple_raw(chip_select)
.map(|r| unit.convert(Reading::Thermocouple.convert(r)))
}
@@ -243,8 +254,7 @@ where
let fault = buffer[1].get_bit(0);
if fault {
- let raw = (buffer[2] as u16) << 8 |
- (buffer[3] as u16) << 0;
+ let raw = (buffer[2] as u16) << 8 | (buffer[3] as u16) << 0;
if raw.get_bit(FAULT_NO_THERMOCOUPLE_BIT) {
Err(Error::MissingThermocoupleFault)?
@@ -259,11 +269,9 @@ where
}
}
- let first_u16 = (buffer[0] as u16) << 8 |
- (buffer[1] as u16) << 0;
- let second_u16 = (buffer[2] as u16) << 8 |
- (buffer[3] as u16) << 0;
-
+ let first_u16 = (buffer[0] as u16) << 8 | (buffer[1] as u16) << 0;
+ let second_u16 = (buffer[2] as u16) << 8 | (buffer[3] as u16) << 0;
+
let thermocouple = bits_to_i16(first_u16.get_bits(THERMOCOUPLE_BITS), 14, 4, 2);
let internal = bits_to_i16(second_u16.get_bits(INTERNAL_BITS), 12, 16, 4);
@@ -274,9 +282,11 @@ 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(&mut self, chip_select: &mut CS, unit: Unit) -> Result<FullResult, Error<SpiE, CsE>> {
- self
- .read_all_raw(chip_select)
- .map(|r| r.convert(unit))
+ 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))
}
-} \ No newline at end of file
+}