forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
beaglebone_adaptor.go
338 lines (296 loc) · 7.78 KB
/
beaglebone_adaptor.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package beaglebone
import (
"bufio"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
multierror "github.com/hashicorp/go-multierror"
"gobot.io/x/gobot"
"gobot.io/x/gobot/drivers/i2c"
"gobot.io/x/gobot/sysfs"
)
var glob = func(pattern string) (matches []string, err error) {
return filepath.Glob(pattern)
}
// Adaptor is the gobot.Adaptor representation for the Beaglebone
type Adaptor struct {
name string
kernel string
digitalPins []sysfs.DigitalPin
pwmPins map[string]*pwmPin
i2cBuses map[int]sysfs.I2cDevice
usrLed string
ocp string
analogPath string
analogPinMap map[string]string
slots string
}
// NewAdaptor returns a new Beaglebone Adaptor
func NewAdaptor() *Adaptor {
b := &Adaptor{
name: gobot.DefaultName("Beaglebone"),
digitalPins: make([]sysfs.DigitalPin, 120),
pwmPins: make(map[string]*pwmPin),
i2cBuses: make(map[int]sysfs.I2cDevice),
}
b.setSlots()
return b
}
func (b *Adaptor) setSlots() {
ocp := "/sys/devices/ocp.*"
slots := "/sys/devices/bone_capemgr.*"
b.kernel = getKernel()
if b.kernel[:1] == "4" {
ocp = "/sys/devices/platform/ocp/ocp*"
slots = "/sys/devices/platform/bone_capemgr"
}
b.usrLed = "/sys/class/leds/beaglebone:green:"
g, _ := glob(ocp)
b.ocp = g[0]
g, _ = glob(slots)
b.slots = fmt.Sprintf("%v/slots", g[0])
}
// Name returns the Adaptor name
func (b *Adaptor) Name() string { return b.name }
// SetName sets the Adaptor name
func (b *Adaptor) SetName(n string) { b.name = n }
// Kernel returns the Linux kernel version for the BeagleBone
func (b *Adaptor) Kernel() string { return b.kernel }
// Connect initializes the pwm and analog dts.
func (b *Adaptor) Connect() error {
// enable analog
if b.kernel[:1] == "4" {
if err := ensureSlot(b.slots, "BB-ADC"); err != nil {
return err
}
b.analogPath = "/sys/bus/iio/devices/iio:device0"
b.analogPinMap = analogPins44
} else {
if err := ensureSlot(b.slots, "cape-bone-iio"); err != nil {
return err
}
g, err := glob(fmt.Sprintf("%v/helper.*", b.ocp))
if err != nil {
return err
}
b.analogPath = g[0]
b.analogPinMap = analogPins3
}
// enable pwm
if err := ensureSlot(b.slots, "am33xx_pwm"); err != nil {
return err
}
return nil
}
// Finalize releases all i2c devices and exported analog, digital, pwm pins.
func (b *Adaptor) Finalize() (err error) {
for _, pin := range b.pwmPins {
if pin != nil {
if e := pin.release(); e != nil {
err = multierror.Append(err, e)
}
}
}
for _, pin := range b.digitalPins {
if pin != nil {
if e := pin.Unexport(); e != nil {
err = multierror.Append(err, e)
}
}
}
for _, bus := range b.i2cBuses {
if bus != nil {
if e := bus.Close(); e != nil {
err = multierror.Append(err, e)
}
}
}
return
}
// PwmWrite writes the 0-254 value to the specified pin
func (b *Adaptor) PwmWrite(pin string, val byte) (err error) {
return b.pwmWrite(pin, val)
}
// ServoWrite writes the 0-180 degree val to the specified pin.
func (b *Adaptor) ServoWrite(pin string, val byte) (err error) {
i, err := b.pwmPin(pin)
if err != nil {
return err
}
period := 16666666.0
duty := (gobot.FromScale(float64(val), 0, 180.0) * 0.115) + 0.05
return b.pwmPins[i].pwmWrite(strconv.Itoa(int(period)), strconv.Itoa(int(period*duty)))
}
// DigitalRead returns a digital value from specified pin
func (b *Adaptor) DigitalRead(pin string) (val int, err error) {
sysfsPin, err := b.digitalPin(pin, sysfs.IN)
if err != nil {
return
}
return sysfsPin.Read()
}
// DigitalWrite writes a digital value to specified pin.
// valid usr pin values are usr0, usr1, usr2 and usr3
func (b *Adaptor) DigitalWrite(pin string, val byte) (err error) {
if strings.Contains(pin, "usr") {
fi, err := sysfs.OpenFile(b.usrLed+pin+"/brightness", os.O_WRONLY|os.O_APPEND, 0666)
defer fi.Close()
if err != nil {
return err
}
_, err = fi.WriteString(strconv.Itoa(int(val)))
return err
}
sysfsPin, err := b.digitalPin(pin, sysfs.OUT)
if err != nil {
return err
}
return sysfsPin.Write(int(val))
}
// AnalogRead returns an analog value from specified pin
func (b *Adaptor) AnalogRead(pin string) (val int, err error) {
analogPin, err := b.translateAnalogPin(pin)
if err != nil {
return
}
fi, err := sysfs.OpenFile(fmt.Sprintf("%v/%v", b.analogPath, analogPin), os.O_RDONLY, 0644)
defer fi.Close()
if err != nil {
return
}
var buf = make([]byte, 1024)
_, err = fi.Read(buf)
if err != nil {
return
}
val, _ = strconv.Atoi(strings.Split(string(buf), "\n")[0])
return
}
// GetConnection returns a connection to a device on a specified bus.
// Valid bus number is either 0 or 2 which corresponds to /dev/i2c-0 or /dev/i2c-2.
func (b *Adaptor) GetConnection(address int, bus int) (connection i2c.Connection, err error) {
if (bus != 0) && (bus != 2) {
return nil, fmt.Errorf("Bus number %d out of range", bus)
}
if b.i2cBuses[bus] == nil {
b.i2cBuses[bus], err = sysfs.NewI2cDevice(fmt.Sprintf("/dev/i2c-%d", bus))
}
return i2c.NewConnection(b.i2cBuses[bus], address), err
}
// GetDefaultBus returns the default i2c bus for this platform
func (b *Adaptor) GetDefaultBus() int {
return 2
}
// translatePin converts digital pin name to pin position
func (b *Adaptor) translatePin(pin string) (value int, err error) {
for key, value := range pins {
if key == pin {
return value, nil
}
}
err = errors.New("Not a valid pin")
return
}
// translatePwmPin converts pwm pin name to pin position
func (b *Adaptor) translatePwmPin(pin string) (value string, err error) {
for key, value := range pwmPins {
if key == pin {
return value, nil
}
}
err = errors.New("Not a valid pin")
return
}
// translateAnalogPin converts analog pin name to pin position
func (b *Adaptor) translateAnalogPin(pin string) (value string, err error) {
for key, value := range b.analogPinMap {
if key == pin {
return value, nil
}
}
err = errors.New("Not a valid pin")
return
}
// digitalPin retrieves digital pin value by name
func (b *Adaptor) digitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPin, err error) {
i, err := b.translatePin(pin)
if err != nil {
return
}
if b.digitalPins[i] == nil {
b.digitalPins[i] = sysfs.NewDigitalPin(i)
err := b.digitalPins[i].Export()
if err != nil {
return nil, err
}
}
if err = b.digitalPins[i].Direction(dir); err != nil {
return
}
return b.digitalPins[i], nil
}
// pwPin retrieves pwm pin value by name
func (b *Adaptor) pwmPin(pin string) (i string, err error) {
i, err = b.translatePwmPin(pin)
if err != nil {
return
}
if b.pwmPins[i] == nil {
err = ensureSlot(b.slots, fmt.Sprintf("bone_pwm_%v", pin))
if err != nil {
return
}
b.pwmPins[i], err = newPwmPin(i, b.ocp)
if err != nil {
return
}
}
return
}
// pwmWrite writes pwm value to specified pin
func (b *Adaptor) pwmWrite(pin string, val byte) (err error) {
i, err := b.pwmPin(pin)
if err != nil {
return
}
period := 500000.0
duty := gobot.FromScale(float64(val), 0, 255.0)
return b.pwmPins[i].pwmWrite(strconv.Itoa(int(period)), strconv.Itoa(int(period*duty)))
}
func ensureSlot(slots, item string) (err error) {
fi, err := sysfs.OpenFile(slots, os.O_RDWR|os.O_APPEND, 0666)
defer fi.Close()
if err != nil {
return
}
// ensure the slot is not already written into the capemanager
// (from: https://github.com/mrmorphic/hwio/blob/master/module_bb_pwm.go#L190)
scanner := bufio.NewScanner(fi)
for scanner.Scan() {
line := scanner.Text()
if strings.Index(line, item) > 0 {
return
}
}
_, err = fi.WriteString(item)
if err != nil {
return err
}
fi.Sync()
scanner = bufio.NewScanner(fi)
for scanner.Scan() {
line := scanner.Text()
if strings.Index(line, item) > 0 {
return
}
}
return
}
func getKernel() string {
result, _ := exec.Command("uname", "-r").Output()
return strings.TrimSpace(string(result))
}