components/
hs3003.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//! Components for the HS3003 Temperature/Humidity Sensor.
6//!
7//! Usage
8//! -----
9//! ```rust
10//! let hs3003 = Hs3003Component::new(mux_i2c, mux_alarm, 0x44).finalize(
11//!     components::hs3003_component_static!(sam4l::ast::Ast));
12//! let temperature = components::temperature::TemperatureComponent::new(board_kernel, hs3003).finalize(());
13//! let humidity = components::humidity::HumidityComponent::new(board_kernel, hs3003).finalize(());
14//! ```
15
16use capsules_core::virtualizers::virtual_i2c::{I2CDevice, MuxI2C};
17use capsules_extra::hs3003::Hs3003;
18use core::mem::MaybeUninit;
19use kernel::component::Component;
20use kernel::hil::i2c;
21
22// Setup static space for the objects.
23#[macro_export]
24macro_rules! hs3003_component_static {
25    ($I:ty $(,)?) => {{
26        let i2c_device =
27            kernel::static_buf!(capsules_core::virtualizers::virtual_i2c::I2CDevice<$I>);
28        let buffer = kernel::static_buf!([u8; 5]);
29        let hs3003 = kernel::static_buf!(
30            capsules_extra::hs3003::Hs3003<
31                'static,
32                capsules_core::virtualizers::virtual_i2c::I2CDevice<$I>,
33            >
34        );
35
36        (i2c_device, buffer, hs3003)
37    };};
38}
39
40pub type Hs3003ComponentType<I> = Hs3003<'static, I>;
41
42pub struct Hs3003Component<I: 'static + i2c::I2CMaster<'static>> {
43    i2c_mux: &'static MuxI2C<'static, I>,
44    i2c_address: u8,
45}
46
47impl<I: 'static + i2c::I2CMaster<'static>> Hs3003Component<I> {
48    pub fn new(i2c: &'static MuxI2C<'static, I>, i2c_address: u8) -> Self {
49        Hs3003Component {
50            i2c_mux: i2c,
51            i2c_address,
52        }
53    }
54}
55
56impl<I: 'static + i2c::I2CMaster<'static>> Component for Hs3003Component<I> {
57    type StaticInput = (
58        &'static mut MaybeUninit<I2CDevice<'static, I>>,
59        &'static mut MaybeUninit<[u8; 5]>,
60        &'static mut MaybeUninit<Hs3003<'static, I2CDevice<'static, I>>>,
61    );
62    type Output = &'static Hs3003<'static, I2CDevice<'static, I>>;
63
64    fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
65        let hs3003_i2c = static_buffer
66            .0
67            .write(I2CDevice::new(self.i2c_mux, self.i2c_address));
68        let buffer = static_buffer.1.write([0; 5]);
69        let hs3003 = static_buffer.2.write(Hs3003::new(hs3003_i2c, buffer));
70
71        hs3003_i2c.set_client(hs3003);
72        hs3003
73    }
74}