2个版本
0.1.1 | 2021年7月4日 |
---|---|
0.1.0 | 2021年7月4日 |
#838 in 嵌入式开发
每月 65 次下载
18KB
259 行
此crate提供了通过I2C协议访问SparkFun Qwiic Button LED的接口。它支持控制LED并响应按钮按下。它基于embedded-hal,因此支持实现该crate特性的任何平台,例如流行的微控制器和树莓派型号。
树莓派示例
use linux_embedded_hal as hal;
use qwiic_button_led::*;
use std::{thread, time};
// The rPi model 4 B has /dev/i2c-1 as its only I2C device
let i2c = hal::I2cdev::new("/dev/i2c-1").unwrap();
// The Qwiic Button LED's default address is 0x6F, but is user-configurable
let address = 0x6F;
let mut button = ButtonLED::init(i2c, address);
loop {
let status = button.button_status().unwrap();
if status.pressed {
// if the button is pressed, turn the LED on
button.set_led_brightness(255).unwrap()
} else {
// otherwise, turn it off
button.set_led_brightness(0).unwrap();
}
// sleep a bit to not hammer the I2C bus
thread::sleep(time::Duration::from_millis(10));
}
此示例在按钮按下时点亮LED,在按钮释放时关闭LED。
按钮LED支持静态亮度设置和动态脉冲。当静态设置亮度时,LED保持开启。脉冲周期和脉冲关闭时间值配置LED脉冲。脉冲周期时间配置LED在脉冲时开启的时间长度,关闭时间配置LED在脉冲时关闭的时间长度。
let mut button = ButtonLED(i2c, address);
// 300 ms on, 300 ms off, in a loop
button.set_led_pulse_cycle_time(300);
button.set_led_pulse_off_time(300);
依赖项
~71KB