components/
screen_on.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2025.
4
5//! Components for implementing outputs on the screen.
6//!
7//! Supported examples:
8//! - LED: draw LEDs on the screen.
9
10use capsules_extra::screen::screen_on_led;
11use core::mem::MaybeUninit;
12use kernel::component::Component;
13use kernel::hil;
14
15#[macro_export]
16macro_rules! screen_on_led_component_static {
17    ($S:ty, $NUM_LEDS:expr, $SCREEN_WIDTH:expr, $SCREEN_HEIGHT:expr $(,)?) => {{
18        let buffer = kernel::static_buf!([u8; ($SCREEN_WIDTH * $SCREEN_HEIGHT) / 8]);
19        let screen_on_led = kernel::static_buf!(
20            capsules_extra::screen::screen_on_led::ScreenOnLed<
21                'static,
22                $S,
23                $NUM_LEDS,
24                $SCREEN_WIDTH,
25                $SCREEN_HEIGHT,
26            >
27        );
28
29        (buffer, screen_on_led)
30    };};
31}
32
33pub type ScreenOnLedComponentType<
34    S,
35    const NUM_LEDS: usize,
36    const SCREEN_WIDTH: usize,
37    const SCREEN_HEIGHT: usize,
38> = screen_on_led::ScreenOnLed<'static, S, NUM_LEDS, SCREEN_WIDTH, SCREEN_HEIGHT>;
39
40pub struct ScreenOnLedComponent<
41    S: hil::screen::Screen<'static> + 'static,
42    const NUM_LEDS: usize,
43    const SCREEN_WIDTH: usize,
44    const SCREEN_HEIGHT: usize,
45    const BUFFER_LENGTH: usize,
46> {
47    screen: &'static S,
48}
49
50impl<
51        S: hil::screen::Screen<'static>,
52        const NUM_LEDS: usize,
53        const SCREEN_WIDTH: usize,
54        const SCREEN_HEIGHT: usize,
55        const BUFFER_LENGTH: usize,
56    > ScreenOnLedComponent<S, NUM_LEDS, SCREEN_WIDTH, SCREEN_HEIGHT, BUFFER_LENGTH>
57{
58    pub fn new(screen: &'static S) -> Self {
59        Self { screen }
60    }
61}
62
63impl<
64        S: hil::screen::Screen<'static> + 'static,
65        const NUM_LEDS: usize,
66        const SCREEN_WIDTH: usize,
67        const SCREEN_HEIGHT: usize,
68        const BUFFER_LENGTH: usize,
69    > Component for ScreenOnLedComponent<S, NUM_LEDS, SCREEN_WIDTH, SCREEN_HEIGHT, BUFFER_LENGTH>
70{
71    type StaticInput = (
72        &'static mut MaybeUninit<[u8; BUFFER_LENGTH]>,
73        &'static mut MaybeUninit<
74            screen_on_led::ScreenOnLed<'static, S, NUM_LEDS, SCREEN_WIDTH, SCREEN_HEIGHT>,
75        >,
76    );
77    type Output =
78        &'static screen_on_led::ScreenOnLed<'static, S, NUM_LEDS, SCREEN_WIDTH, SCREEN_HEIGHT>;
79
80    fn finalize(self, static_input: Self::StaticInput) -> Self::Output {
81        let buffer = static_input.0.write([0; BUFFER_LENGTH]);
82        let screen_on_led = static_input
83            .1
84            .write(screen_on_led::ScreenOnLed::new(self.screen, buffer));
85
86        kernel::hil::screen::Screen::set_client(self.screen, screen_on_led);
87
88        screen_on_led
89    }
90}