-
-
Notifications
You must be signed in to change notification settings - Fork 16
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} | ||
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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
https://kicanvas.org/?github=https%3A%2F%2Fgit.luolix.top%2Fsago35%2Ftinygo-keyboard%2Ftree%2Fmain%2Fkicad%2Ffric10key%2Ffric10key