3
3
package keyboard
4
4
5
5
import (
6
+ "bytes"
6
7
"context"
7
8
"machine"
8
9
k "machine/usb/hid/keyboard"
@@ -18,6 +19,7 @@ type Device struct {
18
19
Keyboard UpDowner
19
20
Mouse Mouser
20
21
Override [][]Keycode
22
+ Macros [2048 ]byte
21
23
Debug bool
22
24
flashCh chan bool
23
25
flashCnt int
@@ -43,6 +45,7 @@ type KBer interface {
43
45
type UpDowner interface {
44
46
Up (c k.Keycode ) error
45
47
Down (c k.Keycode ) error
48
+ Write (b []byte ) (n int , err error )
46
49
}
47
50
48
51
type State uint8
@@ -97,7 +100,7 @@ func (d *Device) Init() error {
97
100
keys := d .GetMaxKeyCount ()
98
101
99
102
// TODO: refactor
100
- rbuf := make ([]byte , 4 + layers * keyboards * keys * 2 )
103
+ rbuf := make ([]byte , 4 + layers * keyboards * keys * 2 + len ( device . Macros ) )
101
104
_ , err := machine .Flash .ReadAt (rbuf , 0 )
102
105
if err != nil {
103
106
return err
@@ -120,6 +123,14 @@ func (d *Device) Init() error {
120
123
}
121
124
}
122
125
126
+ for i , b := range rbuf [offset :] {
127
+ if b == 0xFF {
128
+ b = 0
129
+ }
130
+ device .Macros [i ] = b
131
+ }
132
+ //copy(device.Macros[:], rbuf[offset:])
133
+
123
134
return nil
124
135
}
125
136
@@ -200,6 +211,9 @@ func (d *Device) Tick() error {
200
211
} else if x == keycodes .KeyRestoreDefaultKeymap {
201
212
// restore default keymap for QMK
202
213
machine .Flash .EraseBlocks (0 , 1 )
214
+ } else if x & 0xFF00 == keycodes .TypeMacroKey {
215
+ no := uint8 (x & 0x00FF )
216
+ d .RunMacro (no )
203
217
} else if x & 0xF000 == 0xD000 {
204
218
switch x & 0x00FF {
205
219
case 0x01 , 0x02 , 0x04 , 0x08 , 0x10 :
@@ -269,6 +283,54 @@ func (d *Device) Tick() error {
269
283
return nil
270
284
}
271
285
286
+ func (d * Device ) RunMacro (no uint8 ) error {
287
+ macros := bytes .SplitN (d .Macros [:], []byte {0x00 }, 16 )
288
+
289
+ macro := macros [no ]
290
+
291
+ for i := 0 ; i < len (macro ); {
292
+ if macro [i ] == 0x01 {
293
+ p := macro [i :]
294
+ if p [1 ] == 0x04 {
295
+ // delayMs
296
+ delayMs := int (p [2 ]) + int (p [3 ]- 1 )* 255
297
+ time .Sleep (time .Duration (delayMs ) * time .Millisecond )
298
+ i += 4
299
+ } else {
300
+ kc := keycodeViaToTGK (Keycode (p [2 ]))
301
+ sz := 3
302
+ if p [1 ] > 0x04 {
303
+ kc = Keycode (p [2 ]) + Keycode (p [3 ])<< 8
304
+ sz += 1
305
+ }
306
+ i += sz
307
+ kc = keycodeViaToTGK (kc )
308
+
309
+ switch p [1 ] {
310
+ case 0x01 , 0x05 :
311
+ k .Keyboard .Down (k .Keycode (kc ))
312
+ k .Keyboard .Up (k .Keycode (kc ))
313
+ case 0x02 , 0x06 :
314
+ k .Keyboard .Down (k .Keycode (kc ))
315
+ case 0x03 , 0x07 :
316
+ k .Keyboard .Up (k .Keycode (kc ))
317
+ }
318
+ }
319
+ } else {
320
+ idx := bytes .Index (macro [i :], []byte {0x01 })
321
+ if idx == - 1 {
322
+ idx = len (macro )
323
+ } else {
324
+ idx = i + idx
325
+ }
326
+ k .Keyboard .Write (macro [i :idx ])
327
+ i = idx
328
+ }
329
+ }
330
+
331
+ return nil
332
+ }
333
+
272
334
func encKey (kb , layer , index int ) uint32 {
273
335
return (uint32 (kb ) << 24 ) | (uint32 (layer ) << 16 ) | uint32 (index )
274
336
}
@@ -348,7 +410,11 @@ func (d *Device) KeyVia(layer, kbIndex, index int) Keycode {
348
410
// restore default keymap for QMK
349
411
kc = keycodes .KeyRestoreDefaultKeymap
350
412
default :
351
- kc = kc & 0x0FFF
413
+ if kc & 0xFF00 == keycodes .TypeMacroKey {
414
+ // skip
415
+ } else {
416
+ kc = kc & 0x0FFF
417
+ }
352
418
}
353
419
return kc
354
420
}
@@ -365,6 +431,12 @@ func (d *Device) SetKeycodeVia(layer, kbIndex, index int, key Keycode) {
365
431
return
366
432
}
367
433
//fmt.Printf("SetKeycodeVia(%d, %d, %d, %04X)\n", layer, kbIndex, index, key)
434
+ kc := keycodeViaToTGK (key )
435
+
436
+ d .kb [kbIndex ].SetKeycode (layer , index , kc )
437
+ }
438
+
439
+ func keycodeViaToTGK (key Keycode ) Keycode {
368
440
kc := key | 0xF000
369
441
370
442
switch key {
@@ -395,9 +467,11 @@ func (d *Device) SetKeycodeVia(layer, kbIndex, index int, key Keycode) {
395
467
case keycodes .KeyRestoreDefaultKeymap :
396
468
kc = keycodes .KeyRestoreDefaultKeymap
397
469
default :
470
+ if key & 0xFF00 == keycodes .TypeMacroKey {
471
+ kc = key
472
+ }
398
473
}
399
-
400
- d .kb [kbIndex ].SetKeycode (layer , index , kc )
474
+ return kc
401
475
}
402
476
403
477
func (d * Device ) Layer () int {
@@ -468,6 +542,10 @@ func (k *Keyboard) Down(c k.Keycode) error {
468
542
return nil
469
543
}
470
544
545
+ func (k * Keyboard ) Write (b []byte ) (n int , err error ) {
546
+ return k .Port .Write (b )
547
+ }
548
+
471
549
// UartTxKeyboard is a keyboard that simply sends row/col corresponding to key
472
550
// placement via UART. For instructions on how to set it up, see bellow.
473
551
//
@@ -506,3 +584,7 @@ func (k *UartTxKeyboard) Down(c k.Keycode) error {
506
584
}
507
585
return nil
508
586
}
587
+
588
+ func (k * UartTxKeyboard ) Write (b []byte ) (n int , err error ) {
589
+ return len (b ), nil
590
+ }
0 commit comments