-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio.go
139 lines (113 loc) · 2.86 KB
/
gpio.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
package goartik
import (
"fmt"
"io/ioutil"
"os"
"strconv"
)
const (
// IN : pin in mode
IN = PinMode("in")
// OUT : pin out mode
OUT = PinMode("out")
// LOW : value 0
LOW = PinValue(0)
// HIGH : value 1
HIGH = PinValue(1)
)
const (
exportTestPath = "/test/device/sys/class/gpio/export"
pinTestPath = "/test/device/sys/class/gpio/gpio%d"
directionTestPath = "/test/device/sys/class/gpio/gpio%d/direction"
valueTestPath = "/test/device/sys/class/gpio/gpio%d/value"
exportPath = "/sys/class/gpio/export"
pinPath = "/sys/class/gpio/gpio%d"
directionPath = "/sys/class/gpio/gpio%d/direction"
valuePath = "/sys/class/gpio/gpio%d/value"
isVirtualDevice = false
)
// PinMode : pin mode
type PinMode string
// PinValue : pin value
type PinValue uint8
func (v PinValue) String() string {
return strconv.FormatUint(uint64(v), 10)
}
// DigitalPinMode : set pin mode
func (m *ArtikModule) DigitalPinMode(pin uint, mode PinMode) error {
pin = m.mapPin(pin)
err := m.exportPin(pin)
if err != nil {
fmt.Println("Can't export pin")
return err
}
var path string
if isVirtualDevice {
path = getRelativePath(fmt.Sprintf(directionTestPath, pin))
} else {
path = fmt.Sprintf(directionPath, pin)
}
err = ioutil.WriteFile(path, []byte(mode), 0644)
if err != nil {
fmt.Println("Can't set pin mode")
return err
}
return nil
}
// DigitalRead : read value of pin
func (m *ArtikModule) DigitalRead(pin uint) (PinValue, error) {
pin = m.mapPin(pin)
var path string
if isVirtualDevice {
path = getRelativePath(fmt.Sprintf(valueTestPath, pin))
} else {
path = fmt.Sprintf(valuePath, pin)
}
b, err := ioutil.ReadFile(path)
if err != nil {
return 0, err
}
return convPinValue(b), nil
}
// DigitalWrite : write value to pin
func (m *ArtikModule) DigitalWrite(pin uint, value PinValue) error {
pin = m.mapPin(pin)
var path string
if isVirtualDevice {
path = getRelativePath(fmt.Sprintf(valueTestPath, pin))
} else {
path = fmt.Sprintf(valuePath, pin)
}
err := ioutil.WriteFile(path, []byte(value.String()), 0664)
if err != nil {
return err
}
return nil
}
func (m *ArtikModule) mapPin(pin uint) uint {
p := strconv.FormatUint(uint64(pin), 10)
mappedPin, ok := m.config.Pins[p]
if ok {
return mappedPin
}
return pin
}
func (m *ArtikModule) exportPin(pin uint) error {
pin = m.mapPin(pin)
if isVirtualDevice {
path := getRelativePath(fmt.Sprintf(pinTestPath, pin))
if _, err := os.Stat(path); os.IsNotExist(err) {
if err := os.Mkdir(path, 0777); err == nil {
ioutil.WriteFile(fmt.Sprintf("%s/direction", path), []byte{}, 0664)
ioutil.WriteFile(fmt.Sprintf("%s/value", path), []byte("0"), 0664)
}
}
return nil
}
s := strconv.FormatUint(uint64(pin), 10)
return ioutil.WriteFile(exportPath, []byte(s), 0644)
}
func convPinValue(b []byte) PinValue {
v, _ := strconv.Atoi(string(b))
return PinValue(v)
}