├── example └── app.js ├── package.json ├── test └── test.js ├── LICENSE ├── README.md └── lib └── index.js /example/app.js: -------------------------------------------------------------------------------- 1 | var neurosky = require('../lib') 2 | 3 | var client = neurosky.createClient({ 4 | appName:'NodeNeuroSky', 5 | appKey:'0fc4141b4b45c675cc8d3a765b8d71c5bde9390' 6 | }) 7 | 8 | client.on('data',function(data){ 9 | console.log(data) 10 | }); 11 | 12 | client.connect() 13 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-neurosky", 3 | "description": "A Node.js client library for the MindWave device from NeuroSky", 4 | "author": "Daniel Luxemburg ", 5 | "version": "0.0.1", 6 | "main": "node-neurosky.js", 7 | "license" : { 8 | "type": "MIT", 9 | "url": "https://raw.github.com/dluxemburg/node-neurosky/master/LICENSE" 10 | }, 11 | "devDependencies": { 12 | "mocha": "*" 13 | }, 14 | "repository": "git://github.com/dluxemburg/node-neurosky.git" 15 | } 16 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var neurosky = require('../lib') 2 | var assert = require("assert") 3 | 4 | describe('neurosky', function(){ 5 | describe('#createClient()', function(){ 6 | 7 | it('should raise an exception when appName is not supplied', function(){ 8 | var probe 9 | try{ 10 | neurosky.createClient() 11 | }catch(e){ 12 | probe = e 13 | } 14 | assert.equal(probe.message, "Must specify appName") 15 | }) 16 | 17 | it('should raise an exception when appKey is not supplied', function(){ 18 | var probe 19 | try{ 20 | neurosky.createClient({ 21 | appName:'MyGreatApp' 22 | }) 23 | }catch(e){ 24 | probe = e 25 | } 26 | assert.equal(probe.message, "Must specify appKey") 27 | }) 28 | 29 | it('should creat a ThinkGearClient client instance', function(){ 30 | assert.equal(neurosky.createClient({ 31 | appName:'MyGreatApp', 32 | appKey:'123456789' 33 | }).constructor, neurosky.NeuroSkyClient) 34 | }) 35 | }) 36 | }) 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2012 Daniel Luxemburg 2 | All rights reserved. 3 | 4 | Permission is hereby granted, free of charge, to any person 5 | obtaining a copy of this software and associated documentation 6 | files (the "Software"), to deal in the Software without 7 | restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the 10 | Software is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 18 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 20 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 21 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 | OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #node-neurosky 2 | 3 | Client library for the [ThinkGear Socket Protocol](http://developer.neurosky.com/docs/lib/exe/fetch.php?media=app_notes:thinkgear_socket_protocol.pdf) from [NeuroSky](http://neurosky.com/). 4 | 5 | ###You'll need one of [these](http://store.neurosky.com/products/mindwave-1): 6 | 7 | ![Fashion!](http://home.neurosky.com/wp-content/uploads/2014/01/EEG_Hardware_Section3-1.jpg) 8 | 9 | ###Usage 10 | 11 | Install with NPM: 12 | 13 | ``` 14 | $ npm install node-neurosky 15 | ``` 16 | 17 | 18 | Include the module: 19 | 20 | ```javascript 21 | var neurosky = require('node-neurosky'); 22 | ``` 23 | 24 | Create a client instance: 25 | 26 | ```javascript 27 | var client = neurosky.createClient({ 28 | appName: 'My Great Application', 29 | appKey: '1234567890abcdef...' 30 | }); 31 | ``` 32 | 33 | Add a listener for incoming data: 34 | 35 | ```javascript 36 | client.on('data',function(data){ 37 | 38 | // magical and wonderful things 39 | 40 | }); 41 | ``` 42 | 43 | Connect to the headset: 44 | 45 | ```javascript 46 | client.connect(); 47 | ``` 48 | 49 | All of this is in the `example/app.js` file too. 50 | 51 | ###Data 52 | 53 | The output objects look like this: 54 | 55 | ```javascript 56 | { 57 | eSense: { 58 | attention: 53, 59 | meditation: 47 60 | }, 61 | eegPower: { 62 | delta: 416474, 63 | theta: 33592, 64 | lowAlpha: 3877, 65 | highAlpha: 3142, 66 | lowBeta: 1569, 67 | highBeta: 3125, 68 | lowGamma: 3521, 69 | highGamma: 1451 70 | }, 71 | poorSignalLevel: 0 72 | } 73 | ``` 74 | 75 | With the occasional `{ blinkStrength: 55 }` when you blink. 76 | 77 | ###TO DO 78 | 79 | - Some tests 80 | - A more thorough example 81 | - Deal with raw output 82 | - Make different types of device signal emit different events 83 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | var net = require('net'), 2 | events = require('events'), 3 | util = require('util') 4 | 5 | function NodeNeuroSkyError(message){ 6 | Error.call(this) 7 | Error.captureStackTrace(this, arguments.callee) 8 | this.message = message 9 | this.name = 'NodeThinkGearError' 10 | } 11 | 12 | NodeNeuroSkyError.prototype.__proto__ = Error.prototype 13 | 14 | var NeuroSkyClient = function(opts){ 15 | opts || (opts = {}) 16 | 17 | this.port = opts.port || 13854 18 | this.host = opts.host || 'localhost' 19 | 20 | if(typeof(opts.appName) !== 'string') throw new NodeNeuroSkyError('Must specify appName') 21 | if(typeof(opts.appKey) !== 'string') throw new NodeNeuroSkyError('Must specify appKey') 22 | 23 | this.auth = { 24 | appName:opts.appName, 25 | appKey:opts.appKey 26 | } 27 | 28 | this.config = { 29 | enableRawOutput: false, 30 | format: "Json" 31 | } 32 | 33 | events.EventEmitter.call(this) 34 | } 35 | 36 | util.inherits(NeuroSkyClient, events.EventEmitter) 37 | 38 | NeuroSkyClient.prototype.connect = function(){ 39 | var self = this 40 | 41 | var client = this.client = net.connect(this.port,this.host,function(){ 42 | client.write(JSON.stringify(self.auth)) 43 | }) 44 | 45 | client.on('data',function(data){ 46 | if(!self.configSent){ 47 | self.configSent = true 48 | client.write(JSON.stringify(self.config)) 49 | } else { 50 | try{ 51 | self.emit('data',JSON.parse(data.toString())) 52 | }catch(e){ 53 | self.emit('parse_error',data.toString()) 54 | } 55 | } 56 | }) 57 | 58 | client.on('error', function(err) { 59 | console.log('Error connecting to ThinkGear client. Try starting the ThinkGear Connector app.\n', err) 60 | process.exit(1) 61 | }) 62 | } 63 | 64 | exports.NeuroSkyClient = NeuroSkyClient 65 | 66 | exports.createClient = function(opts){ 67 | return new NeuroSkyClient(opts || {}) 68 | } --------------------------------------------------------------------------------