Skip to content

Commit b0f9333

Browse files
authored
Merge pull request #28 from sago35/debounce
Change processing interval to 1ms and debounce handling to 1ms x 8 times
2 parents 8ee49a2 + 2c00291 commit b0f9333

File tree

9 files changed

+48
-37
lines changed

9 files changed

+48
-37
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 {

targets/macropad-rp2040/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func run() error {
126126
return err
127127
}
128128
ws.WriteColors(wsLeds[:])
129-
time.Sleep(10 * time.Millisecond)
129+
time.Sleep(1 * time.Millisecond)
130130
}
131131

132132
return nil

targets/sg48key/main.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,22 @@ func run() error {
9090
cont := true
9191
x := NewADCDevice(ax, 0x2000, 0xDC00, true)
9292
y := NewADCDevice(ay, 0x2400, 0xD400, true)
93+
ticker := time.Tick(1 * time.Millisecond)
94+
cnt := 0
9395
for cont {
96+
<-ticker
9497
err := d.Tick()
9598
if err != nil {
9699
return err
97100
}
98101

99-
xx := x.Get2()
100-
yy := y.Get2()
101-
//fmt.Printf("%04X %04X %4d %4d %4d %4d\n", x.RawValue, y.RawValue, xx, yy, x.Get(), y.Get())
102-
m.Move(int(xx), int(yy))
103-
104-
time.Sleep(10 * time.Millisecond)
102+
if cnt%10 == 0 {
103+
xx := x.Get2()
104+
yy := y.Get2()
105+
//fmt.Printf("%04X %04X %4d %4d %4d %4d\n", x.RawValue, y.RawValue, xx, yy, x.Get(), y.Get())
106+
m.Move(int(xx), int(yy))
107+
}
108+
cnt++
105109
}
106110

107111
return nil

targets/sgh60/main.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,22 @@ func run() error {
8383
cont := true
8484
x := NewADCDevice(ax, 0x3400, 0xD400, true)
8585
y := NewADCDevice(ay, 0x3800, 0xE990, true)
86+
ticker := time.Tick(1 * time.Millisecond)
87+
cnt := 0
8688
for cont {
89+
<-ticker
8790
err := d.Tick()
8891
if err != nil {
8992
return err
9093
}
9194

92-
xx := x.Get2()
93-
yy := y.Get2()
94-
95-
fmt.Printf("%04X %04X %4d %4d %4d %4d\n", x.RawValue, y.RawValue, xx, yy, x.Get(), y.Get())
96-
m.Move(int(xx), int(yy))
97-
98-
time.Sleep(10 * time.Millisecond)
95+
if cnt%10 == 0 {
96+
xx := x.Get2()
97+
yy := y.Get2()
98+
//fmt.Printf("%04X %04X %4d %4d %4d %4d\n", x.RawValue, y.RawValue, xx, yy, x.Get(), y.Get())
99+
m.Move(int(xx), int(yy))
100+
}
101+
cnt++
99102
}
100103

101104
return nil

targets/xiao-kb01/main.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,21 @@ func run() error {
111111
}
112112

113113
cont := true
114-
ticker := time.Tick(4 * time.Millisecond)
114+
ticker := time.Tick(1 * time.Millisecond)
115+
cnt := 0
115116
for cont {
116117
<-ticker
117118
err := d.Tick()
118119
if err != nil {
119120
return err
120121
}
121-
if changed.Get() != 0 {
122-
ws.WriteColors(wsLeds[:])
123-
changed.Set(0)
122+
if cnt%4 == 0 {
123+
if changed.Get() != 0 {
124+
ws.WriteColors(wsLeds[:])
125+
changed.Set(0)
126+
}
124127
}
128+
cnt++
125129
}
126130

127131
return nil

0 commit comments

Comments
 (0)