#embedded-hal-driver #onewire #1wire

无标准库 one-wire-bus

为嵌入式-hal实现的单总线协议的Rust实现

2个版本

0.1.1 2020年1月18日
0.1.0 2020年1月17日

#1219 in 嵌入式开发

Download history 217/week @ 2024-03-15 123/week @ 2024-03-22 239/week @ 2024-03-29 194/week @ 2024-04-05 181/week @ 2024-04-12 148/week @ 2024-04-19 105/week @ 2024-04-26 125/week @ 2024-05-03 161/week @ 2024-05-10 198/week @ 2024-05-17 199/week @ 2024-05-24 206/week @ 2024-05-31 143/week @ 2024-06-07 139/week @ 2024-06-14 108/week @ 2024-06-21 48/week @ 2024-06-28

每月472次下载
用于ds18b20

MIT/Apache

20KB
385

单总线

Build Status crates.io API

1-Wire协议的嵌入式-hal实现的Rust版本

快速开始

以下示例省略了错误处理以保持简洁。您应该检查所有结果并适当处理。

1-wire总线需要一个配置为开漏输出的单个数字引脚(它要么是开路,要么连接到地),并且总线应连接一个约5K欧姆的上拉电阻。如何从您的具体设备中获取此引脚取决于该设备的嵌入式-hal实现,但它必须实现InputPinOutputPin

use embedded_hal::blocking::delay::DelayUs;
use embedded_hal::digital::v2::{InputPin, OutputPin};
use core::fmt::{Debug, Write};
use one_wire_bus::OneWire;

fn find_devices<P, E>(
    delay: &mut impl DelayUs<u16>,
    tx: &mut impl Write,
    one_wire_pin: P,
)
    where
        P: OutputPin<Error=E> + InputPin<Error=E>,
        E: Debug
{
    let mut one_wire_bus = OneWire::new(one_wire_pin).unwrap();
    for device_address in one_wire_bus.devices(false, delay) {
        // The search could fail at any time, so check each result. The iterator automatically
        // ends after an error.
        let device_address = device_address.unwrap();

        // The family code can be used to identify the type of device
        // If supported, another crate can be used to interact with that device at the given address
        writeln!(tx, "Found device at address {:?} with family code: {:#x?}",
                 device_address, device_address.family_code()).unwrap();
    }
}

示例输出

Found device at address E800000B1FCD1028 with family code: 0x28
Found device at address 70000008AC851628 with family code: 0x28
Found device at address 0B00000B20687E28 with family code: 0x28
Found device at address 5700000B2015FF28 with family code: 0x28

依赖项

~71KB