-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
141 lines (128 loc) · 3.26 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
package main
import (
"CNB/internal/colorTheme"
"CNB/internal/com"
"CNB/internal/comPacket"
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/widget"
"go.bug.st/serial"
"log"
"strconv"
)
func serializeIntSlice(input []int) []string {
var valuesText []string
for i := range input {
number := input[i]
text := strconv.Itoa(number)
valuesText = append(valuesText, text)
}
return valuesText
}
func getParities(p map[string]serial.Parity) []string {
keys := make([]string, 0, len(p))
for s := range p {
keys = append(keys, s)
}
return keys
}
func main() {
a := app.New()
ico, _ := fyne.LoadResourceFromPath(".\\resources\\icon.png")
a.SetIcon(ico)
a.Settings().SetTheme(colorTheme.GreyTheme())
mainWindow := a.NewWindow("OCN LAB2")
mainWindow.Resize(fyne.Size{Width: 400, Height: 250})
var rxPortName string
var txPortName string
rxDropdown := widget.NewSelect(
com.ScanPorts(),
func(v string) {
rxPortName = v
fmt.Println("RX port changed:", v)
},
)
txDropdown := widget.NewSelect(
com.ScanPorts(),
func(v string) {
txPortName = v
fmt.Println("TX port changed:", v)
},
)
availableSpeeds := []int{9600, 115200, 50}
var rxSpeed = availableSpeeds[0]
var txSpeed = availableSpeeds[0]
rxSpeedDropdown := widget.NewSelect(
serializeIntSlice(availableSpeeds),
func(v string) {
rxSpeed, _ = strconv.Atoi(v)
log.Println("RX Speed changed:", v)
},
)
txSpeedDropdown := widget.NewSelect(
serializeIntSlice(availableSpeeds),
func(v string) {
txSpeed, _ = strconv.Atoi(v)
log.Println("TX Speed changed:", v)
},
)
txDropdown.SetSelectedIndex(0)
rxDropdown.SetSelectedIndex(1)
txSpeedDropdown.SetSelectedIndex(0)
rxSpeedDropdown.SetSelectedIndex(0)
var parity serial.Parity
paritySelect := widget.NewSelect(
getParities(com.ParityMap),
func(v string) {
parity = com.ParityMap[v]
log.Println("Parity:", v)
},
)
paritySelect.SetSelectedIndex(0)
input := widget.NewEntry()
var inputMessage string
inputMessageBinding := binding.BindString(&inputMessage)
input.Bind(inputMessageBinding)
input.SetPlaceHolder("Type text to transfer")
receivedText := widget.NewLabel("Received text will be here")
var receivedMessage = "Received text will be here"
receivedMessageBinding := binding.BindString(&receivedMessage)
receivedText.Bind(receivedMessageBinding)
buttonStart := widget.NewButton("Send", func() {
msg, _ := inputMessageBinding.Get()
com.Port{
Name: txPortName,
Speed: txSpeed,
Parity: parity,
}.WriteBytes(comPacket.CreatePacket([]byte(msg + "\n")).SerializePacket())
packet, err := com.Port{
Name: rxPortName,
Speed: rxSpeed,
Parity: parity,
}.ReadPacket()
if err != nil {
log.Fatal(err)
return
}
err = receivedMessageBinding.Set(string(packet.Data))
if err != nil {
log.Fatal(err)
return
}
})
l := container.NewVBox(
container.NewHBox(widget.NewLabel("RX port:"), rxDropdown),
container.NewHBox(widget.NewLabel("TX port:"), txDropdown),
container.NewHBox(widget.NewLabel("RX speed:"), rxSpeedDropdown),
container.NewHBox(widget.NewLabel("TX speed:"), txSpeedDropdown),
paritySelect,
input,
receivedText,
buttonStart,
)
mainWindow.SetContent(l)
mainWindow.ShowAndRun()
}