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