components/
button_keyboard.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//! Component for buttons using keyboard presses.
6//!
7//! Capsule: capsules/extra/src/button_keyboard.rs
8//!
9//! Implements the button system call with keyboard inputs.
10
11use core::mem::MaybeUninit;
12use kernel::capabilities;
13use kernel::component::Component;
14use kernel::create_capability;
15use kernel::hil;
16
17#[macro_export]
18macro_rules! keyboard_button_component_static {
19    () => {{
20        kernel::static_buf!(capsules_extra::button_keyboard::ButtonKeyboard<'static>)
21    };};
22}
23
24pub type KeyboardButtonComponentType = capsules_extra::button_keyboard::ButtonKeyboard<'static>;
25
26pub struct KeyboardButtonComponent<K: 'static + hil::keyboard::Keyboard<'static>> {
27    board_kernel: &'static kernel::Kernel,
28    driver_num: usize,
29    keyboard: &'static K,
30    key_codes: &'static [u16],
31}
32
33impl<K: 'static + hil::keyboard::Keyboard<'static>> KeyboardButtonComponent<K> {
34    pub fn new(
35        board_kernel: &'static kernel::Kernel,
36        driver_num: usize,
37        keyboard: &'static K,
38        key_codes: &'static [u16],
39    ) -> Self {
40        Self {
41            board_kernel,
42            driver_num,
43            keyboard,
44            key_codes,
45        }
46    }
47}
48
49impl<K: 'static + hil::keyboard::Keyboard<'static>> Component for KeyboardButtonComponent<K> {
50    type StaticInput =
51        &'static mut MaybeUninit<capsules_extra::button_keyboard::ButtonKeyboard<'static>>;
52    type Output = &'static capsules_extra::button_keyboard::ButtonKeyboard<'static>;
53
54    fn finalize(self, s: Self::StaticInput) -> Self::Output {
55        let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);
56
57        let button_keyboard = s.write(capsules_extra::button_keyboard::ButtonKeyboard::new(
58            self.key_codes,
59            self.board_kernel.create_grant(self.driver_num, &grant_cap),
60        ));
61        self.keyboard.set_client(button_keyboard);
62
63        button_keyboard
64    }
65}