From 5cd8c984e36682b5091ff33ff2dd0edbac8e9373 Mon Sep 17 00:00:00 2001 From: Uwe Klotz Date: Wed, 30 Aug 2023 13:32:19 +0200 Subject: [PATCH] Update VirtualLed --- src/output/mod.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/output/mod.rs b/src/output/mod.rs index 84f3449..8a48d03 100644 --- a/src/output/mod.rs +++ b/src/output/mod.rs @@ -266,12 +266,30 @@ pub struct VirtualLed { impl VirtualLed { pub const OFF: Self = Self::initial_state(LedState::Off); + /// Create a new virtual LED #[must_use] pub const fn initial_state(state: LedState) -> Self { let output = state.initial_output(); Self { state, output } } + /// Update the state + /// + /// The output is initialized accordingly to reflect the new state. + /// + /// Returns `true` if the state has changed. + pub fn update_state(&mut self, state: LedState) -> bool { + if self.state == state { + // Unchanged + return false; + } + *self = Self::initial_state(state); + true + } + + /// Update the blinking output + /// + /// The output is updated accordingly while the state remains unchanged. pub fn update_blinking_output(&mut self, blinking_leds_output: BlinkingLedsOutput) { let Self { state, output } = self; *output = state.output(blinking_leds_output);