Skip to content

Commit 5c87b85

Browse files
committed
Change processing interval to 1ms and debounce handling to 1ms x 8 times
1 parent 8ee49a2 commit 5c87b85

File tree

5 files changed

+19
-19
lines changed

5 files changed

+19
-19
lines changed

kbduplexmatrix.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ type DuplexMatrixKeyboard struct {
1414
Col []machine.Pin
1515
Row []machine.Pin
1616
cycleCounter []uint8
17+
debounce uint8
1718
}
1819

19-
const duplexMatrixCyclesToPreventChattering = uint8(4)
20-
2120
func (d *Device) AddDuplexMatrixKeyboard(colPins, rowPins []machine.Pin, keys [][]Keycode) *DuplexMatrixKeyboard {
2221
col := len(colPins)
2322
row := len(rowPins)
@@ -49,6 +48,7 @@ func (d *Device) AddDuplexMatrixKeyboard(colPins, rowPins []machine.Pin, keys []
4948
State: state,
5049
Keys: keydef,
5150
callback: func(layer, index int, state State) {},
51+
debounce: 8,
5252
}
5353

5454
d.kb = append(d.kb, k)
@@ -75,7 +75,7 @@ func (d *DuplexMatrixKeyboard) Get() []State {
7575
switch d.State[idx] {
7676
case None:
7777
if current {
78-
if d.cycleCounter[idx] >= duplexMatrixCyclesToPreventChattering {
78+
if d.cycleCounter[idx] >= d.debounce {
7979
d.State[idx] = NoneToPress
8080
d.cycleCounter[idx] = 0
8181
} else {
@@ -90,7 +90,7 @@ func (d *DuplexMatrixKeyboard) Get() []State {
9090
if current {
9191
d.cycleCounter[idx] = 0
9292
} else {
93-
if d.cycleCounter[idx] >= duplexMatrixCyclesToPreventChattering {
93+
if d.cycleCounter[idx] >= d.debounce {
9494
d.State[idx] = PressToRelease
9595
d.cycleCounter[idx] = 0
9696
} else {
@@ -114,7 +114,7 @@ func (d *DuplexMatrixKeyboard) Get() []State {
114114
switch d.State[idx] {
115115
case None:
116116
if current {
117-
if d.cycleCounter[idx] >= duplexMatrixCyclesToPreventChattering {
117+
if d.cycleCounter[idx] >= d.debounce {
118118
d.State[idx] = NoneToPress
119119
d.cycleCounter[idx] = 0
120120
} else {
@@ -129,7 +129,7 @@ func (d *DuplexMatrixKeyboard) Get() []State {
129129
if current {
130130
d.cycleCounter[idx] = 0
131131
} else {
132-
if d.cycleCounter[idx] >= duplexMatrixCyclesToPreventChattering {
132+
if d.cycleCounter[idx] >= d.debounce {
133133
d.State[idx] = PressToRelease
134134
d.cycleCounter[idx] = 0
135135
} else {

kbgpio.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ type GpioKeyboard struct {
1414

1515
Col []machine.Pin
1616
cycleCounter []uint8
17+
debounce uint8
1718
}
1819

19-
const gpioCyclesToPreventChattering = uint8(4)
20-
2120
func (d *Device) AddGpioKeyboard(pins []machine.Pin, keys [][]Keycode, opt ...Option) *GpioKeyboard {
2221
col := len(pins)
2322
state := make([]State, col)
@@ -47,6 +46,7 @@ func (d *Device) AddGpioKeyboard(pins []machine.Pin, keys [][]Keycode, opt ...Op
4746
options: o,
4847
callback: func(layer, index int, state State) {},
4948
cycleCounter: cycleCnt,
49+
debounce: 8,
5050
}
5151

5252
d.kb = append(d.kb, k)
@@ -74,7 +74,7 @@ func (d *GpioKeyboard) Get() []State {
7474
switch d.State[c] {
7575
case None:
7676
if current {
77-
if d.cycleCounter[c] >= gpioCyclesToPreventChattering {
77+
if d.cycleCounter[c] >= d.debounce {
7878
d.State[c] = NoneToPress
7979
d.cycleCounter[c] = 0
8080
} else {
@@ -89,7 +89,7 @@ func (d *GpioKeyboard) Get() []State {
8989
if current {
9090
d.cycleCounter[c] = 0
9191
} else {
92-
if d.cycleCounter[c] >= gpioCyclesToPreventChattering {
92+
if d.cycleCounter[c] >= d.debounce {
9393
d.State[c] = PressToRelease
9494
d.cycleCounter[c] = 0
9595
} else {

kbmatrix.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ type MatrixKeyboard struct {
1515
Col []machine.Pin
1616
Row []machine.Pin
1717
cycleCounter []uint8
18+
debounce uint8
1819
}
1920

20-
const matrixCyclesToPreventChattering = uint8(4)
21-
2221
func (d *Device) AddMatrixKeyboard(colPins, rowPins []machine.Pin, keys [][]Keycode, opt ...Option) *MatrixKeyboard {
2322
col := len(colPins)
2423
row := len(rowPins)
@@ -55,6 +54,7 @@ func (d *Device) AddMatrixKeyboard(colPins, rowPins []machine.Pin, keys [][]Keyc
5554
options: o,
5655
callback: func(layer, index int, state State) {},
5756
cycleCounter: cycleCnt,
57+
debounce: 8,
5858
}
5959

6060
d.kb = append(d.kb, k)
@@ -89,7 +89,7 @@ func (d *MatrixKeyboard) Get() []State {
8989
switch d.State[idx] {
9090
case None:
9191
if current {
92-
if d.cycleCounter[idx] >= matrixCyclesToPreventChattering {
92+
if d.cycleCounter[idx] >= d.debounce {
9393
d.State[idx] = NoneToPress
9494
d.cycleCounter[idx] = 0
9595
} else {
@@ -104,7 +104,7 @@ func (d *MatrixKeyboard) Get() []State {
104104
if current {
105105
d.cycleCounter[idx] = 0
106106
} else {
107-
if d.cycleCounter[idx] >= matrixCyclesToPreventChattering {
107+
if d.cycleCounter[idx] >= d.debounce {
108108
d.State[idx] = PressToRelease
109109
d.cycleCounter[idx] = 0
110110
} else {

kbsquaredmatrix.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ type SquaredMatrixKeyboard struct {
1313

1414
Pins []machine.Pin
1515
cycleCounter []uint8
16+
debounce uint8
1617
}
1718

18-
const squaredMatrixCyclesToPreventChattering = uint8(4)
19-
2019
func (d *Device) AddSquaredMatrixKeyboard(pins []machine.Pin, keys [][]Keycode) *SquaredMatrixKeyboard {
2120
state := make([]State, len(pins)*(len(pins)-1))
2221
cycleCnt := make([]uint8, len(state))
@@ -41,6 +40,7 @@ func (d *Device) AddSquaredMatrixKeyboard(pins []machine.Pin, keys [][]Keycode)
4140
Keys: keydef,
4241
callback: func(layer, index int, state State) {},
4342
cycleCounter: cycleCnt,
43+
debounce: 8,
4444
}
4545

4646
d.kb = append(d.kb, k)
@@ -79,7 +79,7 @@ func (d *SquaredMatrixKeyboard) Get() []State {
7979
switch d.State[idx] {
8080
case None:
8181
if current {
82-
if d.cycleCounter[idx] >= squaredMatrixCyclesToPreventChattering {
82+
if d.cycleCounter[idx] >= d.debounce {
8383
d.State[idx] = NoneToPress
8484
d.cycleCounter[idx] = 0
8585
} else {
@@ -94,7 +94,7 @@ func (d *SquaredMatrixKeyboard) Get() []State {
9494
if current {
9595
d.cycleCounter[idx] = 0
9696
} else {
97-
if d.cycleCounter[idx] >= squaredMatrixCyclesToPreventChattering {
97+
if d.cycleCounter[idx] >= d.debounce {
9898
d.State[idx] = PressToRelease
9999
d.cycleCounter[idx] = 0
100100
} else {

keyboard.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func (d *Device) Loop(ctx context.Context) error {
286286
return err
287287
}
288288

289-
ticker := time.Tick(5 * time.Millisecond)
289+
ticker := time.Tick(1 * time.Millisecond)
290290
cont := true
291291
for cont {
292292
select {

0 commit comments

Comments
 (0)