Skip to content

Commit e2b710b

Browse files
authored
neurosky: use serialport adaptor and move driver to drivers/serial (hybridgroup#1061)
1 parent 9430005 commit e2b710b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+812
-820
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ import (
111111

112112
func main() {
113113
adaptor := serialport.NewAdaptor("/dev/rfcomm0")
114-
driver := serial.NewSpheroDriver(adaptor)
114+
driver := sphero.NewSpheroDriver(adaptor)
115115

116116
work := func() {
117117
gobot.Every(3*time.Second, func() {
@@ -179,14 +179,14 @@ import (
179179

180180
"gobot.io/x/gobot/v2"
181181
"gobot.io/x/gobot/v2/api"
182-
"gobot.io/x/gobot/v2/drivers/common/sphero"
182+
"gobot.io/x/gobot/v2/drivers/common/spherocommon"
183183
"gobot.io/x/gobot/v2/drivers/serial"
184184
"gobot.io/x/gobot/v2/platforms/serialport"
185185
)
186186

187187
func NewSwarmBot(port string) *gobot.Robot {
188188
spheroAdaptor := serialport.NewAdaptor(port)
189-
spheroDriver := serial.NewSpheroDriver(spheroAdaptor, serial.WithName("Sphero" + port))
189+
spheroDriver := sphero.NewSpheroDriver(spheroAdaptor, serial.WithName("Sphero" + port))
190190

191191
work := func() {
192192
spheroDriver.Stop()
@@ -390,6 +390,7 @@ the `gobot/drivers/serial` package:
390390

391391
- [UART](https://en.wikipedia.org/wiki/Serial_port) <=> [Drivers](https://github.com/hybridgroup/gobot/tree/master/drivers/serial)
392392
- Sphero: Sphero
393+
- Neurosky: MindWave
393394

394395
Support for devices that use Serial Peripheral Interface (SPI) have
395396
a shared set of drivers provided using the `gobot/drivers/spi` package:

doc.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ Finally, you can use Master Gobot to add the complete Gobot API or control swarm
8888
8989
"gobot.io/x/gobot/v2"
9090
"gobot.io/x/gobot/v2/api"
91-
"gobot.io/x/gobot/v2/drivers/common/sphero"
91+
"gobot.io/x/gobot/v2/drivers/common/spherocommon"
9292
"gobot.io/x/gobot/v2/drivers/serial"
9393
"gobot.io/x/gobot/v2/platforms/serialport"
9494
)
9595
9696
func NewSwarmBot(port string) *gobot.Robot {
9797
spheroAdaptor := serialport.NewAdaptor(port)
98-
spheroDriver := serial.NewSpheroDriver(spheroAdaptor, serial.WithName("Sphero" + port))
98+
spheroDriver := sphero.NewSpheroDriver(spheroAdaptor, serial.WithName("Sphero" + port))
9999
100100
work := func() {
101101
spheroDriver.Stop()

drivers/MIGRATION.md

+38-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ returned instead and the log output needs to be done at caller side.
8080
### Sphero adaptor split off
8181

8282
The Serial Based Sphero adaptor was split off into a generic serial adaptor and the driver part. With this, the imports
83-
needs to be adjusted. In addition all events now have a postfix "Event", see below.
83+
needs to be adjusted. In addition all events now have a suffix "Event", see below.
8484

8585
```go
8686
// old
@@ -100,19 +100,54 @@ import(
100100
// new
101101
import(
102102
...
103-
"gobot.io/x/gobot/v2/drivers/common/sphero"
103+
"gobot.io/x/gobot/v2/drivers/common/spherocommon"
104104
"gobot.io/x/gobot/v2/drivers/serial"
105105
"gobot.io/x/gobot/v2/platforms/serialport"
106106
...
107107
)
108108
...
109109
adaptor := serialport.NewAdaptor("/dev/rfcomm0")
110-
spheroDriver := serial.NewSpheroDriver(adaptor)
110+
spheroDriver := sphero.NewSpheroDriver(adaptor)
111111
...
112112
_ = spheroDriver.On(sphero.CollisionEvent, func(data interface{}) {
113113
...
114114
```
115115
116+
### Neurosky adaptor split off
117+
118+
The Neurosky adaptor now us the generic serial adaptor. The driver part was moved. With this, the imports needs to be
119+
adjusted. In addition all events now have a suffix "Event", see below.
120+
121+
```go
122+
// old
123+
import(
124+
...
125+
"gobot.io/x/gobot/v2/platforms/neurosky"
126+
...
127+
)
128+
129+
...
130+
adaptor := neurosky.NewAdaptor("/dev/rfcomm0")
131+
neuro := neurosky.NewDriver(adaptor)
132+
...
133+
_ = neuro.On(neurosky.Extended, func(data interface{}) {
134+
...
135+
136+
// new
137+
import(
138+
...
139+
"gobot.io/x/gobot/v2/drivers/serial/neurosky"
140+
"gobot.io/x/gobot/v2/platforms/serialport"
141+
...
142+
)
143+
...
144+
adaptor := serialport.NewAdaptor("/dev/rfcomm0", serialport.WithName("Neurosky"), serialport.WithBaudRate(57600))
145+
neuro := neurosky.NewMindWaveDriver(adaptor)
146+
...
147+
_ = neuro.On(neurosky.ExtendedEvent, func(data interface{}) {
148+
...
149+
```
150+
116151
## Switch from version 2.2.0 (gpio drivers affected)
117152
118153
### gpio.ButtonDriver, gpio.PIRMotionDriver: substitute parameter "v time.duration"

drivers/ble/sphero/sphero_bb8_driver.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package sphero
33
import (
44
"gobot.io/x/gobot/v2"
55
"gobot.io/x/gobot/v2/drivers/ble"
6-
"gobot.io/x/gobot/v2/drivers/common/sphero"
6+
"gobot.io/x/gobot/v2/drivers/common/spherocommon"
77
)
88

99
// BB8Driver represents a Sphero BB-8
@@ -17,8 +17,8 @@ func NewBB8Driver(a gobot.BLEConnector, opts ...ble.OptionApplier) *BB8Driver {
1717
}
1818

1919
// bb8DefaultCollisionConfig returns a CollisionConfig with sensible collision defaults
20-
func bb8DefaultCollisionConfig() sphero.CollisionConfig {
21-
return sphero.CollisionConfig{
20+
func bb8DefaultCollisionConfig() spherocommon.CollisionConfig {
21+
return spherocommon.CollisionConfig{
2222
Method: 0x01,
2323
Xt: 0x20,
2424
Yt: 0x20,

drivers/ble/sphero/sphero_ollie_driver.go

+19-19
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"gobot.io/x/gobot/v2"
1010
"gobot.io/x/gobot/v2/drivers/ble"
11-
"gobot.io/x/gobot/v2/drivers/common/sphero"
11+
"gobot.io/x/gobot/v2/drivers/common/spherocommon"
1212
)
1313

1414
// MotorModes is used to configure the motor
@@ -63,14 +63,14 @@ type Point2D struct {
6363
type OllieDriver struct {
6464
*ble.Driver
6565
gobot.Eventer
66-
defaultCollisionConfig sphero.CollisionConfig
66+
defaultCollisionConfig spherocommon.CollisionConfig
6767
seq uint8
6868
collisionResponse []uint8
6969
packetChannel chan *packet
7070
asyncBuffer []byte
7171
asyncMessage []byte
7272
locatorCallback func(p Point2D)
73-
powerstateCallback func(p sphero.PowerStatePacket)
73+
powerstateCallback func(p spherocommon.PowerStatePacket)
7474
}
7575

7676
// NewOllieDriver creates a driver for a Sphero Ollie
@@ -80,7 +80,7 @@ func NewOllieDriver(a gobot.BLEConnector, opts ...ble.OptionApplier) *OllieDrive
8080

8181
func newOllieBaseDriver(
8282
a gobot.BLEConnector, name string,
83-
dcc sphero.CollisionConfig, opts ...ble.OptionApplier,
83+
dcc spherocommon.CollisionConfig, opts ...ble.OptionApplier,
8484
) *OllieDriver {
8585
d := &OllieDriver{
8686
defaultCollisionConfig: dcc,
@@ -89,8 +89,8 @@ func newOllieBaseDriver(
8989
}
9090
d.Driver = ble.NewDriver(a, name, d.initialize, d.shutdown, opts...)
9191

92-
d.AddEvent(sphero.ErrorEvent)
93-
d.AddEvent(sphero.CollisionEvent)
92+
d.AddEvent(spherocommon.ErrorEvent)
93+
d.AddEvent(spherocommon.CollisionEvent)
9494

9595
return d
9696
}
@@ -118,7 +118,7 @@ func (d *OllieDriver) Wake() error {
118118
}
119119

120120
// ConfigureCollisionDetection configures the sensitivity of the detection.
121-
func (d *OllieDriver) ConfigureCollisionDetection(cc sphero.CollisionConfig) {
121+
func (d *OllieDriver) ConfigureCollisionDetection(cc spherocommon.CollisionConfig) {
122122
d.sendCraftPacket([]uint8{cc.Method, cc.Xt, cc.Yt, cc.Xs, cc.Ys, cc.Dead}, 0x02, 0x12)
123123
}
124124

@@ -130,7 +130,7 @@ func (d *OllieDriver) GetLocatorData(f func(p Point2D)) {
130130
}
131131

132132
// GetPowerState calls the passed function with the Power State information from the sphero
133-
func (d *OllieDriver) GetPowerState(f func(p sphero.PowerStatePacket)) {
133+
func (d *OllieDriver) GetPowerState(f func(p spherocommon.PowerStatePacket)) {
134134
// CID 0x20 is the code for the power state
135135
d.sendCraftPacket([]uint8{}, 0x00, 0x20)
136136
d.powerstateCallback = f
@@ -194,7 +194,7 @@ func (d *OllieDriver) Sleep() {
194194
}
195195

196196
// SetDataStreamingConfig passes the config to the sphero to stream sensor data
197-
func (d *OllieDriver) SetDataStreamingConfig(dsc sphero.DataStreamingConfig) error {
197+
func (d *OllieDriver) SetDataStreamingConfig(dsc spherocommon.DataStreamingConfig) error {
198198
buf := new(bytes.Buffer)
199199
if err := binary.Write(buf, binary.BigEndian, dsc); err != nil {
200200
return err
@@ -225,7 +225,7 @@ func (d *OllieDriver) initialize() error {
225225
packet := <-d.packetChannel
226226
err := d.writeCommand(packet)
227227
if err != nil {
228-
d.Publish(d.Event(sphero.ErrorEvent), err)
228+
d.Publish(d.Event(spherocommon.ErrorEvent), err)
229229
}
230230
}
231231
}()
@@ -331,13 +331,13 @@ func (d *OllieDriver) handleDataStreaming(data []byte) {
331331

332332
// data packet is the same as for the normal sphero, since the same communication api is used
333333
// only difference in communication is that the "newer" spheros use BLE for communications
334-
var dataPacket sphero.DataStreamingPacket
334+
var dataPacket spherocommon.DataStreamingPacket
335335
buffer := bytes.NewBuffer(data[5:]) // skip header
336336
if err := binary.Read(buffer, binary.BigEndian, &dataPacket); err != nil {
337337
panic(err)
338338
}
339339

340-
d.Publish(sphero.SensorDataEvent, dataPacket)
340+
d.Publish(spherocommon.SensorDataEvent, dataPacket)
341341
}
342342

343343
func (d *OllieDriver) handleLocatorDetected(data []uint8) {
@@ -368,7 +368,7 @@ func (d *OllieDriver) handleLocatorDetected(data []uint8) {
368368
}
369369

370370
func (d *OllieDriver) handlePowerStateDetected(data []uint8) {
371-
var dataPacket sphero.PowerStatePacket
371+
var dataPacket spherocommon.PowerStatePacket
372372
buffer := bytes.NewBuffer(data[5:]) // skip header
373373
if err := binary.Read(buffer, binary.BigEndian, &dataPacket); err != nil {
374374
panic(err)
@@ -404,18 +404,18 @@ func (d *OllieDriver) handleCollisionDetected(data []uint8) {
404404
// confirm checksum
405405
size := len(d.collisionResponse)
406406
chk := d.collisionResponse[size-1] // last byte is checksum
407-
if chk != sphero.CalculateChecksum(d.collisionResponse[2:size-1]) {
407+
if chk != spherocommon.CalculateChecksum(d.collisionResponse[2:size-1]) {
408408
return
409409
}
410410

411-
var collision sphero.CollisionPacket
411+
var collision spherocommon.CollisionPacket
412412
buffer := bytes.NewBuffer(d.collisionResponse[5:]) // skip header
413413
if err := binary.Read(buffer, binary.BigEndian, &collision); err != nil {
414414
panic(err)
415415
}
416416
d.collisionResponse = nil // clear the current response
417417

418-
d.Publish(sphero.CollisionEvent, collision)
418+
d.Publish(spherocommon.CollisionEvent, collision)
419419
}
420420

421421
func (d *OllieDriver) sendCraftPacket(body []uint8, did byte, cid byte) {
@@ -430,15 +430,15 @@ func (d *OllieDriver) craftPacket(body []uint8, did byte, cid byte) *packet {
430430
packet := &packet{
431431
body: body,
432432
header: hdr,
433-
checksum: sphero.CalculateChecksum(buf[2:]),
433+
checksum: spherocommon.CalculateChecksum(buf[2:]),
434434
}
435435

436436
return packet
437437
}
438438

439439
// ollieDefaultCollisionConfig returns a CollisionConfig with sensible collision defaults
440-
func ollieDefaultCollisionConfig() sphero.CollisionConfig {
441-
return sphero.CollisionConfig{
440+
func ollieDefaultCollisionConfig() spherocommon.CollisionConfig {
441+
return spherocommon.CollisionConfig{
442442
Method: 0x01,
443443
Xt: 0x20,
444444
Yt: 0x20,

drivers/ble/sphero/sphero_ollie_driver_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"gobot.io/x/gobot/v2"
1313
"gobot.io/x/gobot/v2/drivers/ble"
1414
"gobot.io/x/gobot/v2/drivers/ble/testutil"
15-
"gobot.io/x/gobot/v2/drivers/common/sphero"
15+
"gobot.io/x/gobot/v2/drivers/common/spherocommon"
1616
)
1717

1818
var _ gobot.Driver = (*OllieDriver)(nil)
@@ -82,12 +82,12 @@ func TestLocatorData(t *testing.T) {
8282
func TestDataStreaming(t *testing.T) {
8383
d := initTestOllieDriver()
8484

85-
err := d.SetDataStreamingConfig(sphero.DefaultDataStreamingConfig())
85+
err := d.SetDataStreamingConfig(spherocommon.DefaultDataStreamingConfig())
8686
require.NoError(t, err)
8787

8888
responseChan := make(chan bool)
8989
err = d.On("sensordata", func(data interface{}) {
90-
cont := data.(sphero.DataStreamingPacket)
90+
cont := data.(spherocommon.DataStreamingPacket)
9191
// fmt.Printf("got streaming packet: %+v \n", cont)
9292
assert.Equal(t, int16(10), cont.RawAccX)
9393
responseChan <- true

drivers/ble/sphero/sphero_sprkplus_driver.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package sphero
33
import (
44
"gobot.io/x/gobot/v2"
55
"gobot.io/x/gobot/v2/drivers/ble"
6-
"gobot.io/x/gobot/v2/drivers/common/sphero"
6+
"gobot.io/x/gobot/v2/drivers/common/spherocommon"
77
)
88

99
// SPRKPlusDriver represents a Sphero SPRK+
@@ -17,8 +17,8 @@ func NewSPRKPlusDriver(a gobot.BLEConnector, opts ...ble.OptionApplier) *SPRKPlu
1717
}
1818

1919
// sprkplusDefaultCollisionConfig returns a CollisionConfig with sensible collision defaults
20-
func sprkplusDefaultCollisionConfig() sphero.CollisionConfig {
21-
return sphero.CollisionConfig{
20+
func sprkplusDefaultCollisionConfig() spherocommon.CollisionConfig {
21+
return spherocommon.CollisionConfig{
2222
Method: 0x01,
2323
Xt: 0x20,
2424
Yt: 0x20,

drivers/common/sphero/sphero_driver.go drivers/common/spherocommon/spherocommon.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package sphero
1+
package spherocommon
22

33
const (
44
// ErrorEvent event when error encountered

drivers/common/sphero/sphero_packets.go drivers/common/spherocommon/spherocommon_packets.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package sphero
1+
package spherocommon
22

33
// LocatorConfig provides configuration for the Location api.
44
// https://github.com/orbotix/DeveloperResources/blob/master/docs/Sphero_API_1.50.pdf

drivers/common/sphero/sphero_driver_test.go drivers/common/spherocommon/spherocommon_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package sphero
1+
package spherocommon
22

33
import "testing"
44

drivers/serial/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ Please refer to the main [README.md](https://github.com/hybridgroup/gobot/blob/r
1313
Gobot has a extensible system for connecting to hardware devices. The following Serial devices are currently supported:
1414

1515
- Sphero: Sphero
16+
- Neurosky: MindWave

drivers/serial/helpers_test.go

-28
This file was deleted.

drivers/serial/neurosky/LICENSE

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

0 commit comments

Comments
 (0)