From 87cbbdf30f24dc65c7f2a341f5f2122ea0a67c1d Mon Sep 17 00:00:00 2001 From: Jean-Loup Beaussart Date: Sun, 23 Mar 2025 00:45:40 +0100 Subject: [PATCH] Switch display to black between 23h30 and 7h30 --- Cargo.toml | 1 + src/main.rs | 30 ++++++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index eff2780..41a52d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,7 @@ lto = "thin" [dependencies] anyhow = "1.0.97" +chrono = "0.4.40" config = "0.15.11" image = "0.25.5" macroquad = { version = "0.4.14" } diff --git a/src/main.rs b/src/main.rs index af63332..9f3ef39 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,6 +31,11 @@ fn display_image_centered(texture: &Texture2D) { ); } +enum Mode { + Black, + Displayed, +} + #[macroquad::main(window_conf)] async fn main() -> anyhow::Result<()> { let settings = settings::Settings::new("settings.yml".into())?; @@ -45,12 +50,29 @@ async fn main() -> anyhow::Result<()> { break; } - display_frame(&provider)?; + let now = chrono::Local::now().time(); + let midnight = chrono::NaiveTime::from_hms_opt(23, 30, 0).unwrap(); + let seven_am = chrono::NaiveTime::from_hms_opt(7, 30, 0).unwrap(); - if time.elapsed() > settings.duration { - provider.load_next_image()?; - time = Instant::now(); + let mode = if now < midnight && now > seven_am { + Mode::Displayed + } else { + Mode::Black + }; + + match mode { + Mode::Black => { + clear_background(BLACK); + } + Mode::Displayed => { + display_frame(&provider)?; + if time.elapsed() > settings.duration { + provider.load_next_image()?; + time = Instant::now(); + } + } } + thread::sleep(Duration::from_millis(250)); next_frame().await; }