Finish reorganizing existing examples

This commit is contained in:
Andrew Stewart 2013-11-26 12:21:43 -08:00
parent dbfa4f859e
commit 64809b16cf
24 changed files with 566 additions and 242 deletions

View File

@ -1,13 +1,12 @@
Cylon = require '..'
Cylon = require '../..'
Cylon.api host: '0.0.0.0', port: '8080'
class MyRobot
commands:
["relax"]
commands: ["relax"]
relax: ->
return "#{this.name} says relax"
"#{this.name} says relax"
work: (me) ->
every 1.seconds(), ->

View File

@ -0,0 +1,29 @@
var Cylon = require('../..');
Cylon.api({ host: '0.0.0.0', port: '8080' });
var MyRobot = (function() {
function MyRobot() {}
MyRobot.prototype.commands = ["relax"];
MyRobot.prototype.relax = function() {
return "" + this.name + " says relax";
};
MyRobot.prototype.work = function(me) {
every((1).seconds(), function() {
Logger.info(me.name);
});
};
return MyRobot;
})();
var robot = new MyRobot;
robot.name = "frankie";
Cylon.robot(robot);
Cylon.start();

View File

@ -1,122 +0,0 @@
Cylon = require('..')
Cylon.api host: '0.0.0.0', port: '8080'
class PebbleRobot
connection:
name: 'pebble', adaptor: 'pebble'
device:
name: 'pebble', driver: 'pebble'
message: (robot, msg) =>
robot.message_queue().push(msg)
work: (me) ->
me.pebble.on('connect', ->
Logger.info 'connected!'
)
class SalesforceRobot
connection:
name: 'sfcon',
adaptor: 'force',
sfuser: process.env.SF_USERNAME,
sfpass: process.env.SF_SECURITY_TOKEN,
orgCreds: {
clientId: process.env.SF_CLIENT_ID,
clientSecret: process.env.SF_CLIENT_SECRET,
redirectUri: 'http://localhost:3000/oauth/_callback'
}
device:
name: 'salesforce', driver: 'force'
spheroReport:{}
work: (me) ->
me.salesforce.on('start', () ->
me.salesforce.subscribe('/topic/SpheroMsgOutbound', (data) ->
spheroName = data.sobject.Sphero_Name__c
bucks = data.sobject.Bucks__c
Logger.info "Sphero: #{ spheroName }, data Bucks: #{ bucks }, SM_Id: #{ data.sobject.Id }"
me.master.findRobot(spheroName, (err, spheroBot) ->
spheroBot.react(spheroBot.devices.sphero)
)
me.spheroReport[spheroName] = bucks
toPebble = ""
for key, val of me.spheroReport
toPebble += "#{key}: $#{val}\n"
me.master.findRobot('pebble', (error, pebbleBot) ->
pebbleBot.message(pebbleBot.devices.pebble, toPebble)
)
)
)
class SpheroRobot
totalBucks: 1
payingPower: true
connection:
name: 'sphero', adaptor: 'sphero'
device:
name: 'sphero', driver: 'sphero'
react: (device) ->
device.setRGB(0x00FF00)
device.roll 90, Math.floor(Math.random() * 360)
this.payingPower = true
bankrupt: () ->
every 3.seconds(), () =>
if this.payingPower and this.totalBucks > 0
this.totalBucks += -1
if @totalBucks == 0
this.sphero.setRGB(0xFF000)
this.sphero.stop()
changeDirection: () ->
every 1.seconds(), () =>
this.sphero.roll 90, Math.floor(Math.random() * 360) if @payingPower
work: (me) ->
me.sphero.on 'connect', ->
Logger.info('Setting up Collision Detection...')
me.sphero.detectCollisions()
me.sphero.stop()
me.sphero.setRGB(0x00FF00)
me.sphero.roll 90, Math.floor(Math.random() * 360)
me.bankrupt()
me.changeDirection()
me.sphero.on 'collision', (data) ->
me.sphero.setRGB(0x0000FF)
me.sphero.stop()
me.payingPower = false
toSend = "{ \"spheroName\" :\"#{ me.name }\", \"bucks\": \"#{ me.totalBucks++ }\" }"
me.master.findRobot('salesforce', (err, sf) ->
sf.devices.salesforce.push('SpheroController', 'POST', toSend)
)
sfRobot = new SalesforceRobot()
sfRobot.name = "salesforce"
Cylon.robot sfRobot
pebRobot = new PebbleRobot()
pebRobot.name = "pebble"
Cylon.robot pebRobot
bots = [
{ port: '/dev/tty.Sphero-ROY-AMP-SPP', name: 'ROY' },
{ port: '/dev/tty.Sphero-GBO-AMP-SPP', name: 'GBO'},
{ port: '/dev/tty.Sphero-RRY-AMP-SPP', name: 'RRY'}
]
for bot in bots
robot = new SpheroRobot
robot.connection.port = bot.port
robot.name = bot.name
Cylon.robot robot
Cylon.start()

