components/
servo.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2024.

//! Components for collections of servomotors.
//!
//! Usage
//! -----
//! ```rust
//! let servo = components::servo::ServosComponent::new().finalize(components::servo_component_static!(
//!     servo1, servo2,
//! ));
//! ```
use capsules_extra::servo::Servo as ServoDriver;
use core::mem::MaybeUninit;
use kernel::component::Component;

#[macro_export]
macro_rules! servo_component_static {
    ($($S:expr),+ $(,)?) => {{
        use kernel::count_expressions;
        use kernel::static_init;
        const SERVO_COUNT: usize = count_expressions!($($S),+);
        let arr = static_init!(
            [&'static dyn Servo; SERVO_COUNT],
            [
                $(

                        $S

                ),+
            ]
        );

        let servo = kernel::static_buf!( capsules_extra::servo::Servo<'static, SERVO_COUNT>);
        (servo, arr)
    };};
}

pub type ServosComponentType<const SERVO_COUNT: usize> = ServoDriver<'static, SERVO_COUNT>;

pub struct ServosComponent<const SERVO_COUNT: usize> {}

impl<const SERVO_COUNT: usize> ServosComponent<SERVO_COUNT> {
    pub fn new() -> Self {
        Self {}
    }
}

impl<const SERVO_COUNT: usize> Component for ServosComponent<SERVO_COUNT> {
    type StaticInput = (
        &'static mut MaybeUninit<ServoDriver<'static, SERVO_COUNT>>,
        &'static mut [&'static dyn kernel::hil::servo::Servo<'static>; SERVO_COUNT],
    );
    type Output = &'static ServoDriver<'static, SERVO_COUNT>;

    fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
        static_buffer.0.write(ServoDriver::new(static_buffer.1))
    }
}