components/
text_screen.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
// 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 the Text Screen.
//!
//! Buffer Size
//! -----------
//!
//! Displays can receive a large amount of data and having larger transfer buffers
//! optimizes the number of bus writes.
//!
//! As memory is limited on some MCUs, the `components::screen_buffer_size``
//! macro allows users to define the size of the screen buffer.
//!
//! Usage
//! -----
//!
//! ```rust
//! let text_screen =
//!     components::text_screen::TextScreenComponent::new(board_kernel, tft)
//!         .finalize(components::text_screen_component_static!(40960));
//! ```
//!

use capsules_extra::text_screen::TextScreen;
use core::mem::MaybeUninit;
use kernel::capabilities;
use kernel::component::Component;
use kernel::create_capability;

#[macro_export]
macro_rules! text_screen_component_static {
    ($s:literal $(,)?) => {{
        let buffer = kernel::static_buf!([u8; $s]);
        let screen = kernel::static_buf!(capsules_extra::text_screen::TextScreen);

        (buffer, screen)
    };};
}

pub struct TextScreenComponent<const SCREEN_BUF_LEN: usize> {
    board_kernel: &'static kernel::Kernel,
    driver_num: usize,
    text_screen: &'static dyn kernel::hil::text_screen::TextScreen<'static>,
}

impl<const SCREEN_BUF_LEN: usize> TextScreenComponent<SCREEN_BUF_LEN> {
    pub fn new(
        board_kernel: &'static kernel::Kernel,
        driver_num: usize,
        text_screen: &'static dyn kernel::hil::text_screen::TextScreen<'static>,
    ) -> TextScreenComponent<SCREEN_BUF_LEN> {
        TextScreenComponent {
            board_kernel,
            driver_num,
            text_screen,
        }
    }
}

impl<const SCREEN_BUF_LEN: usize> Component for TextScreenComponent<SCREEN_BUF_LEN> {
    type StaticInput = (
        &'static mut MaybeUninit<[u8; SCREEN_BUF_LEN]>,
        &'static mut MaybeUninit<TextScreen<'static>>,
    );
    type Output = &'static TextScreen<'static>;

    fn finalize(self, static_input: Self::StaticInput) -> Self::Output {
        let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);
        let grant_text_screen = self.board_kernel.create_grant(self.driver_num, &grant_cap);

        let buffer = static_input.0.write([0; SCREEN_BUF_LEN]);

        let text_screen =
            static_input
                .1
                .write(TextScreen::new(self.text_screen, buffer, grant_text_screen));

        kernel::hil::text_screen::TextScreen::set_client(self.text_screen, Some(text_screen));

        text_screen
    }
}