-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgopio.go
185 lines (149 loc) · 3.74 KB
/
gopio.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
// gopio package implements simple API in go to work with sysfs gpios. Read more
// about sysfs here: https://www.kernel.org/doc/Documentation/gpio/sysfs.txt
package gopio
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
)
// Pin struct represents gpio abstraction with methods associated.
type Pin struct {
Id int // Pin id
id string
}
const gpioPath string = "/sys/class/gpio"
// New function initializes gpio by pin id.
func New(id int) (pin *Pin, e error) {
var pinInstance Pin
pin = &pinInstance
pin.Id = id
pin.id = strconv.Itoa(id)
return pin, pin.init()
}
// init function check if current pin id exists. If not, create socket fd
// and export the pin.
func (pin *Pin) init() (e error) {
var (
p string = fmt.Sprintf("%s/gpio%s", gpioPath, pin.id)
ex string = fmt.Sprintf("%s/export", gpioPath)
)
if _, e = os.Stat(p); os.IsNotExist(e) {
var fd *os.File
if fd, e = os.OpenFile(ex, os.O_APPEND|os.O_WRONLY, 0777); e != nil {
return e
}
defer fd.Close()
if _, e = fd.WriteString(pin.id); e != nil {
return e
}
}
return e
}
// Close function unexports pin socket.
func (pin *Pin) Close() (e error) {
var uex string = fmt.Sprintf("%s/unexport", gpioPath)
var fd *os.File
if fd, e = os.OpenFile(uex, os.O_APPEND|os.O_WRONLY, 0777); e != nil {
return e
}
defer fd.Close()
if _, e = fd.WriteString(pin.id); e != nil {
return e
}
return nil
}
// SetMode receives a string that contains new mode to set current pin. First
// check if received mode is valid and then write into the socket.
func (pin *Pin) SetMode(m string) (e error) {
if m != "in" && m != "out" {
return errors.New("Invalid mode.")
}
var (
p string = fmt.Sprintf("%s/gpio%s", gpioPath, pin.id)
dp string = fmt.Sprintf("%s/direction", p)
)
if _, e = os.Stat(p); e != nil {
return e
}
var fd *os.File
if fd, e = os.OpenFile(dp, os.O_APPEND|os.O_WRONLY, 0777); e != nil {
return e
}
defer fd.Close()
if _, e = fd.WriteString(m); e != nil {
return e
}
return nil
}
// GetMode returns current pin mode reading it from direction descriptor
// associated to the pin.
func (pin *Pin) GetMode() (m string, e error) {
var (
p string = fmt.Sprintf("%s/gpio%s", gpioPath, pin.id)
dp string = fmt.Sprintf("%s/direction", p)
)
if _, e = os.Stat(p); e != nil {
return m, e
}
var fd *os.File
if fd, e = os.OpenFile(dp, os.O_RDONLY, 0777); e != nil {
return m, e
}
defer fd.Close()
var c int
data := make([]byte, 3)
if c, e = fd.Read(data); e != nil {
return m, e
}
return strings.TrimSpace(string(data[:c])), e
}
// SetValue receives an int that contains new value to set current pin. First
// check if received value is valid and then write into the socket.
func (pin *Pin) SetValue(v int) (e error) {
if v != 1 && v != 0 {
return errors.New("Invalid value.")
}
var (
p string = fmt.Sprintf("%s/gpio%s", gpioPath, pin.id)
vp string = fmt.Sprintf("%s/value", p)
)
if _, e = os.Stat(p); e != nil {
return e
}
var fd *os.File
if fd, e = os.OpenFile(vp, os.O_APPEND|os.O_WRONLY, 0777); e != nil {
return e
}
defer fd.Close()
if _, e = fd.WriteString(strconv.Itoa(v)); e != nil {
return e
}
return nil
}
// GetValue returns current pin value reading it from direction descriptor
// associated to the pin.
func (pin *Pin) GetValue() (v int, e error) {
var (
p string = fmt.Sprintf("%s/gpio%s", gpioPath, pin.id)
vp string = fmt.Sprintf("%s/value", p)
)
if _, e = os.Stat(p); e != nil {
return -1, e
}
var fd *os.File
if fd, e = os.OpenFile(vp, os.O_RDONLY, 0777); e != nil {
return -1, e
}
defer fd.Close()
d := make([]byte, 1)
if _, e = fd.Read(d); e != nil {
return -1, e
}
var res string = strings.TrimSpace(string(d))
if v, e = strconv.Atoi(res); e != nil {
return -1, e
}
return v, nil
}