Skip to content

Commit

Permalink
chip: auto-detect OS version to adjust pin mappings
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Dec 3, 2016
1 parent 37882b1 commit 4aa0a64
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
4 changes: 0 additions & 4 deletions platforms/chip/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

The [CHIP](http://www.getchip.com/) is a small, inexpensive ARM based single board computer, with many different IO interfaces available on the [pin headers](http://docs.getchip.com/#pin-headers).

The current ChipAdaptor will only work with CHIP devices running OS 4.3.
This is because of the change in the GPIO expander pins between kernel
versions OS 4.3 vs. OS 4.4. See [here](http://docs.getchip.com/chip.html#gpio) for more details.

For documentation about the CHIP platform click [here](http://docs.getchip.com/).

## How to Install
Expand Down
36 changes: 34 additions & 2 deletions platforms/chip/chip_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ package chip

import (
"errors"
"os/exec"
"strings"

multierror "github.com/hashicorp/go-multierror"
"github.com/hybridgroup/gobot/sysfs"
)

// Adaptor represents a Gobot Adaptor for a C.H.I.P.
type Adaptor struct {
name string
digitalPins map[int]sysfs.DigitalPin
pinMap map[string]int
i2cDevice sysfs.I2cDevice
}

var pins = map[string]int{
var pinsOriginal = map[string]int{
"XIO-P0": 408,
"XIO-P1": 409,
"XIO-P2": 410,
Expand All @@ -24,12 +28,25 @@ var pins = map[string]int{
"XIO-P7": 415,
}

var pins44 = map[string]int{
"XIO-P0": 1016,
"XIO-P1": 1017,
"XIO-P2": 1018,
"XIO-P3": 1019,
"XIO-P4": 1020,
"XIO-P5": 1021,
"XIO-P6": 1022,
"XIO-P7": 1023,
}

// NewAdaptor creates a C.H.I.P. Adaptor
func NewAdaptor() *Adaptor {
c := &Adaptor{
name: "CHIP",
digitalPins: make(map[int]sysfs.DigitalPin),
}

c.setPins()
return c
}

Expand Down Expand Up @@ -61,8 +78,17 @@ func (c *Adaptor) Finalize() (err error) {
return
}

func (c *Adaptor) setPins() {
kernel := getKernel()
if string(kernel[0:2]) == "4.4" {
c.pinMap = pins44
} else {
c.pinMap = pinsOriginal
}
}

func (c *Adaptor) translatePin(pin string) (i int, err error) {
if val, ok := pins[pin]; ok {
if val, ok := c.pinMap[pin]; ok {
i = val
} else {
err = errors.New("Not a valid pin")
Expand Down Expand Up @@ -140,3 +166,9 @@ func (c *Adaptor) I2cRead(address int, size int) (data []byte, err error) {
_, err = c.i2cDevice.Read(data)
return
}

func getKernel() string {
result, _ := exec.Command("uname", "-r").Output()

return strings.TrimSpace(string(result))
}
2 changes: 1 addition & 1 deletion platforms/chip/chip_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (n *NullReadWriteCloser) Read(b []byte) (int, error) {
return len(b), nil
}

var closeErr error = nil
var closeErr error

func (n *NullReadWriteCloser) Close() error {
return closeErr
Expand Down

0 comments on commit 4aa0a64

Please sign in to comment.