-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
222 lines (188 loc) · 5.36 KB
/
main.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
_ "embed"
"encoding/json"
"errors"
"fmt"
"github.com/akamensky/argparse"
"html"
"io"
"log"
"net/http"
"os"
"strings"
"syscall"
"time"
"unsafe"
)
type button struct {
Name string
Bytes string // of length 2
}
// Inputs are represented by 3 bytes on /dev/ttyAML3.
// 2 bytes for button ID, then 01 = pressed, 00 = not pressed.
// For sliders the third byte is between 0x00 (lowest) and 0x7f (highest).
// For wheels 0x7f = left , 0x01 = right.
//
// You can find out with strace -e read -e status=successful -fp "$(pidof rc_audio_mixer)" -xx -P /dev/ttyAML3
// (Note: While strace is running audio will be broken, kill it to use the Rødecaster again)
// Do not send commands too quickly! sleep at least 0.01s between pressed and not.
var BUTTONS = []button{
button{"listen 1", "\xb0\x19"},
button{"listen 2", "\xb1\x19"},
button{"listen 3", "\xb2\x19"},
button{"listen 4", "\xb3\x19"},
button{"mute 1", "\xb0\x1d"},
button{"mute 2", "\xb1\x1d"},
button{"mute 3", "\xb2\x1d"},
button{"mute 4", "\xb3\x1d"},
button{"settings 1", "\xb0\x15"},
button{"settings 2", "\xb1\x15"},
button{"settings 3", "\xb2\x15"},
button{"settings 4", "\xb3\x15"},
button{"page left", "\xb0\x37"},
button{"page right", "\xb1\x37"},
button{"SMART Pad 1 (top left)", "\xb0\x23"},
button{"SMART Pad 2 (middle left)", "\xb1\x23"},
button{"SMART Pad 3 (bottom left)", "\xb2\x23"},
button{"SMART Pad 4 (top right)", "\xb3\x23"},
button{"SMART Pad 5 (middle right)", "\xb4\x23"},
button{"SMART Pad 6 (bottom right)", "\xb5\x23"},
button{"Big wheel button", "\xb0\x2f"},
// big wheel: b0 2b
// headphone 1 wheel: b1 2b (button non-functional)
// headphone 1 wheel: b2 2b (button non-functional)
// Fader 1: b0 0f
// Fader 2: b1 0f
// Fader 3: b2 0f
// Fader 4: b3 0f
}
func calcButtonsByName() map[string]button {
res := make(map[string]button)
for _, btn := range BUTTONS {
res[btn.Name] = btn
}
return res
}
var buttonsByName = calcButtonsByName()
//go:embed static/index.html
var INDEX_FILE string
func rootHandler(w http.ResponseWriter, r *http.Request) {
var buttonNames = make([]string, len(BUTTONS))
for i, btn := range BUTTONS {
buttonNames[i] = btn.Name
}
buttonJson, err := json.Marshal(buttonNames)
if err != nil {
panic(err)
}
indexHtml := strings.Replace(INDEX_FILE, "{{ buttonJSON }}", html.EscapeString(string(buttonJson)), -1)
io.WriteString(w, indexHtml)
}
//go:embed static/client.js
var CLIENT_JS_FILE string
func clientJSHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/javascript")
io.WriteString(w, CLIENT_JS_FILE)
}
func sendByte(fd uintptr, b byte) error {
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, fd, syscall.TIOCSTI, uintptr(unsafe.Pointer(&b)))
if errno != 0 {
msg := fmt.Sprintf("TIOCSTI failed with errno %d", errno)
return errors.New(msg)
}
return nil
}
func pressButton(fd uintptr, btn button) error {
var err error
bytes := []byte(btn.Bytes)
fmt.Printf("bytes in button %s: %x\n", btn.Name, btn.Bytes)
for _, b := range bytes {
err = sendByte(fd, b)
if err != nil {
return err
}
}
err = sendByte(fd, byte('\x00'))
if err != nil {
return err
}
// Required: wait some time between button press and release
time.Sleep(50 * time.Millisecond)
for _, b := range bytes {
err = sendByte(fd, b)
if err != nil {
return err
}
}
err = sendByte(fd, byte('\x01'))
if err != nil {
return err
}
return nil
}
func pressButtonsHandler(w http.ResponseWriter, r *http.Request) {
type requestBodyType struct {
Buttons []string `json:"buttons"`
}
buf := requestBodyType{}
err := json.NewDecoder(r.Body).Decode(&buf)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
requestButtonNames := buf.Buttons
buttons := make([]button, 0)
for _, btnName := range requestButtonNames {
btn, ok := buttonsByName[btnName]
if !ok {
msg := fmt.Sprintf("Cannot find button %s", btnName)
http.Error(w, msg, http.StatusBadRequest)
return
}
buttons = append(buttons, btn)
}
file, err := os.OpenFile("/dev/ttyAML3", os.O_WRONLY, 0)
if err != nil {
log.Printf("Failed to set buttons: cannot terminal: %s", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer file.Close()
fd := file.Fd()
for _, b := range buttons {
err := pressButton(fd, b)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, "{}")
}
func createHTTPServer(port string) {
http.HandleFunc("/press-buttons", pressButtonsHandler)
http.HandleFunc("/static/client.js", clientJSHandler)
http.HandleFunc("/", rootHandler)
log.Fatal(http.ListenAndServe(port, nil))
}
func main() {
// Set this program to lowest priority – any audio handling is more important!
syscall.Setpriority(syscall.PRIO_PGRP, 0, 19)
parser := argparse.NewParser("rc2http", "HTTP server for Rødecaster Duo")
installService := parser.Flag("", "install-service", &argparse.Options{Help: "Install and start as a service"})
port := parser.String("p", "port", &argparse.Options{Default: ":80", Help: "Address & port to listen to"})
// Parse input
err := parser.Parse(os.Args)
if err != nil {
fmt.Print(parser.Usage(err))
os.Exit(1)
}
if *installService {
InstallService()
os.Exit(0)
}
go createHTTPServer(*port)
// Wait forever
select {}
}