splink_client/src/lib.rs

138 lines
3.2 KiB
Rust

#![feature(try_blocks)]
#![warn(clippy::pedantic)]
mod strandifier;
pub use strandifier::{Strandifier, StrandifierError};
mod sender;
pub use sender::{send_frame, send_strand, SenderError};
/// The LED wall's layout.
///
/// The wall consists of a grid of panels (`num_panels_h` by `num_panels_v`), each of which
/// contains one strand. Each strand is wound in an S-shape, going sideways for `gang_len`
/// pixels before going down and sideways again in the opposite direction. There are `num_gangs`
/// of these gangs per strand.
///
/// The following is a strand with a `gang_len` of 5 and `num_gangs` of 4:
///
/// ```plain
/// 1 5
/// x - x - x - x - x
/// |
/// x - x - x - x - x
/// |
/// x - x - x - x - x
/// |
/// x - x - x - x - x
/// 20 16
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Layout {
pub gang_len: u32,
pub num_gangs: u32,
pub num_panels_h: u32,
pub num_panels_v: u32,
pub total_strands: u32,
pub first_strand_index: u32,
}
impl Layout {
/// The length of each strand.
///
/// ```rust
/// # use splink_client::Layout;
/// let layout = Layout {
/// gang_len: 5,
/// num_gangs: 4,
///
/// num_panels_h: 3,
/// num_panels_v: 6,
/// };
///
/// assert_eq!(layout.strand_len(), 20);
/// ```
#[must_use]
pub fn strand_len(&self) -> u32 {
self.gang_len * self.num_gangs
}
/// The number of strands/panels on the wall.
///
/// ```rust
/// # use splink_client::Layout;
/// let layout = Layout {
/// gang_len: 5,
/// num_gangs: 4,
///
/// num_panels_h: 3,
/// num_panels_v: 6,
/// };
///
/// assert_eq!(layout.num_strands(), 18);
/// ```
#[must_use]
pub fn num_strands(&self) -> u32 {
self.num_panels_h * self.num_panels_v
}
/// The total number of pixels on the wall.
///
/// ```rust
/// # use splink_client::Layout;
/// let layout = Layout {
/// gang_len: 5,
/// num_gangs: 4,
///
/// num_panels_h: 3,
/// num_panels_v: 6,
/// };
///
/// assert_eq!(layout.num_pixels(), 360);
/// ```
#[must_use]
pub fn num_pixels(&self) -> u32 {
self.strand_len() * self.num_panels_h * self.num_panels_v
}
/// The width of the wall, in pixels.
///
/// ```rust
/// # use splink_client::Layout;
/// let layout = Layout {
/// gang_len: 5,
/// num_gangs: 4,
///
/// num_panels_h: 3,
/// num_panels_v: 6,
/// };
///
/// assert_eq!(layout.width_px(), 15);
/// ```
#[must_use]
pub fn width_px(&self) -> u32 {
self.gang_len * self.num_panels_h
}
/// The height of the wall, in pixels.
///
/// ```rust
/// # use splink_client::Layout;
/// let layout = Layout {
/// gang_len: 5,
/// num_gangs: 4,
///
/// num_panels_h: 3,
/// num_panels_v: 6,
/// };
///
/// assert_eq!(layout.height_px(), 24);
/// ```
#[must_use]
pub fn height_px(&self) -> u32 {
self.num_gangs * self.num_panels_v
}
}