-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request tock#4233 from alistair23/alistair/rainfall
Add support for DFRobot Rain Sensor
- Loading branch information
Showing
12 changed files
with
644 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Licensed under the Apache License, Version 2.0 or the MIT License. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// Copyright Tock Contributors 2022. | ||
|
||
//! Components for the DFRobot Rainfall Sensor. | ||
//! <https://wiki.dfrobot.com/SKU_SEN0575_Gravity_Rainfall_Sensor> | ||
//! | ||
//! Usage | ||
//! ----- | ||
//! ```rust | ||
//! let dfrobot_rainfall = | ||
//! components::dfrobot_rainfall_sensor::DFRobotRainFallSensorComponent::new(mux_i2c, 0x1D) | ||
//! .finalize(components::dfrobot_rainfall_sensor_component_static!( | ||
//! apollo3::iom::Iom<'static> | ||
//! )); | ||
//! ``` | ||
use capsules_core::virtualizers::virtual_alarm::MuxAlarm; | ||
use capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm; | ||
use capsules_core::virtualizers::virtual_i2c::{I2CDevice, MuxI2C}; | ||
use capsules_extra::dfrobot_rainfall_sensor::{DFRobotRainFall, BUFFER_SIZE}; | ||
use core::mem::MaybeUninit; | ||
use kernel::component::Component; | ||
use kernel::hil::i2c; | ||
use kernel::hil::time; | ||
use kernel::hil::time::Alarm; | ||
|
||
// Setup static space for the objects. | ||
#[macro_export] | ||
macro_rules! dfrobot_rainfall_sensor_component_static { | ||
($A:ty, $I:ty $(,)?) => {{ | ||
let i2c_device = | ||
kernel::static_buf!(capsules_core::virtualizers::virtual_i2c::I2CDevice<'static, $I>); | ||
let i2c_buffer = | ||
kernel::static_buf!([u8; capsules_extra::dfrobot_rainfall_sensor::BUFFER_SIZE]); | ||
let dfrobot_rainfall_sensor = kernel::static_buf!( | ||
capsules_extra::dfrobot_rainfall_sensor::DFRobotRainFall< | ||
'static, | ||
VirtualMuxAlarm<'static, $A>, | ||
capsules_core::virtualizers::virtual_i2c::I2CDevice<$I>, | ||
> | ||
); | ||
let alarm = kernel::static_buf!( | ||
capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<'static, $A> | ||
); | ||
|
||
(i2c_device, i2c_buffer, dfrobot_rainfall_sensor, alarm) | ||
};}; | ||
} | ||
|
||
pub type DFRobotRainFallSensorComponentType<A, I> = | ||
capsules_extra::dfrobot_rainfall_sensor::DFRobotRainFall<'static, A, I>; | ||
|
||
pub struct DFRobotRainFallSensorComponent< | ||
A: 'static + time::Alarm<'static>, | ||
I: 'static + i2c::I2CMaster<'static>, | ||
> { | ||
i2c_mux: &'static MuxI2C<'static, I>, | ||
i2c_address: u8, | ||
alarm_mux: &'static MuxAlarm<'static, A>, | ||
} | ||
|
||
impl<A: 'static + time::Alarm<'static>, I: 'static + i2c::I2CMaster<'static>> | ||
DFRobotRainFallSensorComponent<A, I> | ||
{ | ||
pub fn new( | ||
i2c: &'static MuxI2C<'static, I>, | ||
i2c_address: u8, | ||
alarm_mux: &'static MuxAlarm<'static, A>, | ||
) -> Self { | ||
DFRobotRainFallSensorComponent { | ||
i2c_mux: i2c, | ||
i2c_address, | ||
alarm_mux, | ||
} | ||
} | ||
} | ||
|
||
impl<A: 'static + time::Alarm<'static>, I: 'static + i2c::I2CMaster<'static>> Component | ||
for DFRobotRainFallSensorComponent<A, I> | ||
{ | ||
type StaticInput = ( | ||
&'static mut MaybeUninit<I2CDevice<'static, I>>, | ||
&'static mut MaybeUninit<[u8; BUFFER_SIZE]>, | ||
&'static mut MaybeUninit< | ||
DFRobotRainFall<'static, VirtualMuxAlarm<'static, A>, I2CDevice<'static, I>>, | ||
>, | ||
&'static mut MaybeUninit<VirtualMuxAlarm<'static, A>>, | ||
); | ||
type Output = | ||
&'static DFRobotRainFall<'static, VirtualMuxAlarm<'static, A>, I2CDevice<'static, I>>; | ||
|
||
fn finalize(self, s: Self::StaticInput) -> Self::Output { | ||
let dfrobot_rainfall_sensor_i2c = s.0.write(I2CDevice::new(self.i2c_mux, self.i2c_address)); | ||
let i2c_buffer = s.1.write([0; BUFFER_SIZE]); | ||
let alarm = s.3.write(VirtualMuxAlarm::new(self.alarm_mux)); | ||
alarm.setup(); | ||
|
||
let dfrobot_rainfall_sensor = s.2.write(DFRobotRainFall::new( | ||
dfrobot_rainfall_sensor_i2c, | ||
i2c_buffer, | ||
alarm, | ||
)); | ||
|
||
alarm.set_alarm_client(dfrobot_rainfall_sensor); | ||
dfrobot_rainfall_sensor_i2c.set_client(dfrobot_rainfall_sensor); | ||
dfrobot_rainfall_sensor.startup(); | ||
dfrobot_rainfall_sensor | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Licensed under the Apache License, Version 2.0 or the MIT License. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// Copyright Tock Contributors 2022. | ||
|
||
//! Component for any rainfall sensor. | ||
//! | ||
//! Usage | ||
//! ----- | ||
//! ```rust | ||
//! let rainfall = components::rainfall::RainFallComponent::new( | ||
//! board_kernel, | ||
//! capsules_extra::rainfall::DRIVER_NUM, | ||
//! dfrobot_rainfall, | ||
//! ) | ||
//! .finalize(components::rainfall_component_static!(DFRobotRainFallType)); | ||
//! ``` | ||
use capsules_extra::rainfall::RainFallSensor; | ||
use core::mem::MaybeUninit; | ||
use kernel::capabilities; | ||
use kernel::component::Component; | ||
use kernel::create_capability; | ||
use kernel::hil; | ||
|
||
#[macro_export] | ||
macro_rules! rainfall_component_static { | ||
($H: ty $(,)?) => {{ | ||
kernel::static_buf!(capsules_extra::rainfall::RainFallSensor<'static, $H>) | ||
};}; | ||
} | ||
|
||
pub type RainFallComponentType<H> = capsules_extra::rainfall::RainFallSensor<'static, H>; | ||
|
||
pub struct RainFallComponent<T: 'static + hil::sensors::RainFallDriver<'static>> { | ||
board_kernel: &'static kernel::Kernel, | ||
driver_num: usize, | ||
sensor: &'static T, | ||
} | ||
|
||
impl<T: 'static + hil::sensors::RainFallDriver<'static>> RainFallComponent<T> { | ||
pub fn new( | ||
board_kernel: &'static kernel::Kernel, | ||
driver_num: usize, | ||
sensor: &'static T, | ||
) -> RainFallComponent<T> { | ||
RainFallComponent { | ||
board_kernel, | ||
driver_num, | ||
sensor, | ||
} | ||
} | ||
} | ||
|
||
impl<T: 'static + hil::sensors::RainFallDriver<'static>> Component for RainFallComponent<T> { | ||
type StaticInput = &'static mut MaybeUninit<RainFallSensor<'static, T>>; | ||
type Output = &'static RainFallSensor<'static, T>; | ||
|
||
fn finalize(self, s: Self::StaticInput) -> Self::Output { | ||
let grant_cap = create_capability!(capabilities::MemoryAllocationCapability); | ||
|
||
let rainfall = s.write(RainFallSensor::new( | ||
self.sensor, | ||
self.board_kernel.create_grant(self.driver_num, &grant_cap), | ||
)); | ||
|
||
hil::sensors::RainFallDriver::set_client(self.sensor, rainfall); | ||
rainfall | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.