cleanup: factor out animation loop

This commit is contained in:
Xiretza 2022-06-24 20:41:47 +02:00
parent bc6c014b68
commit c29d5b96a8

View file

@ -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<F: Fn(Layout, u32) -> RgbImage>(
socket: &UdpSocket,
layout: Layout,
generator: F,
) -> Result<!, anyhow::Error> {
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,
},
)?;
}
};