Skip to content

Commit

Permalink
Tests for digital io and i2c
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Nov 4, 2014
1 parent 0e3d3cf commit 3e5289f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 31 deletions.
27 changes: 10 additions & 17 deletions platforms/beaglebone/beaglebone_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import (
)

const (
Slots = "/sys/devices/bone_capemgr.*"
Ocp = "/sys/devices/ocp.*"
I2CLocation = "/dev/i2c-1"
UsrLed = "/sys/devices/ocp.3/gpio-leds.8/leds/beaglebone:green:"
Slots = "/sys/devices/bone_capemgr.*"
Ocp = "/sys/devices/ocp.*"
UsrLed = "/sys/devices/ocp.3/gpio-leds.8/leds/beaglebone:green:"
)

var i2cLocation = "/dev/i2c-1"

var pins = map[string]int{
"P8_3": 38,
"P8_4": 39,
Expand Down Expand Up @@ -113,7 +114,7 @@ var analogPins = map[string]string{

type BeagleboneAdaptor struct {
gobot.Adaptor
digitalPins []*sysfs.DigitalPin
digitalPins []sysfs.DigitalPin
pwmPins map[string]*pwmPin
analogPins map[string]*analogPin
i2cDevice io.ReadWriteCloser
Expand All @@ -137,7 +138,7 @@ func NewBeagleboneAdaptor(name string) *BeagleboneAdaptor {
// 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.digitalPins = make([]sysfs.DigitalPin, 120)
b.pwmPins = make(map[string]*pwmPin)
b.analogPins = make(map[string]*analogPin)
b.connect()
Expand All @@ -162,12 +163,6 @@ func (b *BeagleboneAdaptor) Finalize() bool {
return true
}

// Reconnect returns true if connection is stablished succesfully
func (b *BeagleboneAdaptor) Reconnect() bool { return true }

// Disconnect returns true if connection is closed succesfully
func (b *BeagleboneAdaptor) Disconnect() bool { return true }

// PwmWrite writes value in specified pin
func (b *BeagleboneAdaptor) PwmWrite(pin string, val byte) {
b.pwmWrite(pin, val)
Expand Down Expand Up @@ -218,7 +213,7 @@ func (b *BeagleboneAdaptor) AnalogWrite(pin string, val byte) {

// I2cStart starts a i2c device in specified address
func (b *BeagleboneAdaptor) I2cStart(address byte) {
b.i2cDevice, _ = sysfs.NewI2cDevice(I2CLocation, address)
b.i2cDevice, _ = sysfs.NewI2cDevice(i2cLocation, address)
}

// I2CWrite writes data to i2c device
Expand Down Expand Up @@ -273,18 +268,16 @@ func (b *BeagleboneAdaptor) analogPin(pin string) string {
}

// digitalPin retrieves digital pin value by name
func (b *BeagleboneAdaptor) digitalPin(pin string, dir string) *sysfs.DigitalPin {
func (b *BeagleboneAdaptor) digitalPin(pin string, dir string) sysfs.DigitalPin {
i := b.translatePin(pin)
if b.digitalPins[i] == nil {
b.digitalPins[i] = sysfs.NewDigitalPin(i)
err := b.digitalPins[i].Export()
if err != nil {
fmt.Println(err)
}
b.digitalPins[i].SetDirection(dir)
} else if b.digitalPins[i].Direction() != dir {
b.digitalPins[i].SetDirection(dir)
}
b.digitalPins[i].Direction(dir)
return b.digitalPins[i]
}

Expand Down
61 changes: 47 additions & 14 deletions platforms/beaglebone/beaglebone_adaptor_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,62 @@
package beaglebone

import (
"github.com/hybridgroup/gobot"
"io"
"os"
"testing"

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

func initTestBeagleboneAdaptor() *BeagleboneAdaptor {
b := NewBeagleboneAdaptor("bot")
b.connect = func() {}
return b
i2cLocation = os.DevNull
sysfs.WriteFile = func(path string, data []byte) (i int, err error) {
return
}
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) {
a := initTestBeagleboneAdaptor()
gobot.Assert(t, a.Finalize(), true)
}
func TestBeagleboneAdaptorConnect(t *testing.T) {
a := initTestBeagleboneAdaptor()
gobot.Assert(t, a.Connect(), true)
gobot.Assert(t, initTestBeagleboneAdaptor().Finalize(), true)
}
func TestBeagleboneAdaptorDisconnect(t *testing.T) {

func TestBeagleboneAdaptorDigitalIO(t *testing.T) {
a := initTestBeagleboneAdaptor()
gobot.Assert(t, a.Disconnect(), true)
lastWritePath := ""
lastReadPath := ""
lastWriteData := []byte{}

sysfs.WriteFile = func(path string, data []byte) (i int, err error) {
lastWritePath = path
lastWriteData = data
return
}
sysfs.ReadFile = func(path string) (b []byte, err error) {
lastReadPath = path
return []byte("1"), nil
}

a.DigitalWrite("P9_12", 1)
gobot.Assert(t, lastWritePath, "/sys/class/gpio/gpio60/value")
gobot.Assert(t, lastWriteData, []byte{49})

i := a.DigitalRead("P8_31")
gobot.Assert(t, lastReadPath, "/sys/class/gpio/gpio10/value")
gobot.Assert(t, i, 1)
}
func TestBeagleboneAdaptorReconnect(t *testing.T) {

func TestBeagleboneAdaptorI2c(t *testing.T) {
a := initTestBeagleboneAdaptor()
gobot.Assert(t, a.Reconnect(), true)
a.I2cStart(0xff)
var _ io.ReadWriteCloser = a.i2cDevice

a.i2cDevice = new(gobot.NullReadWriteCloser)
a.I2cWrite([]byte{0x00, 0x01})
gobot.Assert(t, a.I2cRead(2), make([]byte, 2))
}

0 comments on commit 3e5289f

Please sign in to comment.