Skip to content

Commit

Permalink
Clean up, fixed tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
thetooth committed Feb 23, 2017
1 parent 83896d1 commit a957de9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 64 deletions.
7 changes: 7 additions & 0 deletions platforms/dragonboard/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
Package dragonboard contains the Gobot adaptor for the DragonBoard 410c
For further information refer to the chip README:
https://github.com/hybridgroup/gobot/blob/master/platforms/dragonboard/README.md
*/
package dragonboard // import "gobot.io/x/gobot/platforms/dragonboard"
59 changes: 12 additions & 47 deletions platforms/dragonboard/dragonboard_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@ package dragonboard
import (
"errors"
"fmt"
"io/ioutil"
"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"
)

// Adaptor represents a Gobot Adaptor for a C.H.I.P.
// Adaptor represents a Gobot Adaptor for a DragonBoard 410c
type Adaptor struct {
name string
digitalPins map[int]sysfs.DigitalPin
Expand All @@ -40,7 +36,7 @@ var fixedPins = map[string]int{
"LED_2": 120,
}

// NewAdaptor creates a C.H.I.P. Adaptor
// NewAdaptor creates a DragonBoard 410c Adaptor
func NewAdaptor() *Adaptor {
c := &Adaptor{
name: gobot.DefaultName("DragonBoard"),
Expand Down Expand Up @@ -83,10 +79,9 @@ func (c *Adaptor) Finalize() (err error) {

func (c *Adaptor) setPins() {
c.pinMap = fixedPins
baseAddr, _ := getXIOBase()
for i := 0; i < 122; i++ {
pin := fmt.Sprintf("GPIO_%d", i)
c.pinMap[pin] = baseAddr + i
c.pinMap[pin] = i
}
}

Expand Down Expand Up @@ -121,10 +116,10 @@ func (c *Adaptor) digitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPin,
return c.digitalPins[i], nil
}

// DigitalRead reads digital value from the specified pin.
// Valids pins are the XIO-P0 through XIO-P7 pins from the
// extender (pins 13-20 on header 14), as well as the SoC pins
// aka all the other pins.
// DigitalRead reads digital value to the specified pin.
// Valids pins are the GPIO_A through GPIO_L pins from the
// extender (pins 23-34 on header J8), as well as the SoC pins
// aka all the other pins, APQ GPIO_0-GPIO_122 and PM_MPP_0-4.
func (c *Adaptor) DigitalRead(pin string) (val int, err error) {
sysfsPin, err := c.digitalPin(pin, sysfs.IN)
if err != nil {
Expand All @@ -134,9 +129,9 @@ func (c *Adaptor) DigitalRead(pin string) (val int, err error) {
}

// DigitalWrite writes digital value to the specified pin.
// Valids pins are the XIO-P0 through XIO-P7 pins from the
// extender (pins 13-20 on header 14), as well as the SoC pins
// aka all the other pins.
// Valids pins are the GPIO_A through GPIO_L pins from the
// extender (pins 23-34 on header J8), as well as the SoC pins
// aka all the other pins, APQ GPIO_0-GPIO_122 and PM_MPP_0-4.
func (c *Adaptor) DigitalWrite(pin string, val byte) (err error) {
sysfsPin, err := c.digitalPin(pin, sysfs.OUT)
if err != nil {
Expand All @@ -146,9 +141,9 @@ func (c *Adaptor) DigitalWrite(pin string, val byte) (err error) {
}

// GetConnection returns a connection to a device on a specified bus.
// Valid bus number is [0..2] which corresponds to /dev/i2c-0 through /dev/i2c-2.
// Valid bus number is [0..1] which corresponds to /dev/i2c-0 through /dev/i2c-1.
func (c *Adaptor) GetConnection(address int, bus int) (connection i2c.Connection, err error) {
if (bus < 0) || (bus > 2) {
if (bus < 0) || (bus > 1) {
return nil, fmt.Errorf("Bus number %d out of range", bus)
}
if c.i2cBuses[bus] == nil {
Expand All @@ -161,33 +156,3 @@ func (c *Adaptor) GetConnection(address int, bus int) (connection i2c.Connection
func (c *Adaptor) GetDefaultBus() int {
return 0
}

func getXIOBase() (baseAddr int, err error) {
// Default to original base from 4.3 kernel
baseAddr = 0
const expanderID = "1000000.pinctrl"

labels, err := filepath.Glob("/sys/class/gpio/*/label")
if err != nil {
return
}

for _, labelPath := range labels {
label, err := ioutil.ReadFile(labelPath)
if err != nil {
return baseAddr, err
}
if strings.HasPrefix(string(label), expanderID) {
expanderPath, _ := filepath.Split(labelPath)
basePath := filepath.Join(expanderPath, "base")
base, err := ioutil.ReadFile(basePath)
if err != nil {
return baseAddr, err
}
baseAddr, _ = strconv.Atoi(strings.TrimSpace(string(base)))
break
}
}

return baseAddr, nil
}
36 changes: 19 additions & 17 deletions platforms/dragonboard/dragonboard_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,37 +43,39 @@ func (n *NullReadWriteCloser) Close() error {
return closeErr
}

func initTestChipAdaptor() *Adaptor {
func initTestDragonBoardAdaptor(t *testing.T) *Adaptor {
a := NewAdaptor()
a.Connect()
if err := a.Connect(); err != nil {
t.Error(err)
}
return a
}

func TestChipAdaptorDigitalIO(t *testing.T) {
a := initTestChipAdaptor()
func TestDragonBoardAdaptorDigitalIO(t *testing.T) {
a := initTestDragonBoardAdaptor(t)
fs := sysfs.NewMockFilesystem([]string{
"/sys/class/gpio/export",
"/sys/class/gpio/unexport",
"/sys/class/gpio/gpio50/value",
"/sys/class/gpio/gpio50/direction",
"/sys/class/gpio/gpio139/value",
"/sys/class/gpio/gpio139/direction",
"/sys/class/gpio/gpio36/value",
"/sys/class/gpio/gpio36/direction",
"/sys/class/gpio/gpio12/value",
"/sys/class/gpio/gpio12/direction",
})

sysfs.SetFilesystem(fs)

a.DigitalWrite("CSID7", 1)
gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio139/value"].Contents, "1")
_ = a.DigitalWrite("GPIO_B", 1)
gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio12/value"].Contents, "1")

fs.Files["/sys/class/gpio/gpio50/value"].Contents = "1"
i, _ := a.DigitalRead("TWI2-SDA")
fs.Files["/sys/class/gpio/gpio36/value"].Contents = "1"
i, _ := a.DigitalRead("GPIO_A")
gobottest.Assert(t, i, 1)

gobottest.Assert(t, a.DigitalWrite("XIO-P10", 1), errors.New("Not a valid pin"))
gobottest.Assert(t, a.DigitalWrite("GPIO_M", 1), errors.New("Not a valid pin"))
}

func TestChipAdaptorI2c(t *testing.T) {
a := initTestChipAdaptor()
func TestDragonBoardAdaptorI2c(t *testing.T) {
a := initTestDragonBoardAdaptor(t)
fs := sysfs.NewMockFilesystem([]string{
"/dev/i2c-1",
})
Expand All @@ -83,9 +85,9 @@ func TestChipAdaptorI2c(t *testing.T) {
con, err := a.GetConnection(0xff, 1)
gobottest.Assert(t, err, nil)

con.Write([]byte{0x00, 0x01})
_, _ = con.Write([]byte{0x00, 0x01})
data := []byte{42, 42}
con.Read(data)
_, _ = con.Read(data)
gobottest.Assert(t, data, []byte{0x00, 0x01})

gobottest.Assert(t, a.Finalize(), nil)
Expand Down

0 comments on commit a957de9

Please sign in to comment.