Skip to content

Commit

Permalink
Update spark package and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed May 23, 2014
1 parent cfc11f8 commit f81aa43
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 88 deletions.
31 changes: 11 additions & 20 deletions examples/spark_core_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,28 @@ package main

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

func main() {
master := gobot.NewMaster()
gobot.Api(master)
master := gobot.NewGobot()
api.Api(master).Start()

sparkCore := spark.NewSparkCoreAdaptor()
sparkCore.Name = "spark"
sparkCore.Params = map[string]interface{}{
"device_id": "",
"access_token": "",
}
sparkCore := spark.NewSparkCoreAdaptor("spark", "device_id", "access_token")

led := gpio.NewLed(sparkCore)
led.Name = "led"
led.Pin = "D7"
led := gpio.NewLed(sparkCore, "led", "D7")

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

master.Robots = append(master.Robots, &gobot.Robot{
Name: "spark",
Connections: []gobot.Connection{sparkCore},
Devices: []gobot.Device{led},
Work: work,
})
master.Robots = append(master.Robots,
gobot.NewRobot("spark", []gobot.Connection{sparkCore}, []gobot.Device{led}, work))

master.Start()
}
28 changes: 10 additions & 18 deletions examples/spark_core_blink.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,25 @@ package main

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

func main() {
sparkCore := spark.NewSparkCoreAdaptor()
sparkCore.Name = "spark"
sparkCore.Params = map[string]interface{}{
"device_id": "",
"access_token": "",
}
master := gobot.NewGobot()

led := gpio.NewLed(sparkCore)
led.Name = "led"
led.Pin = "D7"
sparkCore := spark.NewSparkCoreAdaptor("spark", "device_id", "access_token")
led := gpio.NewLed(sparkCore, "led", "D7")

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

robot := gobot.Robot{
Connections: []gobot.Connection{sparkCore},
Devices: []gobot.Device{led},
Work: work,
}
master.Robots = append(master.Robots,
gobot.NewRobot("spark", []gobot.Connection{sparkCore}, []gobot.Device{led}, work))

robot.Start()
master.Start()
}
33 changes: 10 additions & 23 deletions examples/spark_core_button.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
package main

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

func main() {
sparkCore := spark.NewSparkCoreAdaptor()
sparkCore.Name = "spark"
sparkCore.Params = map[string]interface{}{
"device_id": "",
"access_token": "",
}

button := gpio.NewButton(sparkCore)
button.Name = "button"
button.Pin = "D5"
button.Interval = "2s"
master := gobot.NewGobot()

led := gpio.NewLed(sparkCore)
led.Name = "led"
led.Pin = "D7"
sparkCore := spark.NewSparkCoreAdaptor("spark", "device_id", "access_token")
led := gpio.NewLed(sparkCore, "led", "D7")
button := gpio.NewButton(sparkCore, "button", "D5")

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

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

robot.Start()
master.Start()
}
29 changes: 10 additions & 19 deletions examples/spark_core_led_brithgness.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,31 @@ package main

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

func main() {
sparkCore := spark.NewSparkCoreAdaptor()
sparkCore.Name = "spark"
sparkCore.Params = map[string]interface{}{
"device_id": "",
"access_token": "",
}
master := gobot.NewGobot()

led := gpio.NewLed(sparkCore)
led.Name = "led"
led.Pin = "A1"
sparkCore := spark.NewSparkCoreAdaptor("spark", "device_id", "access_token")
led := gpio.NewLed(sparkCore, "led", "A1")

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

gobot.Every("0.5s", func() {
gobot.Every(0.5*time.Second, func() {
led.Brightness(brightness)
brightness = brightness + fade_amount
if brightness == 0 || brightness == 255 {
fade_amount = -fade_amount
}
})
}
master.Robots = append(master.Robots,
gobot.NewRobot("spark", []gobot.Connection{sparkCore}, []gobot.Device{led}, work))

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

robot.Start()
master.Start()
}
22 changes: 15 additions & 7 deletions platforms/spark/spark_core_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@ import (

type SparkCoreAdaptor struct {
gobot.Adaptor
DeviceId string
AccessToken string
}

func NewSparkCoreAdaptor() *SparkCoreAdaptor {
return &SparkCoreAdaptor{}
func NewSparkCoreAdaptor(name string, deviceId string, accessToken string) *SparkCoreAdaptor {
return &SparkCoreAdaptor{
Adaptor: gobot.Adaptor{
Name: name,
},
DeviceId: deviceId,
AccessToken: accessToken,
}
}

func (s *SparkCoreAdaptor) Connect() bool {
Expand All @@ -30,7 +38,7 @@ func (s *SparkCoreAdaptor) Finalize() bool {
func (s *SparkCoreAdaptor) AnalogRead(pin string) float64 {
params := url.Values{
"params": {pin},
"access_token": {s.Params["access_token"].(string)},
"access_token": {s.AccessToken},
}
url := fmt.Sprintf("%v/analogread", s.deviceUrl())
resp := s.postToSpark(url, params)
Expand All @@ -47,7 +55,7 @@ func (s *SparkCoreAdaptor) PwmWrite(pin string, level byte) {
func (s *SparkCoreAdaptor) AnalogWrite(pin string, level byte) {
params := url.Values{
"params": {fmt.Sprintf("%v,%v", pin, level)},
"access_token": {s.Params["access_token"].(string)},
"access_token": {s.AccessToken},
}
url := fmt.Sprintf("%v/analogwrite", s.deviceUrl())
s.postToSpark(url, params)
Expand All @@ -56,7 +64,7 @@ func (s *SparkCoreAdaptor) AnalogWrite(pin string, level byte) {
func (s *SparkCoreAdaptor) DigitalWrite(pin string, level byte) {
params := url.Values{
"params": {fmt.Sprintf("%v,%v", pin, s.pinLevel(level))},
"access_token": {s.Params["access_token"].(string)},
"access_token": {s.AccessToken},
}
url := fmt.Sprintf("%v/digitalwrite", s.deviceUrl())
s.postToSpark(url, params)
Expand All @@ -65,7 +73,7 @@ func (s *SparkCoreAdaptor) DigitalWrite(pin string, level byte) {
func (s *SparkCoreAdaptor) DigitalRead(pin string) int {
params := url.Values{
"params": {pin},
"access_token": {s.Params["access_token"].(string)},
"access_token": {s.AccessToken},
}
url := fmt.Sprintf("%v/digitalread", s.deviceUrl())
resp := s.postToSpark(url, params)
Expand All @@ -76,7 +84,7 @@ func (s *SparkCoreAdaptor) DigitalRead(pin string) int {
}

func (s *SparkCoreAdaptor) deviceUrl() string {
return fmt.Sprintf("https://api.spark.io/v1/devices/%v", s.Params["device_id"])
return fmt.Sprintf("https://api.spark.io/v1/devices/%v", s.DeviceId)
}

func (s *SparkCoreAdaptor) pinLevel(level byte) string {
Expand Down
2 changes: 1 addition & 1 deletion platforms/spark/spark_core_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var _ = Describe("Spark", func() {
)

BeforeEach(func() {
s = NewSparkCoreAdaptor()
s = NewSparkCoreAdaptor("bot", "", "")
})

It("Must be able to Finalize", func() {
Expand Down

0 comments on commit f81aa43

Please sign in to comment.