components/
led.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
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Components for collections of LEDs.
//!
//! Usage
//! -----
//! ```rust
//! let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
//!     kernel::hil::led::LedLow<'static, sam4l::gpio::GPIOPin>,
//!     LedLow::new(&sam4l::gpio::PORT[LED_RED_PIN]),
//!     LedLow::new(&sam4l::gpio::PORT[LED_GREEN_PIN]),
//!     LedLow::new(&sam4l::gpio::PORT[LED_BLUE_PIN]),
//! ));
//! ```

use capsules_core::led::LedDriver;
use core::marker::PhantomData;
use core::mem::MaybeUninit;
use kernel::component::Component;
use kernel::hil::led::Led;

#[macro_export]
macro_rules! led_component_static {
    ($Led:ty, $($L:expr),+ $(,)?) => {{
        use kernel::count_expressions;
        use kernel::static_init;
        const NUM_LEDS: usize = count_expressions!($($L),+);
        let arr = static_init!(
            [&'static $Led; NUM_LEDS],
            [
                $(
                    static_init!(
                        $Led,
                        $L
                    )
                ),+
            ]
        );

        let led = kernel::static_buf!( capsules_core::led::LedDriver<'static, $Led, NUM_LEDS>);
        (led, arr)
    };};
}

pub type LedsComponentType<L, const NUMLEDS: usize> = LedDriver<'static, L, NUMLEDS>;

pub struct LedsComponent<L: 'static + Led, const NUM_LEDS: usize> {
    _phantom: PhantomData<L>,
}

impl<L: 'static + Led, const NUM_LEDS: usize> LedsComponent<L, NUM_LEDS> {
    pub fn new() -> Self {
        Self {
            _phantom: PhantomData,
        }
    }
}

impl<L: 'static + Led, const NUM_LEDS: usize> Component for LedsComponent<L, NUM_LEDS> {
    type StaticInput = (
        &'static mut MaybeUninit<LedDriver<'static, L, NUM_LEDS>>,
        &'static mut [&'static L; NUM_LEDS],
    );
    type Output = &'static LedDriver<'static, L, NUM_LEDS>;

    fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
        static_buffer.0.write(LedDriver::new(static_buffer.1))
    }
}