rmk 0.8.2

Keyboard firmware written in Rust
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use rmk_types::keycode::{KeyCode, from_ascii, to_ascii};

use crate::MACRO_SPACE_SIZE;
use crate::keymap::fill_vec;

/// encoded with the two bytes, content at the third byte
/// 0b 0000 0001 1000-1010 (VIAL_MACRO_EXT) are not supported
///
/// TODO save space: refactor to use 1 byte for encoding and convert to/from vial 2 byte encoding
#[derive(Debug, Clone)]
pub enum MacroOperation {
    /// 0x00, 1 byte
    /// Marks the end of a macro sequence
    /// Don't use it on your own,
    /// will be automatically removed and added
    /// by MacroOperations::define_macro_sequences()
    End,
    /// 0x01 01 + 1 byte keycode
    Tap(KeyCode),
    /// 0x01 02 + 1 byte keycode
    Press(KeyCode),
    /// 0x01 03 + 1 byte keycode
    Release(KeyCode),
    /// 0x01 04 + 2 byte for the delay in ms
    Delay(u16),
    /// Anything not covered above (and starting at
    /// 0x30 (= b'0'), is the 1 byte ascii character.
    Text(KeyCode, bool), // bool = shifted
}

impl MacroOperation {
    /// Get the next macro operation starting from given index and offset (=position in the sequence)
    /// Return current macro operation and the next operations's offset
    pub(crate) fn get_next_macro_operation(
        macro_sequences: &[u8],
        macro_start_idx: usize,
        offset: usize,
    ) -> (MacroOperation, usize) {
        let idx = macro_start_idx + offset;
        if idx >= macro_sequences.len() - 1 {
            return (MacroOperation::End, offset);
        }
        match (macro_sequences[idx], macro_sequences[idx + 1]) {
            (0, _) => (MacroOperation::End, offset),
            (1, 1) => {
                if idx + 2 < macro_sequences.len() {
                    let keycode = (macro_sequences[idx + 2] as u16).into();
                    (MacroOperation::Tap(keycode), offset + 3)
                } else {
                    (MacroOperation::End, offset + 3)
                }
            }
            (1, 2) => {
                if idx + 2 < macro_sequences.len() {
                    let keycode = (macro_sequences[idx + 2] as u16).into();
                    (MacroOperation::Press(keycode), offset + 3)
                } else {
                    (MacroOperation::End, offset + 3)
                }
            }
            (1, 3) => {
                if idx + 2 < macro_sequences.len() {
                    let keycode = (macro_sequences[idx + 2] as u16).into();
                    (MacroOperation::Release(keycode), offset + 3)
                } else {
                    (MacroOperation::End, offset + 3)
                }
            }
            (1, 4) => {
                if idx + 3 < macro_sequences.len() {
                    let delay_ms = (macro_sequences[idx + 2].max(1) as u16 - 1)
                        + (macro_sequences[idx + 3].max(1) as u16 - 1) * 255;
                    (MacroOperation::Delay(delay_ms), offset + 4)
                } else {
                    (MacroOperation::End, offset + 4)
                }
            }
            (1, 5) | (1, 6) | (1, 7) => {
                warn!("VIAL_MACRO_EXT is not supported");
                (MacroOperation::Delay(0), offset + 4)
            }
            _ => {
                // Current byte is the ascii code, convert it to keyboard keycode(with caps state)
                let (keycode, is_caps) = from_ascii(macro_sequences[idx]);
                (MacroOperation::Text(keycode, is_caps), offset + 1)
            }
        }
    }

    /// finds the start of a macro sequence by providing a guessed start index
    pub(crate) fn get_macro_sequence_start(macro_sequences: &[u8], guessed_macro_start_idx: u8) -> Option<usize> {
        let mut idx = 0;
        // Find idx until the macro start of given index
        let mut potential_start_idx = guessed_macro_start_idx;
        loop {
            if potential_start_idx == 0 || idx >= macro_sequences.len() {
                break;
            }
            if macro_sequences[idx] == 0 {
                potential_start_idx -= 1;
            }
            idx += 1;
        }

        if idx == macro_sequences.len() { None } else { Some(idx) }
    }
}

