-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
kbduplexmatrix.go
176 lines (158 loc) · 3.7 KB
/
kbduplexmatrix.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//go:build tinygo
package keyboard
import (
"machine"
)
type DuplexMatrixKeyboard struct {
State []State
Keys [][]Keycode
callback Callback
Col []machine.Pin
Row []machine.Pin
cycleCounter []uint8
debounce uint8
}
func (d *Device) AddDuplexMatrixKeyboard(colPins, rowPins []machine.Pin, keys [][]Keycode) *DuplexMatrixKeyboard {
col := len(colPins)
row := len(rowPins)
state := make([]State, row*2*col)
cycleCnt := make([]uint8, len(state))
for c := range colPins {
colPins[c].Configure(machine.PinConfig{Mode: machine.PinOutput})
colPins[c].High()
}
for r := range rowPins {
rowPins[r].Configure(machine.PinConfig{Mode: machine.PinInputPullup})
}
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 := &DuplexMatrixKeyboard{
Col: colPins,
Row: rowPins,
cycleCounter: cycleCnt,
State: state,
Keys: keydef,
callback: func(layer, index int, state State) {},
debounce: 8,
}
d.kb = append(d.kb, k)
return k
}
func (d *DuplexMatrixKeyboard) SetCallback(fn Callback) {
d.callback = fn
}
func (d *DuplexMatrixKeyboard) Callback(layer, index int, state State) {
if d.callback != nil {
d.callback(layer, index, state)
}
}
func (d *DuplexMatrixKeyboard) Get() []State {
for c := range d.Col {
d.Col[c].Configure(machine.PinConfig{Mode: machine.PinOutput})
d.Col[c].Low()
for r := range d.Row {
current := !d.Row[r].Get()
idx := r*2*len(d.Col) + 2*len(d.Col) - 1 - 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
}
}
d.Col[c].High()
d.Col[c].Configure(machine.PinConfig{Mode: machine.PinInputPullup})
}
for r := range d.Row {
d.Row[r].Configure(machine.PinConfig{Mode: machine.PinOutput})
d.Row[r].Low()
for c := range d.Col {
current := !d.Col[c].Get()
idx := r*2*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
}
}
d.Row[r].High()
d.Row[r].Configure(machine.PinConfig{Mode: machine.PinInputPullup})
}
return d.State
}
func (d *DuplexMatrixKeyboard) 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 *DuplexMatrixKeyboard) SetKeycode(layer, index int, key Keycode) {
if layer >= LayerCount {
return
}
if index >= len(d.Keys[layer]) {
return
}
d.Keys[layer][index] = key
}
func (d *DuplexMatrixKeyboard) GetKeyCount() int {
return len(d.State)
}
func (d *DuplexMatrixKeyboard) Init() error {
return nil
}