components/
touch.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
// 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 Touch Panel.
//!
//! Usage
//! -----
//!
//! Touch
//!
//! ```rust
//! // Just Touch
//! let touch =
//!     components::touch::TouchComponent::new(board_kernel, ts, None, Some(screen))
//!         .finalize(components::touch_component_static!());
//!
//! // With Gesture
//! let touch =
//!     components::touch::TouchComponent::new(board_kernel, ts, Some(ts), Some(screen))
//!         .finalize(components::touch_component_static!());
//! ```
//!
//! Multi Touch
//!
//! ```rust
//! // Just Multi Touch
//! let touch =
//!     components::touch::MultiTouchComponent::new(board_kernel, ts, None, Some(screen))
//!         .finalize(components::touch_component_static!());
//!
//! // With Gesture
//! let touch =
//!     components::touch::MultiTouchComponent::new(board_kernel, ts, Some(ts), Some(screen))
//!         .finalize(components::touch_component_static!());
//! ```
use capsules_extra::touch::Touch;
use core::mem::MaybeUninit;
use kernel::capabilities;
use kernel::component::Component;
use kernel::create_capability;

#[macro_export]
macro_rules! touch_component_static {
    () => {{
        kernel::static_buf!(capsules_extra::touch::Touch<'static>)
    };};
}

pub struct TouchComponent {
    board_kernel: &'static kernel::Kernel,
    driver_num: usize,
    touch: &'static dyn kernel::hil::touch::Touch<'static>,
    gesture: Option<&'static dyn kernel::hil::touch::Gesture<'static>>,
    screen: Option<&'static dyn kernel::hil::screen::Screen<'static>>,
}

impl TouchComponent {
    pub fn new(
        board_kernel: &'static kernel::Kernel,
        driver_num: usize,
        touch: &'static dyn kernel::hil::touch::Touch<'static>,
        gesture: Option<&'static dyn kernel::hil::touch::Gesture<'static>>,
        screen: Option<&'static dyn kernel::hil::screen::Screen<'static>>,
    ) -> TouchComponent {
        TouchComponent {
            board_kernel,
            driver_num,
            touch,
            gesture,
            screen,
        }
    }
}

impl Component for TouchComponent {
    type StaticInput = &'static mut MaybeUninit<Touch<'static>>;
    type Output = &'static capsules_extra::touch::Touch<'static>;

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

        let touch = static_input.write(capsules_extra::touch::Touch::new(
            Some(self.touch),
            None,
            self.screen,
            grant_touch,
        ));

        kernel::hil::touch::Touch::set_client(self.touch, touch);
        if let Some(gesture) = self.gesture {
            kernel::hil::touch::Gesture::set_client(gesture, touch);
        }

        touch
    }
}

pub struct MultiTouchComponent {
    board_kernel: &'static kernel::Kernel,
    driver_num: usize,
    multi_touch: &'static dyn kernel::hil::touch::MultiTouch<'static>,
    gesture: Option<&'static dyn kernel::hil::touch::Gesture<'static>>,
    screen: Option<&'static dyn kernel::hil::screen::Screen<'static>>,
}

impl MultiTouchComponent {
    pub fn new(
        board_kernel: &'static kernel::Kernel,
        driver_num: usize,
        multi_touch: &'static dyn kernel::hil::touch::MultiTouch<'static>,
        gesture: Option<&'static dyn kernel::hil::touch::Gesture<'static>>,
        screen: Option<&'static dyn kernel::hil::screen::Screen>,
    ) -> MultiTouchComponent {
        MultiTouchComponent {
            board_kernel,
            driver_num,
            multi_touch,
            gesture,
            screen,
        }
    }
}

impl Component for MultiTouchComponent {
    type StaticInput = &'static mut MaybeUninit<Touch<'static>>;
    type Output = &'static capsules_extra::touch::Touch<'static>;

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

        let touch = static_input.write(capsules_extra::touch::Touch::new(
            None,
            Some(self.multi_touch),
            self.screen,
            grant_touch,
        ));

        kernel::hil::touch::MultiTouch::set_client(self.multi_touch, touch);
        if let Some(gesture) = self.gesture {
            kernel::hil::touch::Gesture::set_client(gesture, touch);
        }

        touch
    }
}