diff --git a/kbsquaredmatrix.go b/kbsquaredmatrix.go index f77be47..133970a 100644 --- a/kbsquaredmatrix.go +++ b/kbsquaredmatrix.go @@ -14,6 +14,8 @@ type SquaredMatrixKeyboard struct { Pins []machine.Pin cycleCounter []uint8 debounce uint8 + + colsBuf []int } func (d *Device) AddSquaredMatrixKeyboard(pins []machine.Pin, keys [][]Keycode) *SquaredMatrixKeyboard { @@ -41,6 +43,7 @@ func (d *Device) AddSquaredMatrixKeyboard(pins []machine.Pin, keys [][]Keycode) callback: func(layer, index int, state State) {}, cycleCounter: cycleCnt, debounce: 8, + colsBuf: make([]int, len(pins)), } d.kb = append(d.kb, k) @@ -59,7 +62,7 @@ func (d *SquaredMatrixKeyboard) Callback(layer, index int, state State) { func (d *SquaredMatrixKeyboard) Get() []State { c := int(0) - cols := []int{} + cols := d.colsBuf[:0] for i := range d.Pins { for j := range d.Pins { d.Pins[j].Configure(machine.PinConfig{Mode: machine.PinInputPullup}) diff --git a/keyboard.go b/keyboard.go index 06dd481..58b66f7 100644 --- a/keyboard.go +++ b/keyboard.go @@ -39,6 +39,9 @@ type Device struct { combosReleased []uint32 combosKey uint32 combosFounds []Keycode + + pressToReleaseBuf []uint32 + noneToPressBuf []uint32 } type KBer interface { @@ -83,6 +86,9 @@ func New() *Device { combosReleased: make([]uint32, 0, 10), combosKey: 0xFFFFFFFF, combosFounds: make([]Keycode, 10), + + pressToReleaseBuf: make([]uint32, 0, 20), + noneToPressBuf: make([]uint32, 0, 20), } SetDevice(d) @@ -192,7 +198,7 @@ func (d *Device) GetMaxKeyCount() int { } func (d *Device) Tick() error { - pressToRelease := []uint32{} + pressToRelease := d.pressToReleaseBuf[:0] select { case <-d.flashCh: @@ -210,7 +216,7 @@ func (d *Device) Tick() error { } // read from key matrix - noneToPress := []uint32{} + noneToPress := d.noneToPressBuf[:0] for kbidx, k := range d.kb { state := k.Get() for i := range state {