-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio.go
215 lines (198 loc) · 4.48 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package io manages GPIO pins
package io
import (
"fmt"
"os"
"time"
"golang.org/x/sys/unix"
)
// Mode
const (
IN = iota // Default
OUT = iota
)
// Edge
const (
NONE = iota // Default
RISING = iota
FALLING = iota
BOTH = iota
)
const (
gpioBaseDir = "/sys/class/gpio/"
gpioExportFile = gpioBaseDir + "export"
gpioUnexportFile = gpioBaseDir + "unexport"
gpioDirectionFile = "/direction"
gpioValueFile = "/value"
)
// Gpio represents one GPIO pin.
type Gpio struct {
number int
value *os.File
buf []byte
direction int
edge int
pollfd []unix.PollFd
}
// OutputPin opens a GPIO pin and sets the direction as OUTPUT.
func OutputPin(gpio int) (*Gpio, error) {
g, err := Pin(gpio)
if err != nil {
return nil, err
}
err = g.Direction(OUT)
if err != nil {
g.Close()
return nil, err
}
return g, nil
}
// Pin opens a GPIO pin as an input (by default)
func Pin(gpio int) (*Gpio, error) {
g := new(Gpio)
g.number = gpio
g.buf = make([]byte, 1)
vFile := fmt.Sprintf("%sgpio%d%s", gpioBaseDir, gpio, gpioValueFile)
err := export(vFile, gpioExportFile, gpio)
if err != nil {
return nil, err
}
err = g.Direction(IN)
if err != nil {
unexport(gpioUnexportFile, gpio)
return nil, err
}
err = g.Edge(NONE)
if err != nil {
unexport(gpioUnexportFile, gpio)
return nil, err
}
g.value, err = os.OpenFile(fmt.Sprintf("%sgpio%d%s", gpioBaseDir, gpio, gpioValueFile), os.O_RDWR, 0600)
if err != nil {
unexport(gpioUnexportFile, gpio)
return nil, err
}
g.pollfd = []unix.PollFd{{int32(g.value.Fd()), unix.POLLIN | unix.POLLPRI | unix.POLLERR, 0}}
return g, nil
}
// Direction sets the mode (direction) of the GPIO pin.
func (g *Gpio) Direction(d int) error {
var s string
switch d {
case IN:
s = "in"
case OUT:
s = "out"
default:
return os.ErrInvalid
}
err := writeFile(fmt.Sprintf("%sgpio%d/direction", gpioBaseDir, g.number), s)
if err == nil {
g.direction = d
}
return err
}
// Edge sets the edge detection on the GPIO pin.
func (g *Gpio) Edge(e int) error {
if g.direction != IN {
return fmt.Errorf("gpio%d: not set as an input pin", g.number)
}
var s string
switch e {
case NONE:
s = "none"
case RISING:
s = "rising"
case FALLING:
s = "falling"
case BOTH:
s = "both"
default:
return os.ErrInvalid
}
err := writeFile(fmt.Sprintf("%sgpio%d/edge", gpioBaseDir, g.number), s)
if err == nil {
g.edge = e
}
return err
}
// Set the output of the GPIO pin (only valid for OUTPUT pins)
func (g *Gpio) Set(v int) error {
if g.direction != OUT {
return fmt.Errorf("gpio%d: is not output", g.number)
}
if v == 0 {
g.buf[0] = '0'
} else if v == 1 {
g.buf[0] = '1'
} else {
return os.ErrInvalid
}
_, err := g.value.WriteAt(g.buf, 0)
return err
}
// Get returns the current value of the GPIO pin.
func (g *Gpio) Get() (int, error) {
return g.GetTimeout(0)
}
// GetTimeout is used when detecting edges, and a timeout is required.
// A timeout of 0 is interpreted as no timeout.
func (g *Gpio) GetTimeout(tout time.Duration) (int, error) {
if g.edge != NONE {
// Wait for edge using poll with optional timeout.
var tout_ms int
if tout == 0 {
tout_ms = -1
} else {
tout_ms = int(tout.Milliseconds())
}
for {
g.pollfd[0].Revents = 0
_, err := unix.Poll(g.pollfd, tout_ms)
switch err {
case nil:
// Successful call
case unix.EAGAIN:
continue
case unix.EINTR:
continue
default:
return 0, err
}
break
}
// Check for timeout.
if tout != 0 && (g.pollfd[0].Revents&unix.POLLIN) == 0 {
return 0, os.ErrDeadlineExceeded
}
}
_, err := g.value.ReadAt(g.buf, 0)
if err != nil {
return 0, err
}
if g.buf[0] == '0' {
return 0, nil
} else if g.buf[0] == '1' {
return 1, nil
} else {
return 0, fmt.Errorf("gpio%d: unknown value %s", g.number, g.buf)
}
}
// Close the GPIO pin and unexport it.
func (g *Gpio) Close() {
g.value.Close()
unexport(gpioUnexportFile, g.number)
}