-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
kbsquaredmatrix.go
144 lines (124 loc) · 2.88 KB
/
kbsquaredmatrix.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
//go:build tinygo
package keyboard
import (
"machine"
)
type SquaredMatrixKeyboard struct {
State []State
Keys [][]Keycode
callback Callback
Pins []machine.Pin
cycleCounter []uint8
debounce uint8
colsBuf []int
}
func (d *Device) AddSquaredMatrixKeyboard(pins []machine.Pin, keys [][]Keycode) *SquaredMatrixKeyboard {
state := make([]State, len(pins)*(len(pins)-1))
cycleCnt := make([]uint8, len(state))
for i := range pins {
pins[i].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 := &SquaredMatrixKeyboard{
Pins: pins,
State: state,
Keys: keydef,
callback: func(layer, index int, state State) {},
cycleCounter: cycleCnt,
debounce: 8,
colsBuf: make([]int, len(pins)),
}
d.kb = append(d.kb, k)
return k
}
func (d *SquaredMatrixKeyboard) SetCallback(fn Callback) {
d.callback = fn
}
func (d *SquaredMatrixKeyboard) Callback(layer, index int, state State) {
if d.callback != nil {
d.callback(layer, index, state)
}
}
func (d *SquaredMatrixKeyboard) Get() []State {
c := int(0)
cols := d.colsBuf[:0]
for i := range d.Pins {
for j := range d.Pins {
d.Pins[j].Configure(machine.PinConfig{Mode: machine.PinInputPullup})
if i == j {
c = j
} else {
cols = append(cols, j)
}
}
for r, j := range cols {
d.Pins[j].Configure(machine.PinConfig{Mode: machine.PinOutput})
d.Pins[j].Low()
current := !d.Pins[c].Get()
idx := r*(len(cols)+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.Pins[j].Configure(machine.PinConfig{Mode: machine.PinInputPullup})
}
cols = cols[:0]
}
return d.State
}
func (d *SquaredMatrixKeyboard) 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 *SquaredMatrixKeyboard) SetKeycode(layer, index int, key Keycode) {
if layer >= LayerCount {
return
}
if index >= len(d.Keys[layer]) {
return
}
d.Keys[layer][index] = key
}
func (d *SquaredMatrixKeyboard) GetKeyCount() int {
return len(d.State)
}
func (d *SquaredMatrixKeyboard) Init() error {
return nil
}