-
Notifications
You must be signed in to change notification settings - Fork 4
/
listen.go
115 lines (99 loc) · 3.36 KB
/
listen.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
package loupedeck
import (
"encoding/binary"
"log/slog"
"github.com/gorilla/websocket"
)
// Listen waits for events from the Loupedeck and calls
// callbacks as configured.
func (l *Loupedeck) Listen() {
slog.Info("Listening")
for {
websocketMsgType, message, err := l.conn.ReadMessage()
if err != nil {
slog.Warn("Read error, exiting", "error", err)
// TODO(scottlaird): make this shut down cleanly.
panic("Websocket connection failed")
}
if len(message) == 0 {
slog.Warn("Received a 0-byte message. Skipping")
continue
}
if websocketMsgType != websocket.BinaryMessage {
slog.Warn("Unknown websocket message type received", "type", websocketMsgType)
}
m, _ := l.ParseMessage(message)
slog.Info("Read", "message", m.String())
if m.transactionID != 0 {
if c := l.transactionCallbacks[m.transactionID]; c != nil {
slog.Info("Callback found, calling")
c(m)
l.transactionCallbacks[m.transactionID] = nil
}
} else {
switch m.messageType {
// Status messages in response to previous commands?
case ButtonPress:
button := Button(binary.BigEndian.Uint16(message[2:]))
upDown := ButtonStatus(message[4])
if upDown == ButtonDown && l.buttonBindings[button] != nil {
l.buttonBindings[button](button, upDown)
} else if upDown == ButtonUp && l.buttonUpBindings[button] != nil {
l.buttonUpBindings[button](button, upDown)
} else {
slog.Info("Received uncaught button press message", "button", button, "upDown", upDown, "message", message)
}
case KnobRotate:
knob := Knob(binary.BigEndian.Uint16(message[2:]))
value := int(message[4])
if l.knobBindings[knob] != nil {
v := value
if value == 255 {
v = -1
}
l.knobBindings[knob](knob, v)
} else {
slog.Debug("Received knob rotate message", "knob", knob, "value", value, "message", message)
}
case Touch:
x := binary.BigEndian.Uint16(message[4:])
y := binary.BigEndian.Uint16(message[6:])
id := message[8] // Not sure what this is for
b := touchCoordToButton(x, y)
if l.touchBindings[b] != nil {
l.touchBindings[b](b, ButtonDown, x, y)
} else {
slog.Debug("Received touch message", "x", x, "y", y, "id", id, "b", b, "message", message)
}
case TouchEnd:
x := binary.BigEndian.Uint16(message[4:])
y := binary.BigEndian.Uint16(message[6:])
id := message[8] // Not sure what this is for
b := touchCoordToButton(x, y)
if l.touchUpBindings[b] != nil {
l.touchUpBindings[b](b, ButtonUp, x, y)
} else {
slog.Debug("Received touch end message", "x", x, "y", y, "id", id, "b", b, "message", message)
}
case TouchCT:
x := binary.BigEndian.Uint16(message[4:])
y := binary.BigEndian.Uint16(message[6:])
id := message[8] // Not sure what this is for
slog.Debug("Received CT touch message", "x", x, "y", y, "id", id, "message", message)
if l.touchDKBindings != nil {
l.touchDKBindings(ButtonDown, x, y)
}
case TouchEndCT:
x := binary.BigEndian.Uint16(message[4:])
y := binary.BigEndian.Uint16(message[6:])
id := message[8] // Not sure what this is for
slog.Debug("Received CT touch message", "x", x, "y", y, "id", id, "message", message)
if l.touchDKBindings != nil {
l.touchDKBindings(ButtonUp, x, y)
}
default:
slog.Info("Received unknown message", "message", m.String())
}
}
}
}