Skip to content

Commit

Permalink
Revise blinking LED stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
uklotzde committed Aug 30, 2023
1 parent 6fdcfe1 commit c869a34
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 44 deletions.
4 changes: 4 additions & 0 deletions .justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 }

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/output/blinking_led_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlinkingLedOutput>,
mut publisher: Publisher<BlinkingLedOutput>,
) -> impl Future<Output = ()> + Send + 'static {
async move {
let mut ticker = BlinkingLedTicker::default();
Expand All @@ -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<BlinkingLedOutput> {
let (publisher, subscriber) = new_pubsub(BlinkingLedOutput::ON);
Expand Down
78 changes: 44 additions & 34 deletions src/output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
}
}
Expand All @@ -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]
Expand All @@ -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(
Expand Down Expand Up @@ -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());
}
}

0 comments on commit c869a34

Please sign in to comment.