Skip to content

Commit

Permalink
wip BrightnessGrid
Browse files Browse the repository at this point in the history
  • Loading branch information
kaesaecracker committed Jun 23, 2024
1 parent e0647ba commit c554fbd
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 86 deletions.
4 changes: 2 additions & 2 deletions crates/servicepoint/examples/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use clap::Parser;

use servicepoint::{ByteGrid, Command, Connection, Grid, Origin};
use servicepoint::{Command, Connection, Cp473Grid, Grid, Origin};

#[derive(Parser, Debug)]
struct Cli {
Expand Down Expand Up @@ -33,7 +33,7 @@ fn main() {

let max_width = cli.text.iter().map(|t| t.len()).max().unwrap();

let mut chars = ByteGrid::new(max_width, cli.text.len());
let mut chars = Cp473Grid::new(max_width, cli.text.len());
for y in 0..cli.text.len() {
let row = &cli.text[y];

Expand Down
5 changes: 3 additions & 2 deletions crates/servicepoint/examples/brightness_tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ fn main() {
))
.expect("send failed");

let mut brightnesses = ByteGrid::new(TILE_WIDTH, TILE_HEIGHT);
let max_brightness = usize::from(u8::from(Brightness::MAX));
let mut brightnesses = BrightnessGrid::new(TILE_WIDTH, TILE_HEIGHT);
for (index, byte) in brightnesses.data_ref_mut().iter_mut().enumerate() {
*byte = (index % u8::MAX as usize) as u8;
*byte = Brightness::try_from((index % max_brightness) as u8).unwrap();
}

connection
Expand Down
9 changes: 6 additions & 3 deletions crates/servicepoint/examples/random_brightness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ fn main() {
let mut filled_grid = PixelGrid::max_sized();
filled_grid.fill(true);

let command =
BitmapLinearWin(Origin::new(0, 0), filled_grid, CompressionCode::Lzma);
let command = BitmapLinearWin(
Origin::new(0, 0),
filled_grid,
CompressionCode::Lzma,
);
connection.send(command).expect("send failed");
}

Expand All @@ -50,7 +53,7 @@ fn main() {
let h = rng.gen_range(min_size..=TILE_HEIGHT - y);

let origin = Origin::new(x, y);
let mut luma = ByteGrid::new(w, h);
let mut luma = BrightnessGrid::new(w, h);

for y in 0..h {
for x in 0..w {
Expand Down
53 changes: 51 additions & 2 deletions crates/servicepoint/src/brightness.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::{Grid, PrimitiveGrid};
#[cfg(feature = "rand")]
use rand::{
distributions::{Distribution, Standard},
Expand All @@ -8,18 +9,21 @@ use rand::{
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Brightness(u8);

/// A grid containing brightness values.
pub type BrightnessGrid = PrimitiveGrid<Brightness>;

impl From<Brightness> for u8 {
fn from(brightness: Brightness) -> Self {
brightness.0
}
}

impl TryFrom<u8> for Brightness {
type Error = ();
type Error = u8;

fn try_from(value: u8) -> Result<Self, Self::Error> {
if value > Self::MAX.0 {
Err(())
Err(value)
} else {
Ok(Brightness(value))
}
Expand All @@ -33,6 +37,51 @@ impl Brightness {
pub const MIN: Brightness = Brightness(0);
}

impl Default for Brightness {
fn default() -> Self {
Self::MAX
}
}

impl From<BrightnessGrid> for Vec<u8> {
fn from(value: PrimitiveGrid<Brightness>) -> Self {
value
.iter()
.map(|brightness| (*brightness).into())
.collect()
}
}

impl From<BrightnessGrid> for PrimitiveGrid<u8> {
fn from(value: PrimitiveGrid<Brightness>) -> Self {
let u8s = value
.iter()
.map(|brightness| (*brightness).into())
.collect::<Vec<u8>>();
PrimitiveGrid::load(value.width(), value.height(), &u8s)
}
}

impl TryFrom<PrimitiveGrid<u8>> for BrightnessGrid {
type Error = u8;

fn try_from(value: PrimitiveGrid<u8>) -> Result<Self, Self::Error> {
let brightnesses = value
.iter()
.map(|b| Brightness::try_from(*b))
.collect::<Result<Vec<Brightness>, _>>();
let brightnesses = match brightnesses {
Ok(vec) => vec,
Err(u8) => return Err(u8),
};
Ok(BrightnessGrid::load(
value.width(),
value.height(),
&brightnesses,
))
}
}

#[cfg(feature = "rand")]
impl Distribution<Brightness> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Brightness {
Expand Down
38 changes: 28 additions & 10 deletions crates/servicepoint/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ use bitvec::prelude::BitVec;
use crate::{
command_code::CommandCode,
compression::{into_compressed, into_decompressed},
Brightness, ByteGrid, CompressionCode, Grid, Header, Origin, Packet,
PixelGrid, Pixels, SpBitVec, Tiles, TILE_SIZE,
Brightness, BrightnessGrid, CompressionCode, Grid, Header, Origin, Packet,
PixelGrid, Pixels, PrimitiveGrid, SpBitVec, Tiles, TILE_SIZE,
};

/// Type alias for documenting the meaning of the u16 in enum values
pub type Offset = usize;

/// A grid containing codepage 437 characters.
///
/// The encoding is currently not enforced.
pub type Cp473Grid = PrimitiveGrid<u8>;

/// A command to send to the display.
#[derive(Debug, Clone, PartialEq)]
pub enum Command {
Expand All @@ -22,7 +27,7 @@ pub enum Command {
/// The library does not currently convert between UTF-8 and CP-437.
/// Because Rust expects UTF-8 strings, it might be necessary to only send ASCII for now.
/// </div>
Cp437Data(Origin<Tiles>, ByteGrid),
Cp437Data(Origin<Tiles>, Cp473Grid),

/// Sets a window of pixels to the specified values
BitmapLinearWin(Origin<Pixels>, PixelGrid, CompressionCode),
Expand All @@ -31,7 +36,7 @@ pub enum Command {
Brightness(Brightness),

/// Set the brightness of individual tiles in a rectangular area of the display.
CharBrightness(Origin<Tiles>, ByteGrid),
CharBrightness(Origin<Tiles>, BrightnessGrid),

/// Set pixel data starting at the pixel offset on screen.
///
Expand Down Expand Up @@ -271,14 +276,24 @@ impl TryFrom<Packet> for Command {
let Packet(_, payload) = packet;
Ok(Command::Cp437Data(
Origin::new(a as usize, b as usize),
ByteGrid::load(c as usize, d as usize, &payload),
Cp473Grid::load(c as usize, d as usize, &payload),
))
}
CommandCode::CharBrightness => {
let Packet(_, payload) = packet;

let grid =
PrimitiveGrid::load(c as usize, d as usize, &payload);
let grid = match BrightnessGrid::try_from(grid) {
Ok(grid) => grid,
Err(val) => {
return Err(TryFromPacketError::InvalidBrightness(val))
}
};

Ok(Command::CharBrightness(
Origin::new(a as usize, b as usize),
ByteGrid::load(c as usize, d as usize, &payload),
grid,
))
}
#[allow(deprecated)]
Expand Down Expand Up @@ -424,8 +439,8 @@ impl Command {
mod tests {
use crate::{
bitvec::prelude::BitVec, command::TryFromPacketError,
command_code::CommandCode, origin::Pixels, Brightness, ByteGrid,
Command, CompressionCode, Header, Origin, Packet, PixelGrid,
command_code::CommandCode, origin::Pixels, Brightness, Command,
CompressionCode, Header, Origin, Packet, PixelGrid, PrimitiveGrid,
};

fn round_trip(original: Command) {
Expand Down Expand Up @@ -481,13 +496,16 @@ mod tests {
fn round_trip_char_brightness() {
round_trip(Command::CharBrightness(
Origin::new(5, 2),
ByteGrid::new(7, 5),
PrimitiveGrid::new(7, 5),
));
}

#[test]
fn round_trip_cp437_data() {
round_trip(Command::Cp437Data(Origin::new(5, 2), ByteGrid::new(7, 5)));
round_trip(Command::Cp437Data(
Origin::new(5, 2),
PrimitiveGrid::new(7, 5),
));
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions crates/servicepoint/src/data_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
///
/// The expectation is that you can create an equal instance with this data given the additional
/// metadata needed.
pub trait DataRef {
pub trait DataRef<T> {
/// Get the underlying bytes writable.
fn data_ref_mut(&mut self) -> &mut [u8];
fn data_ref_mut(&mut self) -> &mut [T];

/// Get the underlying bytes read-only.
fn data_ref(&self) -> &[u8];
fn data_ref(&self) -> &[T];
}
8 changes: 4 additions & 4 deletions crates/servicepoint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@ use std::time::Duration;
pub use bitvec;
use bitvec::prelude::{BitVec, Msb0};

pub use crate::brightness::Brightness;
pub use crate::byte_grid::ByteGrid;
pub use crate::command::{Command, Offset};
pub use crate::brightness::{Brightness, BrightnessGrid};
pub use crate::command::{Command, Cp473Grid, Offset};
pub use crate::compression_code::CompressionCode;
pub use crate::connection::Connection;
pub use crate::data_ref::DataRef;
pub use crate::grid::Grid;
pub use crate::origin::{Origin, Pixels, Tiles};
pub use crate::packet::{Header, Packet, Payload};
pub use crate::pixel_grid::PixelGrid;
pub use crate::primitive_grid::PrimitiveGrid;

type SpBitVec = BitVec<u8, Msb0>;

mod brightness;
mod byte_grid;
mod command;
mod command_code;
mod compression;
Expand All @@ -30,6 +29,7 @@ mod grid;
mod origin;
mod packet;
mod pixel_grid;
mod primitive_grid;

/// size of a single tile in one dimension
pub const TILE_SIZE: usize = 8;
Expand Down
2 changes: 1 addition & 1 deletion crates/servicepoint/src/pixel_grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl Grid<bool> for PixelGrid {
}
}

impl DataRef for PixelGrid {
impl DataRef<u8> for PixelGrid {
fn data_ref_mut(&mut self) -> &mut [u8] {
self.bit_vec.as_raw_mut_slice()
}
Expand Down
Loading

0 comments on commit c554fbd

Please sign in to comment.