-
Notifications
You must be signed in to change notification settings - Fork 0
/
qmlRoot.go
162 lines (131 loc) · 3.65 KB
/
qmlRoot.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"os/exec"
"strconv"
"time"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/quick"
"github.com/vchrisr/go-osc"
"github.com/vchrisr/x32control/internal/x32"
)
type QmlRoot struct {
core.QObject
chStrips ChannelStrips
mixer *x32.X32
_ bool `property:"busy"`
_ string `property:"ipaddress"`
_ int `property:"brightness"`
_ func() `constructor:"init"`
_ func(bool) `slot:"shutdown,auto"`
_ func(int) `slot:"changeBrightness,auto"`
_ func(int) `slot:"recallClicked,auto"`
_ func(string, *core.QObject) `slot:"registerChannelStrip,auto"`
_ func(string, int) `signal:"receiveFaderValue"`
_ func(string, float32) `slot:"sendFaderValue,auto"`
_ func(string, bool) `slot:"sendMute,auto"`
}
func initQmlRoot(view *quick.QQuickView, conf config, mixer *x32.X32) *QmlRoot {
q := NewQmlRoot(nil)
q.mixer = mixer
q.chStrips = make(ChannelStrips)
confJson, _ := json.Marshal(conf)
view.RootContext().SetContextProperty2("controllerConfig", core.NewQVariant14(string(confJson)))
view.RootContext().SetContextProperty("QmlRoot", q)
return q
}
func (q *QmlRoot) init() {
q.SetBusy(true)
if b, err := ioutil.ReadFile("/sys/class/backlight/rpi_backlight/brightness"); err == nil {
brightness, _ := strconv.Atoi(string(b[:len(b)-1]))
q.SetBrightness(brightness)
}
//Get and set the IP address every 5 seconds.
//This will update the IP properly even if the app was started when network was not yet available.
//Also account for changed ip address (DHCP)
go func() {
for {
q.SetIpaddress(q.getMyIp())
time.Sleep(5 * time.Second)
}
}()
}
func (q *QmlRoot) getMyIp() string {
ifaces, _ := net.Interfaces()
for _, i := range ifaces {
addrs, _ := i.Addrs()
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if !ip.IsLoopback() && ip.To4() != nil {
return ip.String()
}
}
}
return ""
}
func (q *QmlRoot) shutdown(restart bool) {
if restart {
log.Println("Restarting...")
exec.Command("sudo", "reboot").Start()
return
}
log.Println("Shutting down")
exec.Command("sudo", "poweroff").Start()
}
func (q *QmlRoot) recallClicked(scene int) {
if err := q.mixer.RecallScene(int(scene)); err != nil {
log.Println(err.Error())
}
//board update done through scene recall are not send out over xremote. So we need to get initial channel status this way.
for _, channel := range q.chStrips {
channel.updateFromMixer()
}
}
func (q *QmlRoot) enableBusy() {
q.SetBusy(true)
for _, chStrip := range q.chStrips {
chStrip.lastFaderPosition = 0
}
}
func (q *QmlRoot) disableBusy() {
q.SetBusy(false)
for _, chStrip := range q.chStrips {
chStrip.updateFromMixer()
}
q.mixer.Send(osc.NewMessage("/xremote"))
q.mixer.RequestMetering()
}
func (q *QmlRoot) changeBrightness(brightness int) {
f, err := os.OpenFile("/sys/class/backlight/rpi_backlight/brightness", os.O_RDWR, os.ModeCharDevice)
if err != nil {
return
}
_, err = fmt.Fprint(f, strconv.Itoa(brightness))
if err != nil {
log.Println(err.Error())
}
}
func (q *QmlRoot) registerChannelStrip(addr string, qmlObj *core.QObject) {
q.chStrips[addr] = &ChannelStrip{
qmlObj: qmlObj,
mixerChannel: x32.NewChannel(addr, q.mixer),
lastFaderPosition: 0,
}
}
func (q *QmlRoot) sendFaderValue(address string, pos float32) {
q.chStrips[address].sendFaderPosition(pos)
}
func (q *QmlRoot) sendMute(address string, checked bool) {
q.chStrips[address].mixerChannel.SetMute(checked)
}