Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to embedded-hal 1.0 #9

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ authors = ["Nathan Fox <[email protected]>"]
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 = """
A Rust DS18B20 temperature sensor driver for [embedded-hal](https://github.com/rust-embedded/embedded-hal)
"""

[dependencies]
one-wire-bus = "0.1.1"
embedded-hal = {version="0.2.3", features=["unproven"]}
one-wire-bus.git = "https://github.com/daniel-larsen/one-wire-bus.git"
embedded-hal = "1.0.0"
34 changes: 17 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

//! # Test Test

use embedded_hal::blocking::delay::DelayUs;
use embedded_hal::digital::v2::{InputPin, OutputPin};
use one_wire_bus::{self, Address, OneWire, OneWireError, OneWireResult};
use embedded_hal::delay::DelayNs;
use embedded_hal::digital::{InputPin, OutputPin};
use one_wire_bus::{Address, OneWire, OneWireError, OneWireResult};

pub const FAMILY_CODE: u8 = 0x28;

Expand Down Expand Up @@ -56,7 +56,7 @@ impl Ds18b20 {
pub fn start_temp_measurement<T, E>(
&self,
onewire: &mut OneWire<T>,
delay: &mut impl DelayUs<u16>,
delay: &mut impl DelayNs,
) -> OneWireResult<(), E>
where
T: InputPin<Error = E>,
Expand All @@ -69,7 +69,7 @@ impl Ds18b20 {
pub fn read_data<T, E>(
&self,
onewire: &mut OneWire<T>,
delay: &mut impl DelayUs<u16>,
delay: &mut impl DelayNs,
) -> OneWireResult<SensorData, E>
where
T: InputPin<Error = E>,
Expand All @@ -85,7 +85,7 @@ impl Ds18b20 {
alarm_temp_high: i8,
resolution: Resolution,
onewire: &mut OneWire<T>,
delay: &mut impl DelayUs<u16>,
delay: &mut impl DelayNs,
) -> OneWireResult<(), E>
where
T: InputPin<Error = E>,
Expand All @@ -101,7 +101,7 @@ impl Ds18b20 {
pub fn save_to_eeprom<T, E>(
&self,
onewire: &mut OneWire<T>,
delay: &mut impl DelayUs<u16>,
delay: &mut impl DelayNs,
) -> OneWireResult<(), E>
where
T: InputPin<Error = E>,
Expand All @@ -113,7 +113,7 @@ impl Ds18b20 {
pub fn recall_from_eeprom<T, E>(
&self,
onewire: &mut OneWire<T>,
delay: &mut impl DelayUs<u16>,
delay: &mut impl DelayNs,
) -> OneWireResult<(), E>
where
T: InputPin<Error = E>,
Expand All @@ -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<T, E>(
onewire: &mut OneWire<T>,
delay: &mut impl DelayUs<u16>,
delay: &mut impl DelayNs,
) -> OneWireResult<(), E>
where
T: InputPin<Error = E>,
Expand All @@ -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<T, E>(
onewire: &mut OneWire<T>,
delay: &mut impl DelayUs<u16>,
delay: &mut impl DelayNs,
) -> OneWireResult<(), E>
where
T: InputPin<Error = E>,
Expand All @@ -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<T, E>(
onewire: &mut OneWire<T>,
delay: &mut impl DelayUs<u16>,
delay: &mut impl DelayNs,
) -> OneWireResult<(), E>
where
T: InputPin<Error = E>,
Expand All @@ -165,7 +165,7 @@ where
pub fn read_scratchpad<T, E>(
address: &Address,
onewire: &mut OneWire<T>,
delay: &mut impl DelayUs<u16>,
delay: &mut impl DelayNs,
) -> OneWireResult<[u8; 9], E>
where
T: InputPin<Error = E>,
Expand All @@ -183,7 +183,7 @@ where
fn read_data<T, E>(
address: &Address,
onewire: &mut OneWire<T>,
delay: &mut impl DelayUs<u16>,
delay: &mut impl DelayNs,
) -> OneWireResult<SensorData, E>
where
T: InputPin<Error = E>,
Expand All @@ -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,
Expand All @@ -214,7 +214,7 @@ where
fn recall_from_eeprom<T, E>(
address: Option<&Address>,
onewire: &mut OneWire<T>,
delay: &mut impl DelayUs<u16>,
delay: &mut impl DelayNs,
) -> OneWireResult<(), E>
where
T: InputPin<Error = E>,
Expand All @@ -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(());
}
}
Expand All @@ -235,7 +235,7 @@ where
fn save_to_eeprom<T, E>(
address: Option<&Address>,
onewire: &mut OneWire<T>,
delay: &mut impl DelayUs<u16>,
delay: &mut impl DelayNs,
) -> OneWireResult<(), E>
where
T: InputPin<Error = E>,
Expand Down
10 changes: 5 additions & 5 deletions src/resolution.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use embedded_hal::blocking::delay::DelayMs;
use embedded_hal::delay::DelayNs;

#[repr(u8)]
#[derive(Copy, Clone, Debug)]
Expand All @@ -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,
Expand All @@ -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<u16>) {
pub fn delay_for_measurement_time(&self, delay: &mut impl DelayNs) {
delay.delay_ms(self.max_measurement_time_millis());
}

Expand All @@ -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
}
}