Additional WIP on converting literate coffeescript examples to markdown+js

This commit is contained in:
Andrew Stewart 2014-03-26 17:06:26 -07:00
parent e7a340fd0c
commit 479388e7ad
7 changed files with 96 additions and 92 deletions

View File

@ -6,33 +6,37 @@ take off and land. Before we get started, make sure you've got the
To start us off, let's import Cylon: To start us off, let's import Cylon:
Cylon = require '../..' var Cylon = require('../..');
Now that we've got that set up, we can start defining our robot: Now that we've got that set up, we can start defining our robot:
Cylon.robot Cylon.robot({
We'll be using one connection and one device for this robot, both using the We'll be using one connection and one device for this robot, both using the
crazyflie adaptor. We'll be connecting to the Crazyflie using the CrazyRadio. crazyflie adaptor. We'll be connecting to the Crazyflie using the CrazyRadio.
connection: connection: {
name: 'crazyflie', adaptor: 'crazyflie', port: "radio://1/10/250KPS" name: 'crazyflie',
adaptor: 'crazyflie',
port: "radio://1/10/250KPS"
},
device: device: { name: 'drone', driver: 'crazyflie' }
name: 'drone', driver: 'crazyflie'
With the parts in place, we can start defining our robot's work. With the parts in place, we can start defining our robot's work.
work: (my) -> work: function(my) {
When our drone is ready, we'll make it takeoff. After ten seconds, we'll tell it When our drone is ready, we'll make it takeoff. After ten seconds, we'll tell it
to land, and five seconds after that to stop. to land, and five seconds after that to stop.
my.drone.on 'start', -> my.drone.on('start', function() {
my.drone.takeoff() my.drone.takeoff()
after 10.seconds(), -> my.drone.land() after((10).seconds(), my.drone.land);
after 15.seconds(), -> my.drone.stop() after((15).seconds(), my.drone.stop);
});
}
With all that done, we can start up the robot and get the Crazyflie flying: With all that done, we can start up the robot and get the Crazyflie flying:
.start() }).start();

View File

@ -2,27 +2,26 @@
First, let's import Cylon: First, let's import Cylon:
Cylon = require '../..' var Cylon = require('../..');
Now that we have Cylon imported, we can start defining our robot Now that we have Cylon imported, we can start defining our robot
# Initialize the robot Cylon.robot({
Cylon.robot
Let's define the connections and devices: Let's define the connections and devices:
connection: { name: 'digispark', adaptor: 'digispark'} connection: { name: 'digispark', adaptor: 'digispark' },
device: { name: 'led', driver: 'led', pin: 1 } device: { name: 'led', driver: 'led', pin: 1 },
Now that Cylon knows about the necessary hardware we're going to be using, we'll Now that Cylon knows about the necessary hardware we're going to be using, we'll
tell it what work we want to do: tell it what work we want to do:
work: (my) -> work: function(my) {
every((1).second(), my.led.toggle);
every 1.second(), -> my.led.toggle() }
Now that our robot knows what work to do, and the work it will be doing that Now that our robot knows what work to do, and the work it will be doing that
hardware with, we can start it: hardware with, we can start it:
.start() }).start();

View File

@ -6,22 +6,25 @@ an interval, and after a timeout.
Let's start by importing Cylon: Let's start by importing Cylon:
Cylon = require '../..' var Cylon = require('../..');
Now we can define our robot: Now we can define our robot:
Cylon.robot Cylon.robot({
For work, it's going to print a message to the console every second, and another For work, it's going to print a message to the console every second, and another
message after ten seconds have elapsed. message after ten seconds have elapsed.
work: -> work: function() {
every 1.second(), -> every((1).second(), function() {
console.log("Hello, human!") console.log("Hello, human!")
});
after 10.seconds(), -> after((10).seconds(), function() {
console.log "Impressive." console.log "Impressive."
});
}
Simple as can be. Now that we're done, let's start the robot: Simple as can be. Now that we're done, let's start the robot:
.start() }).start();

View File

@ -1,14 +1,28 @@
# Keyboard # Keyboard
For this Cylon example, we're going to quickly demonstrate getting keyboard
input.
First, let's import Cylon: First, let's import Cylon:
Cylon = require '../..' var Cylon = require('../..');
Cylon.robot With that done, let's define our robot:
device: { name: 'keyboard', driver: 'keyboard' }
connection: { name: 'keyboard', adaptor: 'keyboard' }
work: (my) ->
my.keyboard.on 'a', (key) ->
console.log "A PRESSED!"
.start() Cylon.robot({
It will have a single connection and device, both to the keyboard.
connection: { name: 'keyboard', adaptor: 'keyboard' },
device: { name: 'keyboard', driver: 'keyboard' },
When we tell this robot to work, it's going to listen to the 'a' key on the
keyboard and let us know when it's been pressed.
work: (my) function() {
my.keyboard.on('a', function(key) { console.log "A PRESSED!" });
}
With that done, let's get started!
}).start();

View File

