|
5 | 5 | //!
|
6 | 6 | //!
|
7 | 7 | //! ```rust
|
8 |
| -//! let adc_microphone = components::adc_microphone::AdcMicrophoneComponent::new().finalize( |
9 |
| -//! components::adc_microphone_component_helper!( |
10 |
| -//! // adc |
11 |
| -//! nrf52833::adc::Adc, |
12 |
| -//! // adc channel |
13 |
| -//! nrf52833::adc::AdcChannelSetup::setup( |
14 |
| -//! nrf52833::adc::AdcChannel::AnalogInput3, |
15 |
| -//! nrf52833::adc::AdcChannelGain::Gain4, |
16 |
| -//! nrf52833::adc::AdcChannelResistor::Bypass, |
17 |
| -//! nrf52833::adc::AdcChannelResistor::Pulldown, |
18 |
| -//! nrf52833::adc::AdcChannelSamplingTime::us3 |
19 |
| -//! ), |
20 |
| -//! // adc mux |
21 |
| -//! adc_mux, |
22 |
| -//! // buffer size |
23 |
| -//! 50, |
24 |
| -//! // gpio |
25 |
| -//! nrf52833::gpio::GPIOPin, |
26 |
| -//! // optional gpio pin |
27 |
| -//! Some(&base_peripherals.gpio_port[LED_MICROPHONE_PIN]) |
28 |
| -//! ), |
29 |
| -//! ); |
| 8 | +//! let adc_microphone = components::adc_microphone::AdcMicrophoneComponent::new( |
| 9 | +//! adc_mux, |
| 10 | +//! nrf52833::adc::AdcChannelSetup::setup( |
| 11 | +//! nrf52833::adc::AdcChannel::AnalogInput3, |
| 12 | +//! nrf52833::adc::AdcChannelGain::Gain4, |
| 13 | +//! nrf52833::adc::AdcChannelResistor::Bypass, |
| 14 | +//! nrf52833::adc::AdcChannelResistor::Pulldown, |
| 15 | +//! nrf52833::adc::AdcChannelSamplingTime::us3, |
| 16 | +//! ), |
| 17 | +//! Some(&nrf52833_peripherals.gpio_port[LED_MICROPHONE_PIN]), |
| 18 | +//! ) |
| 19 | +//! .finalize(components::adc_microphone_component_static!( |
| 20 | +//! // adc |
| 21 | +//! nrf52833::adc::Adc, |
| 22 | +//! // buffer size |
| 23 | +//! 50, |
| 24 | +//! // gpio |
| 25 | +//! nrf52833::gpio::GPIOPin |
| 26 | +//! )); |
30 | 27 | //! ```
|
31 | 28 |
|
32 | 29 | use capsules::adc_microphone::AdcMicrophone;
|
33 | 30 | use capsules::virtual_adc::AdcDevice;
|
34 |
| -use core::marker::PhantomData; |
35 | 31 | use core::mem::MaybeUninit;
|
36 | 32 | use kernel::component::Component;
|
37 | 33 | use kernel::hil::adc::{self, AdcChannel};
|
38 | 34 | use kernel::hil::gpio;
|
39 |
| -use kernel::static_init_half; |
40 | 35 |
|
41 | 36 | #[macro_export]
|
42 |
| -macro_rules! adc_microphone_component_helper { |
43 |
| - ($A:ty, $channel:expr, $adc_mux:expr, $LEN:literal, $P: ty, $pin: expr $(,)?) => {{ |
44 |
| - use capsules::adc_microphone::AdcMicrophone; |
45 |
| - use capsules::virtual_adc::AdcDevice; |
46 |
| - use core::mem::MaybeUninit; |
47 |
| - use kernel::hil::adc::Adc; |
48 |
| - use kernel::hil::gpio::Pin; |
| 37 | +macro_rules! adc_microphone_component_static { |
| 38 | + ($A:ty, $LEN:literal, $P: ty $(,)?) => {{ |
| 39 | + let adc_device = components::adc_component_static!($A); |
| 40 | + let buffer = kernel::static_buf!([u16; $LEN]); |
| 41 | + let adc_microphone = |
| 42 | + kernel::static_buf!(capsules::adc_microphone::AdcMicrophone<'static, $P>); |
49 | 43 |
|
50 |
| - static mut BUFFER: [u16; $LEN] = [0; $LEN]; |
51 |
| - |
52 |
| - let mut adc_microphone_adc: &'static capsules::virtual_adc::AdcDevice<'static, $A> = |
53 |
| - components::adc::AdcComponent::new($adc_mux, $channel) |
54 |
| - .finalize(components::adc_component_helper!($A)); |
55 |
| - static mut adc_microphone: MaybeUninit<AdcMicrophone<'static, $P>> = MaybeUninit::uninit(); |
56 |
| - ( |
57 |
| - &mut adc_microphone_adc, |
58 |
| - $pin, |
59 |
| - &mut BUFFER, |
60 |
| - &mut adc_microphone, |
61 |
| - ) |
| 44 | + (adc_device, buffer, adc_microphone) |
62 | 45 | };};
|
63 | 46 | }
|
64 | 47 |
|
65 |
| -pub struct AdcMicrophoneComponent<A: 'static + adc::Adc, P: 'static + gpio::Pin> { |
66 |
| - _adc: PhantomData<A>, |
67 |
| - _pin: PhantomData<P>, |
| 48 | +pub struct AdcMicrophoneComponent< |
| 49 | + A: 'static + adc::Adc, |
| 50 | + P: 'static + gpio::Pin, |
| 51 | + const BUF_LEN: usize, |
| 52 | +> { |
| 53 | + adc_mux: &'static capsules::virtual_adc::MuxAdc<'static, A>, |
| 54 | + adc_channel: A::Channel, |
| 55 | + pin: Option<&'static P>, |
68 | 56 | }
|
69 | 57 |
|
70 |
| -impl<A: 'static + adc::Adc, P: 'static + gpio::Pin> AdcMicrophoneComponent<A, P> { |
71 |
| - pub fn new() -> AdcMicrophoneComponent<A, P> { |
| 58 | +impl<A: 'static + adc::Adc, P: 'static + gpio::Pin, const BUF_LEN: usize> |
| 59 | + AdcMicrophoneComponent<A, P, BUF_LEN> |
| 60 | +{ |
| 61 | + pub fn new( |
| 62 | + adc_mux: &'static capsules::virtual_adc::MuxAdc<'static, A>, |
| 63 | + adc_channel: A::Channel, |
| 64 | + pin: Option<&'static P>, |
| 65 | + ) -> AdcMicrophoneComponent<A, P, BUF_LEN> { |
72 | 66 | AdcMicrophoneComponent {
|
73 |
| - _adc: PhantomData, |
74 |
| - _pin: PhantomData, |
| 67 | + adc_mux, |
| 68 | + adc_channel, |
| 69 | + pin, |
75 | 70 | }
|
76 | 71 | }
|
77 | 72 | }
|
78 | 73 |
|
79 |
| -impl<A: 'static + adc::Adc, P: 'static + gpio::Pin> Component for AdcMicrophoneComponent<A, P> { |
| 74 | +impl<A: 'static + adc::Adc, P: 'static + gpio::Pin, const BUF_LEN: usize> Component |
| 75 | + for AdcMicrophoneComponent<A, P, BUF_LEN> |
| 76 | +{ |
80 | 77 | type StaticInput = (
|
81 |
| - &'static AdcDevice<'static, A>, |
82 |
| - Option<&'static P>, |
83 |
| - &'static mut [u16], |
| 78 | + &'static mut MaybeUninit<AdcDevice<'static, A>>, |
| 79 | + &'static mut MaybeUninit<[u16; BUF_LEN]>, |
84 | 80 | &'static mut MaybeUninit<AdcMicrophone<'static, P>>,
|
85 | 81 | );
|
86 | 82 | type Output = &'static AdcMicrophone<'static, P>;
|
87 | 83 |
|
88 |
| - unsafe fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output { |
89 |
| - let adc_microphone = static_init_half!( |
90 |
| - static_buffer.3, |
91 |
| - AdcMicrophone<'static, P>, |
92 |
| - AdcMicrophone::new(static_buffer.0, static_buffer.1, static_buffer.2) |
93 |
| - ); |
| 84 | + unsafe fn finalize(self, s: Self::StaticInput) -> Self::Output { |
| 85 | + let adc_device = |
| 86 | + crate::adc::AdcComponent::new(self.adc_mux, self.adc_channel).finalize(s.0); |
| 87 | + |
| 88 | + let buffer = s.1.write([0; BUF_LEN]); |
| 89 | + |
| 90 | + let adc_microphone = s.2.write(AdcMicrophone::new(adc_device, self.pin, buffer)); |
94 | 91 |
|
95 |
| - static_buffer.0.set_client(adc_microphone); |
| 92 | + adc_device.set_client(adc_microphone); |
96 | 93 |
|
97 | 94 | adc_microphone
|
98 | 95 | }
|
|
0 commit comments