-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.go
47 lines (40 loc) · 921 Bytes
/
setup.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
package gopio
/*
Functions in this file: New, Close
*/
import (
"fmt"
"io/ioutil"
)
func New(pnum int, dir DIR) (pin Pin, err error) {
if pnum == 27 {
pnum = 0
} else {
pnum = PIN_NUMS[pnum] // converts the physical pin number to BCM numbering
if pnum == 0 {
return Pin{}, fmt.Errorf("INVALID PIN NUMBER")
}
}
file := fmt.Sprintf("%s/export", basedir)
// export the pin
err = ioutil.WriteFile(file, []byte(fmt.Sprintf("%d", pnum)), 0666)
if err != nil {
return Pin{}, err
}
// specify the direction
file = fmt.Sprintf("%s/gpio%d/direction", basedir, pnum)
err = ioutil.WriteFile(file, []byte(dir), 0666)
if err != nil {
return Pin{}, err
}
pin = Pin{Num: pnum, Dir: dir}
return pin, err
}
func (p *Pin) Close() error {
file := fmt.Sprintf("%s/unexport", basedir)
err := ioutil.WriteFile(file, []byte(fmt.Sprintf("%d", p.Num)), 0666)
if err != nil {
return err
}
return nil
}