Compare commits
12 commits
bbaa094f00
...
dc94dc0347
Author | SHA1 | Date | |
---|---|---|---|
dc94dc0347 | |||
966d3027e3 | |||
35a3319e7f | |||
3c36bc6766 | |||
c29d5b96a8 | |||
bc6c014b68 | |||
f63fd2dff2 | |||
11a929eec6 | |||
1b7fb6aeaa | |||
3d49b36f33 | |||
3c431ccac3 | |||
113e0f5ae6 |
3 changed files with 127 additions and 60 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
#![feature(try_blocks)]
|
||||||
#![warn(clippy::pedantic)]
|
#![warn(clippy::pedantic)]
|
||||||
|
|
||||||
mod strandifier;
|
mod strandifier;
|
||||||
|
|
161
src/main.rs
161
src/main.rs
|
@ -1,30 +1,28 @@
|
||||||
#![warn(clippy::pedantic)]
|
#![warn(clippy::pedantic)]
|
||||||
|
#![feature(never_type)]
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
f32::consts::FRAC_PI_2,
|
f32::consts::FRAC_PI_2,
|
||||||
io::stdout,
|
io::{stdin, stdout, Read},
|
||||||
net::{Ipv4Addr, SocketAddr, UdpSocket},
|
net::{Ipv4Addr, SocketAddr, UdpSocket},
|
||||||
num::ParseIntError,
|
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
str::FromStr,
|
|
||||||
thread::sleep,
|
thread::sleep,
|
||||||
time::Duration,
|
time::{Duration, Instant},
|
||||||
};
|
};
|
||||||
|
|
||||||
use bracket_color::prelude::{HSV, RGB};
|
use bracket_color::prelude::{HSV, RGB};
|
||||||
use clap::{Parser, Subcommand, ValueEnum};
|
use clap::{builder::TypedValueParser, Parser, Subcommand, ValueEnum};
|
||||||
use image::{imageops::FilterType, io::Reader as ImageReader, Pixel, Rgb, RgbImage};
|
use image::{imageops::FilterType, io::Reader as ImageReader, Pixel, Rgb, RgbImage};
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
use splink_client::{send_frame, Layout};
|
use splink_client::{send_frame, Layout, SenderError};
|
||||||
use thiserror::Error;
|
|
||||||
|
|
||||||
/// Blinkenwall v3 prototype client
|
/// Blinkenwall v3 prototype client
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
#[clap(author, version, about, long_about = None)]
|
#[clap(author, version, about, long_about = None)]
|
||||||
struct Args {
|
struct Args {
|
||||||
/// Timeout in ms for frame completion responses (0 to disable)
|
/// Timeout in ms for frame completion responses (0 to disable)
|
||||||
#[clap(long, default_value_t = 3)]
|
#[clap(long, default_value_t = 10)]
|
||||||
response_timeout: u64,
|
response_timeout: u64,
|
||||||
|
|
||||||
/// Local address and port to bind to
|
/// Local address and port to bind to
|
||||||
|
@ -39,42 +37,37 @@ struct Args {
|
||||||
action: Action,
|
action: Action,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy)]
|
||||||
struct Color {
|
struct ColorParser;
|
||||||
r: u8,
|
impl TypedValueParser for ColorParser {
|
||||||
g: u8,
|
type Value = Rgb<u8>;
|
||||||
b: u8,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Error)]
|
fn parse_ref(
|
||||||
enum ColorError {
|
&self,
|
||||||
#[error("Wrong parameter length")]
|
_cmd: &clap::Command,
|
||||||
WrongLength,
|
_arg: Option<&clap::Arg>,
|
||||||
#[error("Illegal integer")]
|
value: &std::ffi::OsStr,
|
||||||
BadNumber(#[from] ParseIntError),
|
) -> Result<Self::Value, clap::Error> {
|
||||||
}
|
let s = value
|
||||||
|
.to_str()
|
||||||
impl FromStr for Color {
|
.ok_or(clap::Error::raw(clap::ErrorKind::InvalidUtf8, ""))?;
|
||||||
type Err = ColorError;
|
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
||||||
let s = s.strip_prefix('#').unwrap_or(s);
|
let s = s.strip_prefix('#').unwrap_or(s);
|
||||||
|
|
||||||
if s.len() != 6 {
|
if s.len() != 6 {
|
||||||
return Err(ColorError::WrongLength);
|
return Err(clap::Error::raw(
|
||||||
|
clap::ErrorKind::InvalidValue,
|
||||||
|
"Must be a 6-digit hexadecimal number",
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let r = u8::from_str_radix(&s[0..2], 16)?;
|
let mut channels = [0; 3];
|
||||||
let g = u8::from_str_radix(&s[2..4], 16)?;
|
for i in 0..3 {
|
||||||
let b = u8::from_str_radix(&s[4..6], 16)?;
|
channels[i] = u8::from_str_radix(&s[i * 2..i * 2 + 2], 16).map_err(|_| {
|
||||||
|
clap::Error::raw(clap::ErrorKind::InvalidValue, "Invalid hex literal")
|
||||||
Ok(Self { r, g, b })
|
})?;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Color> for Rgb<u8> {
|
Ok(Rgb(channels))
|
||||||
fn from(c: Color) -> Rgb<u8> {
|
|
||||||
Rgb([c.r, c.g, c.b])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,11 +78,13 @@ enum Action {
|
||||||
animation: Animation,
|
animation: Animation,
|
||||||
},
|
},
|
||||||
Solid {
|
Solid {
|
||||||
color: Color,
|
#[clap(value_parser = ColorParser)]
|
||||||
|
color: Rgb<u8>,
|
||||||
},
|
},
|
||||||
Image {
|
Image {
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
},
|
},
|
||||||
|
Video,
|
||||||
Clear,
|
Clear,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,6 +92,7 @@ enum Action {
|
||||||
enum Animation {
|
enum Animation {
|
||||||
Rainbow,
|
Rainbow,
|
||||||
Bling,
|
Bling,
|
||||||
|
Strobe,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bling(layout: Layout, frame: u32) -> RgbImage {
|
fn bling(layout: Layout, frame: u32) -> RgbImage {
|
||||||
|
@ -150,13 +146,37 @@ fn rainbow(layout: Layout, frame: u32) -> RgbImage {
|
||||||
let w = layout.width_px();
|
let w = layout.width_px();
|
||||||
let h = layout.height_px();
|
let h = layout.height_px();
|
||||||
|
|
||||||
|
let tightness = 100.0;
|
||||||
|
let slowness = 200;
|
||||||
|
let brightness = 0.1;
|
||||||
|
|
||||||
RgbImage::from_fn(w, h, |x, y| {
|
RgbImage::from_fn(w, h, |x, y| {
|
||||||
let RGB { r, g, b } =
|
let RGB { r, g, b } = HSV::from_f32(
|
||||||
HSV::from_f32((x + y + frame) as f32 / 100.0 % 1.0, 1.0, 0.1).to_rgb();
|
(x + y) as f32 / tightness % 1.0 + (frame % slowness) as f32 / slowness as f32,
|
||||||
|
1.0,
|
||||||
|
brightness,
|
||||||
|
)
|
||||||
|
.to_rgb();
|
||||||
Rgb([(r * 255.0) as u8, (g * 255.0) as u8, (b * 255.0) as u8])
|
Rgb([(r * 255.0) as u8, (g * 255.0) as u8, (b * 255.0) as u8])
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn strobe(layout: Layout, frame: u32) -> RgbImage {
|
||||||
|
let w = layout.width_px();
|
||||||
|
let h = layout.height_px();
|
||||||
|
|
||||||
|
let brightness = 120;
|
||||||
|
let period = 15;
|
||||||
|
|
||||||
|
RgbImage::from_fn(w, h, |_, _| {
|
||||||
|
if frame % period < period / 2 {
|
||||||
|
Rgb([brightness; 3])
|
||||||
|
} else {
|
||||||
|
Rgb([0, 0, 0])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn print_image(image: &RgbImage) {
|
fn print_image(image: &RgbImage) {
|
||||||
let _hide = termion::cursor::HideCursor::from(stdout());
|
let _hide = termion::cursor::HideCursor::from(stdout());
|
||||||
|
|
||||||
|
@ -169,6 +189,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<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
|
|
||||||
|
@ -196,33 +240,48 @@ fn main() -> anyhow::Result<()> {
|
||||||
|
|
||||||
let image = match args.action {
|
let image = match args.action {
|
||||||
Action::Solid { color } => {
|
Action::Solid { color } => {
|
||||||
RgbImage::from_pixel(layout.width_px(), layout.height_px(), color.into())
|
RgbImage::from_pixel(layout.width_px(), layout.height_px(), color)
|
||||||
}
|
}
|
||||||
Action::Clear => RgbImage::new(layout.width_px(), layout.height_px()),
|
Action::Clear => RgbImage::new(layout.width_px(), layout.height_px()),
|
||||||
Action::Image { path } => ImageReader::open(path)?
|
Action::Image { path } => ImageReader::open(path)?
|
||||||
.decode()?
|
.decode()?
|
||||||
.resize_to_fill(layout.width_px(), layout.height_px(), FilterType::Gaussian)
|
.resize(layout.width_px(), layout.height_px(), FilterType::Gaussian)
|
||||||
.into_rgb8(),
|
.into_rgb8(),
|
||||||
Action::Animation { animation } => {
|
Action::Animation { animation } => {
|
||||||
let f = match animation {
|
animate(
|
||||||
|
&socket,
|
||||||
|
layout,
|
||||||
|
match animation {
|
||||||
Animation::Rainbow => rainbow,
|
Animation::Rainbow => rainbow,
|
||||||
Animation::Bling => bling,
|
Animation::Bling => bling,
|
||||||
};
|
Animation::Strobe => strobe,
|
||||||
|
},
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
Action::Video => {
|
||||||
print!("{}", termion::clear::All);
|
print!("{}", termion::clear::All);
|
||||||
|
|
||||||
let mut frame = 0;
|
let mut frame_num: u32 = rand::thread_rng().gen();
|
||||||
loop {
|
loop {
|
||||||
let start = Instant::now();
|
let w = layout.width_px();
|
||||||
let image = f(layout, frame);
|
let h = layout.height_px();
|
||||||
|
|
||||||
|
let mut buf = vec![0; w as usize * h as usize * 3];
|
||||||
|
stdin().lock().read_exact(&mut buf)?;
|
||||||
|
for c in &mut buf {
|
||||||
|
*c /= 6;
|
||||||
|
}
|
||||||
|
let image = RgbImage::from_vec(w, h, buf).unwrap();
|
||||||
|
|
||||||
print_image(&image);
|
print_image(&image);
|
||||||
|
|
||||||
let frame_num: u32 = rand::thread_rng().gen();
|
match send_frame(&socket, layout, frame_num, &image) {
|
||||||
send_frame(&socket, layout, frame_num, &image)?;
|
Ok(()) | Err(SenderError::ConfirmationTimeout) => {}
|
||||||
|
Err(e) => return Err(e.into()),
|
||||||
|
};
|
||||||
|
frame_num += 1;
|
||||||
|
|
||||||
sleep(Duration::from_millis(16));
|
sleep(Duration::from_millis(30));
|
||||||
frame += 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -8,8 +8,8 @@ use crate::Layout;
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Error)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Error)]
|
||||||
#[allow(clippy::module_name_repetitions)]
|
#[allow(clippy::module_name_repetitions)]
|
||||||
pub enum StrandifierError {
|
pub enum StrandifierError {
|
||||||
#[error("Wrong image dimensions")]
|
#[error("Image too large")]
|
||||||
WrongDimensions,
|
ImageTooLarge,
|
||||||
#[error("Invalid strand number")]
|
#[error("Invalid strand number")]
|
||||||
InvalidStrand,
|
InvalidStrand,
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ impl<'a> Strandifier<'a> {
|
||||||
strand_num: u32,
|
strand_num: u32,
|
||||||
) -> Result<Self, StrandifierError> {
|
) -> Result<Self, StrandifierError> {
|
||||||
if layout.width_px() < image.width() || layout.height_px() < image.height() {
|
if layout.width_px() < image.width() || layout.height_px() < image.height() {
|
||||||
return Err(StrandifierError::WrongDimensions);
|
return Err(StrandifierError::ImageTooLarge);
|
||||||
}
|
}
|
||||||
|
|
||||||
if strand_num > layout.num_strands() {
|
if strand_num > layout.num_strands() {
|
||||||
|
@ -107,7 +107,14 @@ impl<'a> Iterator for Strandifier<'a> {
|
||||||
}
|
}
|
||||||
self.pixels_remaining -= 1;
|
self.pixels_remaining -= 1;
|
||||||
|
|
||||||
Some(*self.image.get_pixel(x + self.offset_x, y + self.offset_y))
|
let opt_px: Option<_> = try {
|
||||||
|
let x = x.checked_sub(self.offset_x)?;
|
||||||
|
let y = y.checked_sub(self.offset_y)?;
|
||||||
|
|
||||||
|
*self.image.get_pixel_checked(x, y)?
|
||||||
|
};
|
||||||
|
|
||||||
|
Some(opt_px.unwrap_or(Rgb([0, 0, 0])))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||||
|
@ -268,12 +275,12 @@ mod tests {
|
||||||
);
|
);
|
||||||
|
|
||||||
let layout2 = Layout {
|
let layout2 = Layout {
|
||||||
num_gangs: 6,
|
num_gangs: 3,
|
||||||
..layout
|
..layout
|
||||||
};
|
};
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Strandifier::make_strand(layout2, &image, 0).unwrap_err(),
|
Strandifier::make_strand(layout2, &image, 0).unwrap_err(),
|
||||||
StrandifierError::WrongDimensions
|
StrandifierError::ImageTooLarge
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue