-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlines_linux.go
77 lines (67 loc) · 1.82 KB
/
lines_linux.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
package gpio
import (
"fmt"
"runtime"
"unsafe"
"github.com/mkch/gpio/internal/sys"
"golang.org/x/sys/unix"
)
// Line is an opened GPIO line.
type Line Lines
func (l *Line) Close() (err error) {
return (*Lines)(l).Close()
}
// Value returns the current value of the GPIO line. 1 (high) or 0 (low).
func (l *Line) Value() (value byte, err error) {
values, err := (*Lines)(l).Values()
if err != nil {
return
}
value = values[0]
return
}
// SetValue sets the value of the GPIO line.
// Value should be 0 (low) or 1 (high), anything else than 0 will be interpreted as 1 (high).
func (l *Line) SetValue(value byte) (err error) {
var values = [1]byte{value}
err = (*Lines)(l).SetValues(values[:])
runtime.KeepAlive(values)
return
}
// Lines is a batch of opened GPIO lines.
type Lines struct {
fd int
numLines int
}
func (l *Lines) Close() (err error) {
err = unix.Close(l.fd)
l.fd = -1
return
}
// Values returns the current values of the GPIO lines. 1 (high) or 0 (low).
func (l *Lines) Values() (values []byte, err error) {
var arg [64]byte
err = sys.Ioctl(l.fd, sys.GPIOHANDLE_GET_LINE_VALUES_IOCTL, uintptr(unsafe.Pointer(&arg[0])))
if err != nil {
err = fmt.Errorf("get GPIO line values failed: %w", err)
return
}
values = arg[:l.numLines]
return
}
// SetValue sets the value of the GPIO line.
// Value should be 0 (low) or 1 (high), anything else than 0 will be interpreted as 1 (high).
func (l *Lines) SetValues(values []byte) (err error) {
if len(values) > 64 {
err = fmt.Errorf("set GPIO line values failed: length of values(%v) > 64", len(values))
}
var arg [64]byte
copy(arg[:], values)
err = sys.Ioctl(l.fd, sys.GPIOHANDLE_SET_LINE_VALUES_IOCTL, uintptr(unsafe.Pointer(&arg[0])))
runtime.KeepAlive(arg)
if err != nil {
err = fmt.Errorf("set GPIO line values failed: %w", err)
return
}
return
}