View File

@ -0,0 +1,121 @@
Cylon = require '../..'
Cylon.api host: '0.0.0.0', port: '8080'
class PebbleRobot
connection: { name: 'pebble', adaptor: 'pebble' }
device: { name: 'pebble', driver: 'pebble' }
message: (robot, msg) =>
robot.message_queue().push(msg)
work: (me) ->
me.pebble.on 'connect', -> Logger.info "Connected!"
class SalesforceRobot
connection:
name: 'sfcon'
adaptor: 'force'
sfuser: process.env.SF_USERNAME
sfpass: process.env.SF_SECURITY_TOKEN
orgCreds:
clientId: process.env.SF_CLIENT_ID
clientSecret: process.env.SF_CLIENT_SECRET
redirectUri: 'http://localhost:3000/oauth/_callback'
device: { name: 'salesforce', driver: 'force' }
spheroReport: {}
work: (me) ->
me.salesforce.on 'start', () ->
me.salesforce.subscribe '/topic/SpheroMsgOutbound', (data) ->
name = data.sobject.Sphero_Name__c
bucks = data.sobject.Bucks__c
msg = "Sphero: #{name},"
msg += "data Bucks: #{bucks},"
msg += "SM_Id: #{data.sobject.Id}"
Logger.info msg
me.master.findRobot name, (err, spheroBot) ->
spheroBot.react spheroBot.devices.sphero
me.spheroReport[name] = bucks
toPebble = ""
toPebble += "#{key}: $#{val}\n" for key, val of me.spheroReport
me.master.findRobot 'pebble', (error, pebbleBot) ->
pebbleBot.message pebbleBot.devices.pebble, toPebble
class SpheroRobot
totalBucks: 1
payingPower: true
connection: { name: 'sphero', adaptor: 'sphero' }
device: { name: 'sphero', driver: 'sphero' }
react: (device) ->
device.setRGB 0x00FF00
device.roll 90, Math.floor(Math.random() * 360)
@payingPower = true
bankrupt: () ->
every 3.seconds(), () =>
if @payingPower and @totalBucks > 0
@totalBucks += -1
if @totalBucks is 0
@sphero.setRGB 0xFF000
@sphero.stop()
changeDirection: () ->
every 1.seconds(), () =>
@sphero.roll 90, Math.floor(Math.random() * 360) if @payingPower
work: (me) ->
me.sphero.on 'connect', ->
Logger.info 'Setting up Collision Detection...'
me.sphero.detectCollisions()
me.sphero.stop()
me.sphero.setRGB 0x00FF00
me.sphero.roll 90, Math.floor(Math.random() * 360)
me.bankrupt()
me.changeDirection()
me.sphero.on 'collision', (data) ->
me.sphero.setRGB 0x0000FF
me.sphero.stop()
me.payingPower = false
data = JSON.stringify
spheroName: "#{me.name}",
bucks: "#{me.totalBucks++}"
me.master.findRobot 'salesforce', (err, sf) ->
sf.devices.salesforce.push "SpheroController", "POST", data
salesforceRobot = new SalesforceRobot()
salesforceRobot.name = "salesforce"
Cylon.robot salesforceRobot
pebbleRobot = new PebbleRobot()
pebbleRobot.name = "pebble"
Cylon.robot pebbleRobot
bots = [
{ port: '/dev/tty.Sphero-ROY-AMP-SPP', name: 'ROY' },
{ port: '/dev/tty.Sphero-GBO-AMP-SPP', name: 'GBO'},
{ port: '/dev/tty.Sphero-RRY-AMP-SPP', name: 'RRY'}
]
for bot in bots
robot = new SpheroRobot
robot.connection.port = bot.port
robot.name = bot.name
Cylon.robot robot
Cylon.start()

