Skip to content

Commit

Permalink
Update edison package for new i2c interface
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Nov 20, 2014
1 parent 6832c17 commit 0541eee
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 20 deletions.
3 changes: 2 additions & 1 deletion examples/edison_blinkm.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func main() {
g := byte(gobot.Rand(255))
b := byte(gobot.Rand(255))
blinkm.Rgb(r, g, b)
fmt.Println("color", blinkm.Color())
color, _ := blinkm.Color()
fmt.Println("color", color)
})
}

Expand Down
58 changes: 40 additions & 18 deletions platforms/intel-iot/edison/edison_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,41 +437,63 @@ func (e *EdisonAdaptor) AnalogRead(pin string) (val int, err error) {
}

// I2cStart initializes i2c device for addresss
func (e *EdisonAdaptor) I2cStart(address byte) {
e.tristate.Write(sysfs.LOW)
func (e *EdisonAdaptor) I2cStart(address byte) (err error) {
if err = e.tristate.Write(sysfs.LOW); err != nil {
return
}

for _, i := range []int{14, 165, 212, 213} {
io := sysfs.NewDigitalPin(i)
io.Export()
io.Direction(sysfs.IN)
io.Unexport()
if err = io.Export(); err != nil {
return
}
if err = io.Direction(sysfs.IN); err != nil {
return
}
if err = io.Unexport(); err != nil {
return
}
}

for _, i := range []int{236, 237, 204, 205} {
io := sysfs.NewDigitalPin(i)
io.Export()
io.Direction(sysfs.OUT)
io.Write(sysfs.LOW)
io.Unexport()
if err = io.Export(); err != nil {
return
}
if err = io.Direction(sysfs.OUT); err != nil {
return
}
if err = io.Write(sysfs.LOW); err != nil {
return
}
if err = io.Unexport(); err != nil {
return
}
}

for _, i := range []string{"28", "27"} {
changePinMode(i, "1")
if err = changePinMode(i, "1"); err != nil {
return
}
}

e.tristate.Write(sysfs.HIGH)
if err = e.tristate.Write(sysfs.HIGH); err != nil {
return
}

e.i2cDevice, _ = sysfs.NewI2cDevice("/dev/i2c-6", address)
e.i2cDevice, err = sysfs.NewI2cDevice("/dev/i2c-6", address)
return
}

// I2cWrite writes data to i2cDevice
func (e *EdisonAdaptor) I2cWrite(data []byte) {
e.i2cDevice.Write(data)
func (e *EdisonAdaptor) I2cWrite(data []byte) (err error) {
_, err = e.i2cDevice.Write(data)
return
}

// I2cRead reads data from i2cDevice
func (e *EdisonAdaptor) I2cRead(size uint) []byte {
b := make([]byte, size)
e.i2cDevice.Read(b)
return b
func (e *EdisonAdaptor) I2cRead(size uint) (data []byte, err error) {
data = make([]byte, size)
_, err = e.i2cDevice.Read(data)
return
}
22 changes: 21 additions & 1 deletion platforms/intel-iot/edison/edison_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ func initTestEdisonAdaptor() (*EdisonAdaptor, *sysfs.MockFilesystem) {
"/sys/class/gpio/gpio253/direction",
"/sys/class/gpio/gpio261/value",
"/sys/class/gpio/gpio261/direction",
"/sys/class/gpio/gpio214/value",
"/sys/class/gpio/gpio214/direction",
"/sys/class/gpio/gpio14/direction",
"/sys/class/gpio/gpio14/value",
"/sys/class/gpio/gpio165/direction",
"/sys/class/gpio/gpio165/value",
"/sys/class/gpio/gpio212/direction",
"/sys/class/gpio/gpio212/value",
"/sys/class/gpio/gpio213/direction",
"/sys/class/gpio/gpio213/value",
"/sys/class/gpio/gpio236/direction",
"/sys/class/gpio/gpio236/value",
"/sys/class/gpio/gpio237/direction",
"/sys/class/gpio/gpio237/value",
"/sys/class/gpio/gpio204/direction",
"/sys/class/gpio/gpio204/value",
"/sys/class/gpio/gpio205/direction",
"/sys/class/gpio/gpio205/value",
"/dev/i2c-6",
})
sysfs.SetFilesystem(fs)
Expand Down Expand Up @@ -77,7 +95,9 @@ func TestEdisonAdaptorI2c(t *testing.T) {
a.I2cStart(0xff)

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

data, _ := a.I2cRead(2)
gobot.Assert(t, data, []byte{0x00, 0x01})
}

func TestEdisonAdaptorPwm(t *testing.T) {
Expand Down

0 comments on commit 0541eee

Please sign in to comment.