-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
kbmatrix.go
156 lines (138 loc) · 3.21 KB
/
kbmatrix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//go:build tinygo
package keyboard
import (
"machine"
)
type MatrixKeyboard struct {
State []State
Keys [][]Keycode
options Options
callback Callback
Col []machine.Pin
Row []machine.Pin
cycleCounter []uint8
debounce uint8
}
func (d *Device) AddMatrixKeyboard(colPins, rowPins []machine.Pin, keys [][]Keycode, opt ...Option) *MatrixKeyboard {
col := len(colPins)
row := len(rowPins)
state := make([]State, row*col)
cycleCnt := make([]uint8, len(state))
for c := range colPins {
colPins[c].Configure(machine.PinConfig{Mode: machine.PinInputPulldown})
}
for r := range rowPins {
rowPins[r].Configure(machine.PinConfig{Mode: machine.PinInputPulldown})
}
o := Options{}
for _, f := range opt {
f(&o)
}
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 := &MatrixKeyboard{
Col: colPins,
Row: rowPins,
State: state,
Keys: keydef,
options: o,
callback: func(layer, index int, state State) {},
cycleCounter: cycleCnt,
debounce: 8,
}
d.kb = append(d.kb, k)
return k
}
func (d *MatrixKeyboard) SetCallback(fn Callback) {
d.callback = fn
}
func (d *MatrixKeyboard) Callback(layer, index int, state State) {
if d.callback != nil {
d.callback(layer, index, state)
}
}
func (d *MatrixKeyboard) Get() []State {
for c := range d.Col {
for r := range d.Row {
//d.State[r*len(d.Col)+c] = d.Row[r].Get()
current := false
if !d.options.InvertDiode {
d.Col[c].Configure(machine.PinConfig{Mode: machine.PinOutput})
d.Col[c].High()
current = d.Row[r].Get()
} else {
d.Row[r].Configure(machine.PinConfig{Mode: machine.PinOutput})
d.Row[r].High()
current = d.Col[c].Get()
}
idx := r*len(d.Col) + c
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.Col[c].Low()
d.Col[c].Configure(machine.PinConfig{Mode: machine.PinInputPulldown})
} else {
d.Row[r].Low()
d.Row[r].Configure(machine.PinConfig{Mode: machine.PinInputPulldown})
}
}
}
return d.State
}
func (d *MatrixKeyboard) 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 *MatrixKeyboard) SetKeycode(layer, index int, key Keycode) {
if layer >= LayerCount {
return
}
if index >= len(d.Keys[layer]) {
return
}
d.Keys[layer][index] = key
}
func (d *MatrixKeyboard) GetKeyCount() int {
return len(d.State)
}
func (d *MatrixKeyboard) Init() error {
return nil
}