This repository has been archived by the owner on Oct 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
84 lines (70 loc) · 1.72 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
package main
import (
"context"
"fmt"
"strconv"
"github.com/cyp0633/joycon-terminal/app"
"github.com/cyp0633/joycon-terminal/devices"
)
// App struct
type App struct {
ctx context.Context
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
// Greet returns a greeting for the given name
func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello %s, It's show time!", name)
}
// ConnectSerial sets up connection
func (a *App) ConnectSerial(path string) string {
// get available ports
_ = a.GetAvailablePorts()
// connect port
err := devices.ConnectSerial(path)
if err != nil {
return "连接串口失败:" + err.Error()
}
// test connection
err = devices.TestConnection()
if err != nil {
return "连接测试失败:" + err.Error()
}
return "success"
}
func (a *App) GetAvailablePorts() []app.SerialOptions {
return app.GetSerialPortsList()
}
func (a *App) GetDevices() []app.SerialOptions {
return app.GetOnlineDevices()
}
func (a *App) StartListen() {
devices.EnableReadMtx.Lock()
devices.EnableRead = true
devices.EnableReadMtx.Unlock()
devices.RealtimeRead()
}
func (a *App) StopListen() {
devices.EnableReadMtx.Lock()
devices.EnableRead = false
devices.EnableReadMtx.Unlock()
}
func (a *App) Disconnect() {
a.StopListen()
devices.Conn.Close()
}
func (a *App) SetKey(device string, key int, target string) {
deviceNum, _ := strconv.Atoi(device)
// keyNum, _ := strconv.Atoi(key)
devices.SetKey(deviceNum, key, target)
}
func (a *App) UsePresetKeys(preset string) {
devices.UsePreset(preset)
}