/// serializes macro sequences
/// macros are filled up with 0 if shorter than MACRO_SPACE_SIZE
/// so that it has enough space for macros defined my Vial
/// panics if the resulting binary macro sequence is longer than MACRO_SPACE_SIZE
pub fn define_macro_sequences(
    macro_sequences: &[heapless::Vec<MacroOperation, MACRO_SPACE_SIZE>],
) -> [u8; MACRO_SPACE_SIZE] {
    // TODO after binary format is understood and
    // TEXT is smaller than others,
    // refactor, exchanging tab for text (as this is shorter),
    // taking care of press/release LSHIFT and RSHIFT as well
    let mut macro_sequences_linear = fold_to_binary(macro_sequences);

    fill_vec(&mut macro_sequences_linear);
    macro_sequences_linear
        .into_array()
        .expect("as we resized the vector, this can't happen!")
}

impl IntoIterator for MacroOperation {
    type Item = MacroOperation;

    type IntoIter = <heapless::Vec<MacroOperation, MACRO_SPACE_SIZE> as IntoIterator>::IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        heapless::Vec::from_iter([self]).into_iter()
    }
}

/// Convinience function to convert a String into a sequence of MacroOptions::Text.
/// Currently only u8 ascii is supported.
pub fn to_macro_sequence(text: &str) -> heapless::Vec<MacroOperation, MACRO_SPACE_SIZE> {
    // if !text.is_ascii() {
    //     compile_error!("Only ascii text is supported!")
    // };
    text.as_bytes()
        .iter()
        .map(|character| {
            let (keycode, shifted) = from_ascii(*character);
            MacroOperation::Text(keycode, shifted)
        })
        .collect()
}

/// converts macro sequences [Vec<MacroOperation>] binary form and flattens to [Vec<u8, MACRO_SPACE_SIZE>]
/// Note that the Vec is still at it's minimal needed length and needs to be etended with zeros to the desired size
/// (with vec.resize())
fn fold_to_binary(
    macro_sequences: &[heapless::Vec<MacroOperation, MACRO_SPACE_SIZE>],
) -> heapless::Vec<u8, MACRO_SPACE_SIZE> {
    // TODO after binary format is understood and
    // TEXT is smaller than others,
    // refactor, exchanging tab for text (as this is shorter),
    // taking care of press/release LSHIFT and RSHIFT as well
    const TOO_MANY_ELEMENTS_ERROR_TEXT: &str = "Too many Macro Operations! The sum of all Macro Operations of all Macro Sequences cannot be more than MACRO_SPACE_SIZE";

    macro_sequences
        .iter()
        .map(|macro_sequence| {
            let mut vec_seq = macro_sequence
                .into_iter()
                .filter(|macro_operation| !matches!(macro_operation, MacroOperation::End))
                .map(serialize)
                .fold(heapless::Vec::<u8, MACRO_SPACE_SIZE>::new(), |mut acc, e| {
                    acc.extend_from_slice(&e).expect(TOO_MANY_ELEMENTS_ERROR_TEXT);
                    acc
                });
            vec_seq.push(0x00).expect(TOO_MANY_ELEMENTS_ERROR_TEXT); //= serialize(&MacroOperation::End));
            vec_seq
        })
        .fold(heapless::Vec::<u8, MACRO_SPACE_SIZE>::new(), |mut acc, e| {
            acc.extend_from_slice(&e).expect(TOO_MANY_ELEMENTS_ERROR_TEXT);
            acc
        })
}

