1个不稳定版本
使用旧的Rust 2015
0.4.0 | 2019年4月6日 |
---|
#960 in Unix API
在 2 crates 中使用
42KB
724 代码行,不包括注释
Rust I2cdev
The Rust i2cdev
crate seeks to provide full access to the Linux i2cdev driver interface in Rust without the need to wrap any C code or directly make low-level system calls. The documentation for the i2cdev interace can be found at https://linuxkernel.org.cn/doc/Documentation/i2c/dev-interface and in the lm-sensors projects.
设备驱动程序开发者应考虑在 embedded-hal 特性之上构建,而不是直接耦合到此库。Linux中这些泛型特性的实现可以在 linux-embedded-hal 中找到,目前它使用此crate作为I2C的后端。
示例/API
源代码包括使用库与具有i2c接口的Wii Nunchuck通信的示例。 查看示例。
以下是一个快速示例,展示了如何创建设备和与之通信的细节...
extern crate i2cdev;
use std::thread;
use std::time::Duration;
use i2cdev::core::*;
use i2cdev::linux::{LinuxI2CDevice, LinuxI2CError};
const NUNCHUCK_SLAVE_ADDR: u16 = 0x52;
// real code should probably not use unwrap()
fn i2cfun() -> Result<(), LinuxI2CError> {
let mut dev = LinuxI2CDevice::new("/dev/i2c-1", NUNCHUCK_SLAVE_ADDR)?;
// init sequence
dev.smbus_write_byte_data(0xF0, 0x55)?;
dev.smbus_write_byte_data(0xFB, 0x00)?;
thread::sleep(Duration::from_millis(100));
loop {
let mut buf: [u8; 6] = [0; 6];
dev.smbus_write_byte(0x00).unwrap();
thread::sleep(Duration::from_millis(10));
dev.read(&mut buf).unwrap();
println!("Reading: {:?}", buf);
}
}
除了Read/Write traits之外,以下方法通过I2CDevice trait提供。
功能
以下功能已实现和计划在库中实现
- 实现Read trait
- 实现Write trait
- 实现SMBus方法
- 为SMBus方法添加测试/示例
- 添加传感器库(及其示例)
- 添加高级API/宏以简化对具有大型寄存集的设备的访问
- 添加对非SMBus ioctl方法的支持
- 为非smbus ioctl方法添加示例
- 单元测试
交叉编译
您正在运行的机器很可能是您的开发机器(尽管也可能是)。在这种情况下,您需要进行交叉编译。请参阅https://github.com/japaric/rust-cross获取相关信息。
许可证
Copyright (c) 2015, Paul Osborne <[email protected]>
Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
https://apache.ac.cn/license/LICENSE-2.0> or the MIT license
<LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
option. This file may not be copied, modified, or distributed
except according to those terms.
依赖项
~1.6–3MB
~54K SLoC