Skip to content

Commit

Permalink
fix: pipeline and test
Browse files Browse the repository at this point in the history
  • Loading branch information
ost-ing committed Jul 15, 2024
1 parent 3eb1204 commit a9a6978
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

on: [push, pull_request]

name: Pipeline
name: Build

jobs:
check:
Expand All @@ -20,7 +20,6 @@ jobs:

- name: Run cargo check
uses: actions-rs/cargo@v1
continue-on-error: true # WARNING: only for this example, remove it!
with:
command: check

Expand All @@ -40,7 +39,6 @@ jobs:

- name: Run cargo test
uses: actions-rs/cargo@v1
continue-on-error: true # WARNING: only for this example, remove it!
with:
command: test

Expand All @@ -61,14 +59,12 @@ jobs:

- name: Run cargo fmt
uses: actions-rs/cargo@v1
continue-on-error: true # WARNING: only for this example, remove it!
with:
command: fmt
args: --all -- --check

- name: Run cargo clippy
uses: actions-rs/cargo@v1
continue-on-error: true # WARNING: only for this example, remove it!
with:
command: clippy
args: -- -D warnings
14 changes: 8 additions & 6 deletions src/angular_velocity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ where
/// Direction and current Angular Velocity.
/// * `current_time` - Current timestamp in ms (strictly monotonously increasing)
pub fn update(&mut self, current_time_millis: u64) -> Direction {
self.mode.update(self.pin_dt.is_high().unwrap_or_default(), self.pin_clk.is_high().unwrap_or_default(), current_time_millis)
self.mode.update(
self.pin_dt.is_high().unwrap_or_default(),
self.pin_clk.is_high().unwrap_or_default(),
current_time_millis,
)
}

/// Returns the current angular velocity of the RotaryEncoder
Expand All @@ -90,18 +94,16 @@ impl AngularVelocityMode {
velocity_inc_factor: DEFAULT_VELOCITY_INC_FACTOR,
}
}

/// Update to determine the direction
pub fn update(
&mut self,
dt_state: bool,
clk_state: bool,
current_time_millis: u64,
) -> Direction {
self.pin_state[0] =
(self.pin_state[0] << 1) | dt_state as u8;
self.pin_state[1] =
(self.pin_state[1] << 1) | clk_state as u8;
self.pin_state[0] = (self.pin_state[0] << 1) | dt_state as u8;
self.pin_state[1] = (self.pin_state[1] << 1) | clk_state as u8;

let a = self.pin_state[0] & PIN_MASK;
let b = self.pin_state[1] & PIN_MASK;
Expand Down
38 changes: 26 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,36 +74,50 @@ where

#[cfg(test)]
mod test {
use crate::{angular_velocity::AngularVelocityMode, standard::StandardMode, RotaryEncoder};
use embedded_hal_mock::eh0::pin::Mock;

fn get_pins() -> (Mock, Mock) {
(Mock::new([]), Mock::new([]))
}
use crate::{
angular_velocity::AngularVelocityMode, standard::StandardMode, Direction, RotaryEncoder,
};
use embedded_hal_mock::eh0::pin::{Mock, State, Transaction};

#[test]
fn standard_mode_api() {
let (dt, clk) = get_pins();
let expectations = [Transaction::get(State::High)];

let dt = Mock::new(&expectations);
let clk = Mock::new(&expectations);

// Standard mode can be used with embedded-hal pins
let mut encoder = RotaryEncoder::new(dt, clk).into_standard_mode();
let _dir = encoder.update();

// Or it can be used directly, bypassing the pins
// Or it can be used directly, bypassing the pins
let mut raw_encoder = StandardMode::new();
let _dir = raw_encoder.update(true, false);

let (mut dt, mut clk) = encoder.release();
dt.done();
clk.done();
}

#[test]
fn angular_velocity_mode_api() {
let (dt, clk) = get_pins();
let expectations = [Transaction::get(State::High)];

let dt = Mock::new(&expectations);
let clk = Mock::new(&expectations);

// Angular velocity mode can be used with embedded-hal pins
let mut encoder = RotaryEncoder::new(dt, clk).into_angular_velocity_mode();
let _dir = encoder.update(0);
let dir = encoder.update(2);
assert_eq!(dir, Direction::None);

// Or it can be used directly, bypassing the pins
// Or it can be used directly, bypassing the pins
let mut raw_encoder = AngularVelocityMode::new();
let _dir = raw_encoder.update(true, false, 100);
let _dir = raw_encoder.update(false, false, 100);
assert_eq!(dir, Direction::None);

let (mut dt, mut clk) = encoder.release();
dt.done();
clk.done();
}
}
6 changes: 4 additions & 2 deletions src/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ where
{
/// Updates the `RotaryEncoder`, updating the `direction` property
pub fn update(&mut self) -> Direction {
self.mode
.update(self.pin_dt.is_high().unwrap_or_default(), self.pin_clk.is_high().unwrap_or_default())
self.mode.update(
self.pin_dt.is_high().unwrap_or_default(),
self.pin_clk.is_high().unwrap_or_default(),
)
}
}

Expand Down

0 comments on commit a9a6978

Please sign in to comment.