Skip to content

Commit

Permalink
Merge pull request #28 from sago35/debounce
Browse files Browse the repository at this point in the history
Change processing interval to 1ms and debounce handling to 1ms x 8 times
  • Loading branch information
sago35 authored Jan 30, 2024
2 parents 8ee49a2 + 2c00291 commit b0f9333
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 37 deletions.
12 changes: 6 additions & 6 deletions kbduplexmatrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions kbgpio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions kbmatrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions kbsquaredmatrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion targets/macropad-rp2040/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func run() error {
return err
}
ws.WriteColors(wsLeds[:])
time.Sleep(10 * time.Millisecond)
time.Sleep(1 * time.Millisecond)
}

return nil
Expand Down
16 changes: 10 additions & 6 deletions targets/sg48key/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 10 additions & 7 deletions targets/sgh60/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions targets/xiao-kb01/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b0f9333

Please sign in to comment.