View File

@ -0,0 +1,178 @@
var __bind = function(fn, me) {
return function() { return fn.apply(me, arguments); };
};
var Cylon = require('../..');
Cylon.api({ host: '0.0.0.0', port: '8080' });
var PebbleRobot = (function() {
function PebbleRobot() { this.message = __bind(this.message, this); }
PebbleRobot.prototype.connection = { name: 'pebble', adaptor: 'pebble' };
PebbleRobot.prototype.device = { name: 'pebble', driver: 'pebble' };
PebbleRobot.prototype.message = function(robot, msg) {
robot.message_queue().push(msg);
};
PebbleRobot.prototype.work = function(me) {
me.pebble.on('connect', function() { Logger.info("Connected!"); });
};
return PebbleRobot;
})();
var SalesforceRobot = (function() {
function SalesforceRobot() {}
SalesforceRobot.prototype.connection = {
name: 'sfcon',
adaptor: 'force',
sfuser: process.env.SF_USERNAME,
sfpass: process.env.SF_SECURITY_TOKEN,
orgCreds: {
clientId: process.env.SF_CLIENT_ID,
clientSecret: process.env.SF_CLIENT_SECRET,
redirectUri: 'http://localhost:3000/oauth/_callback'
}
};
SalesforceRobot.prototype.device = { name: 'salesforce', driver: 'force' };
SalesforceRobot.prototype.spheroReport = {};
SalesforceRobot.prototype.work = function(me) {
me.salesforce.on('start', function() {
me.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) {
var bucks, key, msg, name, toPebble, val, _ref;
name = data.sobject.Sphero_Name__c;
bucks = data.sobject.Bucks__c;
msg = "Sphero: " + name + ",";
msg += "data Bucks: " + bucks + ",";
msg += "SM_Id: " + data.sobject.Id;
Logger.info(msg);
me.master.findRobot(name, function(err, spheroBot) {
spheroBot.react(spheroBot.devices.sphero);
});
me.spheroReport[name] = bucks;
toPebble = "";
_ref = me.spheroReport;
for (key in _ref) {
val = _ref[key];
toPebble += "" + key + ": $" + val + "\n";
}
me.master.findRobot('pebble', function(error, pebbleBot) {
pebbleBot.message(pebbleBot.devices.pebble, toPebble);
});
});
});
};
return SalesforceRobot;
})();
var SpheroRobot = (function() {
function SpheroRobot() {}
SpheroRobot.prototype.totalBucks = 1;
SpheroRobot.prototype.payingPower = true;
SpheroRobot.prototype.connection = { name: 'sphero', adaptor: 'sphero' };
SpheroRobot.prototype.device = { name: 'sphero', driver: 'sphero' };
SpheroRobot.prototype.react = function(device) {
device.setRGB(0x00FF00);
device.roll(90, Math.floor(Math.random() * 360));
this.payingPower = true;
};
SpheroRobot.prototype.bankrupt = function() {
var _this = this;
every(3..seconds(), function() {
if (_this.payingPower && _this.totalBucks > 0) {
_this.totalBucks += -1;
if (_this.totalBucks === 0) {
_this.sphero.setRGB(0xFF000);
_this.sphero.stop();
}
}
});
};
SpheroRobot.prototype.changeDirection = function() {
var _this = this;
every((1).seconds(), function() {
if (_this.payingPower) {
_this.sphero.roll(90, Math.floor(Math.random() * 360));
}
});
};
SpheroRobot.prototype.work = function(me) {
me.sphero.on('connect', function() {
Logger.info('Setting up Collision Detection...');
me.sphero.detectCollisions();
me.sphero.stop();
me.sphero.setRGB(0x00FF00);
me.sphero.roll(90, Math.floor(Math.random() * 360));
me.bankrupt();
me.changeDirection();
});
me.sphero.on('collision', function(data) {
me.sphero.setRGB(0x0000FF);
me.sphero.stop();
me.payingPower = false;
data = JSON.stringify({
spheroName: "" + me.name,
bucks: "" + (me.totalBucks++)
});
me.master.findRobot('salesforce', function(err, sf) {
sf.devices.salesforce.push("SpheroController", "POST", data);
});
});
};
return SpheroRobot;
})();
var salesforceRobot = new SalesforceRobot();
salesforceRobot.name = "salesforce";
Cylon.robot(salesforceRobot);
var pebbleRobot = new PebbleRobot();
pebbleRobot.name = "pebble";
Cylon.robot(pebbleRobot);
var bots = [
{ port: '/dev/tty.Sphero-ROY-AMP-SPP', name: 'ROY' },
{ port: '/dev/tty.Sphero-GBO-AMP-SPP', name: 'GBO' },
{ port: '/dev/tty.Sphero-RRY-AMP-SPP', name: 'RRY' }
];
for (var i = 0; i < bots.length; i++) {
var bot = bots[i];
var robot = new SpheroRobot;
robot.connection.port = bot.port;
robot.name = bot.name;
Cylon.robot(robot);
}
Cylon.start();

