microbit_common/display/nonblocking/
matrix.rs

1//! Implementation of [`Matrix`] and [`Frame`] for the micro:bit's LED display.
2//!
3//! This module describes the correspondence between the visible layout of
4//! micro:bit's LEDs and the pins controlling them.
5//!
6//! [`Matrix`]: tiny_led_matrix::Matrix
7//! [`Frame`]: tiny_led_matrix::Frame
8
9use crate::gpio::{NUM_COLS, NUM_ROWS};
10use tiny_led_matrix::{Frame, Matrix, RowPlan};
11
12/// Implementation of [`Matrix`] for the microbit's LED display.
13///
14/// [`Matrix`]: tiny_led_matrix::Matrix
15pub struct MicrobitMatrix();
16
17/// Gives the LED (x, y) coordinates for a given pin row and column.
18/// The origin is in the top-left.
19#[cfg(feature = "v1")]
20const MICROBIT_LED_LAYOUT: [[Option<(usize, usize)>; 3]; 9] = [
21    [Some((0, 0)), Some((4, 2)), Some((2, 4))],
22    [Some((2, 0)), Some((0, 2)), Some((4, 4))],
23    [Some((4, 0)), Some((2, 2)), Some((0, 4))],
24    [Some((4, 3)), Some((1, 0)), Some((0, 1))],
25    [Some((3, 3)), Some((3, 0)), Some((1, 1))],
26    [Some((2, 3)), Some((3, 4)), Some((2, 1))],
27    [Some((1, 3)), Some((1, 4)), Some((3, 1))],
28    [Some((0, 3)), None, Some((4, 1))],
29    [Some((1, 2)), None, Some((3, 2))],
30];
31
32impl Matrix for MicrobitMatrix {
33    /// The number of pins connected to LED columns (3).
34    const MATRIX_COLS: usize = NUM_COLS;
35    /// The number of pins connected to LED rows (9).
36    const MATRIX_ROWS: usize = NUM_ROWS;
37    /// The number of visible LED columns (5).
38    const IMAGE_COLS: usize = 5;
39    /// The number of visible LED rows (5).
40    const IMAGE_ROWS: usize = 5;
41
42    #[cfg(feature = "v1")]
43    fn image_coordinates(col: usize, row: usize) -> Option<(usize, usize)> {
44        MICROBIT_LED_LAYOUT[col][row]
45    }
46
47    #[cfg(feature = "v2")]
48    fn image_coordinates(col: usize, row: usize) -> Option<(usize, usize)> {
49        Some((col, row))
50    }
51}
52
53/// A 'Compiled' representation of a 5×5 image to be displayed.
54///
55/// Use the [`.set()`](`Frame::set`) method to store an image (something
56/// implementing [`Render`]) in the frame.
57///
58/// Note you'll have to `use microbit::display::Frame` to make `set()`
59/// available.
60///
61/// [`Frame`]: tiny_led_matrix::Frame
62/// [`Render`]: tiny_led_matrix::Render
63#[derive(Copy, Clone, Debug)]
64pub struct MicrobitFrame([RowPlan; MicrobitFrame::ROWS]);
65
66impl MicrobitFrame {
67    /// Returns a new frame, initially blank.
68    pub const fn default() -> MicrobitFrame {
69        MicrobitFrame([RowPlan::default(); MicrobitFrame::ROWS])
70    }
71}
72
73impl Default for MicrobitFrame {
74    /// Returns a new frame, initially blank.
75    fn default() -> MicrobitFrame {
76        MicrobitFrame::default()
77    }
78}
79
80impl Frame for MicrobitFrame {
81    type Mtx = MicrobitMatrix;
82
83    fn row_plan(&self, row: usize) -> &RowPlan {
84        &self.0[row]
85    }
86
87    fn row_plan_mut(&mut self, row: usize) -> &mut RowPlan {
88        &mut self.0[row]
89    }
90}