Compare commits
7 commits
893fcb8fb1
...
cc29d5226b
Author | SHA1 | Date | |
---|---|---|---|
cc29d5226b | |||
7020006e89 | |||
8ab9cd7010 | |||
0d8f11c712 | |||
031245496e | |||
4a1593801a | |||
e22bf2c436 |
5 changed files with 137 additions and 27 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -49,6 +49,12 @@ version = "1.3.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bracket-color"
|
||||||
|
version = "0.8.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3c1d1b160817fb74eebedccd678055cd688d1a73dc1a14519fa30ff4c9a5bdee"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bumpalo"
|
name = "bumpalo"
|
||||||
version = "3.10.0"
|
version = "3.10.0"
|
||||||
|
@ -662,6 +668,7 @@ name = "splink_client"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
|
"bracket-color",
|
||||||
"clap",
|
"clap",
|
||||||
"image",
|
"image",
|
||||||
"rand",
|
"rand",
|
||||||
|
|
|
@ -7,6 +7,7 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.57"
|
anyhow = "1.0.57"
|
||||||
|
bracket-color = "0.8.2"
|
||||||
clap = { version = "3.1.18", features = ["derive", "deprecated"] }
|
clap = { version = "3.1.18", features = ["derive", "deprecated"] }
|
||||||
image = "0.24.2"
|
image = "0.24.2"
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
|
|
|
@ -4,7 +4,7 @@ mod strandifier;
|
||||||
pub use strandifier::{Strandifier, StrandifierError};
|
pub use strandifier::{Strandifier, StrandifierError};
|
||||||
|
|
||||||
mod sender;
|
mod sender;
|
||||||
pub use sender::{SenderError, send_strand, send_frame};
|
pub use sender::{send_frame, send_strand, SenderError};
|
||||||
|
|
||||||
/// The LED wall's layout.
|
/// The LED wall's layout.
|
||||||
///
|
///
|
||||||
|
@ -33,6 +33,9 @@ pub struct Layout {
|
||||||
|
|
||||||
pub num_panels_h: u32,
|
pub num_panels_h: u32,
|
||||||
pub num_panels_v: u32,
|
pub num_panels_v: u32,
|
||||||
|
|
||||||
|
pub total_strands: u32,
|
||||||
|
pub first_strand_index: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Layout {
|
impl Layout {
|
||||||
|
|
109
src/main.rs
109
src/main.rs
|
@ -4,15 +4,20 @@ use std::{
|
||||||
f32::consts::FRAC_PI_2,
|
f32::consts::FRAC_PI_2,
|
||||||
io::stdout,
|
io::stdout,
|
||||||
net::{Ipv4Addr, SocketAddr, UdpSocket},
|
net::{Ipv4Addr, SocketAddr, UdpSocket},
|
||||||
|
num::ParseIntError,
|
||||||
|
path::PathBuf,
|
||||||
|
str::FromStr,
|
||||||
thread::sleep,
|
thread::sleep,
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
use clap::Parser;
|
use bracket_color::prelude::{HSV, RGB};
|
||||||
|
use clap::{Parser, Subcommand};
|
||||||
use image::{Pixel, Rgb, RgbImage};
|
use image::{Pixel, Rgb, RgbImage};
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
use splink_client::{Layout, send_frame};
|
use splink_client::{send_frame, Layout};
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
/// Blinkenwall v3 prototype client
|
/// Blinkenwall v3 prototype client
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
|
@ -28,10 +33,60 @@ struct Args {
|
||||||
|
|
||||||
/// Controller's address
|
/// Controller's address
|
||||||
remote_addr: SocketAddr,
|
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(
|
#![allow(
|
||||||
clippy::cast_precision_loss,
|
clippy::cast_precision_loss,
|
||||||
clippy::cast_lossless,
|
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) {
|
fn print_image(image: &RgbImage) {
|
||||||
let _hide = termion::cursor::HideCursor::from(stdout());
|
let _hide = termion::cursor::HideCursor::from(stdout());
|
||||||
|
|
||||||
|
@ -103,10 +176,32 @@ fn main() -> anyhow::Result<()> {
|
||||||
|
|
||||||
num_panels_h: 6,
|
num_panels_h: 6,
|
||||||
num_panels_v: 1,
|
num_panels_v: 1,
|
||||||
|
|
||||||
|
total_strands: 24,
|
||||||
|
first_strand_index: 8,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
for frame in 0.. {
|
for frame in 0.. {
|
||||||
let image = get_frame(layout, frame);
|
let image = rainbow(layout, frame);
|
||||||
|
|
||||||
print_image(&image);
|
print_image(&image);
|
||||||
|
|
||||||
|
@ -115,10 +210,8 @@ fn main() -> anyhow::Result<()> {
|
||||||
|
|
||||||
sleep(Duration::from_millis(16));
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,4 @@
|
||||||
use std::{
|
use std::{io, net::UdpSocket};
|
||||||
io::{self, ErrorKind},
|
|
||||||
net::UdpSocket,
|
|
||||||
};
|
|
||||||
|
|
||||||
use image::{Rgb, RgbImage};
|
use image::{Rgb, RgbImage};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
@ -67,18 +64,27 @@ pub fn send_frame(
|
||||||
frame_num: u32,
|
frame_num: u32,
|
||||||
image: &RgbImage,
|
image: &RgbImage,
|
||||||
) -> Result<(), SenderError> {
|
) -> Result<(), SenderError> {
|
||||||
for strand_num in 0..layout.num_strands() {
|
for strand_num in 0..layout.total_strands {
|
||||||
let data = Strandifier::make_strand(layout, image, strand_num)?;
|
if strand_num >= layout.first_strand_index
|
||||||
send_strand(socket, strand_num, frame_num, data)?;
|
&& 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 mut buf = vec![0; 100];
|
||||||
let len = match socket.recv(&mut buf) {
|
let len = socket
|
||||||
Ok(len) => len,
|
.recv(&mut buf)
|
||||||
//Err(e) if e.kind() == ErrorKind::TimedOut => return Err(SenderError::ConfirmationTimeout),
|
.map_err(|_| SenderError::ConfirmationTimeout)?;
|
||||||
Err(e) => return Err(SenderError::ConfirmationTimeout),
|
|
||||||
Err(e) => return Err(SenderError::Io(e)),
|
|
||||||
};
|
|
||||||
let buf = &buf[..len];
|
let buf = &buf[..len];
|
||||||
let response_frame_num = u32::from_be_bytes(
|
let response_frame_num = u32::from_be_bytes(
|
||||||
buf.try_into()
|
buf.try_into()
|
||||||
|
|
Loading…
Reference in a new issue