-
Notifications
You must be signed in to change notification settings - Fork 2
/
command.go
64 lines (60 loc) · 1.59 KB
/
command.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
package main
import (
"encoding/hex"
"fmt"
"log"
"os"
"strings"
)
type Command struct {
name string
description string
function func()
}
var (
commands []Command
args []string
)
func cmdhelp() {
var page = 0
strout(out, config.outputCode, fmt.Sprintf(">-------Help(%v)-------<\n", page))
for i := 0; i < len(commands); i++ {
strout(out, config.outputCode, fmt.Sprintf(" %-10v --%v\n", commands[i].name, commands[i].description))
}
}
func cmdexit() {
CloseTrzsz()
CloseSerial()
os.Exit(0)
}
func cmdargs() {
strout(out, config.outputCode, fmt.Sprintf(">-------Args(%v)-------<\n", len(args)-1))
strout(out, config.outputCode, fmt.Sprintf("%q\n", args[1:]))
}
func cmdctrl() {
var err error
b := []byte(args[1])
x := []byte{b[0] & 0x1f}
_, err = serialPort.Write(x)
ErrorF(err)
strout(out, config.outputCode, fmt.Sprintf("Ctrl+%s\n", b))
}
func cmdhex() {
strout(out, config.outputCode, fmt.Sprintf(">-----Hex Send-----<\n"))
strout(out, config.outputCode, fmt.Sprintf("%q\n", args[1:]))
s := strings.Join(args[1:], "")
b, err := hex.DecodeString(s)
if err != nil {
log.Fatal(err)
}
_, err = serialPort.Write(b)
if err != nil {
log.Fatal(err)
}
}
func cmdinit() {
commands = append(commands, Command{name: ".help", description: "帮助信息", function: cmdhelp})
commands = append(commands, Command{name: ".ctrl", description: "发送Ctrl组合键", function: cmdctrl})
commands = append(commands, Command{name: ".hex", description: "发送Hex", function: cmdhex})
commands = append(commands, Command{name: ".exit", description: "退出终端", function: cmdexit})
}