fn serialize(macro_operation: &MacroOperation) -> heapless::Vec<u8, 4> {
    match macro_operation {
        MacroOperation::End => heapless::Vec::from_slice(&[0x00]).unwrap(),
        MacroOperation::Tap(key_code) => {
            let mut result = heapless::Vec::from_slice(&[0x01, 0x01]).unwrap();
            // TODO check is Keycode is correct
            result
                .extend_from_slice(&[(*key_code as u16).to_be_bytes()[1]])
                .expect("impossible error");
            result
        }
        MacroOperation::Press(key_code) => {
            let mut result = heapless::Vec::from_slice(&[0x01, 0x02]).unwrap();
            // TODO check is Keycode is correct
            result
                .extend_from_slice(&[(*key_code as u16).to_be_bytes()[1]])
                .expect("impossible error");
            result
        }
        MacroOperation::Release(key_code) => {
            let mut result = heapless::Vec::from_slice(&[0x01, 0x03]).unwrap();
            result
                .extend_from_slice(&[(*key_code as u16).to_be_bytes()[1]])
                .expect("impossible error");
            result
        }
        MacroOperation::Delay(duration) => {
            let mut result = heapless::Vec::from_slice(&[0x01, 0x04]).unwrap();
            result
                .extend_from_slice(&duration.to_be_bytes())
                .expect("impossible error");
            result
        }
        MacroOperation::Text(key_code, shifted) => heapless::Vec::from_slice(&[to_ascii(*key_code, *shifted)]).unwrap(),
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_define_one_macro_sequence_manual() {
        let macro_sequences = &[heapless::Vec::from_slice(&[
            MacroOperation::Press(KeyCode::LShift),
            MacroOperation::Tap(KeyCode::P),
            MacroOperation::Release(KeyCode::LShift),
            MacroOperation::Tap(KeyCode::A),
            MacroOperation::Tap(KeyCode::T),
        ])
        .expect("too many elements")];
        let macro_sequences_binary = define_macro_sequences(macro_sequences);
        // let result = [0b 0000 0000 0100 1000]
        let result: [u8; 16] = [
            0x01, 0x02, 0xE1, 0x01, 0x01, 0x13, 0x01, 0x03, 0xE1, 0x01, 0x01, 0x4, 0x01, 0x01, 0x17, 0x00,
        ];
        let mut result_filled = [0; MACRO_SPACE_SIZE];
        for (i, element) in result.into_iter().enumerate() {
            result_filled[i] = element
        }
        assert_eq!(macro_sequences_binary, result_filled);
    }
    #[test]
    fn test_define_two_macro_sequence_manual() {
        let macro_sequences_terminated_uneccessarily = [
            heapless::Vec::from_slice(&[
                MacroOperation::Text(KeyCode::H, true),
                MacroOperation::Text(KeyCode::I, false),
            ])
            .expect("too many elements"),
            heapless::Vec::from_slice(&[
                MacroOperation::Press(KeyCode::LShift),
                MacroOperation::Tap(KeyCode::P),
                MacroOperation::Release(KeyCode::LShift),
                MacroOperation::Tap(KeyCode::A),
                MacroOperation::Tap(KeyCode::T),
            ])
            .expect("too many elements"),
        ];
        let macro_sequences_binary = define_macro_sequences(&macro_sequences_terminated_uneccessarily);
        let result: [u8; 19] = [
            0x48, 0x69, 0x00, 0x01, 0x02, 0xE1, 0x01, 0x01, 0x13, 0x01, 0x03, 0xE1, 0x01, 0x01, 0x4, 0x01, 0x01, 0x17,
            0x00,
        ];
        let mut result_filled = [0; MACRO_SPACE_SIZE];
        for (i, element) in result.into_iter().enumerate() {
            result_filled[i] = element
        }
        assert_eq!(macro_sequences_binary, result_filled);
    }

    #[test]
    fn test_define_macro_sequences_clean() {
        let macro_sequences_clean = [
            heapless::Vec::from_slice(&[
                MacroOperation::Press(KeyCode::LShift),
                MacroOperation::Tap(KeyCode::H),
                MacroOperation::Release(KeyCode::LShift),
                MacroOperation::Tap(KeyCode::E),
                MacroOperation::Tap(KeyCode::L),
                MacroOperation::Tap(KeyCode::L),
                MacroOperation::Tap(KeyCode::O),
            ])
            .expect("too many elements"),
            heapless::Vec::from_slice(&[
                MacroOperation::Tap(KeyCode::W),
                MacroOperation::Tap(KeyCode::O),
                MacroOperation::Tap(KeyCode::R),
                MacroOperation::Tap(KeyCode::L),
                MacroOperation::Tap(KeyCode::D),
            ])
            .expect("too many elements"),
            heapless::Vec::from_slice(&[
                MacroOperation::Press(KeyCode::LShift),
                MacroOperation::Tap(KeyCode::Kc2),
                MacroOperation::Release(KeyCode::LShift),
            ])
            .expect("too many elements"),
        ];
        let macro_sequences_binary = define_macro_sequences(&macro_sequences_clean);
        let result: [u8; 48] = [
            1, 2, 225, 1, 1, 11, 1, 3, 225, 1, 1, 8, 1, 1, 15, 1, 1, 15, 1, 1, 18, 0, 1, 1, 26, 1, 1, 18, 1, 1, 21, 1,
            1, 15, 1, 1, 7, 0, 1, 2, 225, 1, 1, 31, 1, 3, 225, 0,
        ];
        let mut result_filled = [0; MACRO_SPACE_SIZE];
        for (i, element) in result.into_iter().enumerate() {
            result_filled[i] = element
        }
        assert_eq!(macro_sequences_binary, result_filled);
    }

    #[test]
    fn test_define_macro_sequences_uneccessarily_terminated() {
        let macro_sequences_terminated_uneccessarily = [
            heapless::Vec::from_slice(&[
                MacroOperation::Press(KeyCode::LShift),
                MacroOperation::Tap(KeyCode::H),
                MacroOperation::Release(KeyCode::LShift),
                MacroOperation::Tap(KeyCode::E),
                MacroOperation::Tap(KeyCode::L),
                MacroOperation::Tap(KeyCode::L),
                MacroOperation::Tap(KeyCode::O),
                MacroOperation::End,
            ])
            .expect("too many elements"),
            heapless::Vec::from_slice(&[
                MacroOperation::Tap(KeyCode::W),
                MacroOperation::Tap(KeyCode::O),
                MacroOperation::Tap(KeyCode::R),
                MacroOperation::Tap(KeyCode::L),
                MacroOperation::End,
            ])
            .expect("too many elements"),
            heapless::Vec::from_slice(&[
                MacroOperation::Press(KeyCode::LShift),
                MacroOperation::Tap(KeyCode::Kc2),
                MacroOperation::Release(KeyCode::LShift),
                MacroOperation::End,
            ])
            .expect("too many elements"),
        ];
        let macro_sequences_binary = define_macro_sequences(&macro_sequences_terminated_uneccessarily);
        let result: [u8; 45] = [
            1, 2, 225, 1, 1, 11, 1, 3, 225, 1, 1, 8, 1, 1, 15, 1, 1, 15, 1, 1, 18, 0, 1, 1, 26, 1, 1, 18, 1, 1, 21, 1,
            1, 15, 0, 1, 2, 225, 1, 1, 31, 1, 3, 225, 0,
        ];
        let mut result_filled = [0; MACRO_SPACE_SIZE];
        for (i, element) in result.into_iter().enumerate() {
            result_filled[i] = element
        }
        assert_eq!(macro_sequences_binary, result_filled);
    }

    #[test]
    fn test_define_macro_sequences_random_end_markers() {
        let macro_sequences_random_end_markers = [
            heapless::Vec::from_slice(&[
                MacroOperation::Press(KeyCode::LShift),
                MacroOperation::Tap(KeyCode::H),
                MacroOperation::End,
                MacroOperation::Release(KeyCode::LShift),
                MacroOperation::Tap(KeyCode::E),
                MacroOperation::End,
                MacroOperation::End,
                MacroOperation::Tap(KeyCode::L),
                MacroOperation::End,
                MacroOperation::Tap(KeyCode::L),
                MacroOperation::Tap(KeyCode::O),
                MacroOperation::End,
            ])
            .expect("too many elements"),
            heapless::Vec::from_slice(&[
                MacroOperation::Tap(KeyCode::W),
                MacroOperation::Tap(KeyCode::O),
                MacroOperation::End,
                MacroOperation::End,
                MacroOperation::End,
                MacroOperation::End,
                MacroOperation::Tap(KeyCode::R),
                MacroOperation::Tap(KeyCode::L),
            ])
            .expect("too many elements"),
            heapless::Vec::from_slice(&[
                MacroOperation::Press(KeyCode::LShift),
                MacroOperation::Tap(KeyCode::Kc2),
                MacroOperation::Release(KeyCode::LShift),
                MacroOperation::End,
                MacroOperation::End,
                MacroOperation::End,
                MacroOperation::End,
                MacroOperation::End,
            ])
            .expect("too many elements"),
        ];
        let macro_sequences_binary = define_macro_sequences(&macro_sequences_random_end_markers);
        let result: [u8; 45] = [
            1, 2, 225, 1, 1, 11, 1, 3, 225, 1, 1, 8, 1, 1, 15, 1, 1, 15, 1, 1, 18, 0, 1, 1, 26, 1, 1, 18, 1, 1, 21, 1,
            1, 15, 0, 1, 2, 225, 1, 1, 31, 1, 3, 225, 0,
        ];
        let mut result_filled = [0; MACRO_SPACE_SIZE];
        for (i, element) in result.into_iter().enumerate() {
            result_filled[i] = element
        }
        assert_eq!(macro_sequences_binary, result_filled);
    }
}