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