From c869a34289e96790161ab1ca69c540fd6e570b6c Mon Sep 17 00:00:00 2001 From: Uwe Klotz Date: Wed, 30 Aug 2023 22:56:28 +0200 Subject: [PATCH] Revise blinking LED stuff --- .justfile | 4 ++ .pre-commit-config.yaml | 2 +- Cargo.toml | 13 +++--- src/lib.rs | 2 +- src/output/blinking_led_task.rs | 4 +- src/output/mod.rs | 78 +++++++++++++++++++-------------- 6 files changed, 59 insertions(+), 44 deletions(-) diff --git a/.justfile b/.justfile index 2c748ed..c6df32c 100644 --- a/.justfile +++ b/.justfile @@ -16,6 +16,10 @@ clippy: cargo clippy --locked --workspace --all-targets --no-default-features --features all-controllers cargo clippy --locked --workspace --no-deps --all-targets --all-features -- -D warnings --cap-lints warn +# Run cargo check for the WASM target with default features enabled +check-wasm: + cargo check --locked --workspace --target wasm32-unknown-unknown + # Run unit tests test: RUST_BACKTRACE=1 cargo test --locked --all-features -- --nocapture diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index be63e71..b18bcea 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -66,7 +66,7 @@ repos: warnings, ] - repo: https://github.com/pre-commit/mirrors-prettier - rev: v3.0.2 + rev: v3.0.3 hooks: - id: prettier types_or: diff --git a/Cargo.toml b/Cargo.toml index 098472d..b5c4dda 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ [package] name = "djio" description = "DJ Hardware Control(ler) Support" -version = "0.0.13" +version = "0.0.14" license = "MPL-2.0" readme = "README.md" repository = "https://github.com/uklotzde/djio" @@ -25,7 +25,7 @@ strum = { version = "0.25.0", features = ["derive"] } thiserror = "1.0.47" # Optional dependencies -discro = { version = "0.11.0", optional = true } +discro = { version = "0.12.0", optional = true } midir = { version = "0.9.1", optional = true } tokio = { version = "1.32.0", default-features = false, optional = true } @@ -43,18 +43,19 @@ hidapi = "2.4.1" pretty_env_logger = "0.5.0" [features] +# All cross-platform features are enabled by default. default = [ "all-controllers", "midir", - "spawn-blinking-led-task", + "blinking-led-task-tokio-rt", "controller-thread", ] -hid = ["dep:hidapi"] -jack = ["midir?/jack"] midi = [] midir = ["dep:midir"] +jack = ["midir?/jack"] +hid = ["dep:hidapi"] blinking-led-task = ["dep:discro", "discro/tokio", "dep:tokio", "tokio/time"] -spawn-blinking-led-task = ["blinking-led-task", "tokio/rt"] +blinking-led-task-tokio-rt = ["blinking-led-task", "tokio/rt"] controller-thread = ["dep:tokio", "tokio/rt", "tokio/time"] # Controller support features diff --git a/src/lib.rs b/src/lib.rs index 8a0c9e4..677d460 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -56,7 +56,7 @@ pub use self::input::{ mod output; #[cfg(feature = "blinking-led-task")] pub use self::output::blinking_led_task; -#[cfg(feature = "spawn-blinking-led-task")] +#[cfg(feature = "blinking-led-task-tokio-rt")] pub use self::output::spawn_blinking_led_task; pub use self::output::{ BlinkingLedOutput, BlinkingLedTicker, ControlOutputGateway, DimLedOutput, LedOutput, LedState, diff --git a/src/output/blinking_led_task.rs b/src/output/blinking_led_task.rs index 7ac780f..fad03db 100644 --- a/src/output/blinking_led_task.rs +++ b/src/output/blinking_led_task.rs @@ -10,7 +10,7 @@ use crate::{BlinkingLedOutput, BlinkingLedTicker}; #[allow(clippy::manual_async_fn)] // Explicit return type to to enforce the trait bounds pub fn blinking_led_task( period: Duration, - publisher: Publisher, + mut publisher: Publisher, ) -> impl Future + Send + 'static { async move { let mut ticker = BlinkingLedTicker::default(); @@ -23,7 +23,7 @@ pub fn blinking_led_task( } } -#[cfg(feature = "spawn-blinking-led-task")] +#[cfg(feature = "blinking-led-task-tokio-rt")] #[must_use] pub fn spawn_blinking_led_task(period: Duration) -> Subscriber { let (publisher, subscriber) = new_pubsub(BlinkingLedOutput::ON); diff --git a/src/output/mod.rs b/src/output/mod.rs index fdd0af0..434e794 100644 --- a/src/output/mod.rs +++ b/src/output/mod.rs @@ -20,7 +20,7 @@ use crate::{Control, ControlValue}; mod blinking_led_task; #[cfg(feature = "blinking-led-task")] pub use blinking_led_task::blinking_led_task; -#[cfg(feature = "spawn-blinking-led-task")] +#[cfg(feature = "blinking-led-task-tokio-rt")] pub use blinking_led_task::spawn_blinking_led_task; #[derive(Debug, Error)] @@ -187,8 +187,8 @@ impl LedState { pub const fn output(self, blinking_led_output: BlinkingLedOutput) -> LedOutput { match self { Self::Off => LedOutput::Off, - Self::BlinkFast => blinking_led_output.fast, - Self::BlinkSlow => blinking_led_output.slow, + Self::BlinkFast => blinking_led_output.fast(), + Self::BlinkSlow => blinking_led_output.slow(), Self::On => LedOutput::On, } } @@ -197,42 +197,37 @@ impl LedState { pub const DEFAULT_BLINKING_LED_PERIOD: Duration = Duration::from_millis(250); #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct BlinkingLedOutput { - pub fast: LedOutput, - pub slow: LedOutput, -} +pub struct BlinkingLedOutput(u8); impl BlinkingLedOutput { - pub const ON: Self = Self { - fast: LedOutput::On, - slow: LedOutput::On, - }; + pub const ON: Self = Self(0b11); + + #[must_use] + pub const fn fast(self) -> LedOutput { + match self.0 & 0b01 { + 0b00 => LedOutput::Off, + 0b01 => LedOutput::On, + _ => unreachable!(), + } + } + + #[must_use] + pub const fn slow(self) -> LedOutput { + match self.0 & 0b10 { + 0b00 => LedOutput::Off, + 0b10 => LedOutput::On, + _ => unreachable!(), + } + } } #[derive(Debug, Default)] pub struct BlinkingLedTicker(usize); impl BlinkingLedTicker { - fn output_from_value(value: usize) -> BlinkingLedOutput { - match value & 0b11 { - 0b00 => BlinkingLedOutput { - fast: LedOutput::On, - slow: LedOutput::On, - }, - 0b01 => BlinkingLedOutput { - fast: LedOutput::Off, - slow: LedOutput::On, - }, - 0b10 => BlinkingLedOutput { - fast: LedOutput::On, - slow: LedOutput::Off, - }, - 0b11 => BlinkingLedOutput { - fast: LedOutput::Off, - slow: LedOutput::Off, - }, - _ => unreachable!(), - } + const fn output_from_value(value: usize) -> BlinkingLedOutput { + #[allow(clippy::cast_possible_truncation)] + BlinkingLedOutput(!value as u8 & 0b11) } #[must_use] @@ -243,9 +238,8 @@ impl BlinkingLedTicker { } #[must_use] - pub fn output(&self) -> BlinkingLedOutput { - let value = self.0; - Self::output_from_value(value) + pub const fn output(&self) -> BlinkingLedOutput { + Self::output_from_value(self.0) } pub fn map_into_output_stream( @@ -311,3 +305,19 @@ impl Default for VirtualLed { Self::OFF } } + +#[cfg(test)] +mod tests { + use crate::{BlinkingLedOutput, BlinkingLedTicker, LedOutput}; + + #[test] + fn blinking_led_output_on() { + assert_eq!(LedOutput::On, BlinkingLedOutput::ON.fast()); + assert_eq!(LedOutput::On, BlinkingLedOutput::ON.slow()); + } + + #[test] + fn blinking_led_ticker_initial_output_is_on() { + assert_eq!(BlinkingLedOutput::ON, BlinkingLedTicker::default().output()); + } +}