components/
apds9960.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 APDS9960 proximity sensor.
6
7use capsules_core::virtualizers::virtual_i2c::{I2CDevice, MuxI2C};
8use capsules_extra::apds9960::APDS9960;
9use core::mem::MaybeUninit;
10use kernel::component::Component;
11use kernel::hil::gpio;
12use kernel::hil::i2c;
13
14#[macro_export]
15macro_rules! apds9960_component_static {
16    ($I:ty $(,)?) => {{
17        let i2c_device =
18            kernel::static_buf!(capsules_core::virtualizers::virtual_i2c::I2CDevice<'static, $I>);
19        let apds9960 = kernel::static_buf!(
20            capsules_extra::apds9960::APDS9960<
21                'static,
22                capsules_core::virtualizers::virtual_i2c::I2CDevice<$I>,
23            >
24        );
25        let buffer = kernel::static_buf!([u8; capsules_extra::apds9960::BUF_LEN]);
26
27        (i2c_device, apds9960, buffer)
28    };};
29}
30
31pub struct Apds9960Component<I: 'static + i2c::I2CMaster<'static>> {
32    i2c_mux: &'static MuxI2C<'static, I>,
33    i2c_address: u8,
34    interrupt_pin: &'static dyn gpio::InterruptPin<'static>,
35}
36
37impl<I: 'static + i2c::I2CMaster<'static>> Apds9960Component<I> {
38    pub fn new(
39        i2c_mux: &'static MuxI2C<'static, I>,
40        i2c_address: u8,
41        interrupt_pin: &'static dyn gpio::InterruptPin<'static>,
42    ) -> Self {
43        Apds9960Component {
44            i2c_mux,
45            i2c_address,
46            interrupt_pin,
47        }
48    }
49}
50
51impl<I: 'static + i2c::I2CMaster<'static>> Component for Apds9960Component<I> {
52    type StaticInput = (
53        &'static mut MaybeUninit<I2CDevice<'static, I>>,
54        &'static mut MaybeUninit<APDS9960<'static, I2CDevice<'static, I>>>,
55        &'static mut MaybeUninit<[u8; capsules_extra::apds9960::BUF_LEN]>,
56    );
57    type Output = &'static APDS9960<'static, I2CDevice<'static, I>>;
58
59    fn finalize(self, s: Self::StaticInput) -> Self::Output {
60        let apds9960_i2c = s.0.write(I2CDevice::new(self.i2c_mux, self.i2c_address));
61
62        let buffer = s.2.write([0; capsules_extra::apds9960::BUF_LEN]);
63
64        let apds9960 =
65            s.1.write(APDS9960::new(apds9960_i2c, self.interrupt_pin, buffer));
66        apds9960_i2c.set_client(apds9960);
67        self.interrupt_pin.set_client(apds9960);
68
69        apds9960
70    }
71}