Skip to content

Commit

Permalink
firmata: correct problem where last analog pin(s) were being ignored …
Browse files Browse the repository at this point in the history
…from capabilities query

Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Oct 31, 2017
1 parent 2d28e0a commit d2e6c53
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
33 changes: 33 additions & 0 deletions examples/firmata_grove_sound_sensor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// +build example
//
// Do not build by default.

package main

import (
"fmt"
"os"

"gobot.io/x/gobot"
"gobot.io/x/gobot/drivers/aio"
"gobot.io/x/gobot/platforms/firmata"
)

func main() {
board := firmata.NewAdaptor(os.Args[1])
sensor := aio.NewGroveSoundSensorDriver(board, "3")

work := func() {
sensor.On(aio.Data, func(data interface{}) {
fmt.Println("sensor", data)
})
}

robot := gobot.NewRobot("sensorBot",
[]gobot.Connection{board},
[]gobot.Device{sensor},
work,
)

robot.Start()
}
15 changes: 5 additions & 10 deletions platforms/firmata/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,19 +340,15 @@ func (b *Client) I2cConfig(delay int) error {
return b.WriteSysex([]byte{I2CConfig, byte(delay & 0xFF), byte((delay >> 8) & 0xFF)})
}

func (b *Client) togglePinReporting(pin int, state int, mode byte) error {
func (b *Client) togglePinReporting(pin int, state int, mode byte) (err error) {
if state != 0 {
state = 1
} else {
state = 0
}

if err := b.write([]byte{byte(mode) | byte(pin), byte(state)}); err != nil {
return err
}

return nil

err = b.write([]byte{byte(mode) | byte(pin), byte(state)})
return
}

// WriteSysex writes an arbitrary Sysex command to the microcontroller.
Expand Down Expand Up @@ -428,7 +424,7 @@ func (b *Client) process() (err error) {
supportedModes := 0
n := 0

for _, val := range currentBuffer[2:(len(currentBuffer) - 5)] {
for _, val := range currentBuffer[2 : len(currentBuffer)-1] {
if val == 127 {
modes := []int{}
for _, mode := range []int{Input, Output, Analog, Pwm, Servo} {
Expand All @@ -455,8 +451,7 @@ func (b *Client) process() (err error) {
pinIndex := 0
b.analogPins = []int{}

for _, val := range currentBuffer[2 : len(b.pins)-1] {

for _, val := range currentBuffer[2 : len(currentBuffer)-1] {
b.pins[pinIndex].AnalogChannel = int(val)

if val != 127 {
Expand Down
5 changes: 3 additions & 2 deletions platforms/firmata/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ func initTestFirmata() *Client {
func TestPins(t *testing.T) {
b := initTestFirmata()
b.setConnected(true)
//test if functions executes
gobottest.Assert(t, len(b.Pins()), 19)

gobottest.Assert(t, len(b.Pins()), 20)
gobottest.Assert(t, len(b.analogPins), 6)
}

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

0 comments on commit d2e6c53

Please sign in to comment.