From c29d5b96a87b307a5a713565ac5adadf0f4d7e96 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Fri, 24 Jun 2022 20:41:47 +0200 Subject: [PATCH] cleanup: factor out animation loop --- src/main.rs | 56 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index a73f69c..cd4b8a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ #![warn(clippy::pedantic)] +#![feature(never_type)] use std::{ f32::consts::FRAC_PI_2, @@ -6,7 +7,7 @@ use std::{ net::{Ipv4Addr, SocketAddr, UdpSocket}, path::PathBuf, thread::sleep, - time::Duration, + time::{Duration, Instant}, }; use bracket_color::prelude::{HSV, RGB}; @@ -14,7 +15,7 @@ use clap::{builder::TypedValueParser, Parser, Subcommand, ValueEnum}; use image::{imageops::FilterType, io::Reader as ImageReader, Pixel, Rgb, RgbImage}; use rand::Rng; -use splink_client::{send_frame, Layout}; +use splink_client::{send_frame, Layout, SenderError}; /// Blinkenwall v3 prototype client #[derive(Parser, Debug)] @@ -170,6 +171,30 @@ fn print_image(image: &RgbImage) { } } +fn animate RgbImage>( + socket: &UdpSocket, + layout: Layout, + generator: F, +) -> Result { + print!("{}", termion::clear::All); + + let mut frame: u32 = rand::thread_rng().gen(); + loop { + let start = Instant::now(); + let image = generator(layout, frame); + + print_image(&image); + + match send_frame(socket, layout, frame, &image) { + Ok(()) | Err(SenderError::ConfirmationTimeout) => {} + Err(e) => return Err(e.into()), + }; + + println!("{:?}", Instant::now().duration_since(start)); + frame += 1; + } +} + fn main() -> anyhow::Result<()> { let args = Args::parse(); @@ -205,25 +230,14 @@ fn main() -> anyhow::Result<()> { .resize_to_fill(layout.width_px(), layout.height_px(), FilterType::Gaussian) .into_rgb8(), Action::Animation { animation } => { - let f = match animation { - Animation::Rainbow => rainbow, - Animation::Bling => bling, - }; - - print!("{}", termion::clear::All); - - let mut frame = 0; - loop { - let image = f(layout, frame); - - print_image(&image); - - let frame_num: u32 = rand::thread_rng().gen(); - send_frame(&socket, layout, frame_num, &image)?; - - sleep(Duration::from_millis(16)); - frame += 1; - } + animate( + &socket, + layout, + match animation { + Animation::Rainbow => rainbow, + Animation::Bling => bling, + }, + )?; } };