Literate CoffeeScript version of sphero_messages example

This commit is contained in:
Andrew Stewart 2013-11-28 11:10:47 -08:00
parent 8af9a624b0
commit 791b301eda
1 changed files with 67 additions and 0 deletions

View File

@ -0,0 +1,67 @@
# Sphero Messages
For this Cylon example, we're going to hook up to a Sphero, and respond to
a number of events the Sphero adaptor is capable of emitting. Before we get
started, make sure you've got the `cylon-sphero` module installed.
To get started, let's import the Cylon module:
Cylon = require '../..'
With that set up, we can start defining our robot:
Cylon.robot
As with other Sphero examples, we'll have one connection and one device, both
the same Sphero.
connection: { name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0' }
device: { name: 'sphero', driver: 'sphero' }
And now we can start defining our robot's work.
work: (me) ->
When the Sphero's connected, we want to set up collision detection and change
it's color.
me.sphero.on 'connect', ->
Logger.info "Setting up Collision Detection..."
me.sphero.detectCollisions()
me.sphero.setRGB 0x00FF00
When the Sphero emits an 'update' event, we want to log the data it's provided
to us:
me.sphero.on 'update', (data) ->
Logger.info "Update event eventName: #{data} "
Logger.info "Update event args: "
Logger.info data
Similarly, when we get a message from the Sphero, we want to log the data, but
we'll also change it's color while we're at it.
me.sphero.on 'message', (data) ->
me.sphero.setRGB 0x0000FF
Logger.info "Message:"
Logger.info data
In the event of a collision, we want to change the color of the Sphero again, as
well as logging the data provided by the collision event.
me.sphero.on 'collision', (data) ->
me.sphero.setRGB 0xFF0000
Logger.info "Collision:"
Logger.info data
And, last but not least, when we get a notification event we want to record it's
data and change the color.
me.sphero.on 'notification', (data) ->
me.sphero.setRGB 0xFF0000
Logger.info "Notification:"
Logger.info data
And with all that done, we can finally start the robot.
.start()