-
Notifications
You must be signed in to change notification settings - Fork 0
/
mon.go
167 lines (157 loc) · 4.33 KB
/
mon.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
// SPDX-FileCopyrightText: 2019 Kent Gibson <warthog618@gmail.com>
//
// SPDX-License-Identifier: MIT
package main
import (
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/spf13/cobra"
"github.com/warthog618/go-gpiocdev"
)
func init() {
monCmd.Flags().BoolVarP(&monOpts.ActiveLow, "active-low", "l", false, "treat the line state as active low")
monCmd.Flags().StringVarP(&monOpts.Bias, "bias", "b", "as-is", "set the line bias")
monCmd.Flags().DurationVarP(&monOpts.DebouncePeriod, "debounce-period", "d", 0, "set the line debounce period")
monCmd.Flags().StringVarP(&monOpts.Edge, "edge", "e", "both", "select the edge detection")
monCmd.Flags().UintVarP(&monOpts.NumEvents, "num-events", "n", 0, "exit after n edges")
monCmd.Flags().BoolVarP(&monOpts.Quiet, "quiet", "q", false, "don't display event details")
monCmd.Flags().IntVar(&monOpts.AbiV, "abiv", 0, "use specified ABI version.")
monCmd.Flags().MarkHidden("abiv")
monCmd.SetHelpTemplate(monCmd.HelpTemplate() + extendedMonHelp)
rootCmd.AddCommand(monCmd)
}
var extendedMonHelp = `
Edges:
both: both rising and falling edge events are detected
and reported
rising: only rising edge events are detected and reported
falling: only falling edge events are detected and reported
Biases:
as-is: leave bias unchanged
disable: disable bias
pull-up: enable pull-up
pull-down: enable pull-down
`
var (
monCmd = &cobra.Command{
Use: "mon [flags] <chip> <offset1>...",
Short: "Monitor the state of a line or lines",
Long: `Wait for events on GPIO lines and print them to standard output.`,
Args: cobra.MinimumNArgs(2),
RunE: mon,
DisableFlagsInUseLine: true,
}
monOpts = struct {
ActiveLow bool
Bias string
Edge string
Quiet bool
NumEvents uint
DebouncePeriod time.Duration
AbiV int
}{}
)
func mon(cmd *cobra.Command, args []string) error {
name := args[0]
oo, err := parseOffsets(args[1:])
if err != nil {
return err
}
copts := []gpiocdev.ChipOption{gpiocdev.WithConsumer("gpiocdevctl-mon")}
if monOpts.AbiV != 0 {
copts = append(copts, gpiocdev.WithABIVersion(monOpts.AbiV))
}
c, err := gpiocdev.NewChip(name, copts...)
if err != nil {
return err
}
defer c.Close()
evtchan := make(chan gpiocdev.LineEvent)
eh := func(evt gpiocdev.LineEvent) {
evtchan <- evt
}
opts := makeMonOpts(eh)
l, err := c.RequestLines(oo, opts...)
if err != nil {
return fmt.Errorf("error requesting GPIO lines: %s", err)
}
defer l.Close()
monWait(evtchan)
return nil
}
func monWait(evtchan <-chan gpiocdev.LineEvent) {
sigdone := make(chan os.Signal, 1)
signal.Notify(sigdone, os.Interrupt, syscall.SIGTERM)
defer signal.Stop(sigdone)
count := uint(0)
for {
select {
case evt := <-evtchan:
if !monOpts.Quiet {
t := time.Now()
edge := "rising"
if evt.Type == gpiocdev.LineEventFallingEdge {
edge = "falling"
}
if evt.Seqno != 0 {
fmt.Printf("event: #%d(%d)%3d %-7s %s (%s)\n",
evt.Seqno,
evt.LineSeqno,
evt.Offset,
edge,
t.Format(time.RFC3339Nano),
evt.Timestamp)
} else {
fmt.Printf("event:%3d %-7s %s (%s)\n",
evt.Offset,
edge,
t.Format(time.RFC3339Nano),
evt.Timestamp)
}
}
count++
if monOpts.NumEvents > 0 && count >= monOpts.NumEvents {
return
}
case <-sigdone:
return
}
}
}
func makeMonOpts(eh gpiocdev.EventHandler) []gpiocdev.LineReqOption {
opts := []gpiocdev.LineReqOption{gpiocdev.WithEventHandler(eh)}
if monOpts.ActiveLow {
opts = append(opts, gpiocdev.AsActiveLow)
}
edge := strings.ToLower(monOpts.Edge)
switch edge {
case "falling":
opts = append(opts, gpiocdev.WithFallingEdge)
case "rising":
opts = append(opts, gpiocdev.WithRisingEdge)
case "both":
fallthrough
default:
opts = append(opts, gpiocdev.WithBothEdges)
}
bias := strings.ToLower(monOpts.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 monOpts.DebouncePeriod != 0 {
opts = append(opts, gpiocdev.WithDebounce(monOpts.DebouncePeriod))
}
return opts
}