-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
132 lines (110 loc) · 3.15 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
package main
import (
"fmt"
"log/slog"
"os"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"github.com/ssebs/go-mmp/controllers"
"github.com/ssebs/go-mmp/macro"
"github.com/ssebs/go-mmp/models"
"github.com/ssebs/go-mmp/serialdevice"
"github.com/ssebs/go-mmp/views"
)
/*
TODO:
- Fix GUIMode changing not connecting serial after saving (currently shows error)
- On ResetConfig, don't use defaultConfig.yml
*/
func main() {
cliFlags := models.ParseFlags()
conf, err := models.NewConfigFromFile(cliFlags)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
views.ShowErrorDialogAndRun(err) // TODO: only if GUIMode is not set to daemon
}
macroMgr, err := macro.NewMacroManager(conf)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
views.ShowErrorDialogAndRun(err) // TODO: only if GUIMode is not set to daemon
}
// TODO: move this by passing cliFlags to NewMacroManager?
if cliFlags.GUIMode != models.NOTSET {
conf.GUIMode = cliFlags.GUIMode
}
// TODO: move this
mmpApp := app.New()
rootWin := mmpApp.NewWindow("Mini Macro Pad")
rootWin.Resize(fyne.NewSize(400, 400))
rootWin.CenterOnScreen()
mainGUIController := controllers.NewMacroRunnerController(
conf,
views.NewMacroRunnerView(conf.Columns, rootWin),
macroMgr,
)
rootWin.SetContent(mainGUIController.MacroRunnerView)
switch conf.GUIMode {
case models.GUIOnly:
runGUIOnly(rootWin)
case models.NORMAL:
runSerialAndGUI(rootWin, conf, mainGUIController, mmpApp, macroMgr)
default:
fmt.Fprintf(os.Stderr, "%s not supported", conf.GUIMode.String())
}
}
func runSerialAndGUI(
rootWin fyne.Window, conf *models.Config, guiController *controllers.MacroRunnerController,
mmpApp fyne.App, macroMgr *macro.MacroManager,
) {
// Connect Serial Device from the config
arduino, err := serialdevice.NewSerialDeviceFromConfig(conf)
if err != nil {
views.ShowErrorDialogAndRun(err)
rootWin.ShowAndRun()
return
// TODO: show list of devices to select from and update config
}
defer arduino.CloseConnection()
// Add arduino to controller
guiController.SetSerialDevice(arduino)
// listener channels
btnch := make(chan string, 2)
quitch := make(chan struct{})
displayBtnch := make(chan string, 1)
// Run Serial Listener
go arduino.Listen(btnch, quitch)
// Visible button press listener
go guiController.ListenForDisplayButtonPress(displayBtnch, quitch)
// Do something when btnch gets data
go RunMacroOnDataIn(btnch, displayBtnch, quitch, mmpApp, macroMgr)
// Finally, display the GUI once everything is loaded & loop
rootWin.ShowAndRun()
}
func runGUIOnly(rootWin fyne.Window) {
rootWin.SetOnClosed(func() {
fmt.Println("gui only closed")
os.Exit(0)
})
rootWin.ShowAndRun()
}
func RunMacroOnDataIn(btnch chan string, displayBtnch chan string, quitch chan struct{}, mmpApp fyne.App, macroMgr *macro.MacroManager) {
free:
for {
select {
case btn := <-btnch:
// Only run the function if it's not blank, tho
if btn != "" {
// send btn id to show the btn press
displayBtnch <- btn
// Run the action from the btn id
err := macroMgr.RunMacroById(btn)
if err != nil {
slog.Warn(err.Error())
}
}
case <-quitch:
break free
}
}
mmpApp.Quit()
}