Update spark package and examples

This commit is contained in:
Adrian Zankich 2014-05-22 21:04:47 -07:00
parent cfc11f8eda
commit f81aa43336
6 changed files with 57 additions and 88 deletions

View File

@ -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()
}

View File

@ -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()
}

View File

@ -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": "",
}
master := gobot.NewGobot()
button := gpio.NewButton(sparkCore)
button.Name = "button"
button.Pin = "D5"
button.Interval = "2s"
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{}) {
@ -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()
}

View File

@ -2,27 +2,22 @@ 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 {
@ -30,12 +25,8 @@ func main() {
}
})
}
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()
}

View File

@ -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 {
@ -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)
@ -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)
@ -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)
@ -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)
@ -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 {

View File

@ -11,7 +11,7 @@ var _ = Describe("Spark", func() {
)
BeforeEach(func() {
s = NewSparkCoreAdaptor()
s = NewSparkCoreAdaptor("bot", "", "")
})
It("Must be able to Finalize", func() {