Browse Source

initial commit

f_travis
Evan Cameron 3 years ago
commit
a8ef7a800c
  1. 3
      .gitignore
  2. 19
      Cargo.toml
  3. 7
      LICENSE.md
  4. 54
      README.md
  5. 97
      src/lib.rs
  6. 9
      tests/version-numbers.rs

3
.gitignore

@ -0,0 +1,3 @@
/target
**/*.rs.bk
Cargo.lock

19
Cargo.toml

@ -0,0 +1,19 @@
[package]
name = "rotary-encoder-hal"
version = "0.1.0"
authors = ["Evan Cameron <cameron.evan@gmail.com>"]
edition = "2018"
description = "A simple platform agnostic rotary encoder library using embedded_hal"
keywords = ["embedded-hal", "driver", "rotary", "encoder"]
categories = ["embedded", "hardware-support", "no-std"]
license = "MIT"
readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
embedded-hal = { version = "0.2.3", features = ["unproven"] }
either = { version = "1.5.3", default-features = false }
[dev-dependencies]
version-sync = "0.8"

7
LICENSE.md

@ -0,0 +1,7 @@
Copyright 2019 Evan Cameron
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

54
README.md

@ -0,0 +1,54 @@
# rotary-encoder-hal
A simple, platform agnostic rotary encoder library.
```rust
#![no_std]
#![no_main]
extern crate panic_itm;
use cortex_m::iprintln;
use cortex_m_rt::{entry, exception, ExceptionFrame};
use cortex_m_semihosting::{hio, hprintln};
use hal::{delay::Delay, prelude::*, stm32};
use stm32f3xx_hal as hal;
use rotary_encoder_hal::{Direction, Rotary};
#[entry]
fn main() -> ! {
let cp = cortex_m::Peripherals::take().unwrap();
let peripherals = stm32::Peripherals::take().unwrap();
let mut flash = peripherals.FLASH.constrain();
let mut rcc = peripherals.RCC.constrain();
let clocks = rcc.cfgr.freeze(&mut flash.acr);
let mut delay = Delay::new(cp.SYST, clocks);
let mut gpiob = peripherals.GPIOB.split(&mut rcc.ahb);
let pin_a = gpiob
.pb10
.into_pull_up_input(&mut gpiob.moder, &mut gpiob.pupdr);
let pin_b = gpiob
.pb11
.into_pull_up_input(&mut gpiob.moder, &mut gpiob.pupdr);
let mut enc = Rotary::new(pin_a, pin_b);
let mut pos: isize = 0;
loop {
match enc.update().unwrap() {
Direction::Clockwise => {
pos += 1;
}
Direction::CounterClockwise => {
pos -= 1;
}
Direction::None => {}
}
}
}
```

97
src/lib.rs

@ -0,0 +1,97 @@
#![doc(html_root_url = "https://docs.rs/rotary-encoder-hal/0.1.0")]
//! # rotary-encoder-hal
//!
//! A platform agnostic rotary encoder library
//!
//! Built using [`embedded-hal`] traits
//!
//! [`embedded-hal`]: https://docs.rs/embedded-hal/0.2
//!
//! # Examples
//!
//!
//! [rotary_encoder_hal]: https://docs.rs/rotary-encoder-hal/0.1.0
#![deny(missing_docs)]
#![deny(warnings)]
#![no_std]
extern crate embedded_hal;
use either::Either;
use embedded_hal as hal;
use hal::digital::v2::InputPin;
/// Holds current/old state and both [`InputPin`]s
///
/// [InputPin]: https://docs.rs/embedded-hal/0.2.3/embedded_hal/digital/v2/trait.InputPin.html
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Rotary<A, B> {
pin_a: A,
pin_b: B,
state: u8,
}
/// The encoder direction is either [`Clockwise`], [`CounterClockwise`], or [`None`]
///
/// [`Clockwise`]: (enum.Direction.html)
/// [`CounterClockwise`]: (enum.Direction.html)
/// [`None`]: (enum.Direction.html)
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Direction {
/// A clockwise turn
Clockwise,
/// A counterclockwise turn
CounterClockwise,
/// No change
None,
}
impl From<u8> for Direction {
fn from(s: u8) -> Self {
match s {
0b0001 | 0b0111 | 0b1000 | 0b1110 => Direction::Clockwise,
0b0010 | 0b0100 | 0b1011 | 0b1101 => Direction::CounterClockwise,
_ => Direction::None,
}
}
}
impl<A, B> Rotary<A, B>
where
A: InputPin,
B: InputPin,
{
/// Accepts two `InputPin`s, in my testing, works best with both pins set to
/// pull-up.
pub fn new(pin_a: A, pin_b: B) -> Self {
Self {
pin_a,
pin_b,
state: 0u8,
}
}
/// Call `update` to evaluate the next state of the encoder
pub fn update(&mut self) -> Result<Direction, Either<A::Error, B::Error>> {
// use mask to get previous state value
let mut s = self.state & 0b11;
// move in the new state
if self.pin_a.is_low().map_err(Either::Left)? {
s |= 0b100;
}
if self.pin_b.is_low().map_err(Either::Right)? {
s |= 0b1000;
}
// move new state in
self.state = s >> 2;
Ok(s.into())
}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}

9
tests/version-numbers.rs

@ -0,0 +1,9 @@
#[test]
fn test_readme_deps() {
version_sync::assert_markdown_deps_updated!("README.md");
}
#[test]
fn test_html_root_url() {
version_sync::assert_html_root_url_updated!("src/lib.rs");
}
Loading…
Cancel
Save