Update firmata package for new i2c interface

This commit is contained in:
Adrian Zankich 2014-11-19 17:24:17 -08:00
parent 557e5a239c
commit 6832c1739a
4 changed files with 21 additions and 13 deletions

View File

@ -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)
})
}

View File

@ -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)
})
}

View File

@ -212,18 +212,22 @@ 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"]
@ -231,13 +235,13 @@ func (f *FirmataAdaptor) I2cRead(size uint) []byte {
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)
}

View File

@ -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{}
@ -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()