-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsysfs.go
140 lines (125 loc) · 2.83 KB
/
sysfs.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
package gpio
import (
"fmt"
"os"
"strconv"
"time"
)
type direction uint
const (
inDirection direction = iota
outDirection
)
type edge uint
const (
edgeNone edge = iota
edgeRising
edgeFalling
edgeBoth
)
func exportGPIO(p Pin) {
export, err := os.OpenFile("/sys/class/gpio/export", os.O_WRONLY, 0660)
if err != nil {
fmt.Printf("failed to open gpio export file for writing\n")
//os.Exit(1)
return
}
defer export.Close()
export.Write([]byte(strconv.Itoa(int(p.Number))))
time.Sleep(100 * time.Millisecond)
}
func unexportGPIO(p Pin) {
export, err := os.OpenFile("/sys/class/gpio/unexport", os.O_WRONLY, 0660)
if err != nil {
fmt.Printf("failed to open gpio unexport file for writing\n")
//os.Exit(1)
return
}
defer export.Close()
export.Write([]byte(strconv.Itoa(int(p.Number))))
}
func setDirection(p Pin, d direction, initialValue uint) {
dir, err := os.OpenFile(fmt.Sprintf("/sys/class/gpio/gpio%d/direction", p.Number), os.O_WRONLY, 0660)
if err != nil {
fmt.Printf("failed to open gpio %d direction file for writing\n", p.Number)
//os.Exit(1)
return
}
defer dir.Close()
switch {
case d == inDirection:
dir.Write([]byte("in"))
case d == outDirection && initialValue == 0:
dir.Write([]byte("low"))
case d == outDirection && initialValue == 1:
dir.Write([]byte("high"))
default:
panic(fmt.Sprintf("setDirection called with invalid direction or initialValue, %d, %d", d, initialValue))
}
}
func setEdgeTrigger(p Pin, e edge) {
edge, err := os.OpenFile(fmt.Sprintf("/sys/class/gpio/gpio%d/edge", p.Number), os.O_WRONLY, 0660)
if err != nil {
fmt.Printf("failed to open gpio %d edge file for writing\n", p.Number)
//os.Exit(1)
return
}
defer edge.Close()
switch e {
case edgeNone:
edge.Write([]byte("none"))
case edgeRising:
edge.Write([]byte("rising"))
case edgeFalling:
edge.Write([]byte("falling"))
case edgeBoth:
edge.Write([]byte("both"))
default:
panic(fmt.Sprintf("setEdgeTrigger called with invalid edge %d", e))
}
}
func openPin(p Pin, write bool) Pin {
flags := os.O_RDONLY
if write {
flags = os.O_RDWR
}
f, err := os.OpenFile(fmt.Sprintf("/sys/class/gpio/gpio%d/value", p.Number), flags, 0660)
if err != nil {
fmt.Printf("failed to open gpio %d value file for reading\n", p.Number)
//os.Exit(1)
return p
}
p.f = f
return p
}
func readPin(p Pin) (val uint, err error) {
file := p.f
file.Seek(0, 0)
buf := make([]byte, 1)
_, err = file.Read(buf)
if err != nil {
return 0, err
}
c := buf[0]
switch c {
case '0':
return 0, nil
case '1':
return 1, nil
default:
return 0, fmt.Errorf("read inconsistent value in pinfile, %c", c)
}
}
func writePin(p Pin, v uint) error {
var buf []byte
switch v {
case 0:
buf = []byte{'0'}
case 1:
buf = []byte{'1'}
default:
return fmt.Errorf("invalid output value %d", v)
}
_, err := p.f.Write(buf)
return err
}