Skip to content

Commit

Permalink
Fix firmata examples
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jun 7, 2014
1 parent 45b38fd commit f70150c
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 39 deletions.
5 changes: 4 additions & 1 deletion examples/firmata_blinkm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ func main() {

work := func() {
gobot.Every(3*time.Second, func() {
blinkm.Rgb(byte(gobot.Rand(255)), byte(gobot.Rand(255)), byte(gobot.Rand(255)))
r := byte(gobot.Rand(255))
g := byte(gobot.Rand(255))
b := byte(gobot.Rand(255))
blinkm.Rgb(r, g, b)
fmt.Println("color", blinkm.Color())
})
}
Expand Down
6 changes: 1 addition & 5 deletions examples/firmata_button.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/firmata"
"github.com/hybridgroup/gobot/platforms/gpio"
"time"
)

func main() {
Expand All @@ -16,9 +15,6 @@ func main() {
led := gpio.NewLedDriver(firmataAdaptor, "myLed", "13")

work := func() {
gobot.Every(1*time.Second, func() {
led.Toggle()
})
gobot.On(button.Events["push"], func(data interface{}) {
led.On()
})
Expand All @@ -28,7 +24,7 @@ func main() {
}

gbot.Robots = append(gbot.Robots,
gobot.NewRobot("buttonBot", []robot.Connection{firmataAdaptor}, []robot.Device{button, led}, work),
gobot.NewRobot("buttonBot", []gobot.Connection{firmataAdaptor}, []gobot.Device{button, led}, work),
)

gbot.Start()
Expand Down
2 changes: 1 addition & 1 deletion examples/firmata_cat_toy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {
z = gobot.ToScale(gobot.FromScale(hand.Z(), -300, 300), 30, 150)
}
})
gobot.Every(0.01*time.Second, func() {
gobot.Every(10*time.Millisecond, func() {
servo1.Move(uint8(x))
servo2.Move(uint8(z))
fmt.Println("Current Angle: ", servo1.CurrentAngle, ",", servo2.CurrentAngle)
Expand Down
2 changes: 1 addition & 1 deletion examples/firmata_hmc6352.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func main() {
hmc6352 := i2c.NewHMC6352Driver(firmataAdaptor, "hmc6352")

work := func() {
gobot.Every(0.1*time.Second, func() {
gobot.Every(100*time.Millisecond, func() {
fmt.Println("Heading", hmc6352.Heading)
})
}
Expand Down
2 changes: 1 addition & 1 deletion examples/firmata_led_brightness.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func main() {
brightness := uint8(0)
fade_amount := uint8(15)

gobot.Every(0.1*time.Second, func() {
gobot.Every(100*time.Millisecond, func() {
led.Brightness(brightness)
brightness = brightness + fade_amount
if brightness == 0 || brightness == 255 {
Expand Down
8 changes: 4 additions & 4 deletions examples/firmata_led_brightness_with_analog_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
func main() {
gbot := gobot.NewGobot()
firmataAdaptor := firmata.NewFirmataAdaptor("firmata", "/dev/ttyACM0")
sensor := gpio.NewAnalogSensor(firmataAdaptor, "sensor", "0")
led := gpio.NewLed(firmataAdaptor, "led", "3")
sensor := gpio.NewAnalogSensorDriver(firmataAdaptor, "sensor", "0")
led := gpio.NewLedDriver(firmataAdaptor, "led", "3")

work := func() {
gobot.Every(0.1*time.Second, func() {
gobot.Every(100*time.Millisecond, func() {
val := sensor.Read()
brightness := uint8(gpio.ToPwm(val))
brightness := uint8(gobot.ToScale(gobot.FromScale(float64(val), 0, 1024), 0, 255))
fmt.Println("sensor", val)
fmt.Println("brightness", brightness)
led.Brightness(brightness)
Expand Down
2 changes: 1 addition & 1 deletion examples/firmata_motor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func main() {
speed := byte(0)
fade_amount := byte(15)

gobot.Every(0.1*time.Second, func() {
gobot.Every(100*time.Millisecond, func() {
motor.Speed(speed)
speed = speed + fade_amount
if speed == 0 || speed == 255 {
Expand Down
18 changes: 9 additions & 9 deletions platforms/firmata/firmata.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type pin struct {
type event struct {
Name string
Data []byte
I2cReply map[string][]uint16
I2cReply map[string][]byte
}

func newBoard(sp io.ReadWriteCloser) *board {
Expand Down Expand Up @@ -208,11 +208,11 @@ func (b *board) togglePinReporting(pin byte, state byte, mode byte) {
b.write([]byte{mode | pin, state})
}

func (b *board) i2cReadRequest(slave_address byte, num_bytes uint16) {
func (b *board) i2cReadRequest(slave_address byte, num_bytes uint) {
b.write([]byte{START_SYSEX, I2C_REQUEST, slave_address, (I2C_MODE_READ << 3), byte(num_bytes & 0x7F), byte(((num_bytes >> 7) & 0x7F)), END_SYSEX})
}

func (b *board) i2cWriteRequest(slave_address byte, data []uint16) {
func (b *board) i2cWriteRequest(slave_address byte, data []byte) {
ret := []byte{START_SYSEX, I2C_REQUEST, slave_address, (I2C_MODE_WRITE << 3)}
for _, val := range data {
ret = append(ret, byte(val&0x7F))
Expand All @@ -222,7 +222,7 @@ func (b *board) i2cWriteRequest(slave_address byte, data []uint16) {
b.write(ret)
}

func (b *board) i2cConfig(data []uint16) {
func (b *board) i2cConfig(data []byte) {
ret := []byte{START_SYSEX, I2C_CONFIG}
for _, val := range data {
ret = append(ret, byte(val&0xFF))
Expand Down Expand Up @@ -348,10 +348,10 @@ func (me *board) process(data []byte) {

me.Events = append(me.Events, event{Name: fmt.Sprintf("pin_%v_state", current_buffer[2]), Data: []byte{byte(pin.Value & 0xff)}})
case I2C_REPLY:
i2c_reply := map[string][]uint16{
"slave_address": []uint16{uint16(current_buffer[2]) | uint16(current_buffer[3])<<7},
"register": []uint16{uint16(current_buffer[4]) | uint16(current_buffer[5])<<7},
"data": []uint16{uint16(current_buffer[6]) | uint16(current_buffer[7])<<7},
i2c_reply := map[string][]byte{
"slave_address": []byte{byte(current_buffer[2]) | byte(current_buffer[3])<<7},
"register": []byte{byte(current_buffer[4]) | byte(current_buffer[5])<<7},
"data": []byte{byte(current_buffer[6]) | byte(current_buffer[7])<<7},
}
for i := 8; i < len(current_buffer); i = i + 2 {
if current_buffer[i] == byte(0xF7) {
Expand All @@ -360,7 +360,7 @@ func (me *board) process(data []byte) {
if i+2 > len(current_buffer) {
break
}
i2c_reply["data"] = append(i2c_reply["data"], uint16(current_buffer[i])|uint16(current_buffer[i+1])<<7)
i2c_reply["data"] = append(i2c_reply["data"], byte(current_buffer[i])|byte(current_buffer[i+1])<<7)
}
me.Events = append(me.Events, event{Name: "i2c_reply", I2cReply: i2c_reply})

Expand Down
8 changes: 4 additions & 4 deletions platforms/firmata/firmata_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,19 @@ func (f *FirmataAdaptor) digitalPin(pin int) int {

func (f *FirmataAdaptor) I2cStart(address byte) {
f.i2cAddress = address
f.Board.i2cConfig([]uint16{0})
f.Board.i2cConfig([]byte{0})
}

func (f *FirmataAdaptor) I2cRead(size uint16) []uint16 {
func (f *FirmataAdaptor) I2cRead(size uint) []byte {
f.Board.i2cReadRequest(f.i2cAddress, size)

events := f.Board.findEvents("i2c_reply")
if len(events) > 0 {
return events[len(events)-1].I2cReply["data"]
}
return make([]uint16, 0)
return make([]byte, 0)
}

func (f *FirmataAdaptor) I2cWrite(data []uint16) {
func (f *FirmataAdaptor) I2cWrite(data []byte) {
f.Board.i2cWriteRequest(f.i2cAddress, data)
}
8 changes: 4 additions & 4 deletions platforms/firmata/firmata_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ var _ = Describe("FirmataAdaptor", func() {
adaptor.I2cStart(0x00)
})
It("I2cRead should return [] on no data", func() {
Expect(adaptor.I2cRead(1)).To(Equal(make([]uint16, 0)))
Expect(adaptor.I2cRead(1)).To(Equal(make([]byte, 0)))
})
It("I2cRead should return data", func() {
i := []uint16{100}
i2c_reply := map[string][]uint16{}
i := []byte{100}
i2c_reply := map[string][]byte{}
i2c_reply["data"] = i
adaptor.Board.Events = append(adaptor.Board.Events, event{Name: "i2c_reply", I2cReply: i2c_reply})
Expect(adaptor.I2cRead(1)).To(Equal(i))
})
It("Must be able to I2cWrite", func() {
adaptor.I2cWrite([]uint16{0x00, 0x01})
adaptor.I2cWrite([]byte{0x00, 0x01})
})
})
14 changes: 6 additions & 8 deletions platforms/gpio/button_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ func NewButtonDriver(a DigitalReader, name string, pin string) *ButtonDriver {

func (b *ButtonDriver) Start() bool {
state := 0
go func() {
for {
new_value := b.readState()
if new_value != state && new_value != -1 {
state = new_value
b.update(new_value)
}
gobot.Every(b.Interval, func() {
new_value := b.readState()
if new_value != state && new_value != -1 {
state = new_value
b.update(new_value)
}
}()
})
return true
}
func (b *ButtonDriver) Halt() bool { return true }
Expand Down

0 comments on commit f70150c

Please sign in to comment.