components/
led_matrix.rs

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Component for matrices of LEDs.
//!
//! Usage
//! -----
//! ```rust
//! let led_matrix = components::led_matrix::LedMatrixComponent::new(
//!     mux_alarm,
//!     components::led_line_component_static!(
//!         nrf52833::gpio::GPIOPin,
//!         &nrf52833_peripherals.gpio_port[LED_MATRIX_COLS[0]],
//!         &nrf52833_peripherals.gpio_port[LED_MATRIX_COLS[1]],
//!         &nrf52833_peripherals.gpio_port[LED_MATRIX_COLS[2]],
//!         &nrf52833_peripherals.gpio_port[LED_MATRIX_COLS[3]],
//!         &nrf52833_peripherals.gpio_port[LED_MATRIX_COLS[4]],
//!     ),
//!     components::led_line_component_static!(
//!         nrf52833::gpio::GPIOPin,
//!         &nrf52833_peripherals.gpio_port[LED_MATRIX_ROWS[0]],
//!         &nrf52833_peripherals.gpio_port[LED_MATRIX_ROWS[1]],
//!         &nrf52833_peripherals.gpio_port[LED_MATRIX_ROWS[2]],
//!         &nrf52833_peripherals.gpio_port[LED_MATRIX_ROWS[3]],
//!         &nrf52833_peripherals.gpio_port[LED_MATRIX_ROWS[4]],
//!     ),
//!     kernel::hil::gpio::ActivationMode::ActiveLow,
//!     kernel::hil::gpio::ActivationMode::ActiveHigh,
//!     60,
//! )
//! .finalize(components::led_matrix_component_static!(
//!     nrf52833::gpio::GPIOPin,
//!     nrf52::rtc::Rtc<'static>,
//!     5,
//!     5
//! ));
//! ```
//!
//! Single LED usage
//! ----------------
//!
//! ```rust
//! let led = components::led_matrix_led!(
//!     nrf52::gpio::GPIOPin<'static>,
//!     capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<'static, nrf52::rtc::Rtc<'static>>,
//!     led,
//!     1,
//!     2
//! );
//! ```
//!
//! Array LED usage
//! ----------------
//!
//! ```rust
//! let leds = components::led_matrix_leds!(
//!     nrf52::gpio::GPIOPin<'static>,
//!     capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<'static, nrf52::rtc::Rtc<'static>>,
//!     led,
//!     (0, 0),
//!     (1, 0),
//!     (2, 0),
//!     (3, 0),
//!     (4, 0),
//!     (0, 1)
//!     // ...
//! );
//! ```
//!

use capsules_core::virtualizers::virtual_alarm::{MuxAlarm, VirtualMuxAlarm};
use capsules_extra::led_matrix::LedMatrixDriver;
use core::mem::MaybeUninit;
use kernel::component::Component;
use kernel::hil::gpio::{ActivationMode, Pin};
use kernel::hil::time::Alarm;

#[macro_export]
macro_rules! led_matrix_component_static {
    ($Pin:ty, $A: ty, $num_cols: literal, $num_rows: literal $(,)?) => {{
        let buffer = kernel::static_buf!([u8; $num_cols * $num_rows / 8 + 1]);
        let alarm = kernel::static_buf!(
            capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<'static, $A>
        );
        let led = kernel::static_buf!(
            capsules_extra::led_matrix::LedMatrixDriver<
                'static,
                $Pin,
                capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<'static, $A>,
            >
        );

        (alarm, led, buffer)
    };};
}

#[macro_export]
macro_rules! led_line_component_static {
    ($Pin:ty, $($L:expr),+ $(,)?) => {{
        use kernel::count_expressions;
        use kernel::static_init;
        const NUM_LEDS: usize = count_expressions!($($L),+);

        static_init!(
            [&'static $Pin; NUM_LEDS],
            [
                $(
                    static_init!(
                        &'static $Pin,
                        $L
                    )
                ),+
            ]
        )
    };};
}

#[macro_export]
macro_rules! led_matrix_led {
    ($Pin:ty, $A: ty, $led_matrix: expr, $col: expr, $row: expr) => {{
        use capsules_extra::led_matrix::LedMatrixLed;
        static_init!(
            LedMatrixLed<'static, $Pin, $A>,
            LedMatrixLed::new($led_matrix, $col, $row)
        )
    };};
}

#[macro_export]
macro_rules! led_matrix_leds {
    ($Pin:ty, $A: ty, $led_matrix: expr, $(($col: expr, $row: expr)),+) => {{
        use capsules_extra::led_matrix::LedMatrixLed;
        use kernel::count_expressions;

        const NUM_LEDS: usize = count_expressions!($(($col, $row)),+);
        let leds = static_init!(
            [&LedMatrixLed<'static, $Pin, $A>; NUM_LEDS],
            [$(
                $crate::led_matrix_led! ($Pin, $A, $led_matrix, $col, $row)
            ),+]
        );
        leds
    };};
}

pub struct LedMatrixComponent<
    L: 'static + Pin,
    A: 'static + Alarm<'static>,
    const NUM_COLS: usize,
    const NUM_ROWS: usize,
    const NUM_LED_BITS: usize,
> {
    alarm_mux: &'static MuxAlarm<'static, A>,
    col: &'static [&'static L; NUM_COLS],
    row: &'static [&'static L; NUM_ROWS],
    col_active: ActivationMode,
    row_active: ActivationMode,
    refresh_rate: usize,
}

impl<
        L: 'static + Pin,
        A: 'static + Alarm<'static>,
        const NUM_COLS: usize,
        const NUM_ROWS: usize,
        const NUM_LED_BITS: usize,
    > LedMatrixComponent<L, A, NUM_COLS, NUM_ROWS, NUM_LED_BITS>
{
    pub fn new(
        alarm_mux: &'static MuxAlarm<'static, A>,
        col: &'static [&'static L; NUM_COLS],
        row: &'static [&'static L; NUM_ROWS],
        col_active: ActivationMode,
        row_active: ActivationMode,
        refresh_rate: usize,
    ) -> Self {
        Self {
            alarm_mux,
            col,
            row,
            col_active,
            row_active,
            refresh_rate,
        }
    }
}

impl<
        L: 'static + Pin,
        A: 'static + Alarm<'static>,
        const NUM_COLS: usize,
        const NUM_ROWS: usize,
        const NUM_LED_BITS: usize,
    > Component for LedMatrixComponent<L, A, NUM_COLS, NUM_ROWS, NUM_LED_BITS>
{
    type StaticInput = (
        &'static mut MaybeUninit<VirtualMuxAlarm<'static, A>>,
        &'static mut MaybeUninit<LedMatrixDriver<'static, L, VirtualMuxAlarm<'static, A>>>,
        &'static mut MaybeUninit<[u8; NUM_LED_BITS]>,
    );
    type Output = &'static LedMatrixDriver<'static, L, VirtualMuxAlarm<'static, A>>;

    fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
        let led_alarm = static_buffer.0.write(VirtualMuxAlarm::new(self.alarm_mux));
        led_alarm.setup();

        let buffer = static_buffer.2.write([0; NUM_LED_BITS]);

        let led_matrix = static_buffer.1.write(LedMatrixDriver::new(
            self.col,
            self.row,
            buffer,
            led_alarm,
            self.col_active,
            self.row_active,
            self.refresh_rate,
        ));

        led_alarm.set_alarm_client(led_matrix);

        led_matrix.init();

        led_matrix
    }
}