mirror of
https://github.com/cccb/servicepoint.git
synced 2025-01-18 10:00:14 +01:00
add Brightness::saturating_from
This commit is contained in:
parent
593a975d5c
commit
78a4d4dbcf
|
@ -25,10 +25,11 @@ fn main() {
|
|||
);
|
||||
connection.send(command).expect("send failed");
|
||||
|
||||
let max_brightness = usize::from(u8::from(Brightness::MAX));
|
||||
let max_brightness: u8 = Brightness::MAX.into();
|
||||
let mut brightnesses = BrightnessGrid::new(TILE_WIDTH, TILE_HEIGHT);
|
||||
for (index, byte) in brightnesses.data_ref_mut().iter_mut().enumerate() {
|
||||
*byte = Brightness::try_from((index % max_brightness) as u8).unwrap();
|
||||
let level = index as u8 % max_brightness;
|
||||
*byte = Brightness::try_from(level).unwrap();
|
||||
}
|
||||
|
||||
connection
|
||||
|
|
|
@ -60,6 +60,17 @@ impl Brightness {
|
|||
pub const MAX: Brightness = Brightness(11);
|
||||
/// lowest possible brightness value, 0
|
||||
pub const MIN: Brightness = Brightness(0);
|
||||
|
||||
/// Create a brightness value without returning an error for brightnesses above [Brightness::MAX].
|
||||
///
|
||||
/// returns: the specified value as a [Brightness], or [Brightness::MAX].
|
||||
pub fn saturating_from(value: u8) -> Brightness {
|
||||
if value > Brightness::MAX.into() {
|
||||
Brightness::MAX
|
||||
} else {
|
||||
Brightness(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Brightness {
|
||||
|
@ -138,4 +149,10 @@ mod tests {
|
|||
let actual = PrimitiveGrid::from(&grid);
|
||||
assert_eq!(actual.data_ref(), &[11, 0, 11, 11]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn saturating_convert() {
|
||||
assert_eq!(Brightness::MAX, Brightness::saturating_from(100));
|
||||
assert_eq!(Brightness(5), Brightness::saturating_from(5));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -114,6 +114,18 @@ impl<T: PrimitiveGridType> PrimitiveGrid<T> {
|
|||
/// Convert between PrimitiveGrid types.
|
||||
///
|
||||
/// See also [Iterator::map].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Use logic written for u8s and then convert to [Brightness] values for sending in a [Command].
|
||||
/// ```
|
||||
/// # fn foo(grid: &mut PrimitiveGrid<u8>) {}
|
||||
/// # use servicepoint::{Brightness, BrightnessGrid, Command, Origin, PrimitiveGrid, TILE_HEIGHT, TILE_WIDTH};
|
||||
/// let mut grid: PrimitiveGrid<u8> = PrimitiveGrid::new(TILE_WIDTH, TILE_HEIGHT);
|
||||
/// foo(&mut grid);
|
||||
/// let grid: BrightnessGrid = grid.map(Brightness::saturating_from);
|
||||
/// let command = Command::CharBrightness(Origin::ZERO, grid);
|
||||
/// ```
|
||||
pub fn map<TConverted, F>(&self, f: F) -> PrimitiveGrid<TConverted>
|
||||
where
|
||||
TConverted: PrimitiveGridType,
|
||||
|
|
|
@ -21,14 +21,9 @@ use std::intrinsics::transmute;
|
|||
/// SPCommand command = sp_command_char_brightness(grid);
|
||||
/// sp_connection_free(connection);
|
||||
/// ```
|
||||
#[derive(Clone)]
|
||||
pub struct SPBrightnessGrid(pub(crate) servicepoint::BrightnessGrid);
|
||||
|
||||
impl Clone for SPBrightnessGrid {
|
||||
fn clone(&self) -> Self {
|
||||
SPBrightnessGrid(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new [SPBrightnessGrid] with the specified dimensions.
|
||||
///
|
||||
/// returns: [SPBrightnessGrid] initialized to 0. Will never return NULL.
|
||||
|
|
Loading…
Reference in a new issue