Skip to content

Commit

Permalink
beaglebone: remove code no longer needed and increase test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed May 3, 2017
1 parent 971af75 commit 4d49cd4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
11 changes: 3 additions & 8 deletions platforms/beaglebone/beaglebone_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const pwmDefaultPeriod = 500000
// Adaptor is the gobot.Adaptor representation for the Beaglebone
type Adaptor struct {
name string
kernel string
digitalPins []*sysfs.DigitalPin
pwmPins map[string]*sysfs.PWMPin
i2cBuses map[int]sysfs.I2cDevice
Expand All @@ -44,6 +43,7 @@ func NewAdaptor() *Adaptor {
func (b *Adaptor) setSlots() {
b.slots = "/sys/devices/platform/bone_capemgr/slots"
b.usrLed = "/sys/class/leds/beaglebone:green:"
b.analogPath = "/sys/bus/iio/devices/iio:device0"
}

// Name returns the Adaptor name
Expand All @@ -52,30 +52,25 @@ 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 {
if err := ensureSlot(b.slots, "BB-ADC"); err != nil {
return err
}

b.analogPath = "/sys/bus/iio/devices/iio:device0"

return nil
}

// Finalize releases all i2c devices and exported analog, digital, pwm pins.
func (b *Adaptor) Finalize() (err error) {
for _, pin := range b.pwmPins {
for _, pin := range b.digitalPins {
if pin != nil {
if e := pin.Unexport(); e != nil {
err = multierror.Append(err, e)
}
}
}
for _, pin := range b.digitalPins {
for _, pin := range b.pwmPins {
if pin != nil {
if e := pin.Unexport(); e != nil {
err = multierror.Append(err, e)
Expand Down
28 changes: 25 additions & 3 deletions platforms/beaglebone/beaglebone_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ func TestBeagleboneAdaptor(t *testing.T) {
"/dev/i2c-2",
"/sys/devices/platform/bone_capemgr",
"/sys/devices/platform/ocp/ocp:P9_21_pinmux/state",
"/sys/devices/platform/ocp/ocp:P9_22_pinmux/state",
"/sys/class/leds/beaglebone:green:usr1/brightness",
"/sys/bus/iio/devices/iio:device0/in_voltage1_raw",
"/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/export",
"/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/unexport",
"/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm0/enable",
"/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm0/period",
"/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm0/duty_cycle",
"/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm0/polarity",
"/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm1/enable",
"/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm1/period",
"/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm1/duty_cycle",
Expand All @@ -45,15 +50,15 @@ func TestBeagleboneAdaptor(t *testing.T) {
"/sys/class/gpio/gpio66/direction",
"/sys/class/gpio/gpio10/value",
"/sys/class/gpio/gpio10/direction",
"/sys/class/gpio/gpio30/value",
"/sys/class/gpio/gpio30/direction",
})

sysfs.SetFilesystem(fs)
a := NewAdaptor()

a.Connect()

a.analogPath = "/sys/bus/iio/devices/iio:device0"

// PWM
gobottest.Assert(t, a.PwmWrite("P9_99", 175), errors.New("Not a valid PWM pin"))
a.PwmWrite("P9_21", 175)
Expand All @@ -79,9 +84,16 @@ func TestBeagleboneAdaptor(t *testing.T) {
fs.Files["/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm1/duty_cycle"].Contents,
"66666",
)

gobottest.Assert(t, a.ServoWrite("P9_99", 175), errors.New("Not a valid PWM pin"))

fs.WithReadError = true
gobottest.Assert(t, a.PwmWrite("P9_21", 175), errors.New("read error"))
fs.WithReadError = false

fs.WithWriteError = true
gobottest.Assert(t, a.PwmWrite("P9_22", 175), errors.New("write error"))
fs.WithWriteError = false

// Analog
fs.Files["/sys/bus/iio/devices/iio:device0/in_voltage1_raw"].Contents = "567\n"
i, err := a.AnalogRead("P9_40")
Expand All @@ -91,6 +103,11 @@ func TestBeagleboneAdaptor(t *testing.T) {
_, err = a.AnalogRead("P9_99")
gobottest.Assert(t, err, errors.New("Not a valid analog pin"))

fs.WithReadError = true
_, err = a.AnalogRead("P9_40")
gobottest.Assert(t, err, errors.New("read error"))
fs.WithReadError = false

// DigitalIO
a.DigitalWrite("usr1", 1)
gobottest.Assert(t,
Expand Down Expand Up @@ -120,6 +137,11 @@ func TestBeagleboneAdaptor(t *testing.T) {
gobottest.Assert(t, err, errors.New("read error"))
fs.WithReadError = false

fs.WithWriteError = true
_, err = a.DigitalRead("P9_11")
gobottest.Assert(t, err, errors.New("write error"))
fs.WithWriteError = false

// I2c
sysfs.SetSyscall(&sysfs.MockSyscall{})

Expand Down

0 comments on commit 4d49cd4

Please sign in to comment.