-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.go
284 lines (254 loc) · 7.33 KB
/
gui.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
"context"
"log/slog"
"strconv"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
"github.com/duncanvanzyl/xplane-serial-gps-connector/serial"
"github.com/duncanvanzyl/xplane-serial-gps-connector/xplane"
)
// AppUI is the UI for the App
type AppUI struct {
app *App
XPlanes xplane.XPlanes
xplaneSelect *widget.Select
xplaneRefresh *widget.Button
serialPortsSelect *widget.Select
serialPortRefresh *widget.Button
baudRate *widget.Select
refreshFreq *widget.Select
runButton *widget.Button
stopButton *widget.Button
status *widget.Label
cancelCtx context.CancelFunc
Logger *slog.Logger
}
var (
// PossibleBaudeRates is the list of possible baud rates
PossibleBaudeRates = [...]string{"9600", "14400", "19200", "38400", "57600", "115200"}
// PossiblePosFreqs is the list of possible position frequencies
// This will determine the rate that the X-Plane position is read
PossiblePosFreqs = [...]string{"1Hz", "2Hz", "5Hz", "10Hz", "20Hz"}
)
// NewAppUI returns a new AppUI
func NewAppUI(xApp *App, logger *slog.Logger) *AppUI {
ui := &AppUI{
app: xApp,
XPlanes: make(xplane.XPlanes),
status: widget.NewLabel(""),
Logger: logger,
}
ui.xplaneRefresh = widget.NewButton("Refresh X-Plane List", func() {
go ui.findXplanes(5 * time.Second)
})
ui.xplaneSelect = widget.NewSelect([]string{}, ui.setXPlane(xApp))
ui.serialPortRefresh = widget.NewButton("Refresh Serial Port List", ui.getSerial)
ui.serialPortsSelect = widget.NewSelect([]string{}, ui.setSerialPort(xApp))
ui.baudRate = widget.NewSelect(PossibleBaudeRates[:], func(value string) {
ui.Logger.Debug("Set BaudRate", "baud", value)
v, err := strconv.Atoi(value)
if err != nil {
ui.Logger.Error("Failed to convert BaudRate", "err", err)
return
}
xApp.SetBaudRate(v)
})
ui.refreshFreq = widget.NewSelect(PossiblePosFreqs[:], func(value string) {
ui.Logger.Debug("Set PositionFreq", "freq", value)
v, err := strconv.Atoi(value[:len(value)-2])
if err != nil {
ui.Logger.Error("Failed to convert PositionFreq", "err", err)
return
}
xApp.SetPositionFreq(uint(v))
})
ui.runButton = widget.NewButton("Run", ui.run)
ui.runButton.Disable()
ui.stopButton = widget.NewButton("Stop", ui.stop)
// Populate Serial Port List, XPlane List and set default selects
go ui.getSerial()
go ui.findXplanes(5 * time.Second)
ui.baudRate.SetSelected("38400")
ui.refreshFreq.SetSelected("10Hz")
ui.stopButton.Disable()
return ui
}
// watchGUIState will set the GUI state based on the app state
func (ui *AppUI) watchGUIState(state, last_state AppState) {
if state == last_state {
return
}
switch state {
case Running:
ui.xplaneRefresh.Disable()
ui.xplaneSelect.Disable()
ui.serialPortRefresh.Disable()
ui.serialPortsSelect.Disable()
ui.baudRate.Disable()
ui.refreshFreq.Disable()
ui.runButton.Disable()
ui.stopButton.Enable()
case Runable:
ui.xplaneRefresh.Enable()
ui.xplaneSelect.Enable()
ui.serialPortRefresh.Enable()
ui.serialPortsSelect.Enable()
ui.baudRate.Enable()
ui.refreshFreq.Enable()
ui.runButton.Enable()
ui.stopButton.Disable()
case Incomplete:
ui.xplaneRefresh.Enable()
ui.xplaneSelect.Enable()
ui.serialPortRefresh.Enable()
ui.serialPortsSelect.Enable()
ui.baudRate.Enable()
ui.refreshFreq.Enable()
ui.runButton.Disable()
ui.stopButton.Disable()
}
}
// Watch will watch the app state and update the GUI accordingly
func (ui *AppUI) Watch(ctx context.Context) {
ticker := time.NewTicker(500 * time.Millisecond)
last_state := ui.app.State()
last_xplane_count := 0
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
ui.watchGUIState(ui.app.State(), last_state)
last_state = ui.app.State()
if len(ui.XPlanes) != last_xplane_count {
last_xplane_count = len(ui.XPlanes)
ui.xplaneSelect.SetOptions(ui.XPlanes.List())
}
}
}
}
// xplaneLayout returns the X-Plane section layout
func (ui *AppUI) xplaneLayout() fyne.CanvasObject {
title := widget.NewLabel("X-Plane")
title.TextStyle = fyne.TextStyle{Bold: true}
return container.NewVBox(
title,
container.New(
layout.NewFormLayout(),
widget.NewLabel(""), ui.xplaneRefresh,
widget.NewLabel("X-Plane Instance"), ui.xplaneSelect,
widget.NewLabel("Position Interval"), ui.refreshFreq,
),
)
}
// serialLayout returns the Serial Port section layout
func (ui *AppUI) serialLayout() fyne.CanvasObject {
title := widget.NewLabel("Serial Port")
title.TextStyle = fyne.TextStyle{Bold: true}
return container.NewVBox(
title,
container.New(layout.NewFormLayout(),
widget.NewLabel(""), ui.serialPortRefresh,
widget.NewLabel("Port"), ui.serialPortsSelect,
widget.NewLabel("Baud Rate"), ui.baudRate,
),
)
}
// GetContent returns the content of the AppUI
func (ui *AppUI) GetContent() fyne.CanvasObject {
return container.NewVBox(
ui.xplaneLayout(),
widget.NewSeparator(),
ui.serialLayout(),
widget.NewSeparator(),
container.NewHBox(ui.runButton, ui.stopButton, ui.status),
)
}
// getSerial will populate the serialPortsSelect with the serial ports available on the system
func (ui *AppUI) getSerial() {
ui.Logger.Debug("GetSerial")
ports := serial.FindPorts()
ui.Logger.Debug("Serial Ports", "count", len(ports), "ports", ports)
if len(ports) == 0 {
return
}
ui.serialPortsSelect.SetOptions(ports)
}
// FindXplanes will search for X-Plane beacons and add them to the XPlanes map
// It will stop after the timeout
func (ui *AppUI) findXplanes(timeout time.Duration) {
ui.xplaneRefresh.Disable()
defer ui.xplaneRefresh.Enable()
// spend 5 seconds searching for X-Plane beacons
after := time.After(timeout)
ui.XPlanes = make(xplane.XPlanes)
for {
select {
case <-after:
ui.Logger.Debug("FindXplanes Timeout", "timeout", timeout, "count", len(ui.XPlanes), "xplanes", ui.XPlanes.List())
return
default:
beacon, err := xplane.FindXplane(1 * time.Second)
if err != nil {
ui.Logger.Warn("FindXplanes failed", "err", err)
continue
}
ui.XPlanes.Add(beacon)
}
}
}
// setSerialPort returns a function that will set the serial port on the app
func (ui *AppUI) setSerialPort(xApp *App) func(string) {
return xApp.SetSerialPort
}
// setXPlane returns a function that will set the X-Plane on the app
func (ui *AppUI) setXPlane(xApp *App) func(string) {
return func(xp string) {
addr := ui.XPlanes.Find(xp)
xApp.SetXPlane(addr)
}
}
// run will start the app
func (ui *AppUI) run() {
ui.Logger.Debug("Run")
ctx, cancel := context.WithCancel(context.Background())
ui.cancelCtx = cancel
feedback := make(chan string, 3)
feedback <- "Starting"
// start the app
go ui.app.Run(ctx, feedback)
// watch the feedback channel and update the status label
go func() {
t := time.Now()
for msg := range feedback {
if msg == "" && time.Since(t) < 20*time.Second {
continue
}
if msg == "XXX" {
ui.Logger.Warn("Quit on Feedback", "msg", msg)
// stop the app and drain the feedback channel
ui.stop()
for range feedback {
}
return
}
ui.status.SetText(msg)
ui.Logger.Info("Feedback", "msg", msg)
t = time.Now()
}
}()
}
// stop will stop the app
func (ui *AppUI) stop() {
if ui.cancelCtx == nil {
serial.Logger.Info("Can't Stop, no context")
return
}
ui.cancelCtx()
ui.cancelCtx = nil
ui.Logger.Debug("Stop")
}