Merge pull request #80 from hybridgroup/90-i2c-firmata

Updated blink m examples for firmata.
This commit is contained in:
Ron Evans 2014-01-20 13:59:28 -08:00
commit eced9ab6fe
5 changed files with 61 additions and 100 deletions

View File

@ -1,26 +1,30 @@
Cylon = require '../..'
Cylon.robot
connection: { name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' }
connection:
name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0'
device: { name: 'blinkm', driver: 'blinkm' }
device:
name: 'blinkm', driver: 'blinkm'
work: (my) ->
my.blinkm.on 'start', ->
my.blinkm.version (version) ->
console.log "Started BlinkM version #{version}"
my.blinkm.stopScript()
my.blinkm.off()
lit = false
my.blinkm.getFirmware((version) ->
Logger.info "Started BlinkM version #{version}"
)
every 1.second(), ->
if lit
lit = false
console.log 'on'
my.blinkm.rgb 0xaa, 0, 0
else
lit = true
console.log 'off'
my.blinkm.rgb 0, 0, 0
my.blinkm.goToRGB(0,0,0)
my.blinkm.getRGBColor((data) ->
console.log("Starting Color: #{ data }")
)
every 2.second(), () ->
my.blinkm.getRGBColor((data) ->
console.log("Current Color: #{ data }")
)
my.blinkm.fadeToRandomRGB(128, 128, 128)
.start()

View File

@ -7,24 +7,23 @@ Cylon.robot({
work: function(my) {
my.blinkm.on('start', function() {
my.blinkm.version(function(version) {
console.log("Started BlinkM version " + version);
my.blinkm.stopScript();
my.blinkm.getFirmware(function(version) {
Logger.info("Started BlinkM version " + version);
});
my.blinkm.off();
my.blinkm.goToRGB(0,0,0);
my.blinkm.getRGBColor(function(data){
console.log("Starting Color: #{ data }")
});
var lit = false;
every((1).second(), function() {
if (lit) {
lit = false;
console.log('on');
my.blinkm.rgb(0xaa, 0, 0);
} else {
lit = true;
console.log('off');
my.blinkm.rgb(0, 0, 0);
}
every((2).second(), function() {
my.blinkm.getRGBColor(function(data){
console.log("Current Color: #{ data }");
});
my.blinkm.fadeToRandomRGB(128, 128, 128);
});
});
}

View File

@ -13,13 +13,15 @@ With Cylon imported, we can start defining our robot.
Cylon.robot
Our robot will be using an Arduino, and communicating over the Firmata protocol:
Our robot will be using an Arduino, and communicating over the Firmata protocol
connection: { name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' }
connection:
name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0'
And we'll have one device, a BlinkM led.
device: { name: 'blinkm', driver: 'blinkm' }
device:
name: 'blinkm', driver: 'blinkm'
We'll now set up our robot's work.
@ -29,29 +31,33 @@ When the BlinkM sends the 'start' event, we'll set up our events
my.blinkm.on 'start', ->
We'll request the BlinkM's version, and print that to the console:
We stop the default BlinkM's light script
my.blinkm.version (version) ->
console.log "Started BlinkM version #{version}"
my.blinkm.stopScript()
By default, we'll turn the LED off and assign a boolean that we'll use to
determine if it's on or not:
We'll request the BlinkM's version, and print that to the console
my.blinkm.off()
lit = false
my.blinkm.getFirmware((version) ->
Logger.info "Started BlinkM version #{version}"
)
Now, every second, we'll toggle the LED, using the `lit` variable to determine
which state we need to transition the BlinkM to:
By default, we'll turn the LED off
every 1.second(), ->
if lit
lit = false
console.log 'on'
my.blinkm.rgb 0xaa, 0, 0
else
lit = true
console.log 'off'
my.blinkm.rgb 0, 0, 0
my.blinkm.goToRGB(0,0,0)
We print the default starting color (in this case 0,0,0 since we turned the led off)
my.blinkm.getRGBColor((data) ->
console.log("Starting Color: #{ data }")
)
Now, every 2 seconds, we'll change the LED color to a random value:
every 2.second(), ->
my.blinkm.getRGBColor((data) ->
console.log("Current Color: #{ data }")
)
my.blinkm.fadeToRandomRGB(128, 128, 128)
Now that our robot knows what to do, let's get started:

View File

@ -1,4 +1,4 @@
Cylon = require('..')
Cylon = require('../..')
Cylon.robot
connection:

View File

@ -1,48 +0,0 @@
Cylon = require('../..')
Cylon.robot
connection:
name: 'raspi', adaptor: 'raspi'
device:
name: 'pixel', driver: 'blinkm'
work: (my) ->
# Before you can use and work with I2C in the raspberry pi you
# need to configure it, follow the instructions to enable it here:
#
# http://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c
# We first stop the BlinkM light script
my.pixel.stopScript()
# You can pass a callback to all blinkm functions as the last param,
# If you do the command would be executed asynchronously.
# For write operations you get an (err) param passed back,
# null/undefined for success, and containing the error y any encountered.
# BlimkM Write Commands.
my.pixel.goToRGB(255, 0, 0)
my.pixel.fadeToRGB(0, 255, 0)
my.pixel.fadeToRGB(0, 0, 255)
#my.pixel.fadeToHSB(100, 180, 90)
#my.pixel.fadeToRandomRGB(0, 0, 255)
#my.pixel.fadeToRandomHSB(100, 180, 90)
#my.pixel.playLightScript(1, 0, 0)
#my.pixel.stopScript()
#my.pixel.setFadeSpeed(50)
#my.pixel.setTimeAdjust(50)
# For read commands you get (err, data) passed back to the callback,
# data contains the read data buffer, in case of Sync call (no callback)
# you get a regular return with the data buffer.
color = my.pixel.getRGBColor()
console.log(color)
# Example getting the color usinc async call and a callback
my.pixel.getRGBColor((err, data) ->
console.log(data) unless err?
)
.start()