-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.go
117 lines (108 loc) · 2.85 KB
/
get.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
// SPDX-FileCopyrightText: 2019 Kent Gibson <warthog618@gmail.com>
//
// SPDX-License-Identifier: MIT
package main
import (
"fmt"
"strconv"
"strings"
"github.com/spf13/cobra"
"github.com/warthog618/go-gpiocdev"
)
func init() {
getCmd.Flags().BoolVarP(&getOpts.ActiveLow, "active-low", "l", false, "treat the line state as active low")
getCmd.Flags().BoolVarP(&getOpts.AsIs, "as-is", "a", false, "request the line as-is rather than as an input")
getCmd.Flags().StringVarP(&getOpts.Bias, "bias", "b", "as-is", "set the line bias.")
getCmd.Flags().IntVar(&getOpts.AbiV, "abiv", 0, "use specified ABI version.")
getCmd.Flags().MarkHidden("abiv")
getCmd.SetHelpTemplate(getCmd.HelpTemplate() + extendedGetHelp)
rootCmd.AddCommand(getCmd)
}
var extendedGetHelp = `
Biases:
as-is: leave bias unchanged
disable: disable bias
pull-up: enable pull-up
pull-down: enable pull-down
`
var (
getCmd = &cobra.Command{
Use: "get [flags] <chip> <offset1>...",
Short: "Get the state of a line or lines",
Long: `Read the state of a line or lines from a GPIO chip.`,
Args: cobra.MinimumNArgs(2),
RunE: get,
DisableFlagsInUseLine: true,
}
getOpts = struct {
ActiveLow bool
AsIs bool
Bias string
AbiV int
}{}
)
func get(cmd *cobra.Command, args []string) error {
name := args[0]
oo, err := parseOffsets(args[1:])
if err != nil {
return err
}
c, err := gpiocdev.NewChip(name, gpiocdev.WithConsumer("gpiocdevctl-get"))
if err != nil {
return err
}
defer c.Close()
opts := makeGetOpts()
l, err := c.RequestLines(oo, opts...)
if err != nil {
return fmt.Errorf("error requesting GPIO line: %s", err)
}
defer l.Close()
vv := make([]int, len(l.Offsets()))
err = l.Values(vv)
if err != nil {
return fmt.Errorf("error reading GPIO state: %s", err)
}
vstr := fmt.Sprintf("%d", vv[0])
for _, v := range vv[1:] {
vstr += fmt.Sprintf(" %d", v)
}
fmt.Println(vstr)
return nil
}
func makeGetOpts() []gpiocdev.LineReqOption {
opts := []gpiocdev.LineReqOption{}
if getOpts.ActiveLow {
opts = append(opts, gpiocdev.AsActiveLow)
}
if !getOpts.AsIs {
opts = append(opts, gpiocdev.AsInput)
}
bias := strings.ToLower(getOpts.Bias)
switch bias {
case "pull-up":
opts = append(opts, gpiocdev.WithPullUp)
case "pull-down":
opts = append(opts, gpiocdev.WithPullDown)
case "disable":
opts = append(opts, gpiocdev.WithBiasDisabled)
case "as-is":
fallthrough
default:
}
if getOpts.AbiV != 0 {
opts = append(opts, gpiocdev.WithABIVersion(getOpts.AbiV))
}
return opts
}
func parseOffsets(args []string) ([]int, error) {
oo := []int(nil)
for _, arg := range args {
o, err := strconv.ParseUint(arg, 10, 64)
if err != nil {
return nil, fmt.Errorf("can't parse offset '%s'", arg)
}
oo = append(oo, int(o))
}
return oo, nil
}