mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-29 13:49:14 +08:00
Adding godocs documentation to neurosky package
This commit is contained in:
parent
bf7bc97c3a
commit
c8652c483f
@ -41,7 +41,7 @@ You should be able to pair your Mindwave using your normal system tray applet fo
|
|||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
71
platforms/neurosky/doc.go
Normal file
71
platforms/neurosky/doc.go
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
This package contains the Gobot adaptor and driver for the Neurosky Mindwave Mobile EEG (http://store.neurosky.com/products/mindwave-mobile).
|
||||||
|
|
||||||
|
Installing:
|
||||||
|
|
||||||
|
go get github.com/hybridgroup/gobot && go install github.com/hybridgroup/gobot/platforms/neurosky
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/hybridgroup/gobot"
|
||||||
|
"github.com/hybridgroup/gobot/platforms/neurosky"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
gbot := gobot.NewGobot()
|
||||||
|
|
||||||
|
adaptor := neurosky.NewNeuroskyAdaptor("neurosky", "/dev/rfcomm0")
|
||||||
|
neuro := neurosky.NewNeuroskyDriver(adaptor, "neuro")
|
||||||
|
|
||||||
|
work := func() {
|
||||||
|
gobot.On(neuro.Event("extended"), func(data interface{}) {
|
||||||
|
fmt.Println("Extended", data)
|
||||||
|
})
|
||||||
|
gobot.On(neuro.Event("signal"), func(data interface{}) {
|
||||||
|
fmt.Println("Signal", data)
|
||||||
|
})
|
||||||
|
gobot.On(neuro.Event("attention"), func(data interface{}) {
|
||||||
|
fmt.Println("Attention", data)
|
||||||
|
})
|
||||||
|
gobot.On(neuro.Event("meditation"), func(data interface{}) {
|
||||||
|
fmt.Println("Meditation", data)
|
||||||
|
})
|
||||||
|
gobot.On(neuro.Event("blink"), func(data interface{}) {
|
||||||
|
fmt.Println("Blink", data)
|
||||||
|
})
|
||||||
|
gobot.On(neuro.Event("wave"), func(data interface{}) {
|
||||||
|
fmt.Println("Wave", data)
|
||||||
|
})
|
||||||
|
gobot.On(neuro.Event("eeg"), func(data interface{}) {
|
||||||
|
eeg := data.(neurosky.EEG)
|
||||||
|
fmt.Println("Delta", eeg.Delta)
|
||||||
|
fmt.Println("Theta", eeg.Theta)
|
||||||
|
fmt.Println("LoAlpha", eeg.LoAlpha)
|
||||||
|
fmt.Println("HiAlpha", eeg.HiAlpha)
|
||||||
|
fmt.Println("LoBeta", eeg.LoBeta)
|
||||||
|
fmt.Println("HiBeta", eeg.HiBeta)
|
||||||
|
fmt.Println("LoGamma", eeg.LoGamma)
|
||||||
|
fmt.Println("MidGamma", eeg.MidGamma)
|
||||||
|
fmt.Println("\n")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
robot := gobot.NewRobot("brainBot",
|
||||||
|
[]gobot.Connection{adaptor},
|
||||||
|
[]gobot.Device{neuro},
|
||||||
|
work,
|
||||||
|
)
|
||||||
|
|
||||||
|
gbot.AddRobot(robot)
|
||||||
|
gbot.Start()
|
||||||
|
}
|
||||||
|
|
||||||
|
For further information refer to neuroky README:
|
||||||
|
https://github.com/hybridgroup/gobot/blob/master/platforms/neurosky/README.md
|
||||||
|
*/
|
||||||
|
package neurosky
|
@ -13,6 +13,7 @@ type NeuroskyAdaptor struct {
|
|||||||
connect func(*NeuroskyAdaptor)
|
connect func(*NeuroskyAdaptor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewNeuroskyAdaptor creates a neurosky adaptor with specified name
|
||||||
func NewNeuroskyAdaptor(name string, port string) *NeuroskyAdaptor {
|
func NewNeuroskyAdaptor(name string, port string) *NeuroskyAdaptor {
|
||||||
return &NeuroskyAdaptor{
|
return &NeuroskyAdaptor{
|
||||||
Adaptor: *gobot.NewAdaptor(
|
Adaptor: *gobot.NewAdaptor(
|
||||||
@ -30,12 +31,14 @@ func NewNeuroskyAdaptor(name string, port string) *NeuroskyAdaptor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Connect returns true if connection to device is successful
|
||||||
func (n *NeuroskyAdaptor) Connect() bool {
|
func (n *NeuroskyAdaptor) Connect() bool {
|
||||||
n.connect(n)
|
n.connect(n)
|
||||||
n.SetConnected(true)
|
n.SetConnected(true)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Finalize returns true if device finalization is successful
|
||||||
func (n *NeuroskyAdaptor) Finalize() bool {
|
func (n *NeuroskyAdaptor) Finalize() bool {
|
||||||
n.sp.Close()
|
n.sp.Close()
|
||||||
n.SetConnected(false)
|
n.SetConnected(false)
|
||||||
|
@ -7,13 +7,27 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const BTSync byte = 0xAA
|
const BTSync byte = 0xAA
|
||||||
const CodeEx byte = 0x55 // Extended code
|
|
||||||
const CodeSignalQuality byte = 0x02 // POOR_SIGNAL quality 0-255
|
// Extended code
|
||||||
const CodeAttention byte = 0x04 // ATTENTION eSense 0-100
|
const CodeEx byte = 0x55
|
||||||
const CodeMeditation byte = 0x05 // MEDITATION eSense 0-100
|
|
||||||
const CodeBlink byte = 0x16 // BLINK strength 0-255
|
// POOR_SIGNAL quality 0-255
|
||||||
const CodeWave byte = 0x80 // RAW wave value: 2-byte big-endian 2s-complement
|
const CodeSignalQuality byte = 0x02
|
||||||
const CodeAsicEEG byte = 0x83 // ASIC EEG POWER 8 3-byte big-endian integers
|
|
||||||
|
// ATTENTION eSense 0-100
|
||||||
|
const CodeAttention byte = 0x04
|
||||||
|
|
||||||
|
// MEDITATION eSense 0-100
|
||||||
|
const CodeMeditation byte = 0x05
|
||||||
|
|
||||||
|
// BLINK strength 0-255
|
||||||
|
const CodeBlink byte = 0x16
|
||||||
|
|
||||||
|
// RAW wave value: 2-byte big-endian 2s-complement
|
||||||
|
const CodeWave byte = 0x80
|
||||||
|
|
||||||
|
// ASIC EEG POWER 8 3-byte big-endian integers
|
||||||
|
const CodeAsicEEG byte = 0x83
|
||||||
|
|
||||||
type NeuroskyDriver struct {
|
type NeuroskyDriver struct {
|
||||||
gobot.Driver
|
gobot.Driver
|
||||||
@ -30,6 +44,16 @@ type EEG struct {
|
|||||||
MidGamma int
|
MidGamma int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewNeuroskyDriver creates a NeuroskyDriver by name
|
||||||
|
// and adds the following events:
|
||||||
|
//
|
||||||
|
// extended - user's current extended level
|
||||||
|
// signal - shows signal strength
|
||||||
|
// attention - user's current attention level
|
||||||
|
// meditation - user's current meditation level
|
||||||
|
// blink - user's current blink level
|
||||||
|
// wave - shows wave data
|
||||||
|
// eeg - showing eeg data
|
||||||
func NewNeuroskyDriver(a *NeuroskyAdaptor, name string) *NeuroskyDriver {
|
func NewNeuroskyDriver(a *NeuroskyAdaptor, name string) *NeuroskyDriver {
|
||||||
n := &NeuroskyDriver{
|
n := &NeuroskyDriver{
|
||||||
Driver: *gobot.NewDriver(
|
Driver: *gobot.NewDriver(
|
||||||
@ -50,9 +74,13 @@ func NewNeuroskyDriver(a *NeuroskyAdaptor, name string) *NeuroskyDriver {
|
|||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// adaptor returns neurosky adaptor
|
||||||
func (n *NeuroskyDriver) adaptor() *NeuroskyAdaptor {
|
func (n *NeuroskyDriver) adaptor() *NeuroskyAdaptor {
|
||||||
return n.Adaptor().(*NeuroskyAdaptor)
|
return n.Adaptor().(*NeuroskyAdaptor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start creates a go routine to listen from serial port
|
||||||
|
// and parse buffer readings
|
||||||
func (n *NeuroskyDriver) Start() bool {
|
func (n *NeuroskyDriver) Start() bool {
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
@ -67,8 +95,11 @@ func (n *NeuroskyDriver) Start() bool {
|
|||||||
}()
|
}()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Halt stops neurosky driver (void)
|
||||||
func (n *NeuroskyDriver) Halt() bool { return true }
|
func (n *NeuroskyDriver) Halt() bool { return true }
|
||||||
|
|
||||||
|
// parse converts bytes buffer into packets until no more data is present
|
||||||
func (n *NeuroskyDriver) parse(buf *bytes.Buffer) {
|
func (n *NeuroskyDriver) parse(buf *bytes.Buffer) {
|
||||||
for buf.Len() > 2 {
|
for buf.Len() > 2 {
|
||||||
b1, _ := buf.ReadByte()
|
b1, _ := buf.ReadByte()
|
||||||
@ -84,6 +115,7 @@ func (n *NeuroskyDriver) parse(buf *bytes.Buffer) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parsePacket publishes event according to data parsed
|
||||||
func (n *NeuroskyDriver) parsePacket(buf *bytes.Buffer) {
|
func (n *NeuroskyDriver) parsePacket(buf *bytes.Buffer) {
|
||||||
for buf.Len() > 0 {
|
for buf.Len() > 0 {
|
||||||
b, _ := buf.ReadByte()
|
b, _ := buf.ReadByte()
|
||||||
@ -117,6 +149,7 @@ func (n *NeuroskyDriver) parsePacket(buf *bytes.Buffer) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parseEEG returns data converted into EEG map
|
||||||
func (n *NeuroskyDriver) parseEEG(data []byte) EEG {
|
func (n *NeuroskyDriver) parseEEG(data []byte) EEG {
|
||||||
return EEG{
|
return EEG{
|
||||||
Delta: n.parse3ByteInteger(data[0:3]),
|
Delta: n.parse3ByteInteger(data[0:3]),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user