forked from hybridgroup/gobot
/
neurosky_driver.go
209 lines (176 loc) · 4.56 KB
/
neurosky_driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package neurosky
import (
"bytes"
"gobot.io/x/gobot"
)
const (
// BTSync is the sync code
BTSync byte = 0xAA
// CodeEx Extended code
CodeEx byte = 0x55
// CodeSignalQuality POOR_SIGNAL quality 0-255
CodeSignalQuality byte = 0x02
// CodeAttention ATTENTION eSense 0-100
CodeAttention byte = 0x04
// CodeMeditation MEDITATION eSense 0-100
CodeMeditation byte = 0x05
// CodeBlink BLINK strength 0-255
CodeBlink byte = 0x16
// CodeWave RAW wave value: 2-byte big-endian 2s-complement
CodeWave byte = 0x80
// CodeAsicEEG ASIC EEG POWER 8 3-byte big-endian integers
CodeAsicEEG byte = 0x83
// 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
type Driver struct {
name string
connection gobot.Connection
gobot.Eventer
}
// EEGData is the EEG raw data returned from the Mindwave
type EEGData struct {
Delta int
Theta int
LoAlpha int
HiAlpha int
LoBeta int
HiBeta int
LoGamma int
MidGamma int
}
// NewDriver creates a Neurosky Driver
// 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 NewDriver(a *Adaptor) *Driver {
n := &Driver{
name: "Neurosky",
connection: a,
Eventer: gobot.NewEventer(),
}
n.AddEvent(Extended)
n.AddEvent(Signal)
n.AddEvent(Attention)
n.AddEvent(Meditation)
n.AddEvent(Blink)
n.AddEvent(Wave)
n.AddEvent(EEG)
n.AddEvent(Error)
return n
}
// Connection returns the Driver's connection
func (n *Driver) Connection() gobot.Connection { return n.connection }
// 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 }
// adaptor returns neurosky adaptor
func (n *Driver) adaptor() *Adaptor {
return n.Connection().(*Adaptor)
}
// Start creates a go routine to listen from serial port
// and parse buffer readings
func (n *Driver) Start() (err error) {
go func() {
for {
buff := make([]byte, 1024)
_, err := n.adaptor().sp.Read(buff[:])
if err != nil {
n.Publish(n.Event("error"), err)
} else {
n.parse(bytes.NewBuffer(buff))
}
}
}()
return
}
// Halt stops neurosky driver (void)
func (n *Driver) Halt() (err error) { return }
// parse converts bytes buffer into packets until no more data is present
func (n *Driver) parse(buf *bytes.Buffer) {
for buf.Len() > 2 {
b1, _ := buf.ReadByte()
b2, _ := buf.ReadByte()
if b1 == BTSync && b2 == BTSync {
length, _ := buf.ReadByte()
payload := make([]byte, length)
buf.Read(payload)
//checksum, _ := buf.ReadByte()
buf.Next(1)
n.parsePacket(bytes.NewBuffer(payload))
}
}
}
// parsePacket publishes event according to data parsed
func (n *Driver) parsePacket(buf *bytes.Buffer) {
for buf.Len() > 0 {
b, _ := buf.ReadByte()
switch b {
case CodeEx:
n.Publish(n.Event("extended"), nil)
case CodeSignalQuality:
ret, _ := buf.ReadByte()
n.Publish(n.Event("signal"), ret)
case CodeAttention:
ret, _ := buf.ReadByte()
n.Publish(n.Event("attention"), ret)
case CodeMeditation:
ret, _ := buf.ReadByte()
n.Publish(n.Event("meditation"), ret)
case CodeBlink:
ret, _ := buf.ReadByte()
n.Publish(n.Event("blink"), ret)
case CodeWave:
buf.Next(1)
var ret = make([]byte, 2)
buf.Read(ret)
n.Publish(n.Event("wave"), int16(ret[0])<<8|int16(ret[1]))
case CodeAsicEEG:
ret := make([]byte, 25)
i, _ := buf.Read(ret)
if i == 25 {
n.Publish(n.Event("eeg"), n.parseEEG(ret))
}
}
}
}
// parseEEG returns data converted into EEG map
func (n *Driver) parseEEG(data []byte) EEGData {
return EEGData{
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]),
}
}
func (n *Driver) parse3ByteInteger(data []byte) int {
return ((int(data[0]) << 16) |
(((1 << 16) - 1) & (int(data[1]) << 8)) |
(((1 << 8) - 1) & int(data[2])))
}