9个版本
0.3.1 | 2024年7月15日 |
---|---|
0.3.0 | 2024年5月27日 |
0.2.0 | 2022年10月6日 |
0.1.1 | 2022年7月1日 |
0.0.2 | 2021年2月28日 |
#369 在 嵌入式开发
每月 35 次下载
16KB
248 代码行
rotary-encoder-embedded
适用于嵌入式Rust应用程序的旋转编码器库
特性
no-std
支持- 适用于格雷码增量编码器
- 使用嵌入式-hal实现 (https://docs.rs/embedded-hal/0.2.7/embedded_hal)
fn main() -> ! {
// Configure DT and CLK pins, typically pullup input
let rotary_dt = gpio_pin_1.into_pull_up_input()
let rotary_clk = gpio_pin_2.into_pull_up_input();
// Initialize the rotary encoder
let mut rotary_encoder = RotaryEncoder::new(
rotary_dt,
rotary_clk,
).into_standard_mode();
// Now you can update the state of the rotary encoder and get a direction value. Call this from an update routine, timer task or interrupt
let _dir = rotary_encoder.update();
// Alternatively if you want to access the encoder without embedded-hal pin traits and use boolean states, you can use the mode directly:
let mut raw_encoder = StandardMode::new();
let _dir = raw_encoder.update(true, false);
// ...timer initialize at 900Hz to poll the rotary encoder
loop {}
}
fn timer_interrupt_handler() {
// ... get rotary encoder
let rotary_encoder = ...
// Update the encoder, which will compute and return its direction
match rotary_encoder.update() {
Direction::Clockwise => {
// Increment some value
}
Direction::Anticlockwise => {
// Decrement some value
}
Direction::None => {
// Do nothing
}
}
}
关于GPIO或定时器中断使用的说明
我尝试了许多不同的组合,以使旋转编码器的行为可预测,因为一般来说,它们最好也是不可预测的。从我自己的实验中,我了解到基于GPIO引脚的中断通常不是一个好主意,因为它们更容易受到噪声的影响,并增加了误发和跳变的风险。另一方面,定时器提供了低通滤波质量,因为它们不会像GPIO中断那样捕捉高频切换。我发现使用850-1000Hz之间的定时器似乎效果最好。
依赖项
~71KB