Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed linter and small refactor #1

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
30d47a3
add parsing code for battery level
jamuus Oct 23, 2015
662b753
actually call callback with the parsed data
jamuus Oct 23, 2015
9326d5a
fix battery voltage level reading
jamuus Oct 23, 2015
928032e
remove logging
jamuus Oct 23, 2015
1590601
updated serial port
Nov 10, 2015
dff3042
Use versions of serialport abover 2.0.0 if available
Flinnja Nov 11, 2015
bc1611d
Merge branch 'Flinnja-master'
deadprogram Nov 25, 2015
d7f59e6
Corrections for Travis builds to include latest versions of Node.js u…
deadprogram Nov 25, 2015
e239da3
Adds missing install info for Grunt to README
deadprogram Nov 25, 2015
78a265a
Add Node.js 4.2.x to Travis build
deadprogram Nov 26, 2015
46a1a7e
Add compatibility section to README
deadprogram Nov 26, 2015
66f27fa
Update version to 0.4.0
deadprogram Nov 26, 2015
39d4519
Update with Node.js 5.x+ build requirements.
deadprogram Nov 26, 2015
09c139d
Simplify Travis build
deadprogram Nov 27, 2015
2cd953f
Correct command ID for readLocator and properly handle 0x00 did sync …
deadprogram Dec 1, 2015
92b8ea6
sphero port as env variable
dportalesr Nov 30, 2015
c9d42b3
Merge pull request #34 from orbotix/feature/port-as-env-var
deadprogram Dec 2, 2015
605f095
Remove extra line from example
deadprogram Dec 2, 2015
7b0cb96
Update package.json to 0.4.1
deadprogram Dec 2, 2015
c4449c8
Add version checking example
deadprogram Dec 2, 2015
472107b
Add Node.js 5.1 to Travis builds
deadprogram Dec 3, 2015
813a4ad
Adds detailed field output to examples
deadprogram Dec 10, 2015
07f3b08
Some info about setting up udev rule under Ubuntu
deadprogram Dec 10, 2015
8ee8c9c
add signed type to parser and use for locator
bmidgley Dec 10, 2015
26f373e
Merge pull request #36 from bmidgley/signed-parser-type
deadprogram Dec 16, 2015
8ad347b
Starting BLE integration using noble
deadprogram Dec 18, 2015
f0250e5
Can send commands to BLE connected Sphero robotic devices
deadprogram Dec 21, 2015
195f078
Cleanup as demanded by ESlint
deadprogram Dec 21, 2015
45f088f
Allow BLE addresses with colons as returned by all the CLI tools
deadprogram Dec 21, 2015
c12b398
Reformat to remove all errors and warnings from ESLint
deadprogram Dec 21, 2015
61c7cf5
Can send and receive command sync data
deadprogram Dec 22, 2015
0e86e92
Cleanup syntax for ESLint
deadprogram Dec 22, 2015
4183546
Run update before anything else to help accomodate ongoing Travis mig…
deadprogram Dec 22, 2015
cd2964a
Cannot run update (or anything else Travis related) using sudo
deadprogram Dec 22, 2015
d0d0aa6
More incantations to the CI system
deadprogram Dec 22, 2015
834ac1a
Allow existing noble peripheral to be passed into adaptor
deadprogram Dec 22, 2015
cea04d7
Remove pre-installed dependencies, so you can use with either BLE or …
deadprogram Dec 23, 2015
6e9a91a
Adds connection scanning info for BLE devices
deadprogram Dec 23, 2015
303876a
Cleanup logging and begin implmenting better test coverage for BLE in…
deadprogram Dec 23, 2015
0e53231
Remove cruft, thanks ESLint
deadprogram Dec 23, 2015
edd0b1c
Nicer formatting for missing dependency messages
deadprogram Dec 23, 2015
1c1af5c
Moves code example up to the top of README
deadprogram Dec 23, 2015
2d9a8a1
Defaults to use SOP2.both by default
deadprogram Dec 24, 2015
b3ff7d4
Adds emitPacketErrors option (defaults to false) to not require devs …
deadprogram Dec 24, 2015
aff227a
Adds documentation to README on the emitPacketErrors option
deadprogram Dec 24, 2015
a792af6
Leading off with simple example
deadprogram Dec 24, 2015
07c40df
Update package version to 0.5.0
deadprogram Dec 24, 2015
8430db7
Updates to README as suggested by @rhencke and others on issue #26
deadprogram Dec 27, 2015
b354efc
Initial simplistic implementation of freefall event
deadprogram Dec 29, 2015
a39f506
Naive implementation of freefall/landing events
deadprogram Dec 29, 2015
dfc9b62
Removes ESLint errors
deadprogram Dec 29, 2015
04e8ef6
Update JSDocs for detectFreefall function
deadprogram Dec 29, 2015
6df6bf8
Merge pull request #39 from orbotix/feature/freefall
dportalesr Dec 29, 2015
6219890
Merge branch 'master' of https://github.com/jamuus/sphero.js into fix…
Jan 2, 2016
7071d97
Passes linter, fixed whitespace
Jan 2, 2016
5a798aa
Refactored if else statements into previous switch
Jan 3, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
language: node_js
sudo: false
node_js:
- '0.10'
- '0.12'
- 'iojs'
- '4.0'
- '4.1'
- '4.2'
- '5.0'
- '5.1'
env:
- CXX=g++-4.8
before_install:
- sudo apt-get update
- "mkdir -p ~/.npm"
before_script:
install:
- npm install -g node-gyp-install
- node-gyp-install
- npm install serialport
- npm install noble
- npm install
- npm install -g grunt-cli istanbul codeclimate-test-reporter
script:
- grunt lint
- istanbul cover ./node_modules/.bin/_mocha spec/helper.js spec/lib/*.js spec/lib/**/*.js --report lcovonly -- -R spec
- CODECLIMATE_REPO_TOKEN=88ec807c5d22e853e1a21b9289233f525e793f09e5e81322899ee519587d3689 codeclimate < coverage/lcov.info
- CODECLIMATE_REPO_TOKEN=88ec807c5d22e853e1a21b9289233f525e793f09e5e81322899ee519587d3689 codeclimate-test-reporter < coverage/lcov.info
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8
- g++-4.8-multilib
- libbluetooth-dev
201 changes: 155 additions & 46 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -1,44 +1,174 @@
# sphero.js
# Sphero.js

