mirror of
https://github.com/cccb/servicepoint.git
synced 2025-01-18 10:00:14 +01:00
add examples for brightness, replace ByteGrid with PrimitiveGrid
This commit is contained in:
parent
52080c0ad0
commit
e3c418efcf
|
@ -1,4 +1,5 @@
|
|||
use crate::{Grid, PrimitiveGrid};
|
||||
|
||||
#[cfg(feature = "rand")]
|
||||
use rand::{
|
||||
distributions::{Distribution, Standard},
|
||||
|
@ -6,10 +7,34 @@ use rand::{
|
|||
};
|
||||
|
||||
/// 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)]
|
||||
pub struct Brightness(u8);
|
||||
|
||||
/// 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>;
|
||||
|
||||
impl From<Brightness> for u8 {
|
||||
|
|
|
@ -21,7 +21,7 @@ impl<T: PrimitiveGridType> PrimitiveGrid<T> {
|
|||
/// - width: size in x-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 {
|
||||
Self {
|
||||
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
|
||||
///
|
||||
|
@ -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:
|
||||
/// ```
|
||||
|
@ -63,7 +63,7 @@ impl<T: PrimitiveGridType> PrimitiveGrid<T> {
|
|||
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> {
|
||||
IterRows {
|
||||
byte_grid: self,
|
||||
|
@ -113,7 +113,7 @@ impl<T: PrimitiveGridType> 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
|
||||
///
|
||||
|
|
Loading…
Reference in a new issue