Compare commits

...

7 commits

5 changed files with 137 additions and 27 deletions

7
Cargo.lock generated
View file

@ -49,6 +49,12 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bracket-color"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c1d1b160817fb74eebedccd678055cd688d1a73dc1a14519fa30ff4c9a5bdee"
[[package]]
name = "bumpalo"
version = "3.10.0"
@ -662,6 +668,7 @@ name = "splink_client"
version = "0.1.0"
dependencies = [
"anyhow",
"bracket-color",
"clap",
"image",
"rand",

View file

@ -7,6 +7,7 @@ edition = "2021"
[dependencies]
anyhow = "1.0.57"
bracket-color = "0.8.2"
clap = { version = "3.1.18", features = ["derive", "deprecated"] }
image = "0.24.2"
rand = "0.8.5"

View file

@ -4,7 +4,7 @@ mod strandifier;
pub use strandifier::{Strandifier, StrandifierError};
mod sender;
pub use sender::{SenderError, send_strand, send_frame};
pub use sender::{send_frame, send_strand, SenderError};
/// The LED wall's layout.
///
@ -33,6 +33,9 @@ pub struct Layout {
pub num_panels_h: u32,
pub num_panels_v: u32,
pub total_strands: u32,
pub first_strand_index: u32,
}
impl Layout {

View file

@ -4,15 +4,20 @@ use std::{
f32::consts::FRAC_PI_2,
io::stdout,
net::{Ipv4Addr, SocketAddr, UdpSocket},
num::ParseIntError,
path::PathBuf,
str::FromStr,
thread::sleep,
time::Duration,
};
use clap::Parser;
use bracket_color::prelude::{HSV, RGB};
use clap::{Parser, Subcommand};
use image::{Pixel, Rgb, RgbImage};
use rand::Rng;
use splink_client::{Layout, send_frame};
use splink_client::{send_frame, Layout};
use thiserror::Error;
/// Blinkenwall v3 prototype client
#[derive(Parser, Debug)]
@ -28,10 +33,60 @@ struct Args {
/// 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,
}
fn get_frame(layout: Layout, frame: u32) -> RgbImage {
#[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 {
Rainbow,
Solid { color: Color },
Image { path: PathBuf },
Clear,
}
fn bling(layout: Layout, frame: u32) -> RgbImage {
#![allow(
clippy::cast_precision_loss,
clippy::cast_lossless,
@ -71,6 +126,24 @@ fn get_frame(layout: Layout, frame: u32) -> RgbImage {
})
}
fn rainbow(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();
RgbImage::from_fn(w, h, |x, y| {
let RGB { r, g, b } =
HSV::from_f32((x + y + frame) as f32 / 100.0 % 1.0, 1.0, 0.1).to_rgb();
Rgb([(r * 255.0) as u8, (g * 255.0) as u8, (b * 255.0) as u8])
})
}
fn print_image(image: &RgbImage) {
let _hide = termion::cursor::HideCursor::from(stdout());
@ -103,22 +176,42 @@ fn main() -> anyhow::Result<()> {
num_panels_h: 6,
num_panels_v: 1,
total_strands: 24,
first_strand_index: 8,
};
for frame in 0.. {
let image = get_frame(layout, frame);
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)?;
}
Action::Image { path } => {
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)?;
}
Action::Rainbow => {
print!("{}", termion::clear::All);
print_image(&image);
for frame in 0.. {
let image = rainbow(layout, frame);
let frame_num: u32 = rand::thread_rng().gen();
send_frame(&socket, layout, frame_num, &image)?;
print_image(&image);
sleep(Duration::from_millis(16));
let frame_num: u32 = rand::thread_rng().gen();
send_frame(&socket, layout, frame_num, &image)?;
sleep(Duration::from_millis(16));
}
}
}
let image = RgbImage::from_fn(96, 64, |_x, y| {
[Rgb([0, 3, 1]), Rgb([1, 3, 0]), Rgb([3, 0, 1])][y as usize % 3]
});
Ok(())
}

View file

@ -1,7 +1,4 @@
use std::{
io::{self, ErrorKind},
net::UdpSocket,
};
use std::{io, net::UdpSocket};
use image::{Rgb, RgbImage};
use thiserror::Error;
@ -67,18 +64,27 @@ pub fn send_frame(
frame_num: u32,
image: &RgbImage,
) -> Result<(), SenderError> {
for strand_num in 0..layout.num_strands() {
let data = Strandifier::make_strand(layout, image, strand_num)?;
send_strand(socket, strand_num, frame_num, data)?;
for strand_num in 0..layout.total_strands {
if strand_num >= layout.first_strand_index
&& strand_num < layout.num_strands() + layout.first_strand_index
{
let data =
Strandifier::make_strand(layout, image, strand_num - layout.first_strand_index)?;
send_strand(socket, strand_num as u32, frame_num, data)?;
} else {
send_strand(
socket,
strand_num as u32,
frame_num,
vec![Rgb([0, 0, 0]); layout.strand_len() as usize],
)?;
}
}
let mut buf = vec![0; 100];
let len = match socket.recv(&mut buf) {
Ok(len) => len,
//Err(e) if e.kind() == ErrorKind::TimedOut => return Err(SenderError::ConfirmationTimeout),
Err(e) => return Err(SenderError::ConfirmationTimeout),
Err(e) => return Err(SenderError::Io(e)),
};
let len = socket
.recv(&mut buf)
.map_err(|_| SenderError::ConfirmationTimeout)?;
let buf = &buf[..len];
let response_frame_num = u32::from_be_bytes(
buf.try_into()