Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation for IO-Expanders #44

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions kbexpander.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
//go:build tinygo

package keyboard

import (
"tinygo.org/x/drivers/mcp23017"
)

type ExpanderKeyboard struct {
State []State
Keys [][]Keycode
options Options
callback Callback

Col []int
Row []int
cycleCounter []uint8
debounce uint8
expander *mcp23017.Device
}

func (d *Device) AddExpanderKeyboard(expanderDevice *mcp23017.Device, colPins, rowPins []int, keys [][]Keycode, opt ...Option) *ExpanderKeyboard {
col := len(colPins)
row := len(rowPins)
state := make([]State, row*col)
cycleCnt := make([]uint8, len(state))

o := Options{}
for _, f := range opt {
f(&o)
}

for _, c := range colPins {

if !o.InvertDiode {
_ = expanderDevice.Pin(c).SetMode(mcp23017.Output)
_ = expanderDevice.Pin(c).Set(true)
} else {
_ = expanderDevice.Pin(c).SetMode(mcp23017.Input | mcp23017.Pullup)
}
Comment on lines +35 to +40
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason you are using Pullup here? I think the PR can be merged as it is now.
Depending on the reason, it might be necessary to create a PR in the future that allows for selecting either Pullup or Pulldown, including for kbmatrix.go.

I have one more request. Could you share the expected circuit diagram? kbmatrix.go assumes the following. I assume it's the same circuit, just differing in whether to use pullup or pulldown.

image
https://kicanvas.org/?github=https%3A%2F%2Fgithub.com%2Fsago35%2Ftinygo-keyboard%2Ftree%2Fmain%2Fkicad%2Ffric10key%2Ffric10key


}
for _, r := range rowPins {
if !o.InvertDiode {
_ = expanderDevice.Pin(r).SetMode(mcp23017.Input | mcp23017.Pullup)
} else {
_ = expanderDevice.Pin(r).SetMode(mcp23017.Output)
_ = expanderDevice.Pin(r).Set(true)
}
}

keydef := make([][]Keycode, LayerCount)
for l := 0; l < len(keydef); l++ {
keydef[l] = make([]Keycode, len(state))
}
for l := 0; l < len(keys); l++ {
for kc := 0; kc < len(keys[l]); kc++ {
keydef[l][kc] = keys[l][kc]
}
}

k := &ExpanderKeyboard{
Col: colPins,
Row: rowPins,
State: state,
Keys: keydef,
options: o,
callback: func(layer, index int, state State) {},
cycleCounter: cycleCnt,
debounce: 2,
expander: expanderDevice,
}
d.kb = append(d.kb, k)
return k
}

func (d *ExpanderKeyboard) SetCallback(fn Callback) {
d.callback = fn
}

func (d *ExpanderKeyboard) Callback(layer, index int, state State) {
if d.callback != nil {
d.callback(layer, index, state)
}
}

func (d *ExpanderKeyboard) Get() []State {
for cIdx, c := range d.Col {
for rIdx, r := range d.Row {
current := false
if !d.options.InvertDiode {
_ = d.expander.Pin(c).Set(false)
current, _ = d.expander.Pin(r).Get()
} else {
_ = d.expander.Pin(r).Set(false)
current, _ = d.expander.Pin(c).Get()
}
idx := rIdx*len(d.Col) + cIdx
switch d.State[idx] {
case None:
if !current {
if d.cycleCounter[idx] >= d.debounce {
d.State[idx] = NoneToPress
d.cycleCounter[idx] = 0
} else {
d.cycleCounter[idx]++
}
} else {
d.cycleCounter[idx] = 0
}
case NoneToPress:
d.State[idx] = Press
case Press:
if !current {
d.cycleCounter[idx] = 0
} else {
if d.cycleCounter[idx] >= d.debounce {
d.State[idx] = PressToRelease
d.cycleCounter[idx] = 0
} else {
d.cycleCounter[idx]++
}
}
case PressToRelease:
d.State[idx] = None
}
if !d.options.InvertDiode {
_ = d.expander.Pin(c).Set(true)
} else {
_ = d.expander.Pin(r).Set(true)
}
}
}

return d.State
}

func (d *ExpanderKeyboard) Key(layer, index int) Keycode {
if layer >= LayerCount {
return 0
}
if index >= len(d.Keys[layer]) {
return 0
}
return d.Keys[layer][index]
}

func (d *ExpanderKeyboard) SetKeycode(layer, index int, key Keycode) {
if layer >= LayerCount {
return
}
if index >= len(d.Keys[layer]) {
return
}
d.Keys[layer][index] = key
}

func (d *ExpanderKeyboard) GetKeyCount() int {
return len(d.State)
}

func (d *ExpanderKeyboard) Init() error {
return nil
}