| Crates.io | onewire |
| lib.rs | onewire |
| version | 0.4.0 |
| created_at | 2018-02-17 16:50:41.859095+00 |
| updated_at | 2025-05-12 17:34:46.45963+00 |
| description | OneWire implementation using embedded_hal as abstraction layer, based on Arduino OneWire library |
| homepage | |
| repository | https://github.com/kellerkindt/onewire |
| max_upload_size | |
| id | 51606 |
| size | 51,203 |
This crate is an OneWire-Bus implementation ontop of generic Input- and OutputPins from the embedded-hal.
Below is an example how to create a new OneWire instance, search for devices and read the temperature from a DS18B20. The example currently requires the stm32f103xx-hal to be patched with this PR.
fn main() -> ! {
let mut cp: cortex_m::Peripherals = cortex_m::Peripherals::take().unwrap();
let mut peripherals = stm32f103xx::Peripherals::take().unwrap();
let mut flash = peripherals.FLASH.constrain();
let clocks = rcc.cfgr.freeze(&mut flash.acr);
let mut rcc = peripherals.RCC.constrain();
let mut gpioc = peripherals.GPIOC.split(&mut rcc.apb2);
let mut delay = stm32f103xx_hal::delay::Delay::new(cp.SYST, clocks);
let mut one = gpioc
.pc15
.into_open_drain_output(&mut gpioc.crh)
.downgrade();
let mut wire = OneWire::new(&mut one, false);
if wire.reset(&mut delay).is_err() {
// missing pullup or error on line
loop {}
}
// search for devices
let mut search = DeviceSearch::new();
while let Some(device) = wire.search_next(&mut search, &mut delay).unwrap() {
match device.address[0] {
ds18b20::FAMILY_CODE => {
let mut ds18b20 = DS18b20::new(device).unwrap();
// request sensor to measure temperature
let resolution = ds18b20.measure_temperature(&mut wire, &mut delay).unwrap();
// wait for compeltion, depends on resolution
delay.delay_ms(resolution.time_ms());
// read temperature
let temperature = ds18b20.read_temperature(&mut wire, &mut delay).unwrap();
},
_ => {
// unknown device type
}
}
}
loop {}
}
The code from the example is copy&pasted from a working project, but not tested in this specific combination.
defmt supportEnable the optional defmt feature, to be able to print the errors this library produces.
See: https://crates.io/crates/defmt
Enable the optional defmt-debug2format feature, to enable formatting Error::PortError with the defmt::Debug2Format adapter (:warning: performance intensive).