-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpio.go
181 lines (161 loc) · 4.1 KB
/
gpio.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
179
180
181
package gpio
import (
"fmt"
"io/ioutil"
"os"
"path"
"time"
)
type (
// InputPin is the interface satisfied by GPIO input pins.
InputPin interface {
Read() (bool, error)
}
// InterruptPin is the interface satisfied by GPIO interrupt pins.
InterruptPin interface {
InputPin
Wait(time.Duration) error
}
// OutputPin is the interface satisfied by GPIO output pins.
OutputPin interface {
Write(bool) error
}
// Pin represents a GPIO pin.
Pin struct {
number int
dir string
value string
}
)
// Input initializes a GPIO input pin with the given pin number.
func Input(pinNumber int, activeLow bool) (InputPin, error) {
pin, err := newPin(pinNumber, activeLow)
if err != nil {
return nil, err
}
err = writeFile(path.Join(pin.dir, "direction"), "in")
return pin, err
}
// Interrupt initializes a GPIO interrupt pin with the given pin number.
// The edge parameter must be "rising", "falling", or "both".
func Interrupt(pinNumber int, activeLow bool, edge string) (InterruptPin, error) {
pin, err := newPin(pinNumber, activeLow)
if err != nil {
return nil, err
}
err = writeFile(path.Join(pin.dir, "direction"), "in")
if err != nil {
return pin, err
}
err = writeFile(path.Join(pin.dir, "edge"), edge)
return pin, err
}
var gpioDirection = map[bool]string{true: "high", false: "low"}
// Output initializes a GPIO output pin with the given pin number
// and initial logical value.
func Output(pinNumber int, activeLow bool, initialValue bool) (OutputPin, error) {
pin, err := newPin(pinNumber, activeLow)
if err != nil {
return nil, err
}
// Set direction based on initial *logical* value.
direction := gpioDirection[initialValue != activeLow]
err = writeFile(path.Join(pin.dir, "direction"), direction)
return pin, err
}
func (pin *Pin) Read() (bool, error) {
return readBoolFile(pin.value)
}
func fileExists(path string) (bool, error) {
return existsWithPredicate(path, func(info os.FileInfo) bool {
return info.Mode().IsRegular()
})
}
func directoryExists(path string) (bool, error) {
return existsWithPredicate(path, func(info os.FileInfo) bool {
return info.Mode().IsDir()
})
}
func existsWithPredicate(path string, predicate func(os.FileInfo) bool) (bool, error) {
info, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return predicate(info), nil
}
func pinDirectory(pinNumber int) (string, error) {
const gpioDir = "/sys/class/gpio/"
dir := path.Join(gpioDir, fmt.Sprintf("gpio%d/", pinNumber))
tried := false
for {
exists, err := directoryExists(dir)
if err != nil || exists {
return dir, err
}
if tried {
return dir, fmt.Errorf("failed to export GPIO directory %s", dir)
}
err = writeFile(path.Join(gpioDir, "export"), fmt.Sprintf("%d", pinNumber))
if err != nil {
return dir, err
}
tried = true
// Give udev rules a chance to execute on newly-created gpio%d directory.
time.Sleep(time.Second)
}
}
func newPin(pinNumber int, activeLow bool) (*Pin, error) {
dir, err := pinDirectory(pinNumber)
if err != nil {
return nil, err
}
value := path.Join(dir, "value")
exists, err := fileExists(value)
if err != nil || !exists {
return nil, err
}
err = writeBoolFile(path.Join(dir, "active_low"), activeLow)
if err != nil {
return nil, err
}
return &Pin{number: pinNumber, dir: dir, value: value}, nil
}
func readFile(file string) (string, error) {
v, err := ioutil.ReadFile(file)
return string(v), err
}
func readBoolFile(file string) (bool, error) {
v, err := readFile(file)
if err != nil {
return false, err
}
// compare without trailing '\n'
s := v[:len(v)-1]
switch s {
case "0":
return false, nil
case "1":
return true, nil
default:
return false, fmt.Errorf("read %s from %s instead of boolean value", s, file)
}
}
func (pin *Pin) Write(value bool) error {
return writeBoolFile(pin.value, value)
}
func writeFile(file string, contents string) error {
return ioutil.WriteFile(file, []byte(contents), 0644)
}
func writeBoolFile(file string, value bool) error {
var b string
switch value {
case true:
b = "1"
case false:
b = "0"
}
return writeFile(file, b)
}