From 84f8222b305f54e243eba0ef2a90527865930884 Mon Sep 17 00:00:00 2001 From: Joakim Hulthe Date: Fri, 16 Feb 2024 19:11:08 +0100 Subject: [PATCH] Refactor idle animation --- lib/src/keyboard/lights.rs | 6 ++++-- lib/src/lights/shaders.rs | 15 ++++----------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/lib/src/keyboard/lights.rs b/lib/src/keyboard/lights.rs index 58d777e..65f1dde 100644 --- a/lib/src/keyboard/lights.rs +++ b/lib/src/keyboard/lights.rs @@ -5,7 +5,7 @@ use futures::{select_biased, FutureExt}; use tgnt::button::Button; use crate::{ - lights::shaders::{LsdHyperspace, OrthoRainbow, PowerOffAnim, PowerOnAnim, Shader, Shaders}, + lights::shaders::{PowerOffAnim, PowerOnAnim, Shader, Shaders}, rgb::Rgb, usb::{UsbEvent, USB_EVENTS}, }; @@ -18,6 +18,8 @@ const UNTIL_IDLE: Duration = Duration::from_secs(30); /// DUration between each animation frame. const FRAMETIME: Duration = Duration::from_millis(16); +const IDLE_ANIM: Shaders = Shaders::OrthoRainbow; + #[derive(Default)] enum LightsState { Active { @@ -74,7 +76,7 @@ pub(super) async fn task(mut events: KbEvents, state: &'static State) { *idle_at = Instant::now() + UNTIL_IDLE; handle_usb_event(ev, &mut lights).await; } - _ = Timer::at(*idle_at).fuse() => lights = LightsState::Idle(Shaders::LsdHyperspace(LsdHyperspace)), + _ = Timer::at(*idle_at).fuse() => lights = LightsState::Idle(IDLE_ANIM), _ = Timer::at(*next_frame).fuse() => { keypress_tick(state, keys).await; *next_frame = Instant::now() + FRAMETIME; diff --git a/lib/src/lights/shaders.rs b/lib/src/lights/shaders.rs index 1dbfb47..a07c822 100644 --- a/lib/src/lights/shaders.rs +++ b/lib/src/lights/shaders.rs @@ -17,22 +17,15 @@ pub trait Shader { } pub enum Shaders { - OrthoRainbow(OrthoRainbow), - LsdHyperspace(LsdHyperspace), + OrthoRainbow, + LsdHyperspace, } impl Shader for Shaders { fn sample(&self, time: Instant, uv: (f32, f32)) -> Rgb { match self { - Shaders::OrthoRainbow(s) => s.sample(time, uv), - Shaders::LsdHyperspace(s) => s.sample(time, uv), - } - } - - fn end_time(&self) -> Option { - match self { - Shaders::OrthoRainbow(s) => s.end_time(), - Shaders::LsdHyperspace(s) => s.end_time(), + Shaders::OrthoRainbow => OrthoRainbow.sample(time, uv), + Shaders::LsdHyperspace => LsdHyperspace.sample(time, uv), } } }