From 5c87b85efebce70715a553144c23cd12126fdf8b Mon Sep 17 00:00:00 2001 From: sago35 Date: Tue, 30 Jan 2024 21:53:36 +0900 Subject: [PATCH] Change processing interval to 1ms and debounce handling to 1ms x 8 times --- kbduplexmatrix.go | 12 ++++++------ kbgpio.go | 8 ++++---- kbmatrix.go | 8 ++++---- kbsquaredmatrix.go | 8 ++++---- keyboard.go | 2 +- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/kbduplexmatrix.go b/kbduplexmatrix.go index 4ea1070..c0f637d 100644 --- a/kbduplexmatrix.go +++ b/kbduplexmatrix.go @@ -14,10 +14,9 @@ type DuplexMatrixKeyboard struct { Col []machine.Pin Row []machine.Pin cycleCounter []uint8 + debounce uint8 } -const duplexMatrixCyclesToPreventChattering = uint8(4) - func (d *Device) AddDuplexMatrixKeyboard(colPins, rowPins []machine.Pin, keys [][]Keycode) *DuplexMatrixKeyboard { col := len(colPins) row := len(rowPins) @@ -49,6 +48,7 @@ func (d *Device) AddDuplexMatrixKeyboard(colPins, rowPins []machine.Pin, keys [] State: state, Keys: keydef, callback: func(layer, index int, state State) {}, + debounce: 8, } d.kb = append(d.kb, k) @@ -75,7 +75,7 @@ func (d *DuplexMatrixKeyboard) Get() []State { switch d.State[idx] { case None: if current { - if d.cycleCounter[idx] >= duplexMatrixCyclesToPreventChattering { + if d.cycleCounter[idx] >= d.debounce { d.State[idx] = NoneToPress d.cycleCounter[idx] = 0 } else { @@ -90,7 +90,7 @@ func (d *DuplexMatrixKeyboard) Get() []State { if current { d.cycleCounter[idx] = 0 } else { - if d.cycleCounter[idx] >= duplexMatrixCyclesToPreventChattering { + if d.cycleCounter[idx] >= d.debounce { d.State[idx] = PressToRelease d.cycleCounter[idx] = 0 } else { @@ -114,7 +114,7 @@ func (d *DuplexMatrixKeyboard) Get() []State { switch d.State[idx] { case None: if current { - if d.cycleCounter[idx] >= duplexMatrixCyclesToPreventChattering { + if d.cycleCounter[idx] >= d.debounce { d.State[idx] = NoneToPress d.cycleCounter[idx] = 0 } else { @@ -129,7 +129,7 @@ func (d *DuplexMatrixKeyboard) Get() []State { if current { d.cycleCounter[idx] = 0 } else { - if d.cycleCounter[idx] >= duplexMatrixCyclesToPreventChattering { + if d.cycleCounter[idx] >= d.debounce { d.State[idx] = PressToRelease d.cycleCounter[idx] = 0 } else { diff --git a/kbgpio.go b/kbgpio.go index ad9c1c3..c3d78f6 100644 --- a/kbgpio.go +++ b/kbgpio.go @@ -14,10 +14,9 @@ type GpioKeyboard struct { Col []machine.Pin cycleCounter []uint8 + debounce uint8 } -const gpioCyclesToPreventChattering = uint8(4) - func (d *Device) AddGpioKeyboard(pins []machine.Pin, keys [][]Keycode, opt ...Option) *GpioKeyboard { col := len(pins) state := make([]State, col) @@ -47,6 +46,7 @@ func (d *Device) AddGpioKeyboard(pins []machine.Pin, keys [][]Keycode, opt ...Op options: o, callback: func(layer, index int, state State) {}, cycleCounter: cycleCnt, + debounce: 8, } d.kb = append(d.kb, k) @@ -74,7 +74,7 @@ func (d *GpioKeyboard) Get() []State { switch d.State[c] { case None: if current { - if d.cycleCounter[c] >= gpioCyclesToPreventChattering { + if d.cycleCounter[c] >= d.debounce { d.State[c] = NoneToPress d.cycleCounter[c] = 0 } else { @@ -89,7 +89,7 @@ func (d *GpioKeyboard) Get() []State { if current { d.cycleCounter[c] = 0 } else { - if d.cycleCounter[c] >= gpioCyclesToPreventChattering { + if d.cycleCounter[c] >= d.debounce { d.State[c] = PressToRelease d.cycleCounter[c] = 0 } else { diff --git a/kbmatrix.go b/kbmatrix.go index 2b1d07d..a10be33 100644 --- a/kbmatrix.go +++ b/kbmatrix.go @@ -15,10 +15,9 @@ type MatrixKeyboard struct { Col []machine.Pin Row []machine.Pin cycleCounter []uint8 + debounce uint8 } -const matrixCyclesToPreventChattering = uint8(4) - func (d *Device) AddMatrixKeyboard(colPins, rowPins []machine.Pin, keys [][]Keycode, opt ...Option) *MatrixKeyboard { col := len(colPins) row := len(rowPins) @@ -55,6 +54,7 @@ func (d *Device) AddMatrixKeyboard(colPins, rowPins []machine.Pin, keys [][]Keyc options: o, callback: func(layer, index int, state State) {}, cycleCounter: cycleCnt, + debounce: 8, } d.kb = append(d.kb, k) @@ -89,7 +89,7 @@ func (d *MatrixKeyboard) Get() []State { switch d.State[idx] { case None: if current { - if d.cycleCounter[idx] >= matrixCyclesToPreventChattering { + if d.cycleCounter[idx] >= d.debounce { d.State[idx] = NoneToPress d.cycleCounter[idx] = 0 } else { @@ -104,7 +104,7 @@ func (d *MatrixKeyboard) Get() []State { if current { d.cycleCounter[idx] = 0 } else { - if d.cycleCounter[idx] >= matrixCyclesToPreventChattering { + if d.cycleCounter[idx] >= d.debounce { d.State[idx] = PressToRelease d.cycleCounter[idx] = 0 } else { diff --git a/kbsquaredmatrix.go b/kbsquaredmatrix.go index cfe5fc0..f77be47 100644 --- a/kbsquaredmatrix.go +++ b/kbsquaredmatrix.go @@ -13,10 +13,9 @@ type SquaredMatrixKeyboard struct { Pins []machine.Pin cycleCounter []uint8 + debounce uint8 } -const squaredMatrixCyclesToPreventChattering = uint8(4) - func (d *Device) AddSquaredMatrixKeyboard(pins []machine.Pin, keys [][]Keycode) *SquaredMatrixKeyboard { state := make([]State, len(pins)*(len(pins)-1)) cycleCnt := make([]uint8, len(state)) @@ -41,6 +40,7 @@ func (d *Device) AddSquaredMatrixKeyboard(pins []machine.Pin, keys [][]Keycode) Keys: keydef, callback: func(layer, index int, state State) {}, cycleCounter: cycleCnt, + debounce: 8, } d.kb = append(d.kb, k) @@ -79,7 +79,7 @@ func (d *SquaredMatrixKeyboard) Get() []State { switch d.State[idx] { case None: if current { - if d.cycleCounter[idx] >= squaredMatrixCyclesToPreventChattering { + if d.cycleCounter[idx] >= d.debounce { d.State[idx] = NoneToPress d.cycleCounter[idx] = 0 } else { @@ -94,7 +94,7 @@ func (d *SquaredMatrixKeyboard) Get() []State { if current { d.cycleCounter[idx] = 0 } else { - if d.cycleCounter[idx] >= squaredMatrixCyclesToPreventChattering { + if d.cycleCounter[idx] >= d.debounce { d.State[idx] = PressToRelease d.cycleCounter[idx] = 0 } else { diff --git a/keyboard.go b/keyboard.go index 348c225..e6ab636 100644 --- a/keyboard.go +++ b/keyboard.go @@ -286,7 +286,7 @@ func (d *Device) Loop(ctx context.Context) error { return err } - ticker := time.Tick(5 * time.Millisecond) + ticker := time.Tick(1 * time.Millisecond) cont := true for cont { select {