-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathevent.go
More file actions
186 lines (157 loc) · 3.73 KB
/
event.go
File metadata and controls
186 lines (157 loc) · 3.73 KB
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
177
178
179
180
181
182
183
184
185
186
package streamdeck
import (
"fmt"
)
const DialMax = 100
type EventKind int
const (
EventUnknown = iota
EventKeyPressed
EventKeyReleased
EventDialPressed
EventDialReleased
EventDialTurn
)
func (ev EventKind) String() string {
switch ev {
case EventKeyPressed:
return "key-pressed"
case EventKeyReleased:
return "key-released"
case EventDialPressed:
return "dial-pressed"
case EventDialReleased:
return "dial-released"
case EventDialTurn:
return "dial-turn"
default:
return "unknown"
}
}
type Event struct {
Kind EventKind
Which int
}
func (e Event) String() string {
return fmt.Sprintf("%s:%d", e.Kind.String(), e.Which)
}
type State struct {
Keys []bool
DialPush []bool
DialPos []int // 0 -> 100
}
func (s *State) Update(c *Config, b []byte) ([]Event, error) {
if b[0] != 1 {
return nil, fmt.Errorf("why isn't it starting with 1, %v", b)
}
// see https://github.com/dh1tw/streamdeck/pull/9#discussion_r2187628307
if c != nil && c.ConvertKey {
return s.updateKeyPressOriginal(b[1:])
}
switch b[1] {
case 0:
return s.updateKeyPress(b[4:])
case 3:
if b[4] == 0 {
return s.updateDialPush(b[5:])
}
return s.updateDialTurn(b[5:])
default:
return nil, fmt.Errorf("unknown event type %d", b[1])
}
}
func applyBools(in []bool, data []byte) ([]int, []bool) {
changed := []int{}
for i, b := range data {
if len(in) <= i {
in = append(in, false)
}
prev := in[i]
if b == 0 {
in[i] = false
} else {
in[i] = true
}
if prev != in[i] {
changed = append(changed, i)
}
}
return changed, in
}
func (s *State) updateKeyPressOriginal(data []byte) ([]Event, error) {
changedKeys := []int{}
updateEvents := []Event{}
if len(data) < 15 {
return nil, fmt.Errorf("wrong amount of data for updateKeyPressOriginal %d", len(data))
}
nd := make([]byte, 15)
for x := 0; x < 3; x++ {
for y := 0; y < 5; y++ {
nd[(x*5)+y] = data[(x*5)+4-y]
}
}
changedKeys, s.Keys = applyBools(s.Keys, nd)
for _, changedKey := range changedKeys {
if s.Keys[changedKey] {
updateEvents = append(updateEvents, Event{EventKeyPressed, changedKey})
} else {
updateEvents = append(updateEvents, Event{EventKeyReleased, changedKey})
}
}
return updateEvents, nil
}
func (s *State) updateKeyPress(data []byte) ([]Event, error) {
changedKeys := []int{}
updateEvents := []Event{}
changedKeys, s.Keys = applyBools(s.Keys, data)
for _, changedKey := range changedKeys {
if s.Keys[changedKey] {
updateEvents = append(updateEvents, Event{EventKeyPressed, changedKey})
} else {
updateEvents = append(updateEvents, Event{EventKeyReleased, changedKey})
}
}
return updateEvents, nil
}
func (s *State) updateDialPush(data []byte) ([]Event, error) {
var changedDialPushs []int
changedDialPushs, s.DialPush = applyBools(s.DialPush, data)
updateEvents := []Event{}
for _, changedKey := range changedDialPushs {
if s.DialPush[changedKey] {
updateEvents = append(updateEvents, Event{EventDialPressed, changedKey})
} else {
updateEvents = append(updateEvents, Event{EventDialReleased, changedKey})
}
}
return updateEvents, nil
}
func (s *State) updateDialTurn(data []byte) ([]Event, error) {
var changedDialTurns int
updateEvents := []Event{}
changedDialTurns, s.DialPos = applyDelta(s.DialPos, data)
if changedDialTurns >= 0 {
updateEvents = append(updateEvents, Event{EventDialTurn, changedDialTurns})
return updateEvents, nil
}
return nil, nil
}
func applyDelta(in []int, data []byte) (int, []int) {
changed := -1
for i, d := range data {
if d != 0 {
changed = i
}
if len(in) <= i {
in = append(in, 50)
}
output := in[i]
if d < 0x80 {
output = min(DialMax, output+int(d))
} else {
output = max(0, output-(256-int(d)))
}
in[i] = output
}
return changed, in
}