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