-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
kbshifter.go
123 lines (103 loc) · 2.14 KB
/
kbshifter.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
//go:build tinygo
package keyboard
import (
"tinygo.org/x/drivers/shifter"
)
type ShifterKeyboard struct {
State []State
Keys [][]Keycode
options Options
callback Callback
Shifter shifter.Device
}
func (d *Device) AddShifterKeyboard(shifterDevice shifter.Device, keys [][]Keycode, opt ...Option) *ShifterKeyboard {
state := make([]State, len(shifterDevice.Pins))
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 := &ShifterKeyboard{
Shifter: shifterDevice,
State: state,
Keys: keydef,
options: o,
callback: func(layer, index int, state State) {},
}
d.kb = append(d.kb, k)
return k
}
func (d *ShifterKeyboard) SetCallback(fn Callback) {
d.callback = fn
}
func (d *ShifterKeyboard) Callback(layer, index int, state State) {
if d.callback != nil {
d.callback(layer, index, state)
}
}
func (d *ShifterKeyboard) Get() []State {
d.Shifter.Read8Input()
for c := 0; c < len(d.Shifter.Pins); c++ {
current := d.Shifter.Pins[c].Get()
if d.options.InvertButtonState {
current = !current
}
switch d.State[c] {
case None:
if current {
d.State[c] = NoneToPress
} else {
}
case NoneToPress:
if current {
d.State[c] = Press
} else {
d.State[c] = PressToRelease
}
case Press:
if current {
} else {
d.State[c] = PressToRelease
}
case PressToRelease:
if current {
d.State[c] = NoneToPress
} else {
d.State[c] = None
}
}
}
return d.State
}
func (d *ShifterKeyboard) 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 *ShifterKeyboard) SetKeycode(layer, index int, key Keycode) {
if layer >= LayerCount {
return
}
if index >= len(d.Keys[layer]) {
return
}
d.Keys[layer][index] = key
}
func (d *ShifterKeyboard) GetKeyCount() int {
return len(d.State)
}
func (d *ShifterKeyboard) Init() error {
return nil
}