diff --git a/examples/api/api.js b/examples/api/api.js index a09e77e..4505daf 100644 --- a/examples/api/api.js +++ b/examples/api/api.js @@ -6,33 +6,28 @@ Cylon.config({ Cylon.api(); -var bots = [ - { port: '/dev/rfcomm0', name: 'Thelma' }, - { port: '/dev/rfcomm1', name: 'Louise' } -]; - -var SpheroRobot = { - connection: { name: 'Sphero', adaptor: 'sphero' }, - - device: { name: 'sphero', driver: 'sphero' }, - - work: function(my) { - every((1).seconds(), function() { - console.log(my.name); - my.sphero.setRandomColor(); - my.sphero.roll(60, Math.floor(Math.random() * 360)); - }); - } +var bots = { + 'Thelma': '/dev/rfcomm0', + 'Louise': '/dev/rfcomm1' }; -for (var i = 0; i < bots.length; i++) { - var bot = bots[i]; - var robot = Object.create(SpheroRobot); +Object.keys(bots).forEach(function(name) { + var port = bots[name]; - robot.connection.port = bot.port; - robot.name = bot.name; + Cylon.robot({ + name: name, - Cylon.robot(robot); -} + connection: { name: 'sphero', adaptor: 'sphero', port: port }, + device: { name: 'sphero', driver: 'sphero' }, + + work: function(my) { + every((1).seconds(), function() { + console.log(my.name); + my.sphero.setRandomColor(); + my.sphero.roll(60, Math.floor(Math.random() * 360)); + }); + } + }); +}); Cylon.start(); diff --git a/examples/api/api.markdown b/examples/api/api.markdown index 89f9e33..1628f88 100644 --- a/examples/api/api.markdown +++ b/examples/api/api.markdown @@ -24,52 +24,41 @@ the different parts of each robot in objects so we can initialize them later. The only differences between the bots are their names and the port they'll be using. - var bots = [ - { port: '/dev/rfcomm0', name: 'Thelma' }, - { port: '/dev/rfcomm1', name: 'Louise' } - ]; + var bots = { + 'Thelma': '/dev/rfcomm0', + 'Louise': '/dev/rfcomm1' + }; Now we can define the basic robot both of our Sphero robots will be based on. - var SpheroRobot = { + Object.keys(bots).forEach(function(name) { + var port = bots[name]; + + Cylon.robot({ + name: name, Both robots will be connecting to Spheros, and so using the cylon-sphero adaptor: - connection: { name: 'Sphero', adaptor: 'sphero' }, + connection: { name: 'sphero', adaptor: 'sphero', port: port }, And both will be connecting to the same kind of device (you guessed it, a Sphero). - device: { name: 'sphero', driver: 'sphero' }, + device: { name: 'sphero', driver: 'sphero' }, Both robots will be performing the same kind of work as well. Every second, they'll print their name to the console, set themselves to a random color, and roll in a random direction. - work: function(my) { - every((1).seconds(), function() { - console.log(my.name); - my.sphero.setRandomColor(); - my.sphero.roll(60, Math.floor(Math.random() * 360)); - }); - } - }; - -Now that we know what hardware the robots have and what work they'll be doing, -let's create them! We'll iterate through our earlier `bots` array, creating -a new Robot, assigning it it's unique characteristics, then passing it off to -Cylon so it can keep track of it. - - for (var i = 0; i < bots.length; i++) { - var bot = bots[i]; - var robot = Object.create(SpheroRobot); - - robot.connection.port = bot.port; - robot.name = bot.name; - - Cylon.robot(robot); - } + work: function(my) { + every((1).seconds(), function() { + console.log(my.name); + my.sphero.setRandomColor(); + my.sphero.roll(60, Math.floor(Math.random() * 360)); + }); + } + }); And now that Cylon has all the robots we're intending to give it, let's get started!