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