-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
185 lines (154 loc) · 4.01 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
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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"gopkg.in/ini.v1"
)
const version = "v0.2.2"
var header = fmt.Sprintf("\nBifrost %s\n", version)
var helpText = fmt.Sprintf(`%s
Bifrost is a tiny terminal emulator for serial port communication.
Usage:
bifrost [flags]
Flags:
-port-path Name/path of the serial port
-baud The baud rate to use on the connection
-save-config Save a connection configuration
-load-config Load a connection configuration
-help This help message
`, header)
var configDir = os.Getenv("HOME") + "/.bifrost/"
const configFile = "config.ini"
func welcomeMessage(portPath string, baud int) string {
return fmt.Sprintf(`%s
Options:
Port: %s
Baud rate: %d
Press Ctrl+\ to exit
`, header, portPath, baud)
}
func writeConfig(configName string, portPath string, baud int) error {
configPath := configDir + configFile
if _, err := os.Stat(configPath); os.IsNotExist(err) {
err = os.Mkdir(configDir, os.ModePerm)
if err != nil {
return err
}
err = ioutil.WriteFile(configPath, []byte(""), os.ModePerm)
if err != nil {
return err
}
}
config, err := ini.Load(configPath)
if err != nil {
return err
}
_, err = config.NewSection(configName)
if err != nil {
return err
}
config.Section(configName).Key("port").SetValue(portPath)
config.Section(configName).Key("baud").SetValue(strconv.Itoa(baud))
config.SaveTo(configPath)
return nil
}
func readConfig(configName string) (portPath string, baud int, err error) {
configPath := configDir + configFile
config, err := ini.Load(configPath)
if err != nil {
return
}
section, err := config.GetSection(configName)
if err != nil {
return
}
portPath = section.Key("port").String()
baud, _ = section.Key("baud").Int()
return
}
func main() {
var portPath string
var baud int
var saveConfig bool
var loadConfig string
var help bool
flag.StringVar(&portPath, "port-path", "/dev/tty.usbserial", "Name/path of the serial port")
flag.IntVar(&baud, "baud", 115200, "The baud rate to use on the connection")
flag.BoolVar(&saveConfig, "save-config", false, "Save a connection configuration")
flag.StringVar(&loadConfig, "load-config", "", "Load a connection configuration")
flag.BoolVar(&help, "help", false, "A brief help message")
flag.Parse()
if saveConfig {
var configName string
fmt.Println("What name do you want to save this config under?")
fmt.Scanln(&configName)
err := writeConfig(configName, portPath, baud)
if err != nil {
log.Printf("Failed to save config. FatalError: %v\n", err)
return
}
fmt.Printf("Config saved! You can view and edit your configurations at %s%s.\n", configDir, configFile)
return
}
if loadConfig != "" {
fmt.Printf("Loading config %s...\n", loadConfig)
cfgPortPath, cfgBaud, err := readConfig(loadConfig)
if err != nil {
log.Printf("Failed to load config. FatalError: %v\n", err)
return
}
portPath = cfgPortPath
baud = cfgBaud
fmt.Println("Config loaded successfully.")
}
if help {
fmt.Println(helpText)
return
}
connect, err := NewConnection(portPath, baud)
if err != nil {
log.Printf("FatalError: %v\n", err)
return
}
// Welcome message
fmt.Print(welcomeMessage(portPath, baud))
go connect.Start()
for {
key := pollKeyEvents()
if len(key.Value) != 0 {
connect.Write(key.Value)
} else {
switch key.Type {
case Esc:
connect.Write([]byte{'\x1b'})
case CtrlBackslash:
fmt.Println("\nbye!")
return
case Tab:
connect.Write([]byte{'\x09'})
case CtrlC:
connect.Write([]byte{'\x03'})
case Enter:
connect.Write([]byte{'\r'})
case Backspace:
connect.Write([]byte{'\x7F'})
case Delete:
connect.Write([]byte{'\x1b', '[', '3', '~'})
case LeftArrow:
connect.Write([]byte{'\x1b', '[', 'D'})
case RightArrow:
connect.Write([]byte{'\x1b', '[', 'C'})
case UpArrow:
connect.Write([]byte{'\x1b', '[', 'A'})
case DownArrow:
connect.Write([]byte{'\x1b', '[', 'B'})
case Space:
connect.Write([]byte{' '})
}
}
}
}