2014-04-28 08:17:05 +08:00
|
|
|
package neurosky
|
2014-04-26 18:11:51 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2014-07-10 09:32:27 +08:00
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
2014-04-26 18:11:51 +08:00
|
|
|
)
|
|
|
|
|
2016-12-01 19:02:47 +08:00
|
|
|
const (
|
|
|
|
// BTSync is the sync code
|
|
|
|
BTSync byte = 0xAA
|
2014-10-21 00:00:00 +08:00
|
|
|
|
2016-12-01 19:02:47 +08:00
|
|
|
// CodeEx Extended code
|
|
|
|
CodeEx byte = 0x55
|
2014-10-21 00:00:00 +08:00
|
|
|
|
2016-12-01 19:02:47 +08:00
|
|
|
// CodeSignalQuality POOR_SIGNAL quality 0-255
|
|
|
|
CodeSignalQuality byte = 0x02
|
2014-10-21 00:00:00 +08:00
|
|
|
|
2016-12-01 19:02:47 +08:00
|
|
|
// CodeAttention ATTENTION eSense 0-100
|
|
|
|
CodeAttention byte = 0x04
|
2014-10-21 00:00:00 +08:00
|
|
|
|
2016-12-01 19:02:47 +08:00
|
|
|
// CodeMeditation MEDITATION eSense 0-100
|
|
|
|
CodeMeditation byte = 0x05
|
2014-10-21 00:00:00 +08:00
|
|
|
|
2016-12-01 19:02:47 +08:00
|
|
|
// CodeBlink BLINK strength 0-255
|
|
|
|
CodeBlink byte = 0x16
|
2014-10-21 00:00:00 +08:00
|
|
|
|
2016-12-01 19:02:47 +08:00
|
|
|
// CodeWave RAW wave value: 2-byte big-endian 2s-complement
|
|
|
|
CodeWave byte = 0x80
|
2014-10-21 00:00:00 +08:00
|
|
|
|
2016-12-01 19:02:47 +08:00
|
|
|
// CodeAsicEEG ASIC EEG POWER 8 3-byte big-endian integers
|
|
|
|
CodeAsicEEG byte = 0x83
|
2014-04-26 18:11:51 +08:00
|
|
|
|
2016-12-01 19:02:47 +08:00
|
|
|
// Extended event
|
|
|
|
Extended = "extended"
|
|
|
|
|
|
|
|
// Signal event
|
|
|
|
Signal = "signal"
|
|
|
|
|
|
|
|
// Attention event
|
|
|
|
Attention = "attention"
|
|
|
|
|
|
|
|
// Meditation event
|
|
|
|
Meditation = "meditation"
|
|
|
|
|
|
|
|
// Blink event
|
|
|
|
Blink = "blink"
|
|
|
|
|
|
|
|
// Wave event
|
|
|
|
Wave = "wave"
|
|
|
|
|
|
|
|
// EEG event
|
|
|
|
EEG = "eeg"
|
|
|
|
|
|
|
|
// Error event
|
|
|
|
Error = "error"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Driver is the Gobot Driver for the Mindwave
|
2016-10-01 23:44:12 +08:00
|
|
|
type Driver struct {
|
2014-11-29 10:01:31 +08:00
|
|
|
name string
|
|
|
|
connection gobot.Connection
|
|
|
|
gobot.Eventer
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2016-12-01 19:02:47 +08:00
|
|
|
// EEGData is the EEG raw data returned from the Mindwave
|
|
|
|
type EEGData struct {
|
2014-04-26 18:11:51 +08:00
|
|
|
Delta int
|
|
|
|
Theta int
|
|
|
|
LoAlpha int
|
|
|
|
HiAlpha int
|
|
|
|
LoBeta int
|
|
|
|
HiBeta int
|
|
|
|
LoGamma int
|
|
|
|
MidGamma int
|
|
|
|
}
|
|
|
|
|
2016-10-01 23:44:12 +08:00
|
|
|
// NewDriver creates a Neurosky Driver
|
2014-10-21 00:00:00 +08:00
|
|
|
// 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
|
2016-10-01 23:44:12 +08:00
|
|
|
func NewDriver(a *Adaptor) *Driver {
|
|
|
|
n := &Driver{
|
2016-10-04 01:06:37 +08:00
|
|
|
name: "Neurosky",
|
2014-11-29 10:01:31 +08:00
|
|
|
connection: a,
|
|
|
|
Eventer: gobot.NewEventer(),
|
2014-04-28 08:17:05 +08:00
|
|
|
}
|
2014-07-08 08:27:10 +08:00
|
|
|
|
2016-12-01 19:02:47 +08:00
|
|
|
n.AddEvent(Extended)
|
|
|
|
n.AddEvent(Signal)
|
|
|
|
n.AddEvent(Attention)
|
|
|
|
n.AddEvent(Meditation)
|
|
|
|
n.AddEvent(Blink)
|
|
|
|
n.AddEvent(Wave)
|
|
|
|
n.AddEvent(EEG)
|
|
|
|
n.AddEvent(Error)
|
2014-07-08 08:27:10 +08:00
|
|
|
|
|
|
|
return n
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
2016-12-01 19:02:47 +08:00
|
|
|
|
|
|
|
// Connection returns the Driver's connection
|
2016-10-01 23:44:12 +08:00
|
|
|
func (n *Driver) Connection() gobot.Connection { return n.connection }
|
2016-12-01 19:02:47 +08:00
|
|
|
|
|
|
|
// Name returns the Driver name
|
|
|
|
func (n *Driver) Name() string { return n.name }
|
|
|
|
|
|
|
|
// SetName sets the Driver name
|
|
|
|
func (n *Driver) SetName(name string) { n.name = name }
|
2014-04-26 18:11:51 +08:00
|
|
|
|
2014-10-21 00:00:00 +08:00
|
|
|
// adaptor returns neurosky adaptor
|
2016-10-01 23:44:12 +08:00
|
|
|
func (n *Driver) adaptor() *Adaptor {
|
|
|
|
return n.Connection().(*Adaptor)
|
2014-06-16 08:22:50 +08:00
|
|
|
}
|
2014-10-21 00:00:00 +08:00
|
|
|
|
|
|
|
// Start creates a go routine to listen from serial port
|
|
|
|
// and parse buffer readings
|
2016-11-07 21:55:21 +08:00
|
|
|
func (n *Driver) Start() (err error) {
|
2014-04-26 18:11:51 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
2014-07-23 09:00:54 +08:00
|
|
|
buff := make([]byte, 1024)
|
2014-06-16 08:22:50 +08:00
|
|
|
_, err := n.adaptor().sp.Read(buff[:])
|
2014-04-26 18:11:51 +08:00
|
|
|
if err != nil {
|
2016-08-30 23:10:50 +08:00
|
|
|
n.Publish(n.Event("error"), err)
|
2014-04-26 18:11:51 +08:00
|
|
|
} else {
|
2014-04-28 08:17:05 +08:00
|
|
|
n.parse(bytes.NewBuffer(buff))
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2014-11-20 15:21:19 +08:00
|
|
|
return
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
2014-10-21 00:00:00 +08:00
|
|
|
|
|
|
|
// Halt stops neurosky driver (void)
|
2016-11-07 21:55:21 +08:00
|
|
|
func (n *Driver) Halt() (err error) { return }
|
2014-04-26 18:11:51 +08:00
|
|
|
|
2014-10-21 00:00:00 +08:00
|
|
|
// parse converts bytes buffer into packets until no more data is present
|
2016-10-01 23:44:12 +08:00
|
|
|
func (n *Driver) parse(buf *bytes.Buffer) {
|
2014-04-26 18:11:51 +08:00
|
|
|
for buf.Len() > 2 {
|
|
|
|
b1, _ := buf.ReadByte()
|
|
|
|
b2, _ := buf.ReadByte()
|
2014-06-11 06:16:11 +08:00
|
|
|
if b1 == BTSync && b2 == BTSync {
|
2014-04-26 18:11:51 +08:00
|
|
|
length, _ := buf.ReadByte()
|
2014-07-23 09:00:54 +08:00
|
|
|
payload := make([]byte, length)
|
2014-04-26 18:11:51 +08:00
|
|
|
buf.Read(payload)
|
|
|
|
//checksum, _ := buf.ReadByte()
|
|
|
|
buf.Next(1)
|
2014-07-23 09:00:54 +08:00
|
|
|
n.parsePacket(bytes.NewBuffer(payload))
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-21 00:00:00 +08:00
|
|
|
// parsePacket publishes event according to data parsed
|
2016-10-01 23:44:12 +08:00
|
|
|
func (n *Driver) parsePacket(buf *bytes.Buffer) {
|
2014-04-26 18:11:51 +08:00
|
|
|
for buf.Len() > 0 {
|
|
|
|
b, _ := buf.ReadByte()
|
|
|
|
switch b {
|
2014-06-11 06:16:11 +08:00
|
|
|
case CodeEx:
|
2016-08-30 23:10:50 +08:00
|
|
|
n.Publish(n.Event("extended"), nil)
|
2014-06-11 06:16:11 +08:00
|
|
|
case CodeSignalQuality:
|
2014-04-26 18:11:51 +08:00
|
|
|
ret, _ := buf.ReadByte()
|
2016-08-30 23:10:50 +08:00
|
|
|
n.Publish(n.Event("signal"), ret)
|
2014-06-11 06:16:11 +08:00
|
|
|
case CodeAttention:
|
2014-04-26 18:11:51 +08:00
|
|
|
ret, _ := buf.ReadByte()
|
2016-08-30 23:10:50 +08:00
|
|
|
n.Publish(n.Event("attention"), ret)
|
2014-06-11 06:16:11 +08:00
|
|
|
case CodeMeditation:
|
2014-04-26 18:11:51 +08:00
|
|
|
ret, _ := buf.ReadByte()
|
2016-08-30 23:10:50 +08:00
|
|
|
n.Publish(n.Event("meditation"), ret)
|
2014-06-11 06:16:11 +08:00
|
|
|
case CodeBlink:
|
2014-04-26 18:11:51 +08:00
|
|
|
ret, _ := buf.ReadByte()
|
2016-08-30 23:10:50 +08:00
|
|
|
n.Publish(n.Event("blink"), ret)
|
2014-06-11 06:16:11 +08:00
|
|
|
case CodeWave:
|
2014-04-26 18:11:51 +08:00
|
|
|
buf.Next(1)
|
|
|
|
var ret = make([]byte, 2)
|
|
|
|
buf.Read(ret)
|
2016-08-30 23:10:50 +08:00
|
|
|
n.Publish(n.Event("wave"), int16(ret[0])<<8|int16(ret[1]))
|
2014-06-11 06:16:11 +08:00
|
|
|
case CodeAsicEEG:
|
2014-07-23 09:00:54 +08:00
|
|
|
ret := make([]byte, 25)
|
2014-06-11 06:16:11 +08:00
|
|
|
i, _ := buf.Read(ret)
|
|
|
|
if i == 25 {
|
2016-08-30 23:10:50 +08:00
|
|
|
n.Publish(n.Event("eeg"), n.parseEEG(ret))
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-21 00:00:00 +08:00
|
|
|
// parseEEG returns data converted into EEG map
|
2016-12-01 19:02:47 +08:00
|
|
|
func (n *Driver) parseEEG(data []byte) EEGData {
|
|
|
|
return EEGData{
|
2014-06-11 06:16:11 +08:00
|
|
|
Delta: n.parse3ByteInteger(data[0:3]),
|
|
|
|
Theta: n.parse3ByteInteger(data[3:6]),
|
|
|
|
LoAlpha: n.parse3ByteInteger(data[6:9]),
|
|
|
|
HiAlpha: n.parse3ByteInteger(data[9:12]),
|
|
|
|
LoBeta: n.parse3ByteInteger(data[12:15]),
|
|
|
|
HiBeta: n.parse3ByteInteger(data[15:18]),
|
|
|
|
LoGamma: n.parse3ByteInteger(data[18:21]),
|
|
|
|
MidGamma: n.parse3ByteInteger(data[21:25]),
|
2014-04-28 08:17:05 +08:00
|
|
|
}
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2016-10-01 23:44:12 +08:00
|
|
|
func (n *Driver) parse3ByteInteger(data []byte) int {
|
2014-07-23 09:00:54 +08:00
|
|
|
return ((int(data[0]) << 16) |
|
|
|
|
(((1 << 16) - 1) & (int(data[1]) << 8)) |
|
|
|
|
(((1 << 8) - 1) & int(data[2])))
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|