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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//! Input device module for RMK
//!
//! This module defines the `InputDevice` trait, `InputProcessor` trait, `Runnable` trait and several macros for running input devices and processors.
//! The `InputDevice` trait provides the interface for individual input devices, and the macros facilitate their concurrent execution.
use RefCell;
use crateKEYBOARD_REPORT_CHANNEL;
use crateEvent;
use crateReport;
use crateKeyMap;
/// The trait for runnable input devices and processors.
///
/// For some input devices or processors, they should keep running in a separate task.
/// This trait is used to run them in a separate task.
/// The trait for input devices.
///
/// This trait defines the interface for input devices in RMK.
/// The `run_devices` macro is required to run tasks associated with input devices concurrently.
///
/// # Example
/// ```rust
/// // Define an input device
/// struct MyInputDevice;
///
/// impl InputDevice for MyInputDevice {
/// async fn read_event(&mut self) -> Event {
/// // Input device implementation
/// }
/// }
///
/// // Use the input device
/// let d1 = MyInputDevice{};
/// let d2 = MyInputDevice{};
///
/// // Run all devices simultaneously with RMK
/// embassy_futures::join::join(
/// run_devices!((d1, d2) => EVENT_CHANNEL),
/// run_rmk(
/// // .. arguments
/// ),
/// )
/// .await;
/// ```
/// Processing result of the processor chain
/// The trait for input processors.
///
/// The input processor processes the [`Event`] from the input devices and converts it to the final HID report.
/// Take the normal keyboard as the example:
///
/// The [`crate::matrix::Matrix`] is actually an input device and the [`crate::keyboard::Keyboard`] is actually an input processor.
/// Macro to bind input devices to event channels and run all of them.
///
/// This macro simplifies the creation of a task that reads events from multiple input devices
/// and sends them to specified channels. It allows for efficient handling of
/// input events in a concurrent manner.
///
/// # Arguments
///
/// * `dev`: A list of input devices grouped in parentheses.
/// * `channel`: The channel that devices send the events to.
///
/// # Example
/// ```rust
/// use rmk::channel::{blocking_mutex::raw::NoopRawMutex, channel::Channel, EVENT_CHANNEL};
/// // Initialize channel
/// let local_channel: Channel<NoopRawMutex, Event, 16> = Channel::new();
///
/// // Define your input devices, both MyInputDevice and MyInputDevice2 should implement `InputDevice] trait
/// struct MyInputDevice;
/// struct MyInputDevice2;
///
/// let d1 = MyInputDevice{};
/// let d2 = MyInputDevice2{};
/// // Bind devices to channels and run, RMK also provides EVENT_CHANNEL for general use
/// let device_future = run_devices! {
/// (d1, d2) => local_channel,
/// (matrix) => rmk::EVENT_CHANNEL,
/// };
///
/// ```
}
}
}
),*
)
),+
)
}};
}
/// Macro to bind input devices and an input processor directly.
///
/// This macro simplifies the creation of a task that reads events from multiple input devices
/// and processes them using a specified input processor. It allows for efficient handling of
/// input events in a concurrent manner.
///
/// # Arguments
///
/// * `dev`: A list of input devices grouped in parentheses.
/// * `proc`: The input processor that will handle the events from the devices.
///
/// # Example
/// ```rust
/// // Define your input devices and processor
/// struct MyInputDevice;
/// struct MyInputDevice2;
/// struct MyInputProcessor;
///
/// impl InputDevice for MyInputDevice {
/// async fn read_event(&mut self) -> Event {
/// // Implementation for reading an event
/// }
/// }
///
/// impl InputProcessor for MyInputProcessor {
/// async fn process(&mut self, event: Event) {
/// // Implementation for processing an event
/// }
/// }
///
/// // Bind devices and processor into a task, aka use `processor` to process input events from `device1` and `device2`
/// let device_future = bind_device_and_processor_and_run!((device1, device2) => processor);
///
/// ```
;
use $crate;
loop
}
};
}
/// Macro for binding input processor chain to event channel and running them.
///
/// FIXME: For split keyboard, `EVENT_CHANNEL` is REQUIRED as it's the default channel for receiving events from peripherals.
///
/// This macro creates tasks that receive events from channels and process them using specified processor chains.
/// It calls processors in order and decides whether to continue the chain based on the result of each processor.
///
/// # Arguments
///
/// * `channel`: The channel to receive events from
/// * `procs`: The processor list that will handle the events
///
/// # Example
///
/// ```rust
/// use rmk::channel::{blocking_mutex::raw::NoopRawMutex, channel::Channel, EVENT_CHANNEL};
/// // Create a local channel for processor chain
/// let local_channel: Channel<NoopRawMutex, Event, 16> = Channel::new();
/// // Two chains, one use local channel, the other use the built-in channel
/// let processor_future = run_processor_chain! {
/// local_channel => [processor1, processor2, processor3]
/// EVENT_CHANNEL => [processor4, processor5, processor6]
/// };
/// ```
,
$crateContinue =>
}
}
}
),+
)
}};
}