embedded_controls/
macros.rs

1/// Create a config for [`DebouncedInput`](crate::DebouncedInput).
2///
3/// # Example 1
4/// ```ignore
5/// debounced_input_config!(
6///     SomeDebouncedInputConfig,
7///     debounce_timer: MyElapsedTimer = MyElapsedTimer::new(20.millis())
8/// );
9///
10/// type MyDebouncedInput<InputSwitch> = DebouncedInput<InputSwitch, SomeDebouncedInputConfig>;
11/// ```
12///
13/// # Example 2
14/// ```ignore
15/// debounced_input_config!(
16///     pub SomeDebouncedInputConfig,
17///     debounce_timer: MyElapsedTimer = MyElapsedTimer::new(20.millis())
18/// );
19///
20/// type MyDebouncedInput<InputSwitch> = DebouncedInput<InputSwitch, SomeDebouncedInputConfig>;
21/// ```
22///
23/// # Example 3
24/// ```ignore
25/// pub struct SomeDebouncedInputConfig;
26///
27/// debounced_input_config!(
28///     impl SomeDebouncedInputConfig,
29///     debounce_timer: MyElapsedTimer = MyElapsedTimer::new(20.millis())
30/// );
31///
32/// type MyDebouncedInput<InputSwitch> = DebouncedInput<InputSwitch, SomeDebouncedInputConfig>;
33/// ```
34#[macro_export]
35macro_rules! debounced_input_config {
36    (impl $config_name:ty, debounce_timer: $timer_type:ty = $timer_value:expr) => {
37        impl $crate::DebouncedInputConfig for $config_name {
38            type Timer = $timer_type;
39            const DEBOUNCE_TIMER: $timer_type = $timer_value;
40        }
41    };
42    ($vis:vis $config_name:ident, debounce_timer: $timer_type:ty = $timer_value:expr) => {
43        $vis struct $config_name;
44
45        debounced_input_config!(
46            impl $config_name,
47            debounce_timer: $timer_type = $timer_value
48        );
49    };
50}
51
52/// Create a config for [`Encoder`](crate::Encoder).
53///
54/// # Example 1
55/// ```ignore
56/// encoder_config!(
57///     SomeEncoderConfig,
58///     debounce_timer: MyElapsedTimer = MyElapsedTimer::new(2.millis()),
59///     counts_div: i8 = 4
60/// );
61///
62/// type MyEncoder<SwitchA, SwitchB> = Encoder<SwitchA, SwitchB, SomeEncoderConfig>;
63/// ```
64///
65/// # Example 2
66/// ```ignore
67/// encoder_config!(
68///     pub SomeEncoderConfig,
69///     debounce_timer: MyElapsedTimer = MyElapsedTimer::new(2.millis()),
70///     counts_div: i8 = 4
71/// );
72///
73/// type MyEncoder<SwitchA, SwitchB> = Encoder<SwitchA, SwitchB, SomeEncoderConfig>;
74/// ```
75///
76/// # Example 3
77/// ```ignore
78/// pub struct SomeEncoderConfig;
79///
80/// encoder_config!(
81///     impl SomeEncoderConfig,
82///     debounce_timer: MyElapsedTimer = MyElapsedTimer::new(2.millis()),
83///     counts_div: i8 = 4
84/// );
85///
86/// type MyEncoder<SwitchA, SwitchB> = Encoder<SwitchA, SwitchB, SomeEncoderConfig>;
87/// ```
88#[macro_export]
89macro_rules! encoder_config {
90    (
91        impl $config_name:ty,
92        debounce_timer: $timer_type:ty = $timer_value:expr,
93        counts_div: $counts_type:ty = $counts_div_value:expr
94    ) => {
95        $crate::debounced_input_config!(
96            impl $config_name,
97            debounce_timer: $timer_type = $timer_value
98        );
99
100        impl $crate::EncoderConfig for $config_name {
101            type Counts = $counts_type;
102            const COUNTS_DIV: $counts_type = $counts_div_value;
103        }
104    };
105    (
106        $vis:vis $config_name:ident,
107        debounce_timer: $timer_type:ty = $timer_value:expr,
108        counts_div: $counts_type:ty = $counts_div_value:expr
109    ) => {
110        $vis struct $config_name;
111
112        encoder_config!(impl $config_name,
113            debounce_timer: $timer_type = $timer_value,
114            counts_div: $counts_type = $counts_div_value
115        );
116    };
117}