add examples for brightness, replace ByteGrid with PrimitiveGrid

This commit is contained in:
Vinzenz Schroeter 2024-06-25 22:33:53 +02:00
parent 52080c0ad0
commit e3c418efcf
2 changed files with 31 additions and 6 deletions

View file

@ -1,4 +1,5 @@
use crate::{Grid, PrimitiveGrid}; use crate::{Grid, PrimitiveGrid};
#[cfg(feature = "rand")] #[cfg(feature = "rand")]
use rand::{ use rand::{
distributions::{Distribution, Standard}, distributions::{Distribution, Standard},
@ -6,10 +7,34 @@ use rand::{
}; };
/// A display brightness value, checked for correct value range /// A display brightness value, checked for correct value range
///
/// # Examples
///
/// ```
/// # use servicepoint::{Brightness, Command, Connection};
/// let b = Brightness::MAX;
/// let val: u8 = b.into();
///
/// let b = Brightness::try_from(7).unwrap();
/// # let connection = Connection::open("127.0.0.1:2342").unwrap();
/// let result = connection.send(Command::Brightness(b));
/// ```
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, PartialEq)]
pub struct Brightness(u8); pub struct Brightness(u8);
/// A grid containing brightness values. /// A grid containing brightness values.
///
/// # Examples
///
/// ```rust
/// # use servicepoint::{Brightness, BrightnessGrid, Command, Connection, Grid, Origin};
/// let mut grid = BrightnessGrid::new(2,2);
/// grid.set(0, 0, Brightness::MIN);
/// grid.set(1, 1, Brightness::MIN);
///
/// # let connection = Connection::open("127.0.0.1:2342").unwrap();
/// connection.send(Command::CharBrightness(Origin::new(3, 7), grid)).unwrap()
/// ```
pub type BrightnessGrid = PrimitiveGrid<Brightness>; pub type BrightnessGrid = PrimitiveGrid<Brightness>;
impl From<Brightness> for u8 { impl From<Brightness> for u8 {

View file

@ -21,7 +21,7 @@ impl<T: PrimitiveGridType> PrimitiveGrid<T> {
/// - width: size in x-direction /// - width: size in x-direction
/// - height: size in y-direction /// - height: size in y-direction
/// ///
/// returns: `ByteGrid` initialized to default value. /// returns: `PrimitiveGrid` initialized to default value.
pub fn new(width: usize, height: usize) -> Self { pub fn new(width: usize, height: usize) -> Self {
Self { Self {
data: vec![Default::default(); width * height], data: vec![Default::default(); width * height],
@ -30,9 +30,9 @@ impl<T: PrimitiveGridType> PrimitiveGrid<T> {
} }
} }
/// Loads a `ByteGrid` with the specified dimensions from the provided data. /// Loads a `PrimitiveGrid` with the specified dimensions from the provided data.
/// ///
/// returns: `ByteGrid` that contains a copy of the provided data /// returns: `PrimitiveGrid` that contains a copy of the provided data
/// ///
/// # Panics /// # Panics
/// ///
@ -47,7 +47,7 @@ impl<T: PrimitiveGridType> PrimitiveGrid<T> {
} }
} }
/// Iterate over all cells in `ByteGrid`. /// Iterate over all cells in `PrimitiveGrid`.
/// ///
/// Order is equivalent to the following loop: /// Order is equivalent to the following loop:
/// ``` /// ```
@ -63,7 +63,7 @@ impl<T: PrimitiveGridType> PrimitiveGrid<T> {
self.data.iter() self.data.iter()
} }
/// Iterate over all rows in `ByteGrid` top to bottom. /// Iterate over all rows in `PrimitiveGrid` top to bottom.
pub fn iter_rows(&self) -> IterRows<T> { pub fn iter_rows(&self) -> IterRows<T> {
IterRows { IterRows {
byte_grid: self, byte_grid: self,
@ -113,7 +113,7 @@ impl<T: PrimitiveGridType> PrimitiveGrid<T> {
} }
impl<T: PrimitiveGridType> Grid<T> for PrimitiveGrid<T> { impl<T: PrimitiveGridType> Grid<T> for PrimitiveGrid<T> {
/// Sets the value of the cell at the specified position in the `ByteGrid. /// Sets the value of the cell at the specified position in the `PrimitiveGrid.
/// ///
/// # Arguments /// # Arguments
/// ///