-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
193 lines (159 loc) · 4.84 KB
/
app.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
package main
import (
"context"
"sync"
"github.com/fogleman/delaunay"
"github.com/wailsapp/wails/v2/pkg/runtime"
"gitlab.com/patopest/go-sacn"
"gitlab.com/patopest/go-sacn/packet"
)
type App struct {
ctx context.Context
fixtures map[string]Fixture
universeDMXData map[uint16]DMXData
calibrationPoints map[string]CalibrationPoint
sender *sacn.Sender
activeUniverses map[uint16]chan<- packet.SACNPacket
sacnStopLoop chan bool
sacnUpdatedConfig chan bool
sacnConfig *SACNConfig
sacnWorkerWG sync.WaitGroup
linearInterpolators map[string]*Linear2DPanTiltInterpolator
}
func NewApp() *App {
return &App{}
}
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
a.activeUniverses = make(map[uint16]chan<- packet.SACNPacket)
a.fixtures = make(map[string]Fixture)
a.calibrationPoints = make(map[string]CalibrationPoint)
a.universeDMXData = make(map[uint16]DMXData)
a.sacnConfig = &SACNConfig{
Fps: 25,
Multicast: true,
Destinations: []string{},
}
a.sacnWorkerWG = sync.WaitGroup{}
a.sacnStopLoop = make(chan bool)
a.sacnUpdatedConfig = make(chan bool)
a.linearInterpolators = make(map[string]*Linear2DPanTiltInterpolator)
a.findPossibleIPAddresses()
a.sacnWorkerWG.Add(1)
go a.sacnWorker()
}
func (a *App) shutdown(ctx context.Context) {
a.sacnStopLoop <- true
a.sacnWorkerWG.Wait()
}
func (a *App) AlertDialog(title string, message string) {
options := runtime.MessageDialogOptions{
Type: runtime.InfoDialog,
Title: title,
Message: message,
}
_, err := runtime.MessageDialog(a.ctx, options)
if err != nil {
runtime.LogError(a.ctx, err.Error())
return
}
}
func (a *App) ConfirmDialog(title string, message string) string {
options := runtime.MessageDialogOptions{
Type: runtime.QuestionDialog,
Title: title,
Message: message,
Buttons: []string{"Ok", "Cancel"},
DefaultButton: "Ok",
CancelButton: "Cancel",
}
res, err := runtime.MessageDialog(a.ctx, options)
if err != nil {
runtime.LogError(a.ctx, err.Error())
return ""
}
return res
}
func (a *App) SetCalibrationPoints(calibrationPoints map[string]CalibrationPoint) {
a.calibrationPoints = calibrationPoints
a.calculateLinearInterpolator()
}
func (a *App) SetFixtures(fixtures map[string]Fixture) {
a.fixtures = fixtures
a.universeDMXData = make(map[uint16]DMXData)
a.calculateLinearInterpolator()
}
func (a *App) calculateLinearInterpolator() {
a.linearInterpolators = make(map[string]*Linear2DPanTiltInterpolator)
points := make([]Point, len(a.calibrationPoints))
pointsIndexMap := make(map[string]int)
index := 0
for _, calibrationPoint := range a.calibrationPoints {
points[index] = Point{X: calibrationPoint.X, Y: calibrationPoint.Y}
pointsIndexMap[calibrationPoint.Id] = index
index++
}
for _, fixture := range a.fixtures {
calibrated := true
for _, calibrationPoint := range a.calibrationPoints {
_, exists := fixture.Calibration[calibrationPoint.Id]
if !exists {
calibrated = false
break
}
}
if !calibrated {
continue
}
panValues := make([]float64, len(a.calibrationPoints))
tiltValues := make([]float64, len(a.calibrationPoints))
for _, calibrationPoint := range a.calibrationPoints {
panValues[pointsIndexMap[calibrationPoint.Id]] = float64(fixture.Calibration[calibrationPoint.Id].Pan)
tiltValues[pointsIndexMap[calibrationPoint.Id]] = float64(fixture.Calibration[calibrationPoint.Id].Tilt)
}
interp, err := NewLinear2DPanTiltInterpolator(points, panValues, tiltValues, -1.0)
if err != nil {
runtime.LogError(a.ctx, err.Error())
continue
}
a.linearInterpolators[fixture.Id] = interp
}
}
func (a *App) SetMouseForAllFixtures(x float64, y float64) {
for _, fixture := range a.fixtures {
interp, exists := a.linearInterpolators[fixture.Id]
if !exists {
continue
}
pan, tilt, err := interp.Interpolate(delaunay.Point{X: x, Y: y})
if err != nil {
runtime.LogError(a.ctx, err.Error())
continue
}
if pan == interp.fillValue || tilt == interp.fillValue {
continue
}
a.SetPanTiltForFixture(fixture.Id, int(pan), int(tilt))
}
}
func (a *App) SetPanTiltForFixture(fixtureId string, pan int, tilt int) {
fixture, exists := a.fixtures[fixtureId]
if !exists {
runtime.LogError(a.ctx, "Tried to set pan/tilt for non-existing fixture")
return
}
data := a.universeDMXData[fixture.Universe]
if fixture.PanAddress >= 0 && fixture.PanAddress < 512 {
data[fixture.PanAddress] = byte(pan / 256)
}
if fixture.FinePanAddress >= 0 && fixture.FinePanAddress < 512 {
data[fixture.FinePanAddress] = byte(pan % 256)
}
if fixture.TiltAddress >= 0 && fixture.TiltAddress < 512 {
data[fixture.TiltAddress] = byte(tilt / 256)
}
if fixture.FineTiltAddress >= 0 && fixture.FineTiltAddress < 512 {
data[fixture.FineTiltAddress] = byte(tilt % 256)
}
a.universeDMXData[fixture.Universe] = data
}