173 lines
4.2 KiB
Rust
173 lines
4.2 KiB
Rust
#![warn(clippy::pedantic)]
|
|
|
|
use std::{
|
|
f32::consts::FRAC_PI_2,
|
|
io::stdout,
|
|
net::{Ipv4Addr, SocketAddr, UdpSocket},
|
|
num::ParseIntError,
|
|
str::FromStr,
|
|
thread::sleep,
|
|
time::Duration,
|
|
};
|
|
|
|
use clap::{Parser, Subcommand};
|
|
use image::{Pixel, Rgb, RgbImage};
|
|
use rand::Rng;
|
|
|
|
use splink_client::{send_frame, Layout};
|
|
use thiserror::Error;
|
|
|
|
/// Blinkenwall v3 prototype client
|
|
#[derive(Parser, Debug)]
|
|
#[clap(author, version, about, long_about = None)]
|
|
struct Args {
|
|
/// Timeout in ms for frame completion responses (0 to disable)
|
|
#[clap(long, default_value_t = 3)]
|
|
response_timeout: u64,
|
|
|
|
/// Local address and port to bind to
|
|
#[clap(long = "bind")]
|
|
bind_addr: Option<SocketAddr>,
|
|
|
|
/// Controller's address
|
|
remote_addr: SocketAddr,
|
|
|
|
/// The action to perform
|
|
#[clap(subcommand, rename_all = "kebab-case")]
|
|
action: Action,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
struct Color {
|
|
r: u8,
|
|
g: u8,
|
|
b: u8,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Error)]
|
|
enum ColorError {
|
|
#[error("Wrong parameter length")]
|
|
WrongLength,
|
|
#[error("Illegal integer")]
|
|
BadNumber(#[from] ParseIntError),
|
|
}
|
|
|
|
impl FromStr for Color {
|
|
type Err = ColorError;
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
let s = s.strip_prefix('#').unwrap_or(s);
|
|
|
|
if s.len() != 6 {
|
|
return Err(ColorError::WrongLength);
|
|
}
|
|
|
|
let r = u8::from_str_radix(&s[0..2], 16)?;
|
|
let g = u8::from_str_radix(&s[2..4], 16)?;
|
|
let b = u8::from_str_radix(&s[4..6], 16)?;
|
|
|
|
Ok(Self { r, g, b })
|
|
}
|
|
}
|
|
|
|
impl From<Color> for Rgb<u8> {
|
|
fn from(c: Color) -> Rgb<u8> {
|
|
Rgb([c.r, c.g, c.b])
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Subcommand)]
|
|
enum Action {
|
|
Solid { color: Color },
|
|
Clear,
|
|
}
|
|
|
|
fn get_frame(layout: Layout, frame: u32) -> RgbImage {
|
|
#![allow(
|
|
clippy::cast_precision_loss,
|
|
clippy::cast_lossless,
|
|
clippy::cast_possible_truncation,
|
|
clippy::cast_sign_loss
|
|
)]
|
|
|
|
let w = layout.width_px();
|
|
let h = layout.height_px();
|
|
|
|
let frame = frame % 60;
|
|
let frame = frame as f32 / 3.0;
|
|
|
|
RgbImage::from_fn(w, h, |x, y| {
|
|
let dist_from_center = {
|
|
let x = x as f32;
|
|
let y = y as f32;
|
|
|
|
let center_x = w as f32 / 2.0;
|
|
let center_y = h as f32 / 2.0;
|
|
|
|
((x - center_x).powf(2.0) + (y - center_y).powf(2.0)).sqrt()
|
|
};
|
|
|
|
let radius = frame.powf(1.8);
|
|
|
|
let dist_from_radius = dist_from_center - radius;
|
|
let dist = 0.3 * dist_from_radius;
|
|
|
|
Rgb([100, 0, 100]).map(|x| {
|
|
(if dist < 0.0 {
|
|
(1.0 - (dist.abs() / 10.0)).max(0.0)
|
|
} else {
|
|
dist.min(FRAC_PI_2).cos()
|
|
} * x as f32) as u8
|
|
})
|
|
})
|
|
}
|
|
|
|
fn print_image(image: &RgbImage) {
|
|
let _hide = termion::cursor::HideCursor::from(stdout());
|
|
|
|
print!("{}", termion::cursor::Goto(1, 1));
|
|
for row in image.rows() {
|
|
for &Rgb([r, g, b]) in row {
|
|
print!("{} ", termion::color::Rgb(r, g, b).bg_string());
|
|
}
|
|
println!("{}", termion::color::Reset.bg_str());
|
|
}
|
|
}
|
|
|
|
fn main() -> anyhow::Result<()> {
|
|
let args = Args::parse();
|
|
|
|
let bind_addr = args
|
|
.bind_addr
|
|
.unwrap_or_else(|| (Ipv4Addr::UNSPECIFIED, 0).into());
|
|
let socket = UdpSocket::bind(bind_addr)?;
|
|
|
|
socket.connect(args.remote_addr)?;
|
|
socket.set_read_timeout(match args.response_timeout {
|
|
0 => None,
|
|
t => Some(Duration::from_millis(t)),
|
|
})?;
|
|
|
|
let layout = Layout {
|
|
gang_len: 8,
|
|
num_gangs: 32,
|
|
|
|
num_panels_h: 6,
|
|
num_panels_v: 1,
|
|
};
|
|
|
|
match args.action {
|
|
Action::Solid { color } => {
|
|
let image = RgbImage::from_pixel(layout.width_px(), layout.height_px(), color.into());
|
|
let frame_num: u32 = rand::thread_rng().gen();
|
|
send_frame(&socket, layout, frame_num, &image)?;
|
|
}
|
|
Action::Clear => {
|
|
let image = RgbImage::new(layout.width_px(), layout.height_px());
|
|
let frame_num: u32 = rand::thread_rng().gen();
|
|
send_frame(&socket, layout, frame_num, &image)?;
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|