View File

@ -1,14 +0,0 @@
Cylon = require('..')
Cylon.robot
connection:
name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0'
device:
name: 'sphero', driver: 'sphero'
work: (me) ->
every 1.second(), ->
me.sphero.roll 60, Math.floor(Math.random() * 360)
.start()

View File

@ -0,0 +1,11 @@
Cylon = require '../..'
Cylon.robot
connection: { name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0' }
device: { name: 'sphero', driver: 'sphero' }
work: (me) ->
every 1.second(), ->
me.sphero.roll 60, Math.floor(Math.random() * 360)
.start()

12
examples/sphero/sphero.js Normal file
View File

@ -0,0 +1,12 @@
var Cylon = require('../..');
Cylon.robot({
connection: { name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0' },
device: { name: 'sphero', driver: 'sphero' },
work: function(me) {
every((1).second(), function() {
me.sphero.roll(60, Math.floor(Math.random() * 360));
});
}
}).start();

View File

@ -1,28 +0,0 @@
Cylon = require('..')
Cylon.robot
connection:
name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0'
device:
name: 'sphero', driver: 'sphero'
work: (me) ->
color = 0x00FF00
bitFilter = 0xFFFF00
me.sphero.on('connect', ->
Logger.info('Setting up Collision Detection...')
me.sphero.detectCollisions()
me.sphero.setRGB(color)
me.sphero.stop()
)
me.sphero.on 'collision', (data) ->
Logger.info 'collision:'
color = color ^ bitFilter
console.log("color: #{ color.toString(16) } ")
me.sphero.setRGB(color)
me.sphero.roll 90, Math.floor(Math.random() * 360)
.start()

View File

@ -0,0 +1,24 @@
Cylon = require '../..'
Cylon.robot
connection: { name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0' }
device: { name: 'sphero', driver: 'sphero' }
work: (me) ->
color = 0x00FF00
bitFilter = 0xFFFF00
me.sphero.on 'connect', ->
Logger.info "Setting up Collision Detection..."
me.sphero.detectCollisions()
me.sphero.setRGB color
me.sphero.stop()
me.sphero.on 'collision', (data) ->
Logger.info "Collision:"
color = color ^ bitFilter
Logger.info "Color: #{color.toString(16)} "
me.sphero.setRGB color
me.sphero.roll 90, Math.floor(Math.random() * 360)
.start()

View File

@ -0,0 +1,26 @@
var Cylon = require('../..');
Cylon.robot({
connection: { name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0' },
device: { name: 'sphero', driver: 'sphero' },
work: function(me) {
var color = 0x00FF00;
var bitFilter = 0xFFFF00;
me.sphero.on('connect', function() {
Logger.info("Setting up Collision Detection...");
me.sphero.detectCollisions();
me.sphero.setRGB(color);
me.sphero.stop();
});
me.sphero.on('collision', function(data) {
Logger.info("Collision:");
color = color ^ bitFilter;
Logger.info("Color: " + (color.toString(16)) + " ");
me.sphero.setRGB(color);
me.sphero.roll(90, Math.floor(Math.random() * 360));
});
}
}).start();

View File

@ -1,14 +0,0 @@
Cylon = require('..')
Cylon.robot
connection:
name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0'
device:
name: 'sphero', driver: 'sphero'
work: (me) ->
every 1.second(), ->
me.sphero.setRGB Math.floor(Math.random() * 100000)
.start()

View File

@ -0,0 +1,12 @@
Cylon = require '../..'
Cylon.robot
connection: { name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0' }
device: { name: 'sphero', driver: 'sphero' }
work: (me) ->
every 1.second(), ->
me.sphero.setRGB Math.floor(Math.random() * 100000)
.start()

View File

@ -0,0 +1,12 @@
var Cylon = require('../..');
Cylon.robot({
connection: { name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0' },
device: { name: 'sphero', driver: 'sphero' },
work: function(me) {
every((1).second(), function() {
me.sphero.setRGB(Math.floor(Math.random() * 100000));
});
}
}).start();

View File

@ -1,38 +0,0 @@
Cylon = require('..')
Cylon.robot
connection:
name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0'
device:
name: 'sphero', driver: 'sphero'
work: (me) ->
me.sphero.on 'connect', ->
Logger.info('Setting up Collision Detection...')
me.sphero.detectCollisions()
me.sphero.setRGB(0x00FF00)
me.sphero.on 'update', (data) ->
Logger.info("Update event eventName -> #{ data } ")
Logger.info("Update event args -> ")
Logger.info(data)
me.sphero.on 'message', (data) ->
me.sphero.setRGB(0x0000FF)
Logger.info 'message:'
Logger.info data
me.sphero.on 'collision', (data) ->
me.sphero.setRGB(0xFF0000)
Logger.info 'collision:'
Logger.info data
me.sphero.on 'notification', (data) ->
me.sphero.setRGB(0xFF0000)
Logger.info 'notification:'
Logger.info data
.start()

View File

@ -0,0 +1,33 @@
Cylon = require '../..'
Cylon.robot
connection: { name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0' }
device: { name: 'sphero', driver: 'sphero' }
work: (me) ->
me.sphero.on 'connect', ->
Logger.info "Setting up Collision Detection..."
me.sphero.detectCollisions()
me.sphero.setRGB 0x00FF00
me.sphero.on 'update', (data) ->
Logger.info "Update event eventName: #{data} "
Logger.info "Update event args: "
Logger.info data
me.sphero.on 'message', (data) ->
me.sphero.setRGB 0x0000FF
Logger.info "Message:"
Logger.info data
me.sphero.on 'collision', (data) ->
me.sphero.setRGB 0xFF0000
Logger.info "Collision:"
Logger.info data
me.sphero.on 'notification', (data) ->
me.sphero.setRGB 0xFF0000
Logger.info "Notification:"
Logger.info data
.start()

View File

@ -0,0 +1,38 @@
var Cylon = require('../..');
Cylon.robot({
connection: { name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0' },
device: { name: 'sphero', driver: 'sphero' },
work: function(me) {
me.sphero.on('connect', function() {
Logger.info("Setting up Collision Detection...");
me.sphero.detectCollisions();
me.sphero.setRGB(0x00FF00);
});
me.sphero.on('update', function(data) {
Logger.info("Update event eventName: " + data + " ");
Logger.info("Update event args: ");
Logger.info(data);
});
me.sphero.on('message', function(data) {
me.sphero.setRGB(0x0000FF);
Logger.info("Message:");
Logger.info(data);
});
me.sphero.on('collision', function(data) {
me.sphero.setRGB(0xFF0000);
Logger.info("Collision:");
Logger.info(data);
});
me.sphero.on('notification', function(data) {
me.sphero.setRGB(0xFF0000);
Logger.info("Notification:");
Logger.info(data);
});
}
}).start();

View File

@ -0,0 +1,26 @@
Cylon = require '../..'
class SpheroRobot
connection: { name: 'Sphero', adaptor: 'sphero' }
device: { name: 'sphero', driver: 'sphero' }
work: (my) ->
every 1.seconds(), ->
Logger.info my.name
my.sphero.setRandomColor()
my.sphero.roll 60, Math.floor(Math.random() * 360)
bots = [
{ name: "Thelma", port: "/dev/rfcomm0" },
{ name: "Louise", port: "/dev/rfcomm1" }
]
for bot in bots
robot = new SpheroRobot
robot.name = bot.name
robot.connection.port = bot.port
Cylon.robot robot
Cylon.start()

View File

@ -0,0 +1,35 @@
var Cylon = require('../..');
var SpheroRobot = (function() {
function SpheroRobot() {}
SpheroRobot.prototype.connection = { name: 'Sphero', adaptor: 'sphero' };
SpheroRobot.prototype.device = { name: 'sphero', driver: 'sphero' };
SpheroRobot.prototype.work = function(my) {
every((1).second(), function() {
Logger.info(my.name);
my.sphero.setRandomColor();
my.sphero.roll(60, Math.floor(Math.random() * 360));
});
};
return SpheroRobot;
})();
var bots = [
{ name: "Thelma", port: "/dev/rfcomm0" },
{ name: "Louise", port: "/dev/rfcomm1" }
];
for (var i = 0; i < bots.length; i++) {
var bot = bots[i];
var robot = new SpheroRobot;
robot.name = bot.name;
robot.connection.port = bot.port;
Cylon.robot(robot);
}
Cylon.start();

View File

@ -9,7 +9,7 @@ install cylon-sphero`)
First, load up Cylon. Since we're in the Cylon repo, we can use the version
already here:
Cylon = require '..'
Cylon = require '../..'
Since both of our Spheros are going to have faily similar behaviour, we can
define a class to hold their attributes:

View File

@ -1,16 +0,0 @@
var Cylon = require('..');
// Initialize the robot
var robot = Cylon.robot({
connection: { name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' },
device: { name: 'led', driver: 'led', pin: 13 },
work: function(self) {
// we do our thing here
console.log(self.led.name);
every((1).second(), function() { console.log(self.led.name); });
}
});
// start working
robot.start();

View File

@ -1,4 +1,4 @@
Cylon = require '..'
Cylon = require '../..'
Travis = require 'travis-ci'
travis = new Travis

View File

@ -1,4 +1,4 @@
var Cylon = require('..')
var Cylon = require('../..')
var Travis = require('travis-ci')
var travis = new Travis({version: '2.0.0'});

View File

@ -11,7 +11,7 @@ Before you run this, make sure you install the following dependencies:
First of all, let's load up Cylon. We're going to load the version directly from
the repo, since we're here already:
Cylon = require '..'
Cylon = require '../..'
Next, we'll set up Travis. We're going to be using the very useful [travis-ci][]
module.