1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//! Implementation of [`Matrix`] and [`Frame`] for the micro:bit's LED display.
//!
//! This module describes the correspondence between the visible layout of
//! micro:bit's LEDs and the pins controlling them.
//!
//! [`Matrix`]: tiny_led_matrix::Matrix
//! [`Frame`]: tiny_led_matrix::Frame

use tiny_led_matrix::{Frame, Matrix, RowPlan};
use crate::display::display_port::pin_constants::{MATRIX_COLS, MATRIX_ROWS};

/// Implementation of [`Matrix`] for the microbit's LED display.
///
/// [`Matrix`]: tiny_led_matrix::Matrix
pub struct MicrobitMatrix ();

/// Gives the LED (x, y) coordinates for a given pin row and column.
/// The origin is in the top-left.
const MICROBIT_LED_LAYOUT: [[Option<(usize, usize)>; 3]; 9] = [
    [Some((0, 0)), Some((4, 2)), Some((2, 4))],
    [Some((2, 0)), Some((0, 2)), Some((4, 4))],
    [Some((4, 0)), Some((2, 2)), Some((0, 4))],
    [Some((4, 3)), Some((1, 0)), Some((0, 1))],
    [Some((3, 3)), Some((3, 0)), Some((1, 1))],
    [Some((2, 3)), Some((3, 4)), Some((2, 1))],
    [Some((1, 3)), Some((1, 4)), Some((3, 1))],
    [Some((0, 3)), None,         Some((4, 1))],
    [Some((1, 2)), None,         Some((3, 2))],
];

impl Matrix for MicrobitMatrix {

    /// The number of pins connected to LED columns (3).
    const MATRIX_COLS: usize = MATRIX_COLS;
    /// The number of pins connected to LED rows (9).
    const MATRIX_ROWS: usize = MATRIX_ROWS;
    /// The number of visible LED columns (5).
    const IMAGE_COLS: usize = 5;
    /// The number of visible LED rows (5).
    const IMAGE_ROWS: usize = 5;

    fn image_coordinates(col: usize, row: usize) -> Option<(usize, usize)> {
        MICROBIT_LED_LAYOUT[col][row]
    }

}



/// A 'Compiled' representation of a 5×5 image to be displayed.
///
/// Use the [`.set()`](`Frame::set`) method to store an image (something
/// implementing [`Render`]) in the frame.
///
/// Note you'll have to `use rmicrobit::display::Frame` (or `use
/// rmicrobit::prelude::*`) to make `set()` available.
///
/// [`Frame`]: tiny_led_matrix::Frame
/// [`Render`]: tiny_led_matrix::Render
#[derive(Copy, Clone, Debug)]
pub struct MicrobitFrame (
    [RowPlan; MicrobitFrame::ROWS]
);

impl MicrobitFrame {
    /// Returns a new frame, initially blank.
    pub const fn const_default() -> MicrobitFrame {
        MicrobitFrame([RowPlan::default(); MicrobitFrame::ROWS])
    }
}

impl Default for MicrobitFrame {

    /// Returns a new frame, initially blank.
    fn default() -> MicrobitFrame {
        MicrobitFrame::const_default()
    }

}

impl Frame for MicrobitFrame {

    type Mtx = MicrobitMatrix;

    fn row_plan(&self, row: usize) -> &RowPlan {
        &self.0[row]
    }

    fn row_plan_mut(&mut self, row: usize) -> &mut RowPlan {
        &mut self.0[row]
    }

}