components/
proximity.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 2022.
4
5//! Component for any proximity sensor.
6//!
7//! Usage
8//! -----
9//! ```rust
10//! let proximity = ProximityComponent::new(apds9960, board_kernel, capsules_extra::proximity::DRIVER_NUM)
11//!     .finalize(components::proximity_component_static!());
12//! ```
13
14use capsules_extra::proximity::ProximitySensor;
15use core::mem::MaybeUninit;
16use kernel::capabilities;
17use kernel::component::Component;
18use kernel::create_capability;
19use kernel::hil;
20
21#[macro_export]
22macro_rules! proximity_component_static {
23    () => {{
24        kernel::static_buf!(capsules_extra::proximity::ProximitySensor<'static>)
25    };};
26}
27
28pub struct ProximityComponent<P: hil::sensors::ProximityDriver<'static> + 'static> {
29    sensor: &'static P,
30    board_kernel: &'static kernel::Kernel,
31    driver_num: usize,
32}
33
34impl<P: hil::sensors::ProximityDriver<'static>> ProximityComponent<P> {
35    pub fn new(
36        sensor: &'static P,
37        board_kernel: &'static kernel::Kernel,
38        driver_num: usize,
39    ) -> ProximityComponent<P> {
40        ProximityComponent {
41            sensor,
42            board_kernel,
43            driver_num,
44        }
45    }
46}
47
48impl<P: hil::sensors::ProximityDriver<'static>> Component for ProximityComponent<P> {
49    type StaticInput = &'static mut MaybeUninit<ProximitySensor<'static>>;
50    type Output = &'static ProximitySensor<'static>;
51
52    fn finalize(self, s: Self::StaticInput) -> Self::Output {
53        let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);
54        let grant = self.board_kernel.create_grant(self.driver_num, &grant_cap);
55
56        let proximity = s.write(ProximitySensor::new(self.sensor, grant));
57
58        hil::sensors::ProximityDriver::set_client(self.sensor, proximity);
59        proximity
60    }
61}