Skip to content

Commit

Permalink
Merge pull request #20 from aj-ptw/fix-19
Browse files Browse the repository at this point in the history
Add ability to suppress noble startup errors - #19
  • Loading branch information
AJ Keller authored Jan 31, 2017
2 parents a38f083 + 74c5852 commit 1218e42
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# 0.4.2

### New Features
* Add callback function to constructor to catch noble errors.

### Bug Fixes
* Fix #19

# 0.4.1

### New Features
Expand Down
14 changes: 12 additions & 2 deletions examples/ganglionServer/ganglionServer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
const Ganglion = require('../../index').Ganglion;
const k = require('../../openBCIConstants');
const verbose = true;
var ganglion = new Ganglion({
// debug: true,
let ganglion = new Ganglion({
// debug: true; ,
nobleAutoStart: false,
sendCounts: true,
verbose: verbose
}, (error) => {
if (error) {
console.log(error);
} else {
if (verbose) {
console.log('Ganglion initialize completed');
}
}
});

function errorFunc (err) {
Expand Down Expand Up @@ -101,6 +110,7 @@ function exitHandler (options, err) {
ganglion.removeAllListeners('message');
ganglion.removeAllListeners('impedance');
ganglion.removeAllListeners('close');
ganglion.removeAllListeners('error');
ganglion.removeAllListeners('ganglionFound');
ganglion.removeAllListeners('ready');
ganglion.destroyNoble();
Expand Down
31 changes: 23 additions & 8 deletions openBCIGanglion.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const EventEmitter = require('events').EventEmitter;
const _ = require('lodash');
const noble = require('noble');
let noble;
const util = require('util');
// Local imports
const ganglionSample = require('./openBCIGanglionSample');
Expand All @@ -26,7 +26,7 @@ const _options = {

/**
* @description The initialization method to call first, before any other method.
* @param options (optional) - Board optional configurations.
* @param options {object} (optional) - Board optional configurations.
* - `debug` {Boolean} - Print out a raw dump of bytes sent and received. (Default `false`)
*
* - `nobleAutoStart` {Boolean} - Automatically initialize `noble`. Subscribes to blue tooth state changes and such.
Expand Down Expand Up @@ -59,13 +59,19 @@ const _options = {
* setting and this sample rate will be used. (Default is `250`)
*
* - `verbose` {Boolean} - Print out useful debugging events. (Default `false`)
*
* @param callback {function} (optional) - A callback function used to determine if the noble module was able to be started.
* This can be very useful on Windows when there is no compatible BLE device found.
* @constructor
* @author AJ Keller (@pushtheworldllc)
*/
function Ganglion (options) {
function Ganglion (options, callback) {
if (!(this instanceof Ganglion)) {
return new Ganglion(options);
return new Ganglion(options, callback);
}

if (options instanceof Function) {
callback = options;
options = {};
}

options = (typeof options !== 'function') && options || {};
Expand Down Expand Up @@ -128,10 +134,17 @@ function Ganglion (options) {
this.manualDisconnect = false;

/** Initializations */
if (this.options.nobleAutoStart) this._nobleInit(); // It get's the noble going
for (var i = 0; i < 3; i++) {
this._decompressedSamples[i] = [0, 0, 0, 0];
}

try {
noble = require('noble');
if (this.options.nobleAutoStart) this._nobleInit(); // It get's the noble going
if (callback) callback();
} catch (e) {
if (callback) callback(e);
}
}

// This allows us to use the emitter class freely outside of the module
Expand Down Expand Up @@ -594,8 +607,10 @@ Ganglion.prototype._disconnected = function () {
* @private
*/
Ganglion.prototype._nobleDestroy = function () {
noble.removeAllListeners(k.OBCINobleEmitterStateChange);
noble.removeAllListeners(k.OBCINobleEmitterDiscover);
if (noble) {
noble.removeAllListeners(k.OBCINobleEmitterStateChange);
noble.removeAllListeners(k.OBCINobleEmitterDiscover);
}
};

Ganglion.prototype._nobleConnect = function (peripheral) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "openbci-ganglion",
"version": "0.4.1",
"version": "0.4.2",
"description": "The official Node.js SDK for the OpenBCI Ganglion Biosensor Board.",
"main": "index.js",
"scripts": {
Expand Down
15 changes: 15 additions & 0 deletions test/openBCIGanglion-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ const clone = require('clone');
chai.use(chaiAsPromised);
chai.use(sinonChai);

describe('#ganglion-constructor', function () {
it('should callback if only callback used', function (done) {
const cb = (err) => {
done(err);
};
const ganglion_cb = new Ganglion(cb);
});
it('should callback if options and callback', function (done) {
const cb = (err) => {
done(err);
};
const ganglion_cb = new Ganglion({}, cb);
});
});

describe('#ganglion', function () {
const mockProperties = {
nobleAutoStart: false,
Expand Down

0 comments on commit 1218e42

Please sign in to comment.