sprkplus: add new platform for Sphero SPRK+

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-10-31 15:15:55 +01:00
parent d2e6c53ac9
commit 1013863f7c
8 changed files with 262 additions and 1 deletions

View File

@ -215,7 +215,8 @@ Gobot has a extensible system for connecting to hardware devices. The following
- [Raspberry Pi](http://www.raspberrypi.org/) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/raspi)
- [Sphero](http://www.sphero.com/) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/sphero)
- [Sphero BB-8](http://www.sphero.com/bb8) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/sphero/bb8)
- [Sphero Ollie](http://www.sphero.com/) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/sphero/ollie)
- [Sphero Ollie](http://www.sphero.com/ollie) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/sphero/ollie)
- [Sphero SPRK+](http://www.sphero.com/sprk-plus) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/sphero/sprkplus)
- [Tinker Board](https://www.asus.com/us/Single-Board-Computer/Tinker-Board/) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/tinkerboard)
Support for many devices that use General Purpose Input/Output (GPIO) have

45
examples/sprkplus.go Normal file
View File

@ -0,0 +1,45 @@
// +build example
//
// Do not build by default.
/*
How to run
Pass the Bluetooth address or name as the first param:
go run examples/sprkplus.go SK-1234
NOTE: sudo is required to use BLE in Linux
*/
package main
import (
"os"
"time"
"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/ble"
"gobot.io/x/gobot/platforms/sphero/sprkplus"
)
func main() {
bleAdaptor := ble.NewClientAdaptor(os.Args[1])
sprk := sprkplus.NewDriver(bleAdaptor)
work := func() {
gobot.Every(1*time.Second, func() {
r := uint8(gobot.Rand(255))
g := uint8(gobot.Rand(255))
b := uint8(gobot.Rand(255))
sprk.SetRGB(r, g, b)
})
}
robot := gobot.NewRobot("sprkBot",
[]gobot.Connection{bleAdaptor},
[]gobot.Device{sprk},
work,
)
robot.Start()
}

View File

@ -0,0 +1,13 @@
Copyright (c) 2014-2017 The Hybrid Group
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View File

@ -0,0 +1,75 @@
# Sphero SPRK+
The Sphero SPRK+ is a toy robot from Sphero that is controlled using Bluetooth LE. For more information, go to [http://www.sphero.com/sprk-plus](http://www.sphero.com/sprk-plus)
## How to Install
```
go get -d -u gobot.io/x/gobot/...
```
## How to Use
```go
package main
import (
"os"
"time"
"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/ble"
"gobot.io/x/gobot/platforms/sphero/sprkplus"
)
func main() {
bleAdaptor := ble.NewClientAdaptor(os.Args[1])
sprk := sprkplus.NewDriver(bleAdaptor)
work := func() {
gobot.Every(1*time.Second, func() {
r := uint8(gobot.Rand(255))
g := uint8(gobot.Rand(255))
b := uint8(gobot.Rand(255))
sprk.SetRGB(r, g, b)
})
}
robot := gobot.NewRobot("sprk",
[]gobot.Connection{bleAdaptor},
[]gobot.Device{sprk},
work,
)
robot.Start()
}
```
## How to Connect
The Sphero SPRK+ is a Bluetooth LE device.
You need to know the BLE ID of the SPRK+ you want to connect to. The Gobot BLE client adaptor also lets you connect by friendly name, aka "SK-1247".
### OSX
To run any of the Gobot BLE code you must use the `GODEBUG=cgocheck=0` flag in order to get around some of the issues in the CGo-based implementation.
If you connect by name, then you do not need to worry about the Bluetooth LE ID. However, if you want to connect by ID, OS X uses its own Bluetooth ID system which is different from the IDs used on Linux. The code calls thru the XPC interfaces provided by OSX, so as a result does not need to run under sudo.
For example:
GODEBUG=cgocheck=0 go run examples/sprkplus.go SK-1247
### Ubuntu
On Linux the BLE code will need to run as a root user account. The easiest way to accomplish this is probably to use `go build` to build your program, and then to run the requesting executable using `sudo`.
For example:
go build examples/sprkplus.go
sudo ./sprkplus SK-1247
### Windows
Hopefully coming soon...

View File

@ -0,0 +1,7 @@
/*
Package sprkplus contains the Gobot driver for the Sphero SPRK+.
For more information refer to the sprkplus README:
https://github.com/hybridgroup/gobot/blob/master/platforms/sphero/sprkplus/README.md
*/
package sprkplus // import "gobot.io/x/gobot/platforms/sphero/sprkplus"

View File

@ -0,0 +1,69 @@
package sprkplus
import (
"sync"
"gobot.io/x/gobot/platforms/ble"
)
var _ ble.BLEConnector = (*bleTestClientAdaptor)(nil)
type bleTestClientAdaptor struct {
name string
address string
mtx sync.Mutex
withoutReponses bool
testReadCharacteristic func(string) ([]byte, error)
testWriteCharacteristic func(string, []byte) error
}
func (t *bleTestClientAdaptor) Connect() (err error) { return }
func (t *bleTestClientAdaptor) Reconnect() (err error) { return }
func (t *bleTestClientAdaptor) Disconnect() (err error) { return }
func (t *bleTestClientAdaptor) Finalize() (err error) { return }
func (t *bleTestClientAdaptor) Name() string { return t.name }
func (t *bleTestClientAdaptor) SetName(n string) { t.name = n }
func (t *bleTestClientAdaptor) Address() string { return t.address }
func (t *bleTestClientAdaptor) WithoutReponses(use bool) { t.withoutReponses = use }
func (t *bleTestClientAdaptor) ReadCharacteristic(cUUID string) (data []byte, err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
return t.testReadCharacteristic(cUUID)
}
func (t *bleTestClientAdaptor) WriteCharacteristic(cUUID string, data []byte) (err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
return t.testWriteCharacteristic(cUUID, data)
}
func (t *bleTestClientAdaptor) Subscribe(cUUID string, f func([]byte, error)) (err error) {
// TODO: implement this...
return
}
func (t *bleTestClientAdaptor) TestReadCharacteristic(f func(cUUID string) (data []byte, err error)) {
t.mtx.Lock()
defer t.mtx.Unlock()
t.testReadCharacteristic = f
}
func (t *bleTestClientAdaptor) TestWriteCharacteristic(f func(cUUID string, data []byte) (err error)) {
t.mtx.Lock()
defer t.mtx.Unlock()
t.testWriteCharacteristic = f
}
func NewBleTestAdaptor() *bleTestClientAdaptor {
return &bleTestClientAdaptor{
address: "01:02:03:04:05:06",
testReadCharacteristic: func(cUUID string) (data []byte, e error) {
return
},
testWriteCharacteristic: func(cUUID string, data []byte) (e error) {
return
},
}
}

View File

@ -0,0 +1,22 @@
package sprkplus
import (
"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/ble"
"gobot.io/x/gobot/platforms/sphero/ollie"
)
// Driver represents a Sphero SPRK+
type SPRKPlusDriver struct {
*ollie.Driver
}
// NewDriver creates a Driver for a Sphero SPRK+
func NewDriver(a ble.BLEConnector) *SPRKPlusDriver {
d := ollie.NewDriver(a)
d.SetName(gobot.DefaultName("SPRKPlus"))
return &SPRKPlusDriver{
Driver: d,
}
}

View File

@ -0,0 +1,29 @@
package sprkplus
import (
"strings"
"testing"
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
)
var _ gobot.Driver = (*SPRKPlusDriver)(nil)
func initTestSPRKPlusDriver() *SPRKPlusDriver {
d := NewDriver(NewBleTestAdaptor())
return d
}
func TestSPRKPlusDriver(t *testing.T) {
d := initTestSPRKPlusDriver()
gobottest.Assert(t, strings.HasPrefix(d.Name(), "SPRK"), true)
d.SetName("NewName")
gobottest.Assert(t, d.Name(), "NewName")
}
func TestSPRKPlusDriverStartAndHalt(t *testing.T) {
d := initTestSPRKPlusDriver()
gobottest.Assert(t, d.Start(), nil)
gobottest.Assert(t, d.Halt(), nil)
}