use capsules_core::virtualizers::virtual_alarm::{MuxAlarm, VirtualMuxAlarm};
use capsules_core::virtualizers::virtual_i2c::{I2CDevice, MuxI2C};
use capsules_extra::ambient_light::AmbientLight;
use capsules_extra::isl29035::Isl29035;
use core::mem::MaybeUninit;
use kernel::capabilities;
use kernel::component::Component;
use kernel::create_capability;
use kernel::hil;
use kernel::hil::i2c;
use kernel::hil::time::{self, Alarm};
#[macro_export]
macro_rules! isl29035_component_static {
($A:ty, $I:ty $(,)?) => {{
let alarm = kernel::static_buf!(
capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<'static, $A>
);
let i2c_device =
kernel::static_buf!(capsules_core::virtualizers::virtual_i2c::I2CDevice<'static, $I>);
let i2c_buffer = kernel::static_buf!([u8; capsules_extra::isl29035::BUF_LEN]);
let isl29035 = kernel::static_buf!(
capsules_extra::isl29035::Isl29035<
'static,
capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<'static, $A>,
>
);
(alarm, i2c_device, i2c_buffer, isl29035)
};};
}
#[macro_export]
macro_rules! ambient_light_component_static {
() => {{
kernel::static_buf!(capsules_extra::ambient_light::AmbientLight<'static>)
};};
}
pub struct Isl29035Component<
A: 'static + time::Alarm<'static>,
I: 'static + i2c::I2CMaster<'static>,
> {
i2c_mux: &'static MuxI2C<'static, I>,
alarm_mux: &'static MuxAlarm<'static, A>,
}
impl<A: 'static + time::Alarm<'static>, I: 'static + i2c::I2CMaster<'static>>
Isl29035Component<A, I>
{
pub fn new(i2c: &'static MuxI2C<'static, I>, alarm: &'static MuxAlarm<'static, A>) -> Self {
Isl29035Component {
i2c_mux: i2c,
alarm_mux: alarm,
}
}
}
impl<A: 'static + time::Alarm<'static>, I: 'static + i2c::I2CMaster<'static>> Component
for Isl29035Component<A, I>
{
type StaticInput = (
&'static mut MaybeUninit<VirtualMuxAlarm<'static, A>>,
&'static mut MaybeUninit<I2CDevice<'static, I>>,
&'static mut MaybeUninit<[u8; capsules_extra::isl29035::BUF_LEN]>,
&'static mut MaybeUninit<Isl29035<'static, VirtualMuxAlarm<'static, A>>>,
);
type Output = &'static Isl29035<'static, VirtualMuxAlarm<'static, A>>;
fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
let isl29035_i2c = static_buffer.1.write(I2CDevice::new(self.i2c_mux, 0x44));
let isl29035_i2c_buffer = static_buffer
.2
.write([0; capsules_extra::isl29035::BUF_LEN]);
let isl29035_virtual_alarm = static_buffer.0.write(VirtualMuxAlarm::new(self.alarm_mux));
isl29035_virtual_alarm.setup();
let isl29035 = static_buffer.3.write(Isl29035::new(
isl29035_i2c,
isl29035_virtual_alarm,
isl29035_i2c_buffer,
));
isl29035_i2c.set_client(isl29035);
isl29035_virtual_alarm.set_alarm_client(isl29035);
isl29035
}
}
pub struct AmbientLightComponent<L: 'static + hil::sensors::AmbientLight<'static>> {
board_kernel: &'static kernel::Kernel,
driver_num: usize,
light_sensor: &'static L,
}
impl<L: 'static + hil::sensors::AmbientLight<'static>> AmbientLightComponent<L> {
pub fn new(
board_kernel: &'static kernel::Kernel,
driver_num: usize,
light_sensor: &'static L,
) -> Self {
AmbientLightComponent {
board_kernel,
driver_num,
light_sensor,
}
}
}
impl<L: 'static + hil::sensors::AmbientLight<'static>> Component for AmbientLightComponent<L> {
type StaticInput = &'static mut MaybeUninit<AmbientLight<'static>>;
type Output = &'static AmbientLight<'static>;
fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);
let ambient_light = static_buffer.write(AmbientLight::new(
self.light_sensor,
self.board_kernel.create_grant(self.driver_num, &grant_cap),
));
hil::sensors::AmbientLight::set_client(self.light_sensor, ambient_light);
ambient_light
}
}