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)) g := byte(gobot.Rand(255))
b := byte(gobot.Rand(255)) b := byte(gobot.Rand(255))
blinkm.Rgb(r, g, b) 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() { work := func() {
gobot.Every(100*time.Millisecond, 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 // I2cStart initializes board with i2c configuration
func (f *FirmataAdaptor) I2cStart(address byte) { func (f *FirmataAdaptor) I2cStart(address byte) (err error) {
f.i2cAddress = address f.i2cAddress = address
f.board.i2cConfig([]byte{0}) return f.board.i2cConfig([]byte{0})
} }
// I2cRead reads from I2c specified size // I2cRead reads from I2c specified size
// Returns empty byte array if response is timed out // 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) 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{}) { gobot.Once(f.board.events["i2c_reply"], func(data interface{}) {
ret <- data.(map[string][]byte)["data"] ret <- data.(map[string][]byte)["data"]
@ -231,13 +235,13 @@ func (f *FirmataAdaptor) I2cRead(size uint) []byte {
select { select {
case data := <-ret: case data := <-ret:
return data return data, nil
case <-time.After(10 * time.Millisecond): case <-time.After(10 * time.Millisecond):
} }
return []byte{} return []byte{}, nil
} }
// I2cWrite retrieves i2c data // I2cWrite retrieves i2c data
func (f *FirmataAdaptor) I2cWrite(data []byte) { func (f *FirmataAdaptor) I2cWrite(data []byte) (err error) {
f.board.i2cWriteRequest(f.i2cAddress, data) return f.board.i2cWriteRequest(f.i2cAddress, data)
} }

View File

@ -114,7 +114,8 @@ func TestFirmataAdaptorI2cStart(t *testing.T) {
func TestFirmataAdaptorI2cRead(t *testing.T) { func TestFirmataAdaptorI2cRead(t *testing.T) {
a := initTestFirmataAdaptor() a := initTestFirmataAdaptor()
// [] on no data // [] on no data
gobot.Assert(t, a.I2cRead(1), []byte{}) data, _ := a.I2cRead(1)
gobot.Assert(t, data, []byte{})
i := []byte{100} i := []byte{100}
i2cReply := map[string][]byte{} i2cReply := map[string][]byte{}
@ -123,7 +124,8 @@ func TestFirmataAdaptorI2cRead(t *testing.T) {
<-time.After(5 * time.Millisecond) <-time.After(5 * time.Millisecond)
gobot.Publish(a.board.events["i2c_reply"], i2cReply) 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) { func TestFirmataAdaptorI2cWrite(t *testing.T) {
a := initTestFirmataAdaptor() a := initTestFirmataAdaptor()