The official Orbotix JavaScript SDK module to programatically control Sphero robots.
The official Orbotix JavaScript SDK module to programmatically control Sphero robots.

[![Build Status](https://travis-ci.org/orbotix/sphero.js.svg?branch=master)](https://travis-ci.org/orbotix/sphero.js) [![Code Climate](https://codeclimate.com/github/orbotix/sphero.js/badges/gpa.svg)](https://codeclimate.com/github/orbotix/sphero.js) [![Test Coverage](https://codeclimate.com/github/orbotix/sphero.js/badges/coverage.svg)](https://codeclimate.com/github/orbotix/sphero.js/coverage)

## Installation
## Usage

To initialize and connect to a BB-8 or an Ollie:

```javascript
var sphero = require("sphero"),
bb8 = sphero("F3:F2:6D:55:71:09"); // change BLE address accordingly

bb8.connect(function() {
// roll BB-8 in a random direction, changing direction every second
setInterval(function() {
var direction = Math.floor(Math.random() * 360);
bb8.roll(150, direction);
}, 1000);
});
```

To initialize and connect to a Sphero 1.0/2.0 or SPRK, just change the **port** to match your connection:

```javascript
var sphero = require("sphero"),
orb = sphero("/dev/rfcomm0"); // change port accordingly

orb.connect(function() {
// Sphero's connected!
// do some cool stuff here!
});
```

Once connected, you can give your Sphero commands and receive notifications from the built-in sensors:

```javascript
orb.connect(function() {
// roll Sphero forward
orb.roll(150, 0);

// turn Sphero green
orb.color("green");

// have Sphero tell you when it detect collisions
orb.detectCollisions();

// when Sphero detects a collision, turn red for a second, then back to green
orb.on("collision", function(data) {
console.log("collision detected");
console.log(" data:", data);

orb.color("red");

setTimeout(function() {
orb.color("green");
}, 1000);
});
});
```

For more examples, check out the `examples` dir, or the JavaScript SDK documentation on the Sphero developer portal. When running these examples, don't forget to pass the port as an ENV variable like this:
```
PORT=/your/port node example.js
```

## Installation for BB-8 & Ollie

The BB-8 and Ollie use a Bluetooth Low Energy (LE) interface, also known as "Bluetooth Smart" or "Bluetooth 4.0/4.1". You must have a hardware adapter that supports the Bluetooth 4.x+ standard to connect your computer to your BB-8 or Ollie.

Run the following command:

$ npm install sphero noble

To use Sphero.js with your BB-8 or Ollie, you must also install the Node.js noble module (https://github.com/sandeepmistry/noble) from [@sandeepmistry](https://github.com/sandeepmistry).

## Installation for Sphero 1.0/2.0 & SPRK

The Sphero 1.0/2.0 and the SPRK use a Bluetooth Classic interface, also known as "Bluetooth 2.0/3.0".

Run the following command:

$ npm install sphero
$ npm install sphero serialport

## Connecting to Sphero
To use Sphero.js with your Sphero or SPRK, you must also install the Node.js serialport module (https://github.com/voodootikigod/node-serialport) from [@voodootikigod](https://github.com/voodootikigod).

## Connecting to BB-8/Ollie

### OS X

To connect to your Sphero, you first need to pair it. To pair your device on OS X, open the Bluetooth settings here: System Preferences > Bluetooth. From this menu, locate your Sphero in the Devices list and click the **Pair** button to pair it with your computer.
To connect to your BB-8 or Ollie, you first need to determine UUID. Once you have Noble installed, you can use the `advertisement-discovery.js` program to determine the device UUID:

```
$ node ./node_modules/noble/examples/advertisement-discovery.js
peripheral discovered (944f561f8cf441f3b5405ed48f5c63cf with address <unknown, unknown>, connectable true, RSSI -73:
hello my local name is:
BB-131D
can I interest you in any of the following advertised services:
[]
here is my manufacturer data:
"3330"
my TX power level is:
-18
```

In the above output, the device UUID is `944f561f8cf441f3b5405ed48f5c63cf`. Use this value to connect to your BB-8 or Ollie.

### Linux - Ubuntu

Once you've successfully paired your Sphero, open your terminal and navigate to your system's `/dev/` directory and locate the serial device connection for your newly paired Sphero; it should look something like this:
To connect to your BB-8 or Ollie, you first need to determine the MAC address. Once you have Noble installed, you can use the `advertisement-discovery.js` program to determine the device address:

```
tty.Sphero-RGB-AMP-SPP
$ node ./node_modules/noble/examples/advertisement-discovery.js
peripheral discovered (f3f26d557108 with address <f3:f2:6d:55:71:08, random>, connectable true, RSSI -37:
hello my local name is:
BB-7108
can I interest you in any of the following advertised services:
["22bb746f2ba075542d6f726568705327"]
here is my manufacturer data:
"3330"
my TX power level is:
6
```

Note, your device will likely be different depending on its preset color code (the three colors your Sphero cycles through when you first turn it on). The example above is for a Sphero with a Red, Green, and Blue (RGB) color code.
In the above output, the device address is `f3f26d557108` (or alternately `f3:f2:6d:55:71:08`). Use this value to connect to your BB-8 or Ollie.

### Windows

To connect to your BB-8 or Ollie, you will need to be running Windows 8.1+. Instructions coming soon...

## Connecting to Sphero/SPRK

### OS X

To connect to your Sphero 1.0/2.0 or SPRK, you first need to pair it. To pair your device on OS X, open the Bluetooth settings in **System Preferences** > **Bluetooth**. From this menu, locate your Sphero in the Devices list and click the **Pair** button to pair it with your computer.

Once you've successfully paired your Sphero, open your terminal, go to your `/dev` folder and locate the serial device connection (or use `ls -a /dev | grep tty.Sphero`) for your newly paired Sphero; it should look something like `tty.Sphero-RGB-AMP-SPP`. Note, your device will likely be different depending on its preset color code (the three colors your Sphero cycles through when you first turn it on). The previous example is for a Sphero with a Red, Green and Blue (RGB) color code.

So, your Sphero port will be at

```
/dev/tty.Sphero-XXX-XXX-XXX
```

***

### Linux - Ubuntu

To make things easy install `Blueman Bluetooth Manager`. In Ubuntu this is pretty easy, just open the **Ubuntu Software Center**, type the program name and install it from there. When the installation is complete, open the program and search for devices. Make sure the Sphero is flashing its color code. Once the Sphero appears in the list of devices, select it, click on setup and follow the instructions.
To connect to your Sphero 1.0/2.0 or SPRK, you first need to pair it. To make things easy install `Blueman Bluetooth Manager`. In Ubuntu this is pretty easy, just open the **Ubuntu Software Center**, type the program name and install it from there. When the installation is complete, open the program and search for devices. Make sure the Sphero is flashing its color code. Once the Sphero appears in the list of devices, select it, click on setup and follow the instructions.

You can connect and disconnect the Sphero from a serialport interface by right clicking on it inside the Blueman list of devices and selecting `RN-SPP` or `disconnect`, respectively (after it has been setup and added permanently to the list). Make sure to notice the serialport address Blueman displays after the Sphero connects, as this will be the one used in your code. The serialport address displayed for the Sphero should look something like this:

```
/dev/rfcomm0
```

You might need to add a udev rule in order to properly set permissions for your program to be able to access the Bluetooth interface. For example:

```
$ cat /etc/udev/rules.d/55-rfcomm.rules
KERNEL=="rfcomm[0-9]*", NAME="%k", GROUP="dialout"
```

The udev rule above will allow any user who is a member of the `dialout` group to access any port that is added by connecting by running the `rfcomm` command. Note that running the `rfcomm` command itself to create the port, may require running under `sudo`.

***

### Windows

Locate the Bluetooth icon in the taskbar (or inside the system task tray button) and follow the necessary steps to pair with your Sphero.
To connect to your Sphero 1.0/2.0 or SPRK, you first need to pair it. Locate the Bluetooth icon in the taskbar (or inside the system task tray button) and follow the necessary steps to pair with your Sphero.

Once you've successfully paired your Sphero, there are two options available to you to check and see which serialport corresponds to the Sphero you just connected. The first option is to right click on the bluetooth icon in the task bar (same you use to pair), click on `Open Settings`, when the settings window appears, navigate to the `COM Ports` tab where you should see a list of ports which should list your Sphero. If your Sphero is listed in more than one port, take note of the one that has `RN-SPP` in the name, and use that one to connect. The list should look something similar to:

Expand All @@ -55,59 +185,38 @@ The second option is to identify the port number. Click the `start` button and t
```
COM2, COM3, COM4
```
## Error handling

## Usage

To initialize and connect to a Sphero:
Under most circumstances, Sphero.js will attempt to gracefully recover from any bad or incomplete packets that might be received back from the connected wireless device. If you wish to handle these errors yourself, you must set the `emitPacketErrors` option to true, as shown by this example:

```javascript
```
var sphero = require("sphero"),
orb = sphero("/dev/rfcomm0");
orb = sphero("/dev/rfcomm0", {emitPacketErrors: true});

orb.connect(function() {
// Sphero's connected!
// do some cool stuff here!
orb.on("error", function(err, data) {
// Do something with the err or just ignore.
});
});
```

Once connected, you can give Sphero commands:

```javascript
orb.connect(function() {
// roll Sphero forward
orb.roll(150, 0);

// turn Sphero green
orb.color("green");

// have Sphero tell you when it detect collisions
orb.detectCollisions();

// when Sphero detects a collision, turn red for a second, then back to green
orb.on("collision", function(data) {
console.log("collision detected");
console.log(" data:", data);

orb.color("red");
## Compatibility

setTimeout(function() {
orb.color("green");
}, 1000);
});
});
```
The Sphero.js module is currently compatible with Node.js versions 0.10.x thru 5.x

For more examples, check out the `examples` dir, or the JavaScript SDK documentation on the Sphero developer portal.
In order to install it with Node.js 5.x+, you will need to have g++ v4.8 or higher.

## Development

Use `npm install` to install development dependencies.
Use `npm install` to install development dependencies. You will also need to install [Grunt](http://gruntjs.com) by running `npm install -g grunt-cli`

You can run tests with `grunt test`.
You can then run tests with `grunt test`.

Using `grunt lint` to run ESLint against both `lib` and `spec/lib`.
Use `grunt lint` to run ESLint against both `lib` and `spec/lib`.

The `grunt` command will run both of the above tasks.
The `grunt` command by itself will run both of the above tasks.

## License

Expand Down
11 changes: 8 additions & 3 deletions examples/bluetooth-info.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
"use strict";

var sphero = require("../");
var orb = sphero("/dev/rfcomm0");
var orb = sphero(process.env.PORT);

orb.connect(function() {
orb.color("FF00FF");

orb.getBluetoothInfo(function(err, data) {
console.log("bluetooth info fetched");
if (err) { console.error("err:", err); }
console.log("data:", data);
else {
console.log("bluetoothInfo:");
console.log(" name:", data.name);
console.log(" btAddress:", data.btAddress);
console.log(" separator:", data.separator);
console.log(" colors:", data.colors);
}
});
});
2 changes: 1 addition & 1 deletion examples/calibration.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

var sphero = require("../");
var orb = sphero("/dev/rfcomm0");
var orb = sphero(process.env.PORT);

orb.connect(function() {
console.log("::START CALIBRATION::");
Expand Down
2 changes: 1 addition & 1 deletion examples/collision-detection.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

var sphero = require("../");
var orb = sphero("/dev/rfcomm0");
var orb = sphero(process.env.PORT);

orb.connect(function() {
orb.detectCollisions();
Expand Down
2 changes: 1 addition & 1 deletion examples/color.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

var sphero = require("../");
var orb = sphero("/dev/rfcomm0");
var orb = sphero(process.env.PORT);

orb.connect(function() {
// sets color to the provided r/g/b values
Expand Down
2 changes: 1 addition & 1 deletion examples/conway.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
var sphero = require("../");

var spheros = {
Thelma : sphero("/dev/rfcomm0"),
Thelma : sphero(process.env.PORT),
Louise : sphero("/dev/rfcomm1"),
Grace : sphero("/dev/rfcomm2"),
Ada : sphero("/dev/rfcomm3")
Expand Down
2 changes: 1 addition & 1 deletion examples/data-streaming.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

var sphero = require("../");
var orb = sphero("/dev/rfcomm0");
var orb = sphero(process.env.PORT);

orb.connect(function() {
// options for streaming data
Expand Down
19 changes: 19 additions & 0 deletions examples/freefall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use strict";

var sphero = require("../");
var orb = sphero(process.env.PORT, {timeout: 200});

orb.connect(function() {
orb.detectFreefall();

orb.on("freefall", function(data) {
console.log("freefall detected");
console.log(" data:", data);
});

orb.on("landed", function(data) {
console.log("landing detected");
console.log(" data:", data);
});

});
2 changes: 1 addition & 1 deletion examples/get-color.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

var sphero = require("../");
var orb = sphero("/dev/rfcomm0");
var orb = sphero(process.env.PORT);

orb.connect(function() {
// sets color to the provided r/g/b values
Expand Down
Loading