Skip to content

Commit

Permalink
Change processing interval to 1ms and debounce handling to 1ms x 8 times
Browse files Browse the repository at this point in the history
  • Loading branch information
sago35 committed Jan 30, 2024
1 parent 8ee49a2 commit 5c87b85
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 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

0 comments on commit 5c87b85

Please sign in to comment.