-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.go
More file actions
148 lines (138 loc) · 2.98 KB
/
main.go
File metadata and controls
148 lines (138 loc) · 2.98 KB
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
// Copyright 2016 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
// gpio-list prints out the function of each GPIO pin.
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"sort"
"periph.io/x/conn/v3/gpio"
"periph.io/x/conn/v3/gpio/gpioreg"
"periph.io/x/conn/v3/pin"
"periph.io/x/conn/v3/pin/pinreg"
"periph.io/x/host/v3"
)
func printAliases(invalid bool) {
max := 0
aliases := gpioreg.Aliases()
names := make([]string, 0, len(aliases))
m := make(map[string]gpio.PinIO, len(aliases))
for _, p := range aliases {
n := p.Name()
names = append(names, n)
m[n] = p
if l := len(n); l > max {
max = l
}
}
sort.Strings(names)
for _, name := range names {
p := m[name]
if r, ok := p.(gpio.RealPin); ok {
p = r.Real()
}
if invalid || p.String() != "INVALID" {
fmt.Printf("%-*s: %s\n", max, name, p)
}
}
}
func altFuncs(p pin.Pin) string {
r, ok := p.(gpio.RealPin)
if ok {
p = r.Real()
}
alt, ok := p.(pin.PinFunc)
if !ok {
return ""
}
fn := alt.Func()
out := ""
for _, f := range alt.SupportedFuncs() {
if f == gpio.IN || f == gpio.OUT || f == fn {
continue
}
if out != "" {
out += ", "
}
out += string(f)
}
return out
}
func printGPIO(invalid, showFunctions bool) {
maxName := 0
maxFn := len("Func")
maxAltFn := len("Alt")
all := gpioreg.All()
for _, p := range all {
if invalid || pinreg.IsConnected(p) {
if l := len(p.String()); l > maxName {
maxName = l
}
if l := len(p.Function()); l > maxFn {
maxFn = l
}
if l := len(altFuncs(p)); l > maxAltFn {
maxAltFn = l
}
}
}
for _, p := range all {
connected := pinreg.IsConnected(p)
if !connected && !invalid {
continue
}
fmt.Printf("%-*s: %-*s", maxName, p, maxFn, p.Function())
if showFunctions {
fmt.Printf(" %-*s", maxAltFn, altFuncs(p))
}
if !connected {
fmt.Printf(" (not connected)")
}
fmt.Printf("\n")
}
}
func mainImpl() error {
all := flag.Bool("a", false, "print everything")
aliases := flag.Bool("l", false, "print aliases pins (e.g. I2C1_SCL)")
gpios := flag.Bool("g", false, "print GPIO pins (e.g. GPIO1) (default)")
invalid := flag.Bool("n", false, "show not connected/INVALID pins")
showFunctions := flag.Bool("f", false, "show all alternate functions")
verbose := flag.Bool("v", false, "verbose mode")
flag.Parse()
if !*verbose {
log.SetOutput(ioutil.Discard)
}
log.SetFlags(log.Lmicroseconds)
if flag.NArg() != 0 {
return errors.New("unexpected argument, try -help")
}
if *all {
*aliases = true
*gpios = true
*invalid = true
*showFunctions = true
} else if !*aliases && !*gpios {
*gpios = true
}
if _, err := host.Init(); err != nil {
return err
}
if *aliases {
printAliases(*invalid)
}
if *gpios {
printGPIO(*invalid, *showFunctions)
}
return nil
}
func main() {
if err := mainImpl(); err != nil {
fmt.Fprintf(os.Stderr, "gpio-list: %s.\n", err)
os.Exit(1)
}
}