components/
servo.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 2024.
4
5//! Components for collections of servomotors.
6//!
7//! Usage
8//! -----
9//! ```rust
10//! let servo = components::servo::ServosComponent::new().finalize(components::servo_component_static!(
11//!     servo1, servo2,
12//! ));
13//! ```
14use capsules_extra::servo::Servo as ServoDriver;
15use core::mem::MaybeUninit;
16use kernel::component::Component;
17
18#[macro_export]
19macro_rules! servo_component_static {
20    ($($S:expr),+ $(,)?) => {{
21        use kernel::count_expressions;
22        use kernel::static_init;
23        const SERVO_COUNT: usize = count_expressions!($($S),+);
24        let arr = static_init!(
25            [&'static dyn Servo; SERVO_COUNT],
26            [
27                $(
28
29                        $S
30
31                ),+
32            ]
33        );
34
35        let servo = kernel::static_buf!( capsules_extra::servo::Servo<'static, SERVO_COUNT>);
36        (servo, arr)
37    };};
38}
39
40pub type ServosComponentType<const SERVO_COUNT: usize> = ServoDriver<'static, SERVO_COUNT>;
41
42pub struct ServosComponent<const SERVO_COUNT: usize> {}
43
44impl<const SERVO_COUNT: usize> ServosComponent<SERVO_COUNT> {
45    pub fn new() -> Self {
46        Self {}
47    }
48}
49
50impl<const SERVO_COUNT: usize> Component for ServosComponent<SERVO_COUNT> {
51    type StaticInput = (
52        &'static mut MaybeUninit<ServoDriver<'static, SERVO_COUNT>>,
53        &'static mut [&'static dyn kernel::hil::servo::Servo<'static>; SERVO_COUNT],
54    );
55    type Output = &'static ServoDriver<'static, SERVO_COUNT>;
56
57    fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
58        static_buffer.0.write(ServoDriver::new(static_buffer.1))
59    }
60}