Adding godocs documentation to neurosky package

This commit is contained in:
Javier Cervantes 2014-10-20 11:00:00 -05:00
parent bf7bc97c3a
commit c8652c483f
4 changed files with 115 additions and 8 deletions

View File

@ -41,7 +41,7 @@ You should be able to pair your Mindwave using your normal system tray applet fo
## Examples
```go
package main
import (
"fmt"

71
platforms/neurosky/doc.go Normal file
View 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

View File

@ -13,6 +13,7 @@ type NeuroskyAdaptor struct {
connect func(*NeuroskyAdaptor)
}
// NewNeuroskyAdaptor creates a neurosky adaptor with specified name
func NewNeuroskyAdaptor(name string, port string) *NeuroskyAdaptor {
return &NeuroskyAdaptor{
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 {
n.connect(n)
n.SetConnected(true)
return true
}
// Finalize returns true if device finalization is successful
func (n *NeuroskyAdaptor) Finalize() bool {
n.sp.Close()
n.SetConnected(false)

View File

@ -7,13 +7,27 @@ import (
)
const BTSync byte = 0xAA
const CodeEx byte = 0x55 // Extended code
const CodeSignalQuality byte = 0x02 // POOR_SIGNAL quality 0-255
const CodeAttention byte = 0x04 // ATTENTION eSense 0-100
const CodeMeditation byte = 0x05 // MEDITATION eSense 0-100
const CodeBlink byte = 0x16 // BLINK strength 0-255
const CodeWave byte = 0x80 // RAW wave value: 2-byte big-endian 2s-complement
const CodeAsicEEG byte = 0x83 // ASIC EEG POWER 8 3-byte big-endian integers
// Extended code
const CodeEx byte = 0x55
// POOR_SIGNAL quality 0-255
const CodeSignalQuality byte = 0x02
// 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 {
gobot.Driver
@ -30,6 +44,16 @@ type EEG struct {
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 {
n := &NeuroskyDriver{
Driver: *gobot.NewDriver(
@ -50,9 +74,13 @@ func NewNeuroskyDriver(a *NeuroskyAdaptor, name string) *NeuroskyDriver {
return n
}
// adaptor returns neurosky adaptor
func (n *NeuroskyDriver) 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 {
go func() {
for {
@ -67,8 +95,11 @@ func (n *NeuroskyDriver) Start() bool {
}()
return true
}
// Halt stops neurosky driver (void)
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) {
for buf.Len() > 2 {
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) {
for buf.Len() > 0 {
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 {
return EEG{
Delta: n.parse3ByteInteger(data[0:3]),