Skip to content

Commit

Permalink
Update firmata 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 557e5a2 commit 6832c17
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
3 changes: 2 additions & 1 deletion examples/firmata_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
3 changes: 2 additions & 1 deletion examples/firmata_hmc6352.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ func main() {

work := func() {
gobot.Every(100*time.Millisecond, func() {
fmt.Println("Heading", hmc6352.Heading)
heading, _ := hmc6352.Heading()
fmt.Println("Heading", heading)
})
}

Expand Down
22 changes: 13 additions & 9 deletions platforms/firmata/firmata_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,32 +212,36 @@ func (f *FirmataAdaptor) digitalPin(pin int) int {
}

// I2cStart initializes board with i2c configuration
func (f *FirmataAdaptor) I2cStart(address byte) {
func (f *FirmataAdaptor) I2cStart(address byte) (err error) {
f.i2cAddress = address
f.board.i2cConfig([]byte{0})
return f.board.i2cConfig([]byte{0})
}

// I2cRead reads from I2c specified size
// Returns empty byte array if response is timed out
func (f *FirmataAdaptor) I2cRead(size uint) []byte {
func (f *FirmataAdaptor) I2cRead(size uint) (data []byte, err error) {
ret := make(chan []byte)
f.board.i2cReadRequest(f.i2cAddress, size)
if err = f.board.i2cReadRequest(f.i2cAddress, size); err != nil {
return
}

f.board.readAndProcess()
if err = f.board.readAndProcess(); err != nil {
return
}

gobot.Once(f.board.events["i2c_reply"], func(data interface{}) {
ret <- data.(map[string][]byte)["data"]
})

select {
case data := <-ret:
return data
return data, nil
case <-time.After(10 * time.Millisecond):
}
return []byte{}
return []byte{}, nil
}

// I2cWrite retrieves i2c data
func (f *FirmataAdaptor) I2cWrite(data []byte) {
f.board.i2cWriteRequest(f.i2cAddress, data)
func (f *FirmataAdaptor) I2cWrite(data []byte) (err error) {
return f.board.i2cWriteRequest(f.i2cAddress, data)
}
6 changes: 4 additions & 2 deletions platforms/firmata/firmata_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ func TestFirmataAdaptorI2cStart(t *testing.T) {
func TestFirmataAdaptorI2cRead(t *testing.T) {
a := initTestFirmataAdaptor()
// [] on no data
gobot.Assert(t, a.I2cRead(1), []byte{})
data, _ := a.I2cRead(1)
gobot.Assert(t, data, []byte{})

i := []byte{100}
i2cReply := map[string][]byte{}
Expand All @@ -123,7 +124,8 @@ func TestFirmataAdaptorI2cRead(t *testing.T) {
<-time.After(5 * time.Millisecond)
gobot.Publish(a.board.events["i2c_reply"], i2cReply)
}()
gobot.Assert(t, a.I2cRead(1), i)
data, _ = a.I2cRead(1)
gobot.Assert(t, data, i)
}
func TestFirmataAdaptorI2cWrite(t *testing.T) {
a := initTestFirmataAdaptor()
Expand Down

0 comments on commit 6832c17

Please sign in to comment.