diff --git a/examples/spark_core_api.go b/examples/spark_core_api.go index a65c5333..d30386f2 100644 --- a/examples/spark_core_api.go +++ b/examples/spark_core_api.go @@ -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() } diff --git a/examples/spark_core_blink.go b/examples/spark_core_blink.go index 1c398623..d92b918e 100644 --- a/examples/spark_core_blink.go +++ b/examples/spark_core_blink.go @@ -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() } diff --git a/examples/spark_core_button.go b/examples/spark_core_button.go index 2217c97e..8b3329d5 100644 --- a/examples/spark_core_button.go +++ b/examples/spark_core_button.go @@ -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() } diff --git a/examples/spark_core_led_brithgness.go b/examples/spark_core_led_brithgness.go index a37e18a6..c61b9520 100644 --- a/examples/spark_core_led_brithgness.go +++ b/examples/spark_core_led_brithgness.go @@ -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() } diff --git a/platforms/spark/spark_core_adaptor.go b/platforms/spark/spark_core_adaptor.go index 6099ef8f..3dcd5000 100644 --- a/platforms/spark/spark_core_adaptor.go +++ b/platforms/spark/spark_core_adaptor.go @@ -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 { diff --git a/platforms/spark/spark_core_adaptor_test.go b/platforms/spark/spark_core_adaptor_test.go index 0092a494..994a7981 100644 --- a/platforms/spark/spark_core_adaptor_test.go +++ b/platforms/spark/spark_core_adaptor_test.go @@ -11,7 +11,7 @@ var _ = Describe("Spark", func() { ) BeforeEach(func() { - s = NewSparkCoreAdaptor() + s = NewSparkCoreAdaptor("bot", "", "") }) It("Must be able to Finalize", func() {