33package keyboard
44
55import (
6+ "bytes"
67 "context"
78 "machine"
89 k "machine/usb/hid/keyboard"
@@ -18,6 +19,7 @@ type Device struct {
1819 Keyboard UpDowner
1920 Mouse Mouser
2021 Override [][]Keycode
22+ Macros [2048 ]byte
2123 Debug bool
2224 flashCh chan bool
2325 flashCnt int
@@ -43,6 +45,7 @@ type KBer interface {
4345type UpDowner interface {
4446 Up (c k.Keycode ) error
4547 Down (c k.Keycode ) error
48+ Write (b []byte ) (n int , err error )
4649}
4750
4851type State uint8
@@ -97,7 +100,7 @@ func (d *Device) Init() error {
97100 keys := d .GetMaxKeyCount ()
98101
99102 // TODO: refactor
100- rbuf := make ([]byte , 4 + layers * keyboards * keys * 2 )
103+ rbuf := make ([]byte , 4 + layers * keyboards * keys * 2 + len ( device . Macros ) )
101104 _ , err := machine .Flash .ReadAt (rbuf , 0 )
102105 if err != nil {
103106 return err
@@ -120,6 +123,14 @@ func (d *Device) Init() error {
120123 }
121124 }
122125
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+
123134 return nil
124135}
125136
@@ -141,7 +152,7 @@ func (d *Device) Tick() error {
141152 case <- d .flashCh :
142153 d .flashCnt = 1
143154 default :
144- if d .flashCnt >= 500 {
155+ if d .flashCnt >= 5000 {
145156 d .flashCnt = 0
146157 err := Save ()
147158 if err != nil {
@@ -200,6 +211,9 @@ func (d *Device) Tick() error {
200211 } else if x == keycodes .KeyRestoreDefaultKeymap {
201212 // restore default keymap for QMK
202213 machine .Flash .EraseBlocks (0 , 1 )
214+ } else if x & 0xFF00 == keycodes .TypeMacroKey {
215+ no := uint8 (x & 0x00FF )
216+ d .RunMacro (no )
203217 } else if x & 0xF000 == 0xD000 {
204218 switch x & 0x00FF {
205219 case 0x01 , 0x02 , 0x04 , 0x08 , 0x10 :
@@ -269,6 +283,54 @@ func (d *Device) Tick() error {
269283 return nil
270284}
271285
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+
272334func encKey (kb , layer , index int ) uint32 {
273335 return (uint32 (kb ) << 24 ) | (uint32 (layer ) << 16 ) | uint32 (index )
274336}
@@ -348,7 +410,11 @@ func (d *Device) KeyVia(layer, kbIndex, index int) Keycode {
348410 // restore default keymap for QMK
349411 kc = keycodes .KeyRestoreDefaultKeymap
350412 default :
351- kc = kc & 0x0FFF
413+ if kc & 0xFF00 == keycodes .TypeMacroKey {
414+ // skip
415+ } else {
416+ kc = kc & 0x0FFF
417+ }
352418 }
353419 return kc
354420}
@@ -365,6 +431,12 @@ func (d *Device) SetKeycodeVia(layer, kbIndex, index int, key Keycode) {
365431 return
366432 }
367433 //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 {
368440 kc := key | 0xF000
369441
370442 switch key {
@@ -395,9 +467,11 @@ func (d *Device) SetKeycodeVia(layer, kbIndex, index int, key Keycode) {
395467 case keycodes .KeyRestoreDefaultKeymap :
396468 kc = keycodes .KeyRestoreDefaultKeymap
397469 default :
470+ if key & 0xFF00 == keycodes .TypeMacroKey {
471+ kc = key
472+ }
398473 }
399-
400- d .kb [kbIndex ].SetKeycode (layer , index , kc )
474+ return kc
401475}
402476
403477func (d * Device ) Layer () int {
@@ -468,6 +542,10 @@ func (k *Keyboard) Down(c k.Keycode) error {
468542 return nil
469543}
470544
545+ func (k * Keyboard ) Write (b []byte ) (n int , err error ) {
546+ return k .Port .Write (b )
547+ }
548+
471549// UartTxKeyboard is a keyboard that simply sends row/col corresponding to key
472550// placement via UART. For instructions on how to set it up, see bellow.
473551//
@@ -506,3 +584,7 @@ func (k *UartTxKeyboard) Down(c k.Keycode) error {
506584 }
507585 return nil
508586}
587+
588+ func (k * UartTxKeyboard ) Write (b []byte ) (n int , err error ) {
589+ return len (b ), nil
590+ }
0 commit comments