@ -2,32 +2,34 @@
First, let's import Cylon: First, let's import Cylon:
Cylon = require '../..' var Cylon = require('../..');
Now that we have Cylon imported, we can start defining our robot Now that we have Cylon imported, we can start defining our robot
Cylon.robot Cylon.robot({
Let's define the connections and devices: Let's define the connections and devices:
connections: [ connections: [
{ name: 'leapmotion', adaptor: 'leapmotion', port: '127.0.0.1:6437' }, { name: 'leapmotion', adaptor: 'leapmotion', port: '127.0.0.1:6437' },
{ name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' } { name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' }
] ],
devices: [ devices: [
{ name: 'leapmotion', driver: 'leapmotion', connection: 'leapmotion' }, { name: 'leapmotion', driver: 'leapmotion', connection: 'leapmotion' },
{ name: 'led', driver: 'led', pin: 13, connection: 'arduino' } { name: 'led', driver: 'led', pin: 13, connection: 'arduino' }
] ],
Now that Cylon knows about the necessary hardware we're going to be using, we'll Now that Cylon knows about the necessary hardware we're going to be using, we'll
tell it what work we want to do: tell it what work we want to do:
work: (my) -> work: function(my) {
my.leapmotion.on 'frame', (frame) -> my.leapmotion.on('frame', function(frame) {
if (frame.hands.length > 0) then my.led.turnOn() else my.led.turnOff() frame.hands.length > 0 ? my.led.turnOn() : my.led.turnOff();
});
}
Now that our robot knows what work to do, and the work it will be doing that Now that our robot knows what work to do, and the work it will be doing that
hardware with, we can start it: hardware with, we can start it:
.start() }).start();

View File

@ -1,31 +1,21 @@
var Cylon = require('../..'); var Cylon = require('../..');
var bots = [ var bots = [ 'Huey', 'Dewey', 'Louie' ];
{ port: '/dev/cu.Sphero-RGB', name: 'Huey' },
{ port: '/dev/cu.Sphero-GRB', name: 'Dewey' },
{ port: '/dev/cu.Sphero-BRG', name: 'Louie' }
];
var SpheroRobot = (function() { var MinionBot = (function() {
function SpheroRobot() {} function MinionBot() {}
SpheroRobot.prototype.connection = { name: 'Sphero', adaptor: 'sphero' }; MinionBot.prototype.work = function(my) {
SpheroRobot.prototype.work = function(my) {
console.log("Robot " + my.name + " is now working!"); console.log("Robot " + my.name + " is now working!");
}; };
return SpheroRobot; return MinionBot;
})(); })();
for (var i = 0; i < bots.length; i++) { for (var i = 0; i < bots.length; i++) {
var bot = bots[i]; var robot = new MinionBot;
var robot = new SpheroRobot; robot.name = bots[i];
robot.connection.port = bot.port;
robot.name = bot.name;
Cylon.robot(robot); Cylon.robot(robot);
} }

View File

@ -5,57 +5,49 @@ master functionality works. Cylon can be fed an arbitrary number of robots, and
it will then take care of all of them, starting and stopping all of them as it will then take care of all of them, starting and stopping all of them as
needed. needed.
For this example, we're going to be using three Spheros, but you don't actually First, let's load up Cylon:
need the hardware for the example to work. Do make sure you have the
`cylon-sphero` module installed, though.
First, let's load up Cylon var Cylon = require('../..');
Cylon = require '../..'
With that in place, now we can start defining our robots. They'll all behave With that in place, now we can start defining our robots. They'll all behave
similarly, but have unique characteristics (their name and port), so let's similarly, but have unique characteristics (their name), so let's
define an array of objects to hold these differences. Later, we can instantiate define an array to hold these names for now. Later, we can instantiate
a base robot and change it's attributes as needed. a base robot and change it's attributes as needed.
bots = [ var bots = [ 'Huey', 'Dewey', 'Louie' ];
{ port: '/dev/cu.Sphero-RGB', name: 'Huey' },
{ port: '/dev/cu.Sphero-GRB', name: 'Dewey' },
{ port: '/dev/cu.Sphero-BRG', name: 'Louie' }
]
Now we can define our cleverly-named `SpheroRobot` class. This will be the base Now we can define our `MinionBot` class. This will be the base
class for all three of the robots. class for all three of the robots.
class SpheroRobot var MinionBot = (function() {
function MinionBot() {};
Every robot needs to connect to a Sphero. We're doing this using the
`cylon-sphero` adaptor.
connection: { name: 'Sphero', adaptor: 'sphero' }
We'll just give our robots some basic work so we can tell they're actually We'll just give our robots some basic work so we can tell they're actually
working: working:
work: (my) -> MinionBot.prototype.work = function(my) {
console.log "Robot #{my.name} is now working!" console.log("Robot " + my.name + " is now working!");
};
return MinionBot;
})();
And that's all we need for that. And that's all we need for that.
Next up, we'll create Cylon robots by making an instance of the `SpheroRobot` Next up, we'll create Cylon robots by making an instance of the `MinionBot`
class, and modifying the attributes that are unique to each robot. After the class, and modifying the attributes that are unique to each robot. After the
customized robot is ready, we'll feed it into Cylon. customized robot is ready, we'll feed it into Cylon.
for bot in bots for (var i = 0; i < bots.length; i++) {
robot = new SpheroRobot var robot = new MinionBot;
robot.connection.port = bot.port robot.name = bots[i];
robot.name = bot.name Cylon.robot(robot);
}
Cylon.robot robot
And now Cylon knows about all the robots we care about for this example, and And now Cylon knows about all the robots we care about for this example, and
what they do. All that's left is to start them all. The `.start()` method on what they do. All that's left is to start them all. The `.start()` method on
Cylon triggers the `.start()` command on all the robots we've told Cylon about, Cylon triggers the `.start()` command on all the robots we've told Cylon about,
so all three robots will start at once. so all three robots will start at once.
Cylon.start() Cylon.start();