4个版本 (2个重大更改)
0.3.0 | 2024年3月8日 |
---|---|
0.2.1 | 2023年10月27日 |
0.2.0 |
|
0.1.1 | 2023年2月5日 |
0.1.0 | 2023年2月4日 |
#425 in 嵌入式开发
每月下载量 158次
25KB
540 行
SHT31-rs
用于SHT31温度/湿度传感器的Cargo库
使用方法
默认的传感器使用方法包括一个阻塞式读取,它会阻塞当前线程直到传感器完成读取。
use sht31::prelude::*;
fn main() -> Result<()> {
// Requires an i2c connection + delay (both from embedded_hal 1.x.x)
let sht = SHT31::new(i2c, delay);
loop {
let reading = sht.read()?;
}
}
高级单次读取使用
将简单使用拆分为两个命令,一个通知传感器开始读取,另一个请求读取
use sht31::prelude::*;
fn main() -> Result<()> {
// i2c setup
// Use single shot, with high accuracy, an alternate I2C address
// and return temp data in Fahrenheit
let sht = SHT31::new(i2c)
.with_mode(SingleShot::new())
.with_accuracy(Accuracy::High)
.with_unit(TemperatureUnit::Fahrenheit)
.with_address(DeviceAddr::AD1);
loop {
// Start measuring before you need the reading,
// this is more efficient than waiting for readings
sht.measure()?;
// Some other code here...
let reading = sht.read()?;
}
}
周期性使用
周期模式以每秒给定的测量次数(MPS)和给定的精度读取数据。这意味着您不必每次读取数据时都运行 SHT31::measure()
use sht31::prelude::*;
fn main() -> Result<()> {
// i2c setup
// In periodic mode, the sensor keeps updating the reading
// without needing to measure
let mut sht = SHT31::new(sht_i2c)
.with_mode(Periodic::new().with_mps(MPS::Normal))
.with_accuracy(Accuracy::High);
// Trigger the measure before running your loop to initialize the periodic mode
sht.measure()?;
loop {
let reading = sht.read()?;
}
}
带有加速响应时间的周期性使用
周期模式具有一个独特的模式,允许传感器以4Hz的频率读取数据
use sht31::prelude::*;
fn main() -> Result<()> {
// Makes the sensor acquire the data at 4 Hz
let mut sht = SHT31::new(sht_i2c)
.with_mode(Periodic::new().with_art());
sht.measure()?;
loop {
let reading = sht.read()?;
}
}
模式切换
此crate还支持更复杂的场景,例如在周期和单次读取之间切换
use sht31::prelude::*;
fn main() -> Result<()> {
// i2c setup
let mut sht = SHT31::new(sht_i2c)
.with_mode(Periodic::new());
// Trigger the measure before running your loop to initialize the periodic mode
sht.measure()?;
// Do a periodic read
let reading = sht.read()?;
// Cancel the currently running command (periodic)
sht.break_command()?;
sht = sht.with_mode(SingleShot::new());
sht.measure()?;
let new_reading = sht.read()?;
}
依赖项
~155–270KB