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