Skip to content
This repository has been archived by the owner on Dec 20, 2019. It is now read-only.

Commit

Permalink
Merge pull request #70 from orbotix/feature/promises-dual-interface
Browse files Browse the repository at this point in the history
:shipit:
  • Loading branch information
dportalesr committed Apr 14, 2016
2 parents 0a4b22e + 6c771fb commit fbaeeff
Show file tree
Hide file tree
Showing 11 changed files with 374 additions and 282 deletions.
23 changes: 22 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,32 @@ orb.connect(function() {

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

You can also use a Promises/A+ interface for your code, instead of the callback-based API:

```javascript
orb.connect().then(function() {
return orb.roll(155, 0);
}).then(function() {
return orb.color("green");
}).then(orb.detectCollisions);

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

orb.color("red")
.delay(100)
.then(function() {
return orb.color("green");
});
});
```

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
Expand Down
18 changes: 18 additions & 0 deletions examples/promise-bluetooth-info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict";

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

orb.connect().then(function() {
return orb.color("FF00FF");
}).then(function() {
return orb.getBluetoothInfo();
}).then(function(data) {
console.log("bluetoothInfo:");
console.log(" name:", data.name);
console.log(" btAddress:", data.btAddress);
console.log(" separator:", data.separator);
console.log(" colors:", data.colors);
}).catch(function(err) {
console.error("err:", err);
});
19 changes: 19 additions & 0 deletions examples/promise-collision-detection.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);

orb.connect().then(orb.detectCollisions).then(function() {
return orb.color("green");
}).then(function() {
return orb.roll(155, 0);
});

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

orb.color("red").delay(100).then(function() {
return orb.color("green");
});
});
20 changes: 20 additions & 0 deletions examples/promise-color.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

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

orb.connect().then(function() {
return orb.color({ red: 255, green: 0, blue: 255 });
}).delay(1000).then(function() {
console.log("color 1");
// sets color to the provided hex value
return orb.color(0xff0000);
}).delay(1000).then(function() {
console.log("color 2");
// hex numbers can also be passed in strings
return orb.color("00ff00");
}).delay(1000).then(function() {
console.log("color 3");
// sets color to the provided color name
return orb.color("magenta");
});
12 changes: 12 additions & 0 deletions examples/promise-roll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"use strict";

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

orb.connect().then(function() {
// roll orb in a random direction, changing direction every second
setInterval(function() {
var direction = Math.floor(Math.random() * 360);
orb.roll(150, direction);
}, 1000);
});
78 changes: 39 additions & 39 deletions lib/devices/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ module.exports = function core(device) {
* orb.ping(function(err, data) {
* console.log(err || "data: " + data);
* }
* @return {void}
* @return {object} promise for command
*/
device.ping = function(callback) {
command(commands.ping, null, callback);
return command(commands.ping, null, callback);
};

/**
Expand All @@ -44,10 +44,10 @@ module.exports = function core(device) {
* console.log(" apiMin:", data.apiMin);
* }
* }
* @return {void}
* @return {object} promise for command
*/
device.version = function(callback) {
command(commands.version, null, callback);
return command(commands.version, null, callback);
};

/**
Expand All @@ -59,10 +59,10 @@ module.exports = function core(device) {
* orb.controlUartTx(function(err, data) {
* console.log(err || "data: " + data);
* }
* @return {void}
* @return {object} promise for command
*/
device.controlUartTx = function(callback) {
command(commands.controlUARTTx, null, callback);
return command(commands.controlUARTTx, null, callback);
};

/**
Expand All @@ -80,7 +80,7 @@ module.exports = function core(device) {
* orb.setDeviceName("rollingOrb", function(err, data) {
* console.log(err || "data: " + data);
* }
* @return {void}
* @return {object} promise for command
*/
device.setDeviceName = function(name, callback) {
var data = [];
Expand All @@ -89,7 +89,7 @@ module.exports = function core(device) {
data[i] = name.charCodeAt(i);
}

command(commands.setDeviceName, data, callback);
return command(commands.setDeviceName, data, callback);
};

/**
Expand All @@ -112,10 +112,10 @@ module.exports = function core(device) {
* console.log(" colors:", data.colors);
* }
* }
* @return {void}
* @return {object} promise for command
*/
device.getBluetoothInfo = function(callback) {
command(commands.getBtInfo, null, callback);
return command(commands.getBtInfo, null, callback);
};

/**
Expand All @@ -130,10 +130,10 @@ module.exports = function core(device) {
* orb.setAutoReconnect(1, 20, function(err, data) {
* console.log(err || "data: " + data);
* }
* @return {void}
* @return {object} promise for command
*/
device.setAutoReconnect = function(flag, time, callback) {
command(commands.setAutoReconnect, [flag, time], callback);
return command(commands.setAutoReconnect, [flag, time], callback);
};

/**
Expand All @@ -151,10 +151,10 @@ module.exports = function core(device) {
* console.log(" time:", data.time);
* }
* }
* @return {void}
* @return {object} promise for command
*/
device.getAutoReconnect = function(callback) {
command(commands.getAutoReconnect, null, callback);
return command(commands.getAutoReconnect, null, callback);
};

/**
Expand Down Expand Up @@ -189,10 +189,10 @@ module.exports = function core(device) {
* console.log(" secondsSinceCharge:", data.secondsSinceCharge);
* }
* }
* @return {void}
* @return {object} promise for command
*/
device.getPowerState = function(callback) {
command(commands.getPwrState, null, callback);
return command(commands.getPwrState, null, callback);
};

/**
Expand All @@ -208,10 +208,10 @@ module.exports = function core(device) {
* orb.setPowerNotification(1, function(err, data) {
* console.log(err || "data: " + data);
* }
* @return {void}
* @return {object} promise for command
*/
device.setPowerNotification = function(flag, callback) {
command(commands.setPwrNotify, [flag], callback);
return command(commands.setPwrNotify, [flag], callback);
};

/**
Expand All @@ -229,15 +229,15 @@ module.exports = function core(device) {
* orb.sleep(10, 0, 0, function(err, data) {
* console.log(err || "data: " + data);
* }
* @return {void}
* @return {object} promise for command
*/
device.sleep = function(wakeup, macro, orbBasic, callback) {
wakeup = utils.intToHexArray(wakeup, 2);
orbBasic = utils.intToHexArray(orbBasic, 2);

var data = [].concat(wakeup, macro, orbBasic);

command(commands.sleep, data, callback);
return command(commands.sleep, data, callback);
};

/**
Expand All @@ -258,10 +258,10 @@ module.exports = function core(device) {
* console.log(" vCrit:", data.vCrit);
* }
* }
* @return {void}
* @return {object} promise for command
*/
device.getVoltageTripPoints = function(callback) {
command(commands.getPowerTrips, null, callback);
return command(commands.getPowerTrips, null, callback);
};

/**
Expand All @@ -286,15 +286,15 @@ module.exports = function core(device) {
* orb.setVoltageTripPoints(675, 650, function(err, data) {
* console.log(err || "data: " + data);
* }
* @return {void}
* @return {object} promise for command
*/
device.setVoltageTripPoints = function(vLow, vCrit, callback) {
vLow = utils.intToHexArray(vLow, 2);
vCrit = utils.intToHexArray(vCrit, 2);

var data = [].concat(vLow, vCrit);

command(commands.setPowerTrips, data, callback);
return command(commands.setPowerTrips, data, callback);
};

/**
Expand All @@ -310,11 +310,11 @@ module.exports = function core(device) {
* orb.setInactivityTimeout(120, function(err, data) {
* console.log(err || "data: " + data);
* }
* @return {void}
* @return {object} promise for command
*/
device.setInactivityTimeout = function(time, callback) {
var data = utils.intToHexArray(time, 2);
command(commands.setInactiveTimer, data, callback);
return command(commands.setInactiveTimer, data, callback);
};

/**
Expand All @@ -329,10 +329,10 @@ module.exports = function core(device) {
* orb.jumpToBootLoader(function(err, data) {
* console.log(err || "data: " + data);
* }
* @return {void}
* @return {object} promise for command
*/
device.jumpToBootloader = function(callback) {
command(commands.goToBl, null, callback);
return command(commands.goToBl, null, callback);
};

/**
Expand All @@ -349,10 +349,10 @@ module.exports = function core(device) {
* orb.runL1Diags(function(err, data) {
* console.log(err || "data: " + data);
* }
* @return {void}
* @return {object} promise for command
*/
device.runL1Diags = function(callback) {
command(commands.runL1Diags, null, callback);
return command(commands.runL1Diags, null, callback);
};

/**
Expand Down Expand Up @@ -390,10 +390,10 @@ module.exports = function core(device) {
* console.log(" gyroAdjustCount:", data.gyroAdjustCount);
* }
* }
* @return {void}
* @return {object} promise for command
*/
device.runL2Diags = function(callback) {
command(commands.runL2Diags, null, callback);
return command(commands.runL2Diags, null, callback);
};

/**
Expand All @@ -408,15 +408,15 @@ module.exports = function core(device) {
* orb.clearCounters(function(err, data) {
* console.log(err || "data: " + data);
* }
* @return {void}
* @return {object} promise for command
*/
device.clearCounters = function(callback) {
command(commands.clearCounters, null, callback);
return command(commands.clearCounters, null, callback);
};

device._coreTimeCmd = function(cmd, time, callback) {
var data = utils.intToHexArray(time, 4);
command(cmd, data, callback);
return command(cmd, data, callback);
};

/**
Expand All @@ -429,10 +429,10 @@ module.exports = function core(device) {
* orb.assignTime(0x00ffff00, function(err, data) {
* console.log(err || "data: " + data);
* }
* @return {void}
* @return {object} promise for command
*/
device.assignTime = function(time, callback) {
device._coreTimeCmd(commands.assignTime, time, callback);
return device._coreTimeCmd(commands.assignTime, time, callback);
};

/**
Expand All @@ -454,9 +454,9 @@ module.exports = function core(device) {
* console.log(" t3:", data.t3);
* }
* }
* @return {void}
* @return {object} promise for command
*/
device.pollPacketTimes = function(time, callback) {
device._coreTimeCmd(commands.pollTimes, time, callback);
return device._coreTimeCmd(commands.pollTimes, time, callback);
};
};

0 comments on commit fbaeeff

Please sign in to comment.