From 5c87b85efebce70715a553144c23cd12126fdf8b Mon Sep 17 00:00:00 2001 From: sago35 Date: Tue, 30 Jan 2024 21:53:36 +0900 Subject: [PATCH 1/2] 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 { From 2c002914db373c72fa68ac0707d507ba7598ce50 Mon Sep 17 00:00:00 2001 From: sago35 Date: Tue, 30 Jan 2024 21:56:39 +0900 Subject: [PATCH 2/2] Update ./targets --- targets/macropad-rp2040/main.go | 2 +- targets/sg48key/main.go | 16 ++++++++++------ targets/sgh60/main.go | 17 ++++++++++------- targets/xiao-kb01/main.go | 12 ++++++++---- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/targets/macropad-rp2040/main.go b/targets/macropad-rp2040/main.go index 0a72f53..cd4b41e 100644 --- a/targets/macropad-rp2040/main.go +++ b/targets/macropad-rp2040/main.go @@ -126,7 +126,7 @@ func run() error { return err } ws.WriteColors(wsLeds[:]) - time.Sleep(10 * time.Millisecond) + time.Sleep(1 * time.Millisecond) } return nil diff --git a/targets/sg48key/main.go b/targets/sg48key/main.go index 6d6cf15..c709335 100644 --- a/targets/sg48key/main.go +++ b/targets/sg48key/main.go @@ -90,18 +90,22 @@ func run() error { cont := true x := NewADCDevice(ax, 0x2000, 0xDC00, true) y := NewADCDevice(ay, 0x2400, 0xD400, true) + ticker := time.Tick(1 * time.Millisecond) + cnt := 0 for cont { + <-ticker err := d.Tick() if err != nil { return err } - xx := x.Get2() - yy := y.Get2() - //fmt.Printf("%04X %04X %4d %4d %4d %4d\n", x.RawValue, y.RawValue, xx, yy, x.Get(), y.Get()) - m.Move(int(xx), int(yy)) - - time.Sleep(10 * time.Millisecond) + if cnt%10 == 0 { + xx := x.Get2() + yy := y.Get2() + //fmt.Printf("%04X %04X %4d %4d %4d %4d\n", x.RawValue, y.RawValue, xx, yy, x.Get(), y.Get()) + m.Move(int(xx), int(yy)) + } + cnt++ } return nil diff --git a/targets/sgh60/main.go b/targets/sgh60/main.go index 2ffc558..d746f85 100644 --- a/targets/sgh60/main.go +++ b/targets/sgh60/main.go @@ -83,19 +83,22 @@ func run() error { cont := true x := NewADCDevice(ax, 0x3400, 0xD400, true) y := NewADCDevice(ay, 0x3800, 0xE990, true) + ticker := time.Tick(1 * time.Millisecond) + cnt := 0 for cont { + <-ticker err := d.Tick() if err != nil { return err } - xx := x.Get2() - yy := y.Get2() - - fmt.Printf("%04X %04X %4d %4d %4d %4d\n", x.RawValue, y.RawValue, xx, yy, x.Get(), y.Get()) - m.Move(int(xx), int(yy)) - - time.Sleep(10 * time.Millisecond) + if cnt%10 == 0 { + xx := x.Get2() + yy := y.Get2() + //fmt.Printf("%04X %04X %4d %4d %4d %4d\n", x.RawValue, y.RawValue, xx, yy, x.Get(), y.Get()) + m.Move(int(xx), int(yy)) + } + cnt++ } return nil diff --git a/targets/xiao-kb01/main.go b/targets/xiao-kb01/main.go index f2e25d9..7f16239 100644 --- a/targets/xiao-kb01/main.go +++ b/targets/xiao-kb01/main.go @@ -111,17 +111,21 @@ func run() error { } cont := true - ticker := time.Tick(4 * time.Millisecond) + ticker := time.Tick(1 * time.Millisecond) + cnt := 0 for cont { <-ticker err := d.Tick() if err != nil { return err } - if changed.Get() != 0 { - ws.WriteColors(wsLeds[:]) - changed.Set(0) + if cnt%4 == 0 { + if changed.Get() != 0 { + ws.WriteColors(wsLeds[:]) + changed.Set(0) + } } + cnt++ } return nil