forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeaglebone_adaptor_test.go
109 lines (91 loc) · 2.67 KB
/
beaglebone_adaptor_test.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
package beaglebone
import (
"errors"
"strings"
"testing"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/sysfs"
)
func TestBeagleboneAdaptor(t *testing.T) {
glob = func(pattern string) (matches []string, err error) {
return make([]string, 2), nil
}
fs := sysfs.NewMockFilesystem([]string{
"/dev/i2c-1",
"/sys/devices/bone_capemgr.4",
"/sys/devices/ocp.3",
"/sys/devices/ocp.3/gpio-leds.8/leds/beaglebone:green:usr1/brightness",
"/sys/devices/ocp.3/helper.5",
"/sys/devices/ocp.3/helper.5/AIN1",
"/sys/devices/ocp.3/pwm_test_P9_14.5",
"/sys/devices/ocp.3/pwm_test_P9_14.5/run",
"/sys/devices/ocp.3/pwm_test_P9_14.5/period",
"/sys/devices/ocp.3/pwm_test_P9_14.5/polarity",
"/sys/devices/ocp.3/pwm_test_P9_14.5/duty",
"/sys/class/gpio/export",
"/sys/class/gpio/unexport",
"/sys/class/gpio/gpio60/value",
"/sys/class/gpio/gpio60/direction",
"/sys/class/gpio/gpio10/value",
"/sys/class/gpio/gpio10/direction",
})
sysfs.SetFilesystem(fs)
a := NewBeagleboneAdaptor("myAdaptor")
a.slots = "/sys/devices/bone_capemgr.4"
a.ocp = "/sys/devices/ocp.3"
a.Connect()
a.helper = "/sys/devices/ocp.3/helper.5"
// PWM
glob = func(pattern string) (matches []string, err error) {
pattern = strings.TrimSuffix(pattern, "*")
return []string{pattern + "5"}, nil
}
gobot.Assert(t, a.PwmWrite("P9_99", 175), errors.New("Not a valid pin"))
a.PwmWrite("P9_14", 175)
gobot.Assert(
t,
fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/period"].Contents,
"500000",
)
gobot.Assert(
t,
fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/duty"].Contents,
"343137",
)
a.ServoWrite("P9_14", 100)
gobot.Assert(
t,
fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/period"].Contents,
"16666666",
)
gobot.Assert(
t,
fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/duty"].Contents,
"1898148",
)
// Analog
fs.Files["/sys/devices/ocp.3/helper.5/AIN1"].Contents = "567\n"
i, _ := a.AnalogRead("P9_40")
gobot.Assert(t, i, 567)
i, err := a.AnalogRead("P9_99")
gobot.Assert(t, err, errors.New("Not a valid pin"))
// DigitalIO
a.DigitalWrite("usr1", 1)
gobot.Assert(t,
fs.Files["/sys/devices/ocp.3/gpio-leds.8/leds/beaglebone:green:usr1/brightness"].Contents,
"1",
)
a.DigitalWrite("P9_12", 1)
gobot.Assert(t, fs.Files["/sys/class/gpio/gpio60/value"].Contents, "1")
gobot.Assert(t, a.DigitalWrite("P9_99", 1), errors.New("Not a valid pin"))
fs.Files["/sys/class/gpio/gpio10/value"].Contents = "1"
i, _ = a.DigitalRead("P8_31")
gobot.Assert(t, i, 1)
// I2c
sysfs.SetSyscall(&sysfs.MockSyscall{})
a.I2cStart(0xff)
a.I2cWrite([]byte{0x00, 0x01})
data, _ := a.I2cRead(2)
gobot.Assert(t, data, []byte{0x00, 0x01})
gobot.Assert(t, len(a.Finalize()), 0)
}