From 0c2fd208a6baa3d42e965584fa10326f178118d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20R=C3=B6ssler?= Date: Wed, 28 Feb 2024 10:52:38 +0100 Subject: [PATCH 1/5] fixes for new one-wire-bus and embedded-hal --- Cargo.toml | 12 +++++++++--- src/lib.rs | 30 +++++++++++++++--------------- src/resolution.rs | 10 +++++----- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 88073bc..55bb652 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,13 @@ authors = ["Nathan Fox "] edition = "2018" license = "MIT/Apache-2.0" repository = "https://github.com/fuchsnj/ds18b20" -keywords = ["onewire", "embedded-hal-driver", "ds18b20", "temperature", "sensor"] +keywords = [ + "onewire", + "embedded-hal-driver", + "ds18b20", + "temperature", + "sensor", +] readme = "README.md" categories = ["embedded", "hardware-support", "no-std"] description = """ @@ -13,5 +19,5 @@ A Rust DS18B20 temperature sensor driver for [embedded-hal](https://github.com/r """ [dependencies] -one-wire-bus = "0.1.1" -embedded-hal = {version="0.2.3", features=["unproven"]} \ No newline at end of file +one-wire-bus.path = "../one-wire-bus" +embedded-hal = "1.0.0" diff --git a/src/lib.rs b/src/lib.rs index a822134..2dcff9a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,8 +2,8 @@ //! # Test Test -use embedded_hal::blocking::delay::DelayUs; -use embedded_hal::digital::v2::{InputPin, OutputPin}; +use embedded_hal::delay::DelayNs; +use embedded_hal::digital::{InputPin, OutputPin}; use one_wire_bus::{self, Address, OneWire, OneWireError, OneWireResult}; pub const FAMILY_CODE: u8 = 0x28; @@ -56,7 +56,7 @@ impl Ds18b20 { pub fn start_temp_measurement( &self, onewire: &mut OneWire, - delay: &mut impl DelayUs, + delay: &mut impl DelayNs, ) -> OneWireResult<(), E> where T: InputPin, @@ -69,7 +69,7 @@ impl Ds18b20 { pub fn read_data( &self, onewire: &mut OneWire, - delay: &mut impl DelayUs, + delay: &mut impl DelayNs, ) -> OneWireResult where T: InputPin, @@ -85,7 +85,7 @@ impl Ds18b20 { alarm_temp_high: i8, resolution: Resolution, onewire: &mut OneWire, - delay: &mut impl DelayUs, + delay: &mut impl DelayNs, ) -> OneWireResult<(), E> where T: InputPin, @@ -101,7 +101,7 @@ impl Ds18b20 { pub fn save_to_eeprom( &self, onewire: &mut OneWire, - delay: &mut impl DelayUs, + delay: &mut impl DelayNs, ) -> OneWireResult<(), E> where T: InputPin, @@ -113,7 +113,7 @@ impl Ds18b20 { pub fn recall_from_eeprom( &self, onewire: &mut OneWire, - delay: &mut impl DelayUs, + delay: &mut impl DelayNs, ) -> OneWireResult<(), E> where T: InputPin, @@ -126,7 +126,7 @@ impl Ds18b20 { /// Starts a temperature measurement for all devices on this one-wire bus, simultaneously pub fn start_simultaneous_temp_measurement( onewire: &mut OneWire, - delay: &mut impl DelayUs, + delay: &mut impl DelayNs, ) -> OneWireResult<(), E> where T: InputPin, @@ -141,7 +141,7 @@ where /// Read the contents of the EEPROM config to the scratchpad for all devices simultaneously. pub fn simultaneous_recall_from_eeprom( onewire: &mut OneWire, - delay: &mut impl DelayUs, + delay: &mut impl DelayNs, ) -> OneWireResult<(), E> where T: InputPin, @@ -153,7 +153,7 @@ where /// Read the config contents of the scratchpad memory to the EEPROMfor all devices simultaneously. pub fn simultaneous_save_to_eeprom( onewire: &mut OneWire, - delay: &mut impl DelayUs, + delay: &mut impl DelayNs, ) -> OneWireResult<(), E> where T: InputPin, @@ -165,7 +165,7 @@ where pub fn read_scratchpad( address: &Address, onewire: &mut OneWire, - delay: &mut impl DelayUs, + delay: &mut impl DelayNs, ) -> OneWireResult<[u8; 9], E> where T: InputPin, @@ -183,7 +183,7 @@ where fn read_data( address: &Address, onewire: &mut OneWire, - delay: &mut impl DelayUs, + delay: &mut impl DelayNs, ) -> OneWireResult where T: InputPin, @@ -214,7 +214,7 @@ where fn recall_from_eeprom( address: Option<&Address>, onewire: &mut OneWire, - delay: &mut impl DelayUs, + delay: &mut impl DelayNs, ) -> OneWireResult<(), E> where T: InputPin, @@ -225,7 +225,7 @@ where // wait for the recall to finish (up to 10ms) let max_retries = (10000 / one_wire_bus::READ_SLOT_DURATION_MICROS) + 1; for _ in 0..max_retries { - if onewire.read_bit(delay)? == true { + if onewire.read_bit(delay)? { return Ok(()); } } @@ -235,7 +235,7 @@ where fn save_to_eeprom( address: Option<&Address>, onewire: &mut OneWire, - delay: &mut impl DelayUs, + delay: &mut impl DelayNs, ) -> OneWireResult<(), E> where T: InputPin, diff --git a/src/resolution.rs b/src/resolution.rs index 3c33441..cc342a7 100644 --- a/src/resolution.rs +++ b/src/resolution.rs @@ -1,4 +1,4 @@ -use embedded_hal::blocking::delay::DelayMs; +use embedded_hal::delay::DelayNs; #[repr(u8)] #[derive(Copy, Clone, Debug)] @@ -10,7 +10,7 @@ pub enum Resolution { } impl Resolution { - pub fn max_measurement_time_millis(&self) -> u16 { + pub fn max_measurement_time_millis(&self) -> u32 { match self { Resolution::Bits9 => 94, Resolution::Bits10 => 188, @@ -21,7 +21,7 @@ impl Resolution { /// Blocks for the amount of time required to finished measuring temperature /// using this resolution - pub fn delay_for_measurement_time(&self, delay: &mut impl DelayMs) { + pub fn delay_for_measurement_time(&self, delay: &mut impl DelayNs) { delay.delay_ms(self.max_measurement_time_millis()); } @@ -35,7 +35,7 @@ impl Resolution { } } - pub(crate) fn to_config_register(&self) -> u8 { - *self as u8 + pub(crate) fn to_config_register(self) -> u8 { + self as u8 } } From 839fe9be0ca0e08c4c5c3adfca1e3c1317c912b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20R=C3=B6ssler?= Date: Wed, 28 Feb 2024 11:10:45 +0100 Subject: [PATCH 2/5] cleanup --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 2dcff9a..312cc36 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,7 @@ use embedded_hal::delay::DelayNs; use embedded_hal::digital::{InputPin, OutputPin}; -use one_wire_bus::{self, Address, OneWire, OneWireError, OneWireResult}; +use one_wire_bus::{Address, OneWire, OneWireError, OneWireResult}; pub const FAMILY_CODE: u8 = 0x28; From 6fd3fbb5767673d3620e1d37312845ba12afaf40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20R=C3=B6ssler?= Date: Wed, 28 Feb 2024 11:11:50 +0100 Subject: [PATCH 3/5] switch to fork --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 55bb652..0920b78 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,5 +19,5 @@ A Rust DS18B20 temperature sensor driver for [embedded-hal](https://github.com/r """ [dependencies] -one-wire-bus.path = "../one-wire-bus" +one-wire-bus.git = "https://github.com/elwerene/one-wire-bus.git" embedded-hal = "1.0.0" From 35f85a525119936f28c60b772b2804c9a79e704f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20R=C3=B6ssler?= Date: Wed, 28 Feb 2024 11:15:45 +0100 Subject: [PATCH 4/5] use daniel larsens fork --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 0920b78..93daae7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,5 +19,5 @@ A Rust DS18B20 temperature sensor driver for [embedded-hal](https://github.com/r """ [dependencies] -one-wire-bus.git = "https://github.com/elwerene/one-wire-bus.git" +one-wire-bus.git = "https://github.com/daniel-larsen/one-wire-bus.git" embedded-hal = "1.0.0" From 703386ed79c8541670f7606d12b0b470cace2ddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20R=C3=B6ssler?= Date: Wed, 28 Feb 2024 11:21:25 +0100 Subject: [PATCH 5/5] support negative temperatures --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 312cc36..1c4475b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -196,7 +196,7 @@ where } else { return Err(OneWireError::CrcMismatch); }; - let raw_temp = u16::from_le_bytes([scratchpad[0], scratchpad[1]]); + let raw_temp = i16::from_le_bytes([scratchpad[0], scratchpad[1]]); let temperature = match resolution { Resolution::Bits12 => (raw_temp as f32) / 16.0, Resolution::Bits11 => (raw_temp as f32) / 8.0,