tiny_led_matrix/
render.rs

1//! The interface between images and the display.
2
3/// The number of brightness levels for greyscale images (ie, 10).
4pub const BRIGHTNESSES : usize = 10;
5
6/// The maximum brightness level for greyscale images (ie, 9; the minimum is 0).
7pub const MAX_BRIGHTNESS : u8 = (BRIGHTNESSES as u8)-1;
8
9
10/// A trait providing the information that [`Display`] needs to render an image.
11///
12/// [`Display`]: crate::display::Display
13///
14/// # Example implementation
15///
16/// Here's an example of a 5×5 image type, using one byte for each 'pixel':
17///
18/// ```
19/// # use tiny_led_matrix::Render;
20/// struct GreyscaleImage (
21///     [[u8; 5]; 5]
22/// );
23///
24/// impl GreyscaleImage {
25///     const fn new(data: &[[u8; 5]; 5]) -> GreyscaleImage {
26///         GreyscaleImage(*data)
27///     }
28/// }
29///
30/// impl Render for GreyscaleImage {
31///     fn brightness_at(&self, x: usize, y: usize) -> u8 {
32///         self.0[y][x]
33///     }
34/// }
35/// const GREY_HEART: GreyscaleImage = GreyscaleImage::new(&[
36///     [0, 9, 0, 9, 0],
37///     [9, 5, 9, 5, 9],
38///     [9, 5, 5, 5, 9],
39///     [0, 9, 5, 9, 0],
40///     [0, 0, 9, 0, 0],
41/// ]);
42/// ```
43
44
45pub trait Render {
46
47    /// Returns the brightness value for a single LED.
48    ///
49    /// The ranges for the x and y coordinates are 0..IMAGE_COLS and
50    /// 0..IMAGE_ROWS, as defined by the [`Matrix`] for the [`Display`]'s
51    /// [`Frame`].
52    ///
53    /// (0, 0) is the top left.
54    ///
55    /// The result must be in the range 0..=[`MAX_BRIGHTNESS`]
56    ///
57    /// # Panics
58    ///
59    /// If the provided coordinates are out of range, may panic or return an
60    /// arbitrary in-range result.
61    ///
62    /// [`Display`]: crate::display::Display
63    /// [`Matrix`]: crate::display::Matrix
64    /// [`Frame`]: crate::display::Frame
65    fn brightness_at(&self, x: usize, y: usize) -> u8;
66
67}
68