brightness to command not packet, move docs, clippy

This commit is contained in:
Vinzenz Schroeter 2025-03-08 12:17:31 +01:00
parent d6229ece87
commit c8a38870d1
6 changed files with 83 additions and 59 deletions

View file

@ -38,7 +38,8 @@ fn main() {
// set all pixels to the same random brightness
let mut rng = rand::thread_rng();
connection.send(rng.gen::<Brightness>()).unwrap();
let command: BrightnessCommand = rng.gen::<Brightness>().into();
connection.send(command).unwrap();
// continuously update random windows to new random brightness
loop {

View file

@ -73,8 +73,21 @@ impl From<BrightnessCommand> for TypedCommand {
}
}
impl From<Brightness> for Packet {
impl From<Brightness> for BrightnessCommand {
fn from(brightness: Brightness) -> Self {
Packet::from(BrightnessCommand { brightness })
BrightnessCommand { brightness }
}
}
#[cfg(test)]
mod tests {
use crate::{Brightness, BrightnessCommand};
#[test]
fn brightness_as_command() {
assert_eq!(
BrightnessCommand { brightness: Brightness::MAX },
Brightness::MAX.into()
)
}
}

View file

@ -38,7 +38,7 @@ impl TryFrom<Packet> for BrightnessGridCommand {
payload,
} = packet;
let grid = ByteGrid::load(width as usize, height as usize, &*payload);
let grid = ByteGrid::load(width as usize, height as usize, &payload);
let grid = match BrightnessGrid::try_from(grid) {
Ok(grid) => grid,
Err(val) => return Err(TryFromPacketError::InvalidBrightness(val)),

View file

@ -61,7 +61,7 @@ impl TryFrom<Packet> for Cp437GridCommand {
} = packet;
Ok(Self {
origin: Origin::new(a as usize, b as usize),
grid: Cp437Grid::load(c as usize, d as usize, &*payload),
grid: Cp437Grid::load(c as usize, d as usize, &payload),
})
}
}

View file

@ -1,52 +1,3 @@
//! This module contains the basic commands the display can handle, which all implement [Command].
//!
//! To send a [Command], use a [connection][crate::Connection].
//!
//! # Available commands
//!
//! To send text, take a look at [Cp437GridCommand].
//!
//! To draw pixels, the easiest command to use is [BitmapCommand].
//!
//! The other BitmapLinear-Commands operate on a region of pixel memory directly.
//! [BitVecCommand] overwrites a region.
//! [BitmapLinearOr], [BitmapLinearAnd] and [BitmapLinearXor] apply logical operations per pixel.
//!
//! Out of bounds operations may be truncated or ignored by the display.
//!
//! # Compression
//!
//! Some commands can contain compressed payloads.
//! To get started, use [CompressionCode::default].
//!
//! If you want to archive the best performance (e.g. latency),
//! you can try the different compression algorithms for your hardware and use case.
//!
//! In memory, the payload is not compressed in the [Command].
//! Payload (de-)compression happens when converting the [Command] into a [Packet] or vice versa.
//!
//! # Examples
//!
//! ```rust
//! use servicepoint::*;
//!
//! // create command
//! let command = BrightnessCommand{ brightness: Brightness::MAX };
//!
//! // turn command into Packet
//! let packet: Packet = command.clone().into();
//!
//! // read command from packet
//! let round_tripped = TypedCommand::try_from(packet).unwrap();
//!
//! // round tripping produces exact copy
//! assert_eq!(round_tripped, TypedCommand::from(command.clone()));
//!
//! // send command
//! # let connection = FakeConnection;
//! connection.send(command).unwrap();
//! ```
mod bitmap;
mod bitmap_legacy;
mod bitvec;
@ -75,12 +26,59 @@ pub use hard_reset::*;
pub use typed::*;
pub use char_grid::*;
/// Represents a command that can be sent to the display.
/// This trait represents a command that can be sent to the display.
///
/// To send a [Command], use a [connection][crate::Connection].
///
/// # Available commands
///
/// To send text, take a look at [Cp437GridCommand].
///
/// To draw pixels, the easiest command to use is [BitmapCommand].
///
/// The other BitmapLinear-Commands operate on a region of pixel memory directly.
/// [BitVecCommand] overwrites a region.
/// [BitmapLinearOr], [BitmapLinearAnd] and [BitmapLinearXor] apply logical operations per pixel.
///
/// Out of bounds operations may be truncated or ignored by the display.
///
/// # Compression
///
/// Some commands can contain compressed payloads.
/// To get started, use [CompressionCode::default].
///
/// If you want to archive the best performance (e.g. latency),
/// you can try the different compression algorithms for your hardware and use case.
///
/// In memory, the payload is not compressed in the [Command].
/// Payload (de-)compression happens when converting the [Command] into a [Packet] or vice versa.
///
/// # Examples
///
/// ```rust
/// use servicepoint::*;
///
/// // create command
/// let command = BrightnessCommand{ brightness: Brightness::MAX };
///
/// // turn command into Packet
/// let packet: Packet = command.clone().into();
///
/// // read command from packet
/// let round_tripped = TypedCommand::try_from(packet).unwrap();
///
/// // round tripping produces exact copy
/// assert_eq!(round_tripped, TypedCommand::from(command.clone()));
///
/// // send command
/// # let connection = FakeConnection;
/// connection.send(command).unwrap();
/// ```
pub trait Command: Debug + Clone + PartialEq + Into<Packet> {}
impl<T: Debug + Clone + PartialEq + Into<Packet>> Command for T {}
pub(self) fn check_command_code_only(
fn check_command_code_only(
packet: Packet,
code: CommandCode,
) -> Option<TryFromPacketError> {

View file

@ -1,5 +1,17 @@
use crate::{BitVecCommand, BitmapCommand, BitmapLegacyCommand, BrightnessCommand, BrightnessGridCommand, CharGridCommand, ClearCommand, Command, Cp437GridCommand, FadeOutCommand, HardResetCommand, Header, Packet};
use crate::command_code::CommandCode;
use crate::{
command_code::CommandCode,
BitVecCommand,
BitmapCommand,
BrightnessCommand,
BrightnessGridCommand,
CharGridCommand,
ClearCommand,
Cp437GridCommand,
FadeOutCommand,
HardResetCommand,
Header,
Packet
};
/// This enum contains all commands provided by the library.
/// This is useful in case you want one data type for all kinds of commands without using `dyn`.
@ -28,7 +40,7 @@ pub enum TypedCommand {
#[allow(deprecated)]
#[deprecated]
BitmapLegacy(BitmapLegacyCommand),
BitmapLegacy(crate::BitmapLegacyCommand),
}
/// Err values for [TypedCommand::try_from].