Skip to content

Commit

Permalink
Update firmata examples
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed May 23, 2014
1 parent d32a527 commit cb3f65c
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 199 deletions.
10 changes: 4 additions & 6 deletions examples/firmata_blink.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ import (
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/firmata"
"github.com/hybridgroup/gobot/platforms/gpio"
"time"
)

func main() {
gbot := gobot.NewGobot()

firmataAdaptor := firmata.NewFirmataAdaptor("myFirmata", "/dev/ttyACM0")

led := gpio.NewLedDriver(firmataAdaptor, "myLed", "13")

work := func() {
gobot.Every("1s", func() {
gobot.Every(1*time.Second, func() {
led.Toggle()
})
}

gbot.Robots = append(gbot.Robots, gobot.NewRobot("Jerry", []gobot.Connection{firmataAdaptor}, []gobot.Device{led}, work))
gbot.Robots = append(gbot.Robots,
gobot.NewRobot("blinkBot", []gobot.Connection{firmataAdaptor}, []gobot.Device{led}, work))
gbot.Start()
}
26 changes: 10 additions & 16 deletions examples/firmata_blinkm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,24 @@ package main
import (
"fmt"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/firmata"
"github.com/hybridgroup/gobot/i2c"
"github.com/hybridgroup/gobot/platforms/firmata"
"github.com/hybridgroup/gobot/platforms/i2c"
"time"
)

func main() {
firmataAdaptor := firmata.NewFirmataAdaptor()
firmataAdaptor.Name = "firmata"
firmataAdaptor.Port = "/dev/ttyACM0"

blinkm := i2c.NewBlinkMDriver(firmataAdaptor)
blinkm.Name = "blinkm"
gbot := gobot.NewGobot()
firmataAdaptor := firmata.NewFirmataAdaptor("firmata", "/dev/ttyACM0")
blinkm := i2c.NewBlinkMDriver(firmataAdaptor, "blinkm")

work := func() {
gobot.Every("3s", func() {
gobot.Every(3*time.Second, func() {
blinkm.Rgb(byte(gobot.Rand(255)), byte(gobot.Rand(255)), byte(gobot.Rand(255)))
fmt.Println("color", blinkm.Color())
})
}

robot := gobot.Robot{
Connections: []gobot.Connection{firmataAdaptor},
Devices: []gobot.Device{blinkm},
Work: work,
}

robot.Start()
gbot.Robots = append(gbot.Robots,
gobot.NewRobot("blinkmBot", []gobot.Connection{firmataAdaptor}, []gobot.Device{blinkm}, work))
gbot.Start()
}
12 changes: 4 additions & 8 deletions examples/firmata_button.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,33 @@ package main

import (
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/core/api"
"github.com/hybridgroup/gobot/core/robot"
"github.com/hybridgroup/gobot/core/utils"
"github.com/hybridgroup/gobot/platforms/firmata"
"github.com/hybridgroup/gobot/platforms/gpio"
"time"
)

func main() {
gbot := gobot.NewGobot()
gbot.Api = api.NewApi()

firmataAdaptor := firmata.NewFirmataAdaptor("myFirmata", "/dev/ttyACM0")

button := gpio.NewButtonDriver(firmataAdaptor, "myButton", "2")
led := gpio.NewLedDriver(firmataAdaptor, "myLed", "13")

work := func() {
utils.Every((1 * time.Second), func() {
gobot.Every(1*time.Second, func() {
led.Toggle()
})
utils.On(button.Events["push"], func(data interface{}) {
gobot.On(button.Events["push"], func(data interface{}) {
led.On()
})
utils.On(button.Events["release"], func(data interface{}) {
gobot.On(button.Events["release"], func(data interface{}) {
led.Off()
})
}

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

gbot.Start()
Expand Down
41 changes: 14 additions & 27 deletions examples/firmata_cat_toy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,21 @@ package main
import (
"fmt"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/firmata"
"github.com/hybridgroup/gobot/gpio"
"github.com/hybridgroup/gobot/leap"
"github.com/hybridgroup/gobot/gplatforms/pio"
"github.com/hybridgroup/gobot/platforms/firmata"
"github.com/hybridgroup/gobot/platforms/leap"
"time"
)

func main() {
firmataAdaptor := firmata.NewFirmataAdaptor()
firmataAdaptor.Name = "firmata"
firmataAdaptor.Port = "/dev/ttyACM0"
gbot := gobot.NewGobot()

servo1 := gpio.NewServoDriver(firmataAdaptor)
servo1.Name = "servo"
servo1.Pin = "5"
firmataAdaptor := firmata.NewFirmataAdaptor("firmata", "/dev/ttyACM0")
servo1 := gpio.NewServoDriver(firmataAdaptor, "servo", "5")
servo2 := gpio.NewServoDriver(firmataAdaptor, "servo", "3")

servo2 := gpio.NewServoDriver(firmataAdaptor)
servo2.Name = "servo"
servo2.Pin = "3"

leapAdaptor := leap.NewLeapMotionAdaptor()
leapAdaptor.Name = "leap"
leapAdaptor.Port = "127.0.0.1:6437"

leapDriver := leap.NewLeapMotionDriver(leapAdaptor)
leapDriver.Name = "leap"
leapAdaptor := leap.NewLeapMotionAdaptor("leap", "127.0.0.1:6437")
leapDriver := leap.NewLeapMotionDriver(leapAdaptor, "leap")

work := func() {
x := 90.0
Expand All @@ -38,18 +29,14 @@ func main() {
z = gobot.ToScale(gobot.FromScale(hand.Z(), -300, 300), 30, 150)
}
})
gobot.Every("0.01s", func() {
gobot.Every(0.01*time.Second, func() {
servo1.Move(uint8(x))
servo2.Move(uint8(z))
fmt.Println("Current Angle: ", servo1.CurrentAngle, ",", servo2.CurrentAngle)
})
}

robot := gobot.Robot{
Connections: []gobot.Connection{firmataAdaptor, leapAdaptor},
Devices: []gobot.Device{servo1, servo2, leapDriver},
Work: work,
}

robot.Start()
gbot.Robots = append(gbot.Robots,
gobot.NewRobot("pwmBot", []gobot.Connection{firmataAdaptor, leapAdaptor}, []gobot.Device{servo1, servo2, leapDriver}, work))
gbot.Start()
}
25 changes: 10 additions & 15 deletions examples/firmata_hmc6352.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,24 @@ package main
import (
"fmt"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/firmata"
"github.com/hybridgroup/gobot/i2c"
"github.com/hybridgroup/gobot/platforms/firmata"
"github.com/hybridgroup/gobot/platforms/i2c"
"time"
)

func main() {
firmataAdaptor := firmata.NewFirmataAdaptor()
firmataAdaptor.Name = "firmata"
firmataAdaptor.Port = "/dev/ttyACM0"
gbot := gobot.NewGobot()
firmataAdaptor := firmata.NewFirmataAdaptor("firmata", "/dev/ttyACM0")

hmc6352 := i2c.NewHMC6352Driver(firmataAdaptor)
hmc6352.Name = "hmc6352"
hmc6352 := i2c.NewHMC6352Driver(firmataAdaptor, "hmc6352")

work := func() {
gobot.Every("0.1s", func() {
gobot.Every(0.1*time.Second, func() {
fmt.Println("Heading", hmc6352.Heading)
})
}

robot := gobot.Robot{
Connections: []gobot.Connection{firmataAdaptor},
Devices: []gobot.Device{hmc6352},
Work: work,
}

robot.Start()
gbot.Robots = append(gbot.Robots,
gobot.NewRobot("hmc6352Bot", []gobot.Connection{firmataAdaptor}, []gobot.Device{hmc6352}, work))
gbot.Start()
}
26 changes: 10 additions & 16 deletions examples/firmata_led_brightness.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,21 @@ package main

import (
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/firmata"
"github.com/hybridgroup/gobot/gpio"
"github.com/hybridgroup/gobot/platforms/firmata"
"github.com/hybridgroup/gobot/platforms/gpio"
"time"
)

func main() {
firmataAdaptor := firmata.NewFirmataAdaptor()
firmataAdaptor.Name = "firmata"
firmataAdaptor.Port = "/dev/ttyACM0"

led := gpio.NewLedDriver(firmataAdaptor)
led.Name = "led"
led.Pin = "3"
gbot := gobot.NewGobot()
firmataAdaptor := firmata.NewFirmataAdaptor("firmata", "/dev/ttyACM0")
led := gpio.NewLedDriver(firmataAdaptor, "led", "3")

work := func() {
brightness := uint8(0)
fade_amount := uint8(15)

gobot.Every("0.1s", func() {
gobot.Every(0.1*time.Second, func() {
led.Brightness(brightness)
brightness = brightness + fade_amount
if brightness == 0 || brightness == 255 {
Expand All @@ -28,11 +25,8 @@ func main() {
})
}

robot := gobot.Robot{
Connections: []gobot.Connection{firmataAdaptor},
Devices: []gobot.Device{led},
Work: work,
}
gbot.Robots = append(gbot.Robots,
gobot.NewRobot("pwmBot", []gobot.Connection{firmataAdaptor}, []gobot.Device{led}, work))
gbot.Start()

robot.Start()
}
31 changes: 11 additions & 20 deletions examples/firmata_led_brightness_with_analog_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,19 @@ package main
import (
"fmt"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/firmata"
"github.com/hybridgroup/gobot/gpio"
"github.com/hybridgroup/gobot/platforms/firmata"
"github.com/hybridgroup/gobot/platforms/gpio"
"time"
)

func main() {
firmataAdaptor := firmata.NewFirmataAdaptor()
firmataAdaptor.Name = "firmata"
firmataAdaptor.Port = "/dev/ttyACM0"

sensor := gpio.NewAnalogSensor(firmataAdaptor)
sensor.Name = "sensor"
sensor.Pin = "0"

led := gpio.NewLed(firmataAdaptor)
led.Name = "led"
led.Pin = "3"
gbot := gobot.NewGobot()
firmataAdaptor := firmata.NewFirmataAdaptor("firmata", "/dev/ttyACM0")
sensor := gpio.NewAnalogSensor(firmataAdaptor, "sensor", "0")
led := gpio.NewLed(firmataAdaptor, "led", "3")

work := func() {
gobot.Every("0.1s", func() {
gobot.Every(0.1*time.Second, func() {
val := sensor.Read()
brightness := uint8(gpio.ToPwm(val))
fmt.Println("sensor", val)
Expand All @@ -30,11 +24,8 @@ func main() {
})
}

robot := gobot.Robot{
Connections: []gobot.Connection{firmataAdaptor},
Devices: []gobot.Device{sensor, led},
Work: work,
}
gbot.Robots = append(gbot.Robots,
gobot.NewRobot("sensorBot", []gobot.Connection{firmataAdaptor}, []gobot.Device{sensor, led}, work))

robot.Start()
gbot.Start()
}
28 changes: 9 additions & 19 deletions examples/firmata_makey_button.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@ package main

import (
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/firmata"
"github.com/hybridgroup/gobot/gpio"
"github.com/hybridgroup/gobot/platforms/firmata"
"github.com/hybridgroup/gobot/platforms/gpio"
)

func main() {
firmataAdaptor := firmata.NewFirmataAdaptor()
firmataAdaptor.Name = "firmata"
firmataAdaptor.Port = "/dev/ttyACM0"

button := gpio.NewMakeyButtonDriver(firmataAdaptor)
button.Name = "button"
button.Pin = "2"

led := gpio.NewLedDriver(firmataAdaptor)
led.Name = "led"
led.Pin = "13"
gbot := gobot.NewGobot()
firmataAdaptor := firmata.NewFirmataAdaptor("firmata", "/dev/ttyACM0")
button := gpio.NewMakeyButtonDriver(firmataAdaptor, "button", "2")
led := gpio.NewLedDriver(firmataAdaptor, "led", "13")

work := func() {
gobot.On(button.Events["push"], func(data interface{}) {
Expand All @@ -29,11 +22,8 @@ func main() {
})
}

robot := gobot.Robot{
Connections: []gobot.Connection{firmataAdaptor},
Devices: []gobot.Device{button, led},
Work: work,
}
gbot.Robots = append(gbot.Robots,
gobot.NewRobot("makeyBot", []gobot.Connection{firmataAdaptor}, []gobot.Device{button, led}, work))

robot.Start()
gbot.Start()
}
26 changes: 10 additions & 16 deletions examples/firmata_motor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,21 @@ package main

import (
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/firmata"
"github.com/hybridgroup/gobot/gpio"
"github.com/hybridgroup/gobot/platforms/firmata"
"github.com/hybridgroup/gobot/platforms/gpio"
"time"
)

func main() {
firmataAdaptor := firmata.NewFirmataAdaptor()
firmataAdaptor.Name = "firmata"
firmataAdaptor.Port = "/dev/ttyACM0"

motor := gpio.NewMotorDriver(firmataAdaptor)
motor.Name = "motor"
motor.SpeedPin = "3"
gbot := gobot.NewGobot()
firmataAdaptor := firmata.NewFirmataAdaptor("firmata", "/dev/ttyACM0")
motor := gpio.NewMotorDriver(firmataAdaptor, "motor", "3")

work := func() {
speed := byte(0)
fade_amount := byte(15)

gobot.Every("0.1s", func() {
gobot.Every(0.1*time.Second, func() {
motor.Speed(speed)
speed = speed + fade_amount
if speed == 0 || speed == 255 {
Expand All @@ -28,11 +25,8 @@ func main() {
})
}

robot := gobot.Robot{
Connections: []gobot.Connection{firmataAdaptor},
Devices: []gobot.Device{motor},
Work: work,
}
gbot.Robots = append(gbot.Robots,
gobot.NewRobot("motorBot", []gobot.Connection{firmataAdaptor}, []gobot.Device{motor}, work))

robot.Start()
gbot.Start()
}
Loading

0 comments on commit cb3f65c

Please sign in to comment.