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