forked from brian-armstrong/gpio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sysfs.go
165 lines (145 loc) · 3.26 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
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
package gpio
import (
"errors"
"fmt"
"os"
"strconv"
)
type direction uint
const (
inDirection direction = iota
outDirection
)
type Edge uint
const (
EdgeNone Edge = iota
EdgeRising
EdgeFalling
EdgeBoth
)
type LogicLevel uint
const (
ActiveHigh LogicLevel = iota
ActiveLow
)
type Value uint
const (
Inactive Value = 0
Active Value = 1
)
func exportGPIO(p Pin) {
export, err := os.OpenFile("/sys/class/gpio/export", os.O_WRONLY, 0600)
if err != nil {
fmt.Printf("failed to open gpio export file for writing\n")
os.Exit(1)
}
defer export.Close()
export.Write([]byte(strconv.Itoa(int(p.Number))))
}
func unexportGPIO(p Pin) {
export, err := os.OpenFile("/sys/class/gpio/unexport", os.O_WRONLY, 0600)
if err != nil {
fmt.Printf("failed to open gpio unexport file for writing\n")
os.Exit(1)
}
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, 0600)
if err != nil {
fmt.Printf("failed to open gpio %d direction file for writing\n", p.Number)
os.Exit(1)
}
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, 0600)
if err != nil {
fmt.Printf("failed to open gpio %d edge file for writing\n", p.Number)
os.Exit(1)
}
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 setLogicLevel(p Pin, l LogicLevel) error {
level, err := os.OpenFile(fmt.Sprintf("/sys/class/gpio/gpio%d/active_low", p.Number), os.O_WRONLY, 0600)
if err != nil {
return err
}
defer level.Close()
switch l {
case ActiveHigh:
level.Write([]byte("0"))
case ActiveLow:
level.Write([]byte("1"))
default:
return errors.New("Invalid logic level setting.")
}
return nil
}
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, 0600)
if err != nil {
fmt.Printf("failed to open gpio %d value file for reading\n", p.Number)
os.Exit(1)
}
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
}