quadrature-encoder

Crates.ioquadrature-encoder
lib.rsquadrature-encoder
version0.1.1
sourcesrc
created_at2024-05-20 22:05:17.482402
updated_at2024-06-05 08:00:26.895521
descriptionHardware-level implementations of drivers for incremental encoders with support for full-, half- an quad-stepping.
homepagehttps://github.com/regexident/quadrature
repositoryhttps://github.com/regexident/quadrature
max_upload_size
id1246225
size25,295
Vincent Esche (regexident)

documentation

https://docs.rs/quadrature

README

quadrature-encoder

Crates.io Crates.io Crates.io docs.rs

Hardware-level implementations of drivers for incremental encoders with support for full-, half- an quad-stepping.


Incremental Encoder

use quadrature_encoder::{FullStep, IncrementalEncoder};

let mut encoder: IncrementalEncoder<...> = Default::new(pin_clk, pin_dt);

// Update the encoder with pulse trains `a` and `b` and handle the result:
match encoder.poll() {
    Ok(Some(movement)) => println!("Movement detected: {movement:?}."),
    Ok(None) => println!("No movement detected."),
    Err(error) => println!("Error detected: {error:?}."),
}

// Or, if you only care about correctly detected movement:
if let Some(movement) = encoder.poll().unwrap_or_default() {
    println!("Movement detected: {movement:?}.")
}

println!("Encoder is at position: {:?}.", encoder.position());

See the examples directory for a more comprehensive example.

Indexed Incremental Encoder

An indexed encoder resets its position whenever a raising edge is detected on the idx pin.

use quadrature_encoder::{IndexedIncrementalEncoder};

let mut encoder: IndexedIncrementalEncoder<...> = Default::new(pin_clk, pin_dt, pin_idx);

// Update the encoder with pulse trains `a`, `b` and `z` and handle the result:
match encoder.poll() {
    Ok(Some(movement)) => println!("Movement detected: {movement:?}."),
    Ok(None) => println!("No movement detected."),
    Err(error) => println!("Error detected: {error:?}."),
}

// Or, if you only care about correctly detected movement:
if let Some(movement) = encoder.poll().unwrap_or_default() {
    println!("Movement detected: {movement:?}.")
}

println!("Encoder is at position: {:?}.", encoder.position());

See the examples directory for a more comprehensive example.

Convenience Aliases

Since the full typename IncrementalEncoder<Mode, ..., Step, T> can be quite a mouth-full a couple of convenience type-aliases are provided for the most common use-cases:

Rotary Encoders

use quadrature_encoder::{RotaryEncoder, IndexedRotaryEncoder};

let mut encoder: RotaryEncoder::new(pin_clk, pin_dt);
let mut indexed_encoder: IndexedRotaryEncoder::new(pin_clk, pin_dt, pin_idx);

Linear Encoders

use quadrature_encoder::{LinearEncoder, IndexedLinearEncoder};

let mut encoder: LinearEncoder::new(pin_clk, pin_dt);
let mut indexed_encoder: IndexedLinearEncoder::new(pin_clk, pin_dt, pin_idx);

Decoding Strategies

Full-step Decoding

A full-step encoder is able to detect up to 1 change(s) per quadrature cycle.

use quadrature_encoder::{FullStep, IncrementalEncoder};

let mut encoder: IncrementalEncoder<_, _, FullStep> = Default::new(...);

Half-step Decoding

A full-step encoder is able to detect up to 2 change(s) per quadrature cycle.

use quadrature_encoder::{HalfStep, IncrementalEncoder};

let mut encoder: IncrementalEncoder<_, _, HalfStep> = Default::new(...);

Quad-step Decoding

A full-step encoder is able to detect up to 4 change(s) per quadrature cycle.

use quadrature_encoder::{QuadStep, IncrementalEncoder};

let mut encoder: IncrementalEncoder<_, _, QuadStep> = Default::new(...);

Documentation

Please refer to the documentation on docs.rs.

Contributing

Please read CONTRIBUTING.md for details on our code of conduct,
and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

License

This project is licensed under the MPL-2.0 – see the LICENSE.md file for details.

Commit count: 35

cargo fmt