Skip to content

Commit

Permalink
Refactor beaglebone tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Nov 9, 2014
1 parent 7c60742 commit aad77ff
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 139 deletions.
50 changes: 0 additions & 50 deletions platforms/beaglebone/analog_pin.go

This file was deleted.

78 changes: 40 additions & 38 deletions platforms/beaglebone/beaglebone_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import (
"github.com/hybridgroup/gobot/sysfs"
)

const (
Slots = "/sys/devices/bone_capemgr.*"
Ocp = "/sys/devices/ocp.*"
UsrLed = "/sys/devices/ocp.3/gpio-leds.8/leds/beaglebone:green:"
)
var slots = "/sys/devices/bone_capemgr.*"
var ocp = "/sys/devices/ocp.*"
var usrLed = "/sys/devices/ocp.3/gpio-leds.8/leds/beaglebone:green:"

var glob = func(pattern string) (matches []string, err error) {
return filepath.Glob(pattern)
}

var pins = map[string]int{
"P8_3": 38,
Expand Down Expand Up @@ -114,32 +116,38 @@ type BeagleboneAdaptor struct {
gobot.Adaptor
digitalPins []sysfs.DigitalPin
pwmPins map[string]*pwmPin
analogPins map[string]*analogPin
i2cDevice io.ReadWriteCloser
connect func()
ocp string
helper string
slots string
}

// NewBeagleboneAdaptor returns a new beaglebone adaptor with specified name
func NewBeagleboneAdaptor(name string) *BeagleboneAdaptor {
return &BeagleboneAdaptor{
b := &BeagleboneAdaptor{
Adaptor: *gobot.NewAdaptor(
name,
"BeagleboneAdaptor",
),
connect: func() {
ensureSlot("cape-bone-iio")
ensureSlot("am33xx_pwm")
},
digitalPins: make([]sysfs.DigitalPin, 120),
pwmPins: make(map[string]*pwmPin),
}

g, _ := glob(ocp)
b.ocp = g[0]
g, _ = glob(fmt.Sprintf("%v/helper.*", b.ocp))
b.helper = g[0]
g, _ = glob(slots)
b.slots = fmt.Sprintf("%v/slots", g[0])

return b
}

// Connect returns true on a succesful connection to beaglebone board.
// It initializes digital, pwm and analog pins
func (b *BeagleboneAdaptor) Connect() bool {
b.digitalPins = make([]sysfs.DigitalPin, 120)
b.pwmPins = make(map[string]*pwmPin)
b.analogPins = make(map[string]*analogPin)
b.connect()
ensureSlot(b.slots, "cape-bone-iio")
ensureSlot(b.slots, "am33xx_pwm")
return true
}

Expand Down Expand Up @@ -187,7 +195,7 @@ func (b *BeagleboneAdaptor) DigitalRead(pin string) (i int) {
// valid usr pin values are usr0, usr1, usr2 and usr3
func (b *BeagleboneAdaptor) DigitalWrite(pin string, val byte) {
if strings.Contains(pin, "usr") {
fi, err := os.OpenFile(UsrLed+pin+"/brightness", os.O_WRONLY|os.O_APPEND, 0666)
fi, err := sysfs.OpenFile(usrLed+pin+"/brightness", os.O_WRONLY|os.O_APPEND, 0666)
defer fi.Close()
if err != nil {
log.Fatal(err)
Expand All @@ -200,8 +208,17 @@ func (b *BeagleboneAdaptor) DigitalWrite(pin string, val byte) {

// AnalogRead returns an analog value from specified pin
func (b *BeagleboneAdaptor) AnalogRead(pin string) int {
i := b.analogPin(pin)
return b.analogPins[i].analogRead()
fi, err := sysfs.OpenFile(fmt.Sprintf("%v/%v", b.helper, b.translateAnalogPin(pin)), os.O_RDONLY, 0644)
defer fi.Close()
if err != nil {
panic(err)
}

var buf = make([]byte, 1024)
fi.Read(buf)

i, _ := strconv.Atoi(strings.Split(string(buf), "\n")[0])
return i
}

// AnalogWrite writes an analog value to specified pin
Expand Down Expand Up @@ -256,15 +273,6 @@ func (b *BeagleboneAdaptor) translateAnalogPin(pin string) string {
panic("Not a valid pin")
}

// analogPin retrieves analog pin value by name
func (b *BeagleboneAdaptor) analogPin(pin string) string {
i := b.translateAnalogPin(pin)
if b.analogPins[i] == nil {
b.analogPins[i] = newAnalogPin(i)
}
return i
}

// digitalPin retrieves digital pin value by name
func (b *BeagleboneAdaptor) digitalPin(pin string, dir string) sysfs.DigitalPin {
i := b.translatePin(pin)
Expand All @@ -283,7 +291,8 @@ func (b *BeagleboneAdaptor) digitalPin(pin string, dir string) sysfs.DigitalPin
func (b *BeagleboneAdaptor) pwmPin(pin string) string {
i := b.translatePwmPin(pin)
if b.pwmPins[i] == nil {
b.pwmPins[i] = newPwmPin(i)
ensureSlot(b.slots, fmt.Sprintf("bone_pwm_%v", pin))
b.pwmPins[i] = newPwmPin(i, b.ocp)
}
return i
}
Expand All @@ -296,15 +305,8 @@ func (b *BeagleboneAdaptor) pwmWrite(pin string, val byte) {
b.pwmPins[i].pwmWrite(strconv.Itoa(int(period)), strconv.Itoa(int(period*duty)))
}

func ensureSlot(item string) {
var err error
var fi *os.File

slot, err := filepath.Glob(Slots)
if err != nil {
panic(err)
}
fi, err = os.OpenFile(fmt.Sprintf("%v/slots", slot[0]), os.O_RDWR|os.O_APPEND, 0666)
func ensureSlot(slots, item string) {
fi, err := sysfs.OpenFile(slots, os.O_RDWR|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
Expand Down
104 changes: 80 additions & 24 deletions platforms/beaglebone/beaglebone_adaptor_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
package beaglebone

import (
"strings"
"testing"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/sysfs"
)

func initTestBeagleboneAdaptor() *BeagleboneAdaptor {
a := NewBeagleboneAdaptor("myAdaptor")
a.connect = func() {}
a.Connect()
a.DigitalWrite("P9_12", 1)
a.i2cDevice = new(gobot.NullReadWriteCloser)
return a
}

func TestBeagleboneAdaptorFinalize(t *testing.T) {
gobot.Assert(t, initTestBeagleboneAdaptor().Finalize(), true)
}

func TestBeagleboneAdaptorDigitalIO(t *testing.T) {
a := initTestBeagleboneAdaptor()

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/duty",
"/sys/class/gpio/export",
"/sys/class/gpio/unexport",
"/sys/class/gpio/gpio60/value",
Expand All @@ -33,23 +32,80 @@ func TestBeagleboneAdaptorDigitalIO(t *testing.T) {
})

sysfs.SetFilesystem(fs)
a := NewBeagleboneAdaptor("myAdaptor")
a.slots = "/sys/devices/bone_capemgr.4"
a.ocp = "/sys/devices/ocp.3"
a.helper = "/sys/devices/ocp.3/helper.5"

a.Connect()

// PWM
glob = func(pattern string) (matches []string, err error) {
pattern = strings.TrimSuffix(pattern, "*")
return []string{pattern + "5"}, nil
}

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,
"156862",
)

a.AnalogWrite("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,
"156862",
)

a.ServoWrite("P9_14", 100)
gobot.Assert(
t,
fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/period"].Contents,
"20000000",
)
gobot.Assert(
t,
fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/duty"].Contents,
"17222222",
)

// Analog
fs.Files["/sys/devices/ocp.3/helper.5/AIN1"].Contents = "567\n"
i := a.AnalogRead("P9_40")
gobot.Assert(t, i, 567)

// 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")

fs.Files["/sys/class/gpio/gpio10/value"].Contents = "1"
i := a.DigitalRead("P8_31")
i = a.DigitalRead("P8_31")
gobot.Assert(t, i, 1)
}

func TestBeagleboneAdaptorI2c(t *testing.T) {
a := initTestBeagleboneAdaptor()
fs := sysfs.NewMockFilesystem([]string{
"/dev/i2c-1",
})
sysfs.SetFilesystem(fs)
// I2c
sysfs.SetSyscall(&sysfs.MockSyscall{})
a.I2cStart(0xff)

a.I2cWrite([]byte{0x00, 0x01})
gobot.Assert(t, a.I2cRead(2), []byte{0x00, 0x01})

gobot.Assert(t, a.Finalize(), true)
}
Loading

0 comments on commit aad77ff

Please sign in to comment.