forked from mezzato/revpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgopitest.go
178 lines (145 loc) · 4.57 KB
/
gopitest.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
package main
import (
"flag"
"fmt"
"os"
"github.com/couillonnade/revpi/pkg/gopicontrol"
)
func main() {
// Subcommands
readCmd := flag.NewFlagSet("read", flag.ExitOnError)
writeCmd := flag.NewFlagSet("write", flag.ExitOnError)
lsCmd := flag.NewFlagSet("ls", flag.ExitOnError)
resetCmd := flag.NewFlagSet("reset", flag.ExitOnError)
variableCmd := flag.NewFlagSet("variable", flag.ExitOnError)
readCmdVarName := readCmd.String("n", "", "variable name. (required)")
readCmdVarFormat := readCmd.String("f", "d", "variable format. (optional)")
// List subcommand flag pointers
writeCmdVarName := writeCmd.String("n", "", "variable name. (required)")
writeCmdVarValue := writeCmd.Uint64("v", 0, "variable value. (required)")
variableCmdVarName := variableCmd.String("n", "", "variable name. (required)")
// Verify that a subcommand has been provided
// os.Arg[0] is the main command
// os.Arg[1] will be the subcommand
if len(os.Args) < 2 || os.Args[1] == "-h" {
fmt.Printf(`a subcommand is required, valid options are [read|write|ls|reset]:
read: read variable value
write: write variable value
variable: show variable info
ls: list devices
reset: reset the driver
Type
%s -h
for help with a verb
For example to read the RevPi Core LED:
read -n RevPiLED
`, os.Args[0])
os.Exit(1)
}
// Switch on the subcommand
// Parse the flags for appropriate FlagSet
// FlagSet.Parse() requires a set of arguments to parse as input
// os.Args[2:] will be all arguments starting after the subcommand at os.Args[1]
switch os.Args[1] {
case "write":
writeCmd.Parse(os.Args[2:])
case "read":
readCmd.Parse(os.Args[2:])
case "variable":
variableCmd.Parse(os.Args[2:])
case "ls":
lsCmd.Parse(os.Args[2:])
case "reset":
resetCmd.Parse(os.Args[2:])
default:
fmt.Printf("invalid command\n")
flag.PrintDefaults()
os.Exit(1)
}
rpctl := gopicontrol.NewRevPiControl()
defer rpctl.Close()
// Check which subcommand was Parsed using the FlagSet.Parsed() function. Handle each case accordingly.
// FlagSet.Parse() will evaluate to false if no flags were parsed (i.e. the user did not provide any flags)
if writeCmd.Parsed() {
// Required Flags
if *writeCmdVarName == "" {
writeCmd.PrintDefaults()
os.Exit(1)
}
fmt.Printf("writing variable: %s, value: %d\n", *writeCmdVarName, *writeCmdVarValue)
if err := writeVariableValue(rpctl, *writeCmdVarName, (uint32)(*writeCmdVarValue)); err != nil {
fmt.Println(err)
return
}
}
if readCmd.Parsed() {
// Required Flags
if *readCmdVarName == "" {
readCmd.PrintDefaults()
os.Exit(1)
}
fmt.Printf("reading variable: %s\n", *readCmdVarName)
if err := readVariableValue(rpctl, *readCmdVarName, (*readCmdVarFormat)[0], false); err != nil {
fmt.Println(err)
return
}
}
if variableCmd.Parsed() {
// Required Flags
if *variableCmdVarName == "" {
variableCmd.PrintDefaults()
os.Exit(1)
}
fmt.Printf("reading variable info: %s\n", *variableCmdVarName)
if err := showVariableInfo(rpctl, *variableCmdVarName); err != nil {
fmt.Println(err)
return
}
}
if lsCmd.Parsed() {
devices, err := rpctl.GetDeviceInfoList()
if err != nil {
fmt.Println(err)
return
}
if err := showDeviceList(devices); err != nil {
fmt.Println(err)
return
}
}
if resetCmd.Parsed() {
if err := rpctl.Reset(); err != nil {
fmt.Println(err)
return
}
}
}
func showDeviceList(asDevList []gopicontrol.SDeviceInfo) (err error) {
devcount := len(asDevList)
fmt.Printf("Found %d devices:\n", devcount)
for dev := 0; dev < devcount; dev++ {
mn := gopicontrol.GetModuleName(asDevList[dev].I16uModuleType)
// Show device number, address and module type
fmt.Printf("Address: %d module type: %d (0x%x) %s V%d.%d\n", asDevList[dev].I8uAddress,
asDevList[dev].I16uModuleType, asDevList[dev].I16uModuleType,
mn,
asDevList[dev].I16uSW_Major, asDevList[dev].I16uSW_Minor)
if asDevList[dev].I8uActive > 0 {
fmt.Printf("Module is present\n")
} else {
if gopicontrol.IsModuleConnected(asDevList[dev].I16uModuleType) {
fmt.Printf("Module is NOT present, data is NOT available!!!\n")
} else {
fmt.Printf("Module is present, but NOT CONFIGURED!!!\n")
}
}
// Show offset and length of input section in process image
fmt.Printf(" input offset: %d length: %d\n", asDevList[dev].I16uInputOffset,
asDevList[dev].I16uInputLength)
// Show offset and length of output section in process image
fmt.Printf(" output offset: %d length: %d\n", asDevList[dev].I16uOutputOffset,
asDevList[dev].I16uOutputLength)
fmt.Printf("\n")
}
return nil
}