mirror of
https://github.com/cccb/servicepoint.git
synced 2025-01-18 18:10:14 +01:00
limit brightness to valid levels, optional rand dependency to implement Distribution trait
fixup! limit brightness to valid levels, optional rand dependency to implement Distribution trait
This commit is contained in:
parent
e1f009ee6f
commit
c4c6708533
|
@ -19,6 +19,7 @@ flate2 = { version = "1.0", optional = true }
|
||||||
bzip2 = { version = "0.4", optional = true }
|
bzip2 = { version = "0.4", optional = true }
|
||||||
zstd = { version = "0.13", optional = true }
|
zstd = { version = "0.13", optional = true }
|
||||||
rust-lzma = { version = "0.6.0", optional = true }
|
rust-lzma = { version = "0.6.0", optional = true }
|
||||||
|
rand = { version = "0.8", optional = true }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["compression_lzma"]
|
default = ["compression_lzma"]
|
||||||
|
@ -27,6 +28,11 @@ compression_bzip2 = ["dep:bzip2"]
|
||||||
compression_lzma = ["dep:rust-lzma"]
|
compression_lzma = ["dep:rust-lzma"]
|
||||||
compression_zstd = ["dep:zstd"]
|
compression_zstd = ["dep:zstd"]
|
||||||
all_compressions = ["compression_zlib", "compression_bzip2", "compression_lzma", "compression_zstd"]
|
all_compressions = ["compression_zlib", "compression_bzip2", "compression_lzma", "compression_zstd"]
|
||||||
|
rand = ["dep:rand"]
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "random_brightness"
|
||||||
|
required-features = ["rand"]
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
# for examples
|
# for examples
|
||||||
|
|
|
@ -32,7 +32,7 @@ fn main() {
|
||||||
filled_grid.fill(true);
|
filled_grid.fill(true);
|
||||||
|
|
||||||
let command =
|
let command =
|
||||||
BitmapLinearWin(Origin(0, 0), filled_grid, CompressionCode::Lzma);
|
BitmapLinearWin(Origin::new(0, 0), filled_grid, CompressionCode::Lzma);
|
||||||
connection.send(command).expect("send failed");
|
connection.send(command).expect("send failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,7 +49,7 @@ fn main() {
|
||||||
let w = rng.gen_range(min_size..=TILE_WIDTH - x);
|
let w = rng.gen_range(min_size..=TILE_WIDTH - x);
|
||||||
let h = rng.gen_range(min_size..=TILE_HEIGHT - y);
|
let h = rng.gen_range(min_size..=TILE_HEIGHT - y);
|
||||||
|
|
||||||
let origin = Origin(x, y);
|
let origin = Origin::new(x, y);
|
||||||
let mut luma = ByteGrid::new(w, h);
|
let mut luma = ByteGrid::new(w, h);
|
||||||
|
|
||||||
for y in 0..h {
|
for y in 0..h {
|
||||||
|
|
41
crates/servicepoint/src/brightness.rs
Normal file
41
crates/servicepoint/src/brightness.rs
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
use rand::distributions::Standard;
|
||||||
|
#[cfg(feature = "rand")]
|
||||||
|
use rand::prelude::Distribution;
|
||||||
|
#[cfg(feature = "rand")]
|
||||||
|
use rand::Rng;
|
||||||
|
|
||||||
|
/// A display brightness value, checked for correct value range
|
||||||
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||||
|
pub struct Brightness(u8);
|
||||||
|
|
||||||
|
impl From<Brightness> for u8 {
|
||||||
|
fn from(brightness: Brightness) -> Self {
|
||||||
|
brightness.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<u8> for Brightness {
|
||||||
|
type Error = ();
|
||||||
|
|
||||||
|
fn try_from(value: u8) -> Result<Self, Self::Error> {
|
||||||
|
if value > Self::MAX.0 {
|
||||||
|
Err(())
|
||||||
|
} else {
|
||||||
|
Ok(Brightness(value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Brightness {
|
||||||
|
/// highest possible brightness value, 11
|
||||||
|
pub const MAX: Brightness = Brightness(11);
|
||||||
|
/// lowest possible brightness value, 0
|
||||||
|
pub const MIN: Brightness = Brightness(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "rand")]
|
||||||
|
impl Distribution<Brightness> for Standard {
|
||||||
|
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Brightness {
|
||||||
|
Brightness(rng.gen_range(Brightness::MIN.0..(Brightness::MAX.0 + 1)))
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,8 +3,8 @@ use bitvec::prelude::BitVec;
|
||||||
use crate::command_code::CommandCode;
|
use crate::command_code::CommandCode;
|
||||||
use crate::compression::{into_compressed, into_decompressed};
|
use crate::compression::{into_compressed, into_decompressed};
|
||||||
use crate::{
|
use crate::{
|
||||||
ByteGrid, CompressionCode, Grid, Header, Packet, PixelGrid, SpBitVec,
|
Brightness, ByteGrid, CompressionCode, Grid, Header, Packet, PixelGrid,
|
||||||
TILE_SIZE,
|
SpBitVec, TILE_SIZE,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// An origin marks the top left position of a window sent to the display.
|
/// An origin marks the top left position of a window sent to the display.
|
||||||
|
@ -24,9 +24,6 @@ impl std::ops::Add<Origin> for Origin {
|
||||||
/// Type alias for documenting the meaning of the u16 in enum values
|
/// Type alias for documenting the meaning of the u16 in enum values
|
||||||
pub type Offset = usize;
|
pub type Offset = usize;
|
||||||
|
|
||||||
/// Type alias for documenting the meaning of the u16 in enum values
|
|
||||||
pub type Brightness = u8;
|
|
||||||
|
|
||||||
/// A command to send to the display.
|
/// A command to send to the display.
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
|
@ -95,7 +92,7 @@ impl From<Command> for Packet {
|
||||||
0x0000,
|
0x0000,
|
||||||
0x0000,
|
0x0000,
|
||||||
),
|
),
|
||||||
vec![brightness],
|
vec![brightness.into()],
|
||||||
),
|
),
|
||||||
Command::BitmapLinearWin(origin, pixels, compression) => {
|
Command::BitmapLinearWin(origin, pixels, compression) => {
|
||||||
bitmap_win_into_packet(origin, pixels, compression)
|
bitmap_win_into_packet(origin, pixels, compression)
|
||||||
|
@ -196,6 +193,8 @@ pub enum TryFromPacketError {
|
||||||
InvalidCompressionCode(u16),
|
InvalidCompressionCode(u16),
|
||||||
/// Decompression of the payload failed. This can be caused by corrupted packets.
|
/// Decompression of the payload failed. This can be caused by corrupted packets.
|
||||||
DecompressionFailed,
|
DecompressionFailed,
|
||||||
|
/// The given brightness value is out of bounds
|
||||||
|
InvalidBrightness(u8),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<Packet> for Command {
|
impl TryFrom<Packet> for Command {
|
||||||
|
@ -227,9 +226,12 @@ impl TryFrom<Packet> for Command {
|
||||||
|
|
||||||
let Header(_, a, b, c, d) = header;
|
let Header(_, a, b, c, d) = header;
|
||||||
if a != 0 || b != 0 || c != 0 || d != 0 {
|
if a != 0 || b != 0 || c != 0 || d != 0 {
|
||||||
Err(TryFromPacketError::ExtraneousHeaderValues)
|
return Err(TryFromPacketError::ExtraneousHeaderValues)
|
||||||
} else {
|
}
|
||||||
Ok(Command::Brightness(payload[0]))
|
|
||||||
|
match Brightness::try_from(payload[0]) {
|
||||||
|
Ok(b) => Ok(Command::Brightness(b)),
|
||||||
|
Err(_) => Err(TryFromPacketError::InvalidBrightness(payload[0]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CommandCode::HardReset => match Self::check_command_only(packet) {
|
CommandCode::HardReset => match Self::check_command_only(packet) {
|
||||||
|
@ -399,9 +401,7 @@ mod tests {
|
||||||
|
|
||||||
use crate::command::TryFromPacketError;
|
use crate::command::TryFromPacketError;
|
||||||
use crate::command_code::CommandCode;
|
use crate::command_code::CommandCode;
|
||||||
use crate::{
|
use crate::{Brightness, ByteGrid, Command, CompressionCode, Header, Origin, Packet, PixelGrid};
|
||||||
ByteGrid, Command, CompressionCode, Header, Origin, Packet, PixelGrid,
|
|
||||||
};
|
|
||||||
|
|
||||||
fn round_trip(original: Command) {
|
fn round_trip(original: Command) {
|
||||||
let packet: Packet = original.clone().into();
|
let packet: Packet = original.clone().into();
|
||||||
|
@ -443,7 +443,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn round_trip_brightness() {
|
fn round_trip_brightness() {
|
||||||
round_trip(Command::Brightness(6));
|
round_trip(Command::Brightness(Brightness::try_from(6).unwrap()));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -5,8 +5,9 @@ use std::time::Duration;
|
||||||
pub use bitvec;
|
pub use bitvec;
|
||||||
use bitvec::prelude::{BitVec, Msb0};
|
use bitvec::prelude::{BitVec, Msb0};
|
||||||
|
|
||||||
|
pub use crate::brightness::Brightness;
|
||||||
pub use crate::byte_grid::ByteGrid;
|
pub use crate::byte_grid::ByteGrid;
|
||||||
pub use crate::command::{Brightness, Command, Offset, Origin};
|
pub use crate::command::{Command, Offset, Origin};
|
||||||
pub use crate::compression_code::CompressionCode;
|
pub use crate::compression_code::CompressionCode;
|
||||||
pub use crate::connection::Connection;
|
pub use crate::connection::Connection;
|
||||||
pub use crate::data_ref::DataRef;
|
pub use crate::data_ref::DataRef;
|
||||||
|
@ -16,6 +17,7 @@ pub use crate::pixel_grid::PixelGrid;
|
||||||
|
|
||||||
type SpBitVec = BitVec<u8, Msb0>;
|
type SpBitVec = BitVec<u8, Msb0>;
|
||||||
|
|
||||||
|
mod brightness;
|
||||||
mod byte_grid;
|
mod byte_grid;
|
||||||
mod command;
|
mod command;
|
||||||
mod command_code;
|
mod command_code;
|
||||||
|
|
Loading…
Reference in a new issue