From 47bc59fe3d9ee4e0d7bc8d8b31ee6c2b3c0fee4a Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Mon, 25 Nov 2013 16:06:55 -0800 Subject: [PATCH] WIP on organizing examples and making sure there are JS and CS versions of both --- examples/analog_sensor.coffee | 23 ----- examples/analog_sensor/analog_sensor.coffee | 23 +++++ examples/analog_sensor/analog_sensor.js | 28 ++++++ examples/{ => api}/api.coffee | 11 +-- examples/api/api.js | 34 +++++++ examples/blink.coffee | 15 --- examples/blink/blink.coffee | 12 +++ examples/{ => blink}/blink.js | 3 +- examples/{ => blinkm}/blinkm.coffee | 13 +-- examples/blinkm/blinkm.js | 31 ++++++ examples/button.coffee | 17 ---- examples/button.js | 12 --- examples/button/button.coffee | 15 +++ examples/button/button.js | 16 ++++ .../{ => conway_sphero}/conway_sphero.coffee | 41 ++++---- examples/conway_sphero/conway_sphero.js | 94 +++++++++++++++++++ examples/drone.coffee | 19 ---- examples/drone.js | 17 ---- examples/drone/drone.coffee | 12 +++ examples/drone/drone.js | 12 +++ examples/drone_nav.coffee | 19 ---- examples/drone_nav/drone_nav.coffee | 16 ++++ examples/drone_nav/drone_nav.js | 14 +++ examples/hello.js | 10 -- examples/{ => hello}/hello.coffee | 0 examples/hello/hello.js | 8 ++ .../led_brightness.coffee | 12 +-- examples/led_brightness/led_brightness.js | 19 ++++ examples/{ => master}/master.coffee | 6 +- examples/master/master.js | 28 ++++++ examples/motor.coffee | 20 ---- examples/motor/motor.coffee | 17 ++++ examples/motor/motor.js | 20 ++++ examples/{ => multiple}/multiple.coffee | 11 +-- examples/multiple/multiple.js | 40 ++++++++ examples/multiple_devices.coffee | 0 examples/no_such_method.coffee | 21 ----- examples/pure_gpio.coffee | 18 ---- examples/pure_gpio/pure_gpio.coffee | 14 +++ examples/pure_gpio/pure_gpio.js | 19 ++++ examples/pure_spheron.coffee | 34 ------- examples/pure_spheron/pure_spheron.coffee | 19 ++++ examples/pure_spheron/pure_spheron.js | 34 +++++++ examples/raspi_blink.coffee | 15 --- examples/raspi_blink/raspi_blink.coffee | 10 ++ examples/raspi_blink/raspi_blink.js | 10 ++ examples/raspi_button.coffee | 17 ---- examples/raspi_button/raspi_button.coffee | 14 +++ examples/raspi_button/raspi_button.js | 14 +++ examples/salesforce.coffee | 32 ------- examples/salesforce/salesforce.coffee | 31 ++++++ examples/salesforce/salesforce.js | 40 ++++++++ examples/servo.coffee | 20 ---- examples/servo/servo.coffee | 19 ++++ examples/servo/servo.js | 20 ++++ examples/sf-client.coffee | 52 ---------- examples/sf-client/sf-client.coffee | 48 ++++++++++ examples/sf-client/sf-client.js | 67 +++++++++++++ examples/sf-sphero.coffee | 68 -------------- examples/sf-sphero/sf-sphero.coffee | 67 +++++++++++++ examples/sf-sphero/sf-sphero.js | 92 ++++++++++++++++++ examples/sf-test.coffee | 29 ------ examples/sf-test/sf-test.coffee | 36 +++++++ examples/sf-test/sf-test.js | 38 ++++++++ 64 files changed, 1071 insertions(+), 515 deletions(-) delete mode 100644 examples/analog_sensor.coffee create mode 100644 examples/analog_sensor/analog_sensor.coffee create mode 100644 examples/analog_sensor/analog_sensor.js rename examples/{ => api}/api.coffee (73%) create mode 100644 examples/api/api.js delete mode 100644 examples/blink.coffee create mode 100644 examples/blink/blink.coffee rename examples/{ => blink}/blink.js (82%) rename examples/{ => blinkm}/blinkm.coffee (62%) create mode 100644 examples/blinkm/blinkm.js delete mode 100644 examples/button.coffee delete mode 100644 examples/button.js create mode 100644 examples/button/button.coffee create mode 100644 examples/button/button.js rename examples/{ => conway_sphero}/conway_sphero.coffee (55%) create mode 100644 examples/conway_sphero/conway_sphero.js delete mode 100644 examples/drone.coffee delete mode 100644 examples/drone.js create mode 100644 examples/drone/drone.coffee create mode 100644 examples/drone/drone.js delete mode 100644 examples/drone_nav.coffee create mode 100644 examples/drone_nav/drone_nav.coffee create mode 100644 examples/drone_nav/drone_nav.js delete mode 100644 examples/hello.js rename examples/{ => hello}/hello.coffee (100%) create mode 100644 examples/hello/hello.js rename examples/{ => led_brightness}/led_brightness.coffee (53%) create mode 100644 examples/led_brightness/led_brightness.js rename examples/{ => master}/master.coffee (80%) create mode 100644 examples/master/master.js delete mode 100644 examples/motor.coffee create mode 100644 examples/motor/motor.coffee create mode 100644 examples/motor/motor.js rename examples/{ => multiple}/multiple.coffee (68%) create mode 100644 examples/multiple/multiple.js delete mode 100644 examples/multiple_devices.coffee delete mode 100644 examples/no_such_method.coffee delete mode 100644 examples/pure_gpio.coffee create mode 100644 examples/pure_gpio/pure_gpio.coffee create mode 100644 examples/pure_gpio/pure_gpio.js delete mode 100644 examples/pure_spheron.coffee create mode 100644 examples/pure_spheron/pure_spheron.coffee create mode 100644 examples/pure_spheron/pure_spheron.js delete mode 100644 examples/raspi_blink.coffee create mode 100644 examples/raspi_blink/raspi_blink.coffee create mode 100644 examples/raspi_blink/raspi_blink.js delete mode 100644 examples/raspi_button.coffee create mode 100644 examples/raspi_button/raspi_button.coffee create mode 100644 examples/raspi_button/raspi_button.js delete mode 100644 examples/salesforce.coffee create mode 100644 examples/salesforce/salesforce.coffee create mode 100644 examples/salesforce/salesforce.js delete mode 100644 examples/servo.coffee create mode 100644 examples/servo/servo.coffee create mode 100644 examples/servo/servo.js delete mode 100644 examples/sf-client.coffee create mode 100644 examples/sf-client/sf-client.coffee create mode 100644 examples/sf-client/sf-client.js delete mode 100644 examples/sf-sphero.coffee create mode 100644 examples/sf-sphero/sf-sphero.coffee create mode 100644 examples/sf-sphero/sf-sphero.js delete mode 100644 examples/sf-test.coffee create mode 100644 examples/sf-test/sf-test.coffee create mode 100644 examples/sf-test/sf-test.js diff --git a/examples/analog_sensor.coffee b/examples/analog_sensor.coffee deleted file mode 100644 index 6dd3d25..0000000 --- a/examples/analog_sensor.coffee +++ /dev/null @@ -1,23 +0,0 @@ -Cylon = require('..') - -# Initialize the robot -Cylon.robot - connection: - name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' - - devices: - [ - {name: 'sensor', driver: 'analogSensor', pin: 0, upperLimit: 900, lowerLimit: 100}, - ] - - work: (my) -> - #my.sensor.on('analogRead', (val) -> - # console.log("AnalogValue ===> #{ val }") - #) - my.sensor.on('upperLimit', (val) -> - console.log("Upper limit reached ===> #{ val }") - ) - my.sensor.on('lowerLimit', (val) -> - console.log("Lower limit reached ===> #{ val }") - ) -.start() diff --git a/examples/analog_sensor/analog_sensor.coffee b/examples/analog_sensor/analog_sensor.coffee new file mode 100644 index 0000000..66ab87c --- /dev/null +++ b/examples/analog_sensor/analog_sensor.coffee @@ -0,0 +1,23 @@ +Cylon = require '../..' + +Cylon.robot + connection: + name: 'arduino' + adaptor: 'firmata' + port: '/dev/ttyACM0' + + device: + name: 'sensor' + driver: 'analogSensor' + pin: 0 + upperLimit: 900 + lowerLimit: 100 + + work: (my) -> + my.sensor.on 'upperLimit', (val) -> + Logger.info "Upper limit reached ===> #{val}" + + my.sensor.on 'lowerLimit', (val) -> + Logger.info "Lower limit reached ===> #{val}" + +.start() diff --git a/examples/analog_sensor/analog_sensor.js b/examples/analog_sensor/analog_sensor.js new file mode 100644 index 0000000..c41a965 --- /dev/null +++ b/examples/analog_sensor/analog_sensor.js @@ -0,0 +1,28 @@ +var Cylon = require('../..'); + +Cylon.robot({ + connection: { + name: 'arduino', + adaptor: 'firmata', + port: '/dev/ttyACM0' + }, + + device: { + name: 'sensor', + driver: 'analogSensor', + pin: 0, + upperLimit: 900, + lowerLimit: 100 + }, + + work: function(my) { + my.sensor.on('upperLimit', function(val) { + Logger.info("Upper limit reached ===> " + val); + }); + + my.sensor.on('lowerLimit', function(val) { + Logger.info("Lower limit reached ===> " + val); + }); + } + +}).start(); diff --git a/examples/api.coffee b/examples/api/api.coffee similarity index 73% rename from examples/api.coffee rename to examples/api/api.coffee index a202965..0160428 100644 --- a/examples/api.coffee +++ b/examples/api/api.coffee @@ -1,4 +1,4 @@ -Cylon = require '..' +Cylon = require '../..' Cylon.api host: '0.0.0.0', port: '8080' @@ -8,11 +8,9 @@ bots = [ ] SpheroRobot = - connection: - name: 'Sphero', adaptor: 'sphero' + connection: { name: 'Sphero', adaptor: 'sphero' } - device: - name: 'sphero', driver: 'sphero' + device: { name: 'sphero', driver: 'sphero' } work: (me) -> every 1.seconds(), -> @@ -21,9 +19,10 @@ SpheroRobot = me.sphero.roll 60, Math.floor(Math.random() * 360) for bot in bots - robot = Object.create(SpheroRobot) + robot = Object.create SpheroRobot robot.connection.port = bot.port robot.name = bot.name + Cylon.robot robot Cylon.start() diff --git a/examples/api/api.js b/examples/api/api.js new file mode 100644 index 0000000..c35e074 --- /dev/null +++ b/examples/api/api.js @@ -0,0 +1,34 @@ +var Cylon = require('../..'); + +Cylon.api({ host: '0.0.0.0', port: '8080' }); + +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(me) { + every((1).seconds(), function() { + Logger.info(me.name); + me.sphero.setRGB(Math.floor(Math.random() * 100000)); + return me.sphero.roll(60, Math.floor(Math.random() * 360)); + }); + } +}; + +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); +} + +Cylon.start(); diff --git a/examples/blink.coffee b/examples/blink.coffee deleted file mode 100644 index c42727d..0000000 --- a/examples/blink.coffee +++ /dev/null @@ -1,15 +0,0 @@ -Cylon = require('..') - -# Initialize the robot -Cylon.robot - connection: - name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' - - device: - name: 'led', driver: 'led', pin: 13 - - work: (my) -> - # we do our thing here - every 1.second(), -> my.led.toggle() - -.start() diff --git a/examples/blink/blink.coffee b/examples/blink/blink.coffee new file mode 100644 index 0000000..77e0fe1 --- /dev/null +++ b/examples/blink/blink.coffee @@ -0,0 +1,12 @@ +Cylon = require '../..' + +# Initialize the robot +Cylon.robot + connection: { name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' } + + device: { name: 'led', driver: 'led', pin: 13 } + + work: (my) -> + every 1.second(), -> my.led.toggle() + +.start() diff --git a/examples/blink.js b/examples/blink/blink.js similarity index 82% rename from examples/blink.js rename to examples/blink/blink.js index 8b81ea3..8cf9712 100644 --- a/examples/blink.js +++ b/examples/blink/blink.js @@ -1,6 +1,5 @@ -var Cylon = require('..'); +var Cylon = require('../..'); -// Initialize the robot Cylon.robot({ connection: { name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' }, device: {name: 'led', driver: 'led', pin: 13}, diff --git a/examples/blinkm.coffee b/examples/blinkm/blinkm.coffee similarity index 62% rename from examples/blinkm.coffee rename to examples/blinkm/blinkm.coffee index 04b0502..a5b2c68 100644 --- a/examples/blinkm.coffee +++ b/examples/blinkm/blinkm.coffee @@ -1,21 +1,18 @@ -Cylon = require('..') +Cylon = require '../..' -# Initialize the robot 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) -> - # we do our thing here my.blinkm.on 'start', -> my.blinkm.version (version) -> Logger.info "Started BlinkM version #{version}" my.blinkm.off() lit = false + every 1.second(), -> if lit lit = false @@ -24,6 +21,6 @@ Cylon.robot else lit = true Logger.info 'off' - my.blinkm.rgb 0, 0, 0 + my.blinkm.rgb 0, 0, 0 .start() diff --git a/examples/blinkm/blinkm.js b/examples/blinkm/blinkm.js new file mode 100644 index 0000000..b4b0d65 --- /dev/null +++ b/examples/blinkm/blinkm.js @@ -0,0 +1,31 @@ +var Cylon = require('../..'); + +Cylon.robot({ + connection: { name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' }, + + device: { name: 'blinkm', driver: 'blinkm' }, + + work: function(my) { + my.blinkm.on('start', function() { + my.blinkm.version(function(version) { + Logger.info("Started BlinkM version " + version); + }); + + my.blinkm.off(); + + var lit = false; + + every((1).second(), function() { + if (lit) { + lit = false; + Logger.info('on'); + my.blinkm.rgb(0xaa, 0, 0); + } else { + lit = true; + Logger.info('off'); + my.blinkm.rgb(0, 0, 0); + } + }); + }); + } +}).start(); diff --git a/examples/button.coffee b/examples/button.coffee deleted file mode 100644 index 6945483..0000000 --- a/examples/button.coffee +++ /dev/null @@ -1,17 +0,0 @@ -Cylon = require('..') - -# Initialize the robot -Cylon.robot - connection: - name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' - - devices: - [ - {name: 'led', driver: 'led', pin: 13}, - {name: 'button', driver: 'button', pin: 2} - ] - - work: (my) -> - my.button.on 'push', -> my.led.toggle() - -.start() diff --git a/examples/button.js b/examples/button.js deleted file mode 100644 index a86b8fb..0000000 --- a/examples/button.js +++ /dev/null @@ -1,12 +0,0 @@ -var Cylon = require('..'); - -// Initialize the robot -Cylon.robot({ - connection: { name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' }, - devices: [{name: 'led', driver: 'led', pin: 13}, - {name: 'button', driver: 'button', pin: 2}], - - work: function(my) { - my.button.on('push', function() {my.led.toggle()}); - } -}).start(); diff --git a/examples/button/button.coffee b/examples/button/button.coffee new file mode 100644 index 0000000..daee6bf --- /dev/null +++ b/examples/button/button.coffee @@ -0,0 +1,15 @@ +Cylon = require('../..') + +Cylon.robot + connection: + name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' + + devices: [ + { name: 'led', driver: 'led', pin: 13 }, + { name: 'button', driver: 'button', pin: 2 } + ] + + work: (my) -> + my.button.on 'push', -> my.led.toggle() + +.start() diff --git a/examples/button/button.js b/examples/button/button.js new file mode 100644 index 0000000..885eff0 --- /dev/null +++ b/examples/button/button.js @@ -0,0 +1,16 @@ +var Cylon = require('../..'); + +Cylon.robot({ + connection: { name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' }, + + devices: [ + { name: 'led', driver: 'led', pin: 13 }, + { name: 'button', driver: 'button', pin: 2 } + ], + + work: function(my) { + my.button.on('push', function() { + my.led.toggle(); + }); + } +}).start(); diff --git a/examples/conway_sphero.coffee b/examples/conway_sphero/conway_sphero.coffee similarity index 55% rename from examples/conway_sphero.coffee rename to examples/conway_sphero/conway_sphero.coffee index c31a1fa..f90d980 100644 --- a/examples/conway_sphero.coffee +++ b/examples/conway_sphero/conway_sphero.coffee @@ -1,53 +1,49 @@ -Cylon = require '..' +Cylon = require '../..' bots = [ { port: '/dev/rfcomm0', name: 'Thelma' } -# { port: '/dev/rfcomm1', name: 'Louise' }, -# { port: '/dev/rfcomm2', name: 'Grace' }, -# { port: '/dev/rfcomm3', name: 'Ada' } + { port: '/dev/rfcomm1', name: 'Louise' }, + { port: '/dev/rfcomm2', name: 'Grace' }, + { port: '/dev/rfcomm3', name: 'Ada' } ] Green = 0x0000FF Red = 0xFF0000 class ConwayRobot - connection: - name: 'Sphero', adaptor: 'sphero' - - device: - name: 'sphero', driver: 'sphero' + connection: { name: 'Sphero', adaptor: 'sphero' } + device: { name: 'sphero', driver: 'sphero' } born: -> @contacts = 0 @age = 0 - this.life() - this.move() + @life() + @move() move: -> - this.sphero.roll 60, Math.floor(Math.random() * 360) + @sphero.roll 60, Math.floor(Math.random() * 360) life: -> @alive = true - this.sphero.setRGB Green + @sphero.setRGB Green death: -> @alive = false - this.sphero.setRGB Green - this.sphero.stop() + @sphero.setRGB Green + @sphero.stop() enoughContacts: -> - if @contacts >= 2 and @contacts < 7 - true - else - false + if @contacts >= 2 and @contacts < 7 then true else false birthday: -> @age += 1 + Logger.info "Happy birthday, #{@name}. You are #{@age} and had #{@contacts} contacts." - if this.enoughContacts() - this.rebirth() if not @alive? + + if @enoughContacts() + @rebirth() if not @alive? else - this.death() + @death() @contacts = 0 @@ -63,7 +59,6 @@ class ConwayRobot every 10.seconds(), -> me.birthday() if me.alive? - for bot in bots robot = new ConwayRobot robot.connection.port = bot.port diff --git a/examples/conway_sphero/conway_sphero.js b/examples/conway_sphero/conway_sphero.js new file mode 100644 index 0000000..c89fd83 --- /dev/null +++ b/examples/conway_sphero/conway_sphero.js @@ -0,0 +1,94 @@ +var ConwayRobot, Cylon, Green, Red, bot, bots, robot, _i, _len; + +var Cylon = require('../..'); + +var bots = [ + { port: '/dev/rfcomm0', name: 'Thelma' }, + { port: '/dev/rfcomm1', name: 'Louise' }, + { port: '/dev/rfcomm2', name: 'Grace' }, + { port: '/dev/rfcomm3', name: 'Ada' } +]; + +var Green = 0x0000FF; +var Red = 0xFF0000; + +var ConwayRobot = (function() { + function ConwayRobot() {} + + ConwayRobot.prototype.connection = { name: 'Sphero', adaptor: 'sphero' }; + ConwayRobot.prototype.device = { name: 'sphero', driver: 'sphero' }; + + ConwayRobot.prototype.born = function() { + this.contacts = 0; + this.age = 0; + this.life(); + return this.move(); + }; + + ConwayRobot.prototype.move = function() { + return this.sphero.roll(60, Math.floor(Math.random() * 360)); + }; + + ConwayRobot.prototype.life = function() { + this.alive = true; + return this.sphero.setRGB(Green); + }; + + ConwayRobot.prototype.death = function() { + this.alive = false; + this.sphero.setRGB(Green); + return this.sphero.stop(); + }; + + ConwayRobot.prototype.enoughContacts = function() { + if (this.contacts >= 2 && this.contacts < 7) { + return true; + } else { + return false; + } + }; + + ConwayRobot.prototype.birthday = function() { + this.age += 1; + + Logger.info("Happy birthday, " + this.name + ". You are " + this.age + " and had " + this.contacts + " contacts."); + + if (this.enoughContacts()) { + if (this.alive == null) { this.rebirth(); } + } else { + this.death(); + } + return this.contacts = 0; + }; + + ConwayRobot.prototype.work = function(me) { + me.born(); + + me.sphero.on('collision', function() { + return this.contacts += 1; + }); + + every((3).seconds(), function() { + if (me.alive != null) { return me.move(); } + }); + + return every((10).seconds(), function() { + if (me.alive != null) { return me.birthday(); } + }); + }; + + return ConwayRobot; + +})(); + +for (var i = 0; i < bots.length; i++) { + var bot = bots[i]; + var robot = new ConwayRobot; + + robot.connection.port = bot.port; + robot.name = bot.name; + + Cylon.robot(robot); +} + +Cylon.start(); diff --git a/examples/drone.coffee b/examples/drone.coffee deleted file mode 100644 index ccfb3c3..0000000 --- a/examples/drone.coffee +++ /dev/null @@ -1,19 +0,0 @@ -Cylon = require('..') - -Cylon.robot - connection: - name: 'ardrone', adaptor: 'ardrone', port: '192.168.1.1' - - device: - name: 'drone', driver: 'ardrone' - - work: (my) -> - my.drone.takeoff() - - after 10.seconds(), -> - my.drone.land() - - after 15.seconds(), -> - my.drone.stop() - -.start() diff --git a/examples/drone.js b/examples/drone.js deleted file mode 100644 index 913b2ce..0000000 --- a/examples/drone.js +++ /dev/null @@ -1,17 +0,0 @@ -var Cylon = require('..'); - -Cylon.robot({ - connection: {name: 'ardrone', adaptor: 'ardrone', port: '192.168.1.1'}, - device: {name: 'drone', driver: 'ardrone'}, - work: function(my) { - my.drone.takeoff(); - - after((10).seconds(), function() { - my.drone.land(); - } - - after((15).seconds(), function() { - my.drone.stop(); - } - } -}).start(); diff --git a/examples/drone/drone.coffee b/examples/drone/drone.coffee new file mode 100644 index 0000000..bd3ed6f --- /dev/null +++ b/examples/drone/drone.coffee @@ -0,0 +1,12 @@ +Cylon = require '../..' + +Cylon.robot + connection: { name: 'ardrone', adaptor: 'ardrone', port: '192.168.1.1' } + device: { name: 'drone', driver: 'ardrone' } + + work: (my) -> + my.drone.takeoff() + after 10.seconds(), -> my.drone.land() + after 15.seconds(), -> my.drone.stop() + +.start() diff --git a/examples/drone/drone.js b/examples/drone/drone.js new file mode 100644 index 0000000..a5278b2 --- /dev/null +++ b/examples/drone/drone.js @@ -0,0 +1,12 @@ +var Cylon = require('../..'); + +Cylon.robot({ + connection: { name: 'ardrone', adaptor: 'ardrone', port: '192.168.1.1' }, + device: { name: 'drone', driver: 'ardrone' }, + + work: function(my) { + my.drone.takeoff(); + after(10..seconds(), function() { my.drone.land(); }); + after(15..seconds(), function() { my.drone.stop(); }); + } +}).start(); diff --git a/examples/drone_nav.coffee b/examples/drone_nav.coffee deleted file mode 100644 index b67f90e..0000000 --- a/examples/drone_nav.coffee +++ /dev/null @@ -1,19 +0,0 @@ -Cylon = require('..') - -Cylon.robot - connection: - name: 'ardrone', adaptor: 'ardrone', port: '192.168.1.1' - - devices: - [ - {name: 'drone', driver: 'ardrone'}, - {name: 'nav', driver: 'ardroneNav'} - ] - - work: (my) -> - my.drone.config('general:navdata_demo', 'TRUE') - - my.nav.on 'update', (data) -> - Logger.info data - -.start() diff --git a/examples/drone_nav/drone_nav.coffee b/examples/drone_nav/drone_nav.coffee new file mode 100644 index 0000000..232c0f7 --- /dev/null +++ b/examples/drone_nav/drone_nav.coffee @@ -0,0 +1,16 @@ +Cylon = require '../..' + +Cylon.robot + connection: + name: 'ardrone', adaptor: 'ardrone', port: '192.168.1.1' + + devices: [ + {name: 'drone', driver: 'ardrone'}, + {name: 'nav', driver: 'ardroneNav'} + ] + + work: (my) -> + my.drone.config 'general:navdata_demo', 'TRUE' + my.nav.on 'update', (data) -> Logger.info data + +.start() diff --git a/examples/drone_nav/drone_nav.js b/examples/drone_nav/drone_nav.js new file mode 100644 index 0000000..ebebde3 --- /dev/null +++ b/examples/drone_nav/drone_nav.js @@ -0,0 +1,14 @@ +var Cylon = require('../..'); + +Cylon.robot({ + connection: { name: 'ardrone', adaptor: 'ardrone', port: '192.168.1.1' }, + devices: [ + { name: 'drone', driver: 'ardrone' }, + { name: 'nav', driver: 'ardroneNav' } + ], + + work: function(my) { + my.drone.config('general:navdata_demo', 'TRUE'); + my.nav.on('update', function(data) { Logger.info(data); }); + } +}).start(); diff --git a/examples/hello.js b/examples/hello.js deleted file mode 100644 index 69c4c55..0000000 --- a/examples/hello.js +++ /dev/null @@ -1,10 +0,0 @@ -var Cylon = require('..'); - -Cylon.robot({ - connection: { name: 'looped', adaptor: 'loopback'}, - - work: function() { - every((1).second(), function() { Logger.info("Hello, human!"); }); - after((10).seconds(), function() { Logger.info("Impressive."); }); - } -}).start(); diff --git a/examples/hello.coffee b/examples/hello/hello.coffee similarity index 100% rename from examples/hello.coffee rename to examples/hello/hello.coffee diff --git a/examples/hello/hello.js b/examples/hello/hello.js new file mode 100644 index 0000000..7c2ab85 --- /dev/null +++ b/examples/hello/hello.js @@ -0,0 +1,8 @@ +var Cylon = require('../..'); + +Cylon.robot({ + work: function() { + every(1..second(), function() { Logger.info("Hello, human!"); }); + after(10..seconds(), function() { Logger.info("Impressive."); }); + } +}).start(); diff --git a/examples/led_brightness.coffee b/examples/led_brightness/led_brightness.coffee similarity index 53% rename from examples/led_brightness.coffee rename to examples/led_brightness/led_brightness.coffee index 6eeff84..5f85d93 100644 --- a/examples/led_brightness.coffee +++ b/examples/led_brightness/led_brightness.coffee @@ -1,17 +1,13 @@ -Cylon = require('..') +Cylon = require '../..' -# Initialize the robot Cylon.robot - connection: - name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' - - device: - name: 'led', driver: 'led', pin: 3 + connection: { name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' } + device: { name: 'led', driver: 'led', pin: 3 } work: (my) -> - # we do our thing here brightness = 0 fade = 5 + every 0.05.seconds(), -> brightness += fade my.led.brightness(brightness) diff --git a/examples/led_brightness/led_brightness.js b/examples/led_brightness/led_brightness.js new file mode 100644 index 0000000..2e709ad --- /dev/null +++ b/examples/led_brightness/led_brightness.js @@ -0,0 +1,19 @@ +var Cylon = require('../..'); + +Cylon.robot({ + connection: { name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' }, + device: { name: 'led', driver: 'led', pin: 3 }, + + work: function(my) { + var brightness = 0; + var fade = 5; + + every(0.05.seconds(), function() { + brightness += fade; + my.led.brightness(brightness); + if ((brightness === 0) || (brightness === 255)) { + fade = -fade; + } + }); + } +}).start(); diff --git a/examples/master.coffee b/examples/master/master.coffee similarity index 80% rename from examples/master.coffee rename to examples/master/master.coffee index e678fd2..cb814a9 100644 --- a/examples/master.coffee +++ b/examples/master/master.coffee @@ -1,4 +1,4 @@ -Cylon = require '..' +Cylon = require '../..' bots = [ { port: '/dev/cu.Sphero-RGB', name: 'Huey' }, @@ -10,8 +10,8 @@ SpheroRobot = connection: name: 'Sphero', adaptor: 'sphero' - work: (self) -> - console.log "Robot #{self.name} is now working!" + work: (my) -> + Logger.info "Robot #{my.name} is now working!" for bot in bots robot = Object.create(SpheroRobot) diff --git a/examples/master/master.js b/examples/master/master.js new file mode 100644 index 0000000..fb08607 --- /dev/null +++ b/examples/master/master.js @@ -0,0 +1,28 @@ +// Generated by CoffeeScript 1.6.3 +var Cylon = require('../..'); + +var bots = [ + { port: '/dev/cu.Sphero-RGB', name: 'Huey' }, + { port: '/dev/cu.Sphero-GRB', name: 'Dewey' }, + { port: '/dev/cu.Sphero-BRG', name: 'Louie' } +]; + +var SpheroRobot = { + connection: { name: 'Sphero', adaptor: 'sphero' }, + + work: function(my) { + Logger.info("Robot " + my.name + " is now working!"); + } +}; + +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); +} + +Cylon.start(); diff --git a/examples/motor.coffee b/examples/motor.coffee deleted file mode 100644 index 7c8ecb3..0000000 --- a/examples/motor.coffee +++ /dev/null @@ -1,20 +0,0 @@ -Cylon = require('..') - -# Initialize the robot -Cylon.robot - connection: - name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' - - device: - name: 'motor', driver: 'motor', pin: 3 - - work: (my) -> - speed = 0 - increment = 5 - every 0.05.seconds(), -> - speed += increment - my.motor.speed(speed) - console.log("current speed => #{ my.motor.currentSpeed() }") - increment = -increment if (speed is 0) or (speed is 255) - -.start() diff --git a/examples/motor/motor.coffee b/examples/motor/motor.coffee new file mode 100644 index 0000000..ed50237 --- /dev/null +++ b/examples/motor/motor.coffee @@ -0,0 +1,17 @@ +Cylon = require '../..' + +Cylon.robot + connection: { name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' } + device: { name: 'motor', driver: 'motor', pin: 3 } + + work: (my) -> + speed = 0 + increment = 5 + + every 0.05.seconds(), -> + speed += increment + my.motor.speed(speed) + Logger.info "Current Speed: #{my.motor.currentSpeed() }" + increment = -increment if (speed is 0) or (speed is 255) + +.start() diff --git a/examples/motor/motor.js b/examples/motor/motor.js new file mode 100644 index 0000000..6680179 --- /dev/null +++ b/examples/motor/motor.js @@ -0,0 +1,20 @@ +var Cylon = require('../..'); + +Cylon.robot({ + connection: { name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' }, + device: { name: 'motor', driver: 'motor', pin: 3 }, + + work: function(my) { + var speed = 0; + var increment = 5; + + every(0.05.seconds(), function() { + speed += increment; + my.motor.speed(speed); + + Logger.info("Current Speed: " + (my.motor.currentSpeed())); + + if ((speed === 0) || (speed === 255)) { increment = -increment; } + }); + } +}).start(); diff --git a/examples/multiple.coffee b/examples/multiple/multiple.coffee similarity index 68% rename from examples/multiple.coffee rename to examples/multiple/multiple.coffee index c85c0c6..3602d73 100644 --- a/examples/multiple.coffee +++ b/examples/multiple/multiple.coffee @@ -1,4 +1,4 @@ -Cylon = require '..' +Cylon = require '../..' bots = [ { name: 'Huey' }, @@ -7,18 +7,15 @@ bots = [ ] class ChattyRobot - connection: - name: 'loopback', adaptor: 'loopback' - - device: - name: 'ping', driver: 'ping' + connection: { name: 'loopback', adaptor: 'loopback' } + device: { name: 'ping', driver: 'ping' } hello: (my) -> Logger.info "#{my.name}: #{my.ping.ping()}" work: (my) -> every 1.seconds(), -> - my.hello(my) + my.hello(my) for bot in bots robot = new ChattyRobot diff --git a/examples/multiple/multiple.js b/examples/multiple/multiple.js new file mode 100644 index 0000000..d35f280 --- /dev/null +++ b/examples/multiple/multiple.js @@ -0,0 +1,40 @@ +var bot, bots, robot, _i, _len; + +var Cylon = require('../..'); + +bots = [ + { name: 'Huey' }, + { name: 'Dewey' }, + { name: 'Louie' } +]; + +var ChattyRobot = (function() { + function ChattyRobot() {} + + ChattyRobot.prototype.connection = { name: 'loopback', adaptor: 'loopback' }; + ChattyRobot.prototype.device = { name: 'ping', driver: 'ping' }; + + ChattyRobot.prototype.hello = function(my) { + Logger.info("" + my.name + ": " + (my.ping.ping())); + }; + + ChattyRobot.prototype.work = function(my) { + every((1).seconds(), function() { + my.hello(my); + }); + }; + + return ChattyRobot; + +})(); + +for (var i = 0; i < bots.length; i++) { + var bot = bots[i]; + var robot = new ChattyRobot; + + robot.name = bot.name; + + Cylon.robot(robot); +} + +Cylon.start(); diff --git a/examples/multiple_devices.coffee b/examples/multiple_devices.coffee deleted file mode 100644 index e69de29..0000000 diff --git a/examples/no_such_method.coffee b/examples/no_such_method.coffee deleted file mode 100644 index cbceebc..0000000 --- a/examples/no_such_method.coffee +++ /dev/null @@ -1,21 +0,0 @@ -class Person - constructor: (args = {}) -> - @name = args.name - @age = args.age - - noSuchMethod: (args) -> - console.log("You called method #{ args[0] } with params #{ args }, no such method exist") - - sayHi: (var1, var2, var3) -> - console.log("Saying Hi! #{var1}, #{var2}, #{var3}") - - execute: () -> - if typeof(this[arguments[0]]) is 'function' - this[arguments[0]].apply(this, Array.prototype.slice.call(arguments, 1)) - else - this.noSuchMethod(arguments) - -person = new Person({name: 'Leonidas', age:123}) - -person.execute('sayHi', 1, 2, 3) -person.execute('sayHello', 1, 2, 3) diff --git a/examples/pure_gpio.coffee b/examples/pure_gpio.coffee deleted file mode 100644 index 0c44e9c..0000000 --- a/examples/pure_gpio.coffee +++ /dev/null @@ -1,18 +0,0 @@ -require('../dist/digital-pin') - -pin4 = new Cylon.IO.DigitalPin(pin: 4, mode: 'w') - -pin4.on('open', (data) -> - console.log("Pin files have been created") -) - -pin4.on('digitalWrite', (value) -> - console.log("Value writen to pin -> #{ value }") -) - -pin4.on('connect', (data) -> - console.log("Pin mode has been setup!") - pin4.setHigh() -) - -pin4.connect() diff --git a/examples/pure_gpio/pure_gpio.coffee b/examples/pure_gpio/pure_gpio.coffee new file mode 100644 index 0000000..771bfff --- /dev/null +++ b/examples/pure_gpio/pure_gpio.coffee @@ -0,0 +1,14 @@ +require '../../dist/digital-pin' + +pin4 = new Cylon.IO.DigitalPin(pin: 4, mode: 'w') + +pin4.on 'open', (data) -> console.log "Pin files have been created" + +pin4.on 'digitalWrite', (value) -> + console.log "Value written to pin: #{value}" + +pin4.on 'connect', (data) -> + console.log "Pin mode has been setup!" + pin4.setHigh() + +pin4.connect() diff --git a/examples/pure_gpio/pure_gpio.js b/examples/pure_gpio/pure_gpio.js new file mode 100644 index 0000000..088c72b --- /dev/null +++ b/examples/pure_gpio/pure_gpio.js @@ -0,0 +1,19 @@ +// Generated by CoffeeScript 1.6.3 +require('../../dist/digital-pin'); + +var pin4 = new Cylon.IO.DigitalPin({ pin: 4, mode: 'w' }); + +pin4.on('open', function(data) { + console.log("Pin files have been created"); +}); + +pin4.on('digitalWrite', function(value) { + console.log("Value written to pin: " + value); +}); + +pin4.on('connect', function(data) { + console.log("Pin mode has been setup!"); + pin4.setHigh(); +}); + +pin4.connect(); diff --git a/examples/pure_spheron.coffee b/examples/pure_spheron.coffee deleted file mode 100644 index b8fc28c..0000000 --- a/examples/pure_spheron.coffee +++ /dev/null @@ -1,34 +0,0 @@ -spheron = require('spheron') -sphero = spheron.sphero() -spheroPort = '/dev/rfcomm0' -COLORS = spheron.toolbelt.COLORS - -sphero.on('open', -> - console.log('EVENT OPEN!') - sphero.configureCollisionDetection(0x01, 0x20, 0x20, 0x20, 0x20, 0x50) - sphero.setRGB(COLORS.GREEN, false) -) - -sphero.on('close', -> - console.log('EVENT CLOSE!') -) - -sphero.on('end', -> - console.log('EVENT END!') -) - -sphero.on('error', -> - console.log('EVENT ERROR!') -) - -sphero.on('notification', (packet) -> - console.log('Packet contents:') - console.log(packet) -) - -sphero.on('message', (packet) -> - console.log('Packet contents:') - console.log(packet) -) - -sphero.open(spheroPort) diff --git a/examples/pure_spheron/pure_spheron.coffee b/examples/pure_spheron/pure_spheron.coffee new file mode 100644 index 0000000..0abb536 --- /dev/null +++ b/examples/pure_spheron/pure_spheron.coffee @@ -0,0 +1,19 @@ +spheron = require('spheron') + +sphero = spheron.sphero() +spheroPort = '/dev/rfcomm0' + +COLORS = spheron.toolbelt.COLORS + +sphero.on 'open', -> + console.log 'EVENT OPEN!' + sphero.configureCollisionDetection 0x01, 0x20, 0x20, 0x20, 0x20, 0x50 + sphero.setRGB COLORS.GREEN, false + +sphero.on 'close', -> console.log 'EVENT CLOSE!' +sphero.on 'end', -> console.log 'EVENT END!' +sphero.on 'error', -> console.log 'EVENT ERROR!' +sphero.on 'notification', (packet) -> console.log "Packet contents: #{packet}" +sphero.on 'message', (packet) -> console.log "Packet contents: #{packet}" + +sphero.open spheroPort diff --git a/examples/pure_spheron/pure_spheron.js b/examples/pure_spheron/pure_spheron.js new file mode 100644 index 0000000..ddb54f1 --- /dev/null +++ b/examples/pure_spheron/pure_spheron.js @@ -0,0 +1,34 @@ +var spheron = require('spheron'); + +var sphero = spheron.sphero(); +var spheroPort = '/dev/rfcomm0'; + +var COLORS = spheron.toolbelt.COLORS; + +sphero.on('open', function() { + console.log('EVENT OPEN!'); + sphero.configureCollisionDetection(0x01, 0x20, 0x20, 0x20, 0x20, 0x50); + sphero.setRGB(COLORS.GREEN, false); +}); + +sphero.on('close', function() { + console.log('EVENT CLOSE!'); +}); + +sphero.on('end', function() { + console.log('EVENT END!'); +}); + +sphero.on('error', function() { + console.log('EVENT ERROR!'); +}); + +sphero.on('notification', function(packet) { + console.log("Packet contents: " + packet); +}); + +sphero.on('message', function(packet) { + console.log("Packet contents: " + packet); +}); + +sphero.open(spheroPort); diff --git a/examples/raspi_blink.coffee b/examples/raspi_blink.coffee deleted file mode 100644 index 9a4b922..0000000 --- a/examples/raspi_blink.coffee +++ /dev/null @@ -1,15 +0,0 @@ -Cylon = require('..') - -# Initialize the robot -Cylon.robot - connection: - name: 'raspi', adaptor: 'raspi' - - device: - name: 'led', driver: 'led', pin: 11 - - work: (my) -> - # we do our thing here - every 1.second(), -> my.led.toggle() - -.start() diff --git a/examples/raspi_blink/raspi_blink.coffee b/examples/raspi_blink/raspi_blink.coffee new file mode 100644 index 0000000..d8a9377 --- /dev/null +++ b/examples/raspi_blink/raspi_blink.coffee @@ -0,0 +1,10 @@ +Cylon = require '../..' + +Cylon.robot + connection: { name: 'raspi', adaptor: 'raspi' } + device: { name: 'led', driver: 'led', pin: 11 } + + work: (my) -> + every 1.second(), -> my.led.toggle() + +.start() diff --git a/examples/raspi_blink/raspi_blink.js b/examples/raspi_blink/raspi_blink.js new file mode 100644 index 0000000..1fe9211 --- /dev/null +++ b/examples/raspi_blink/raspi_blink.js @@ -0,0 +1,10 @@ +var Cylon = require('../..'); + +Cylon.robot({ + connection: { name: 'raspi', adaptor: 'raspi' }, + device: { name: 'led', driver: 'led', pin: 11 }, + + work: function(my) { + every((1).second(), function() { my.led.toggle(); }); + } +}).start(); diff --git a/examples/raspi_button.coffee b/examples/raspi_button.coffee deleted file mode 100644 index 5983b5c..0000000 --- a/examples/raspi_button.coffee +++ /dev/null @@ -1,17 +0,0 @@ -Cylon = require('..') - -# Initialize the robot -Cylon.robot - connection: - name: 'raspi', adaptor: 'raspi' - - devices: - [ - {name: 'led', driver: 'led', pin: 11}, - {name: 'button', driver: 'button', pin: 7} - ] - - work: (my) -> - my.button.on 'push', () -> my.led.toggle() - -.start() diff --git a/examples/raspi_button/raspi_button.coffee b/examples/raspi_button/raspi_button.coffee new file mode 100644 index 0000000..af01d16 --- /dev/null +++ b/examples/raspi_button/raspi_button.coffee @@ -0,0 +1,14 @@ +Cylon = require '../..' + +Cylon.robot + connection: { name: 'raspi', adaptor: 'raspi' } + + devices: [ + {name: 'led', driver: 'led', pin: 11}, + {name: 'button', driver: 'button', pin: 7} + ] + + work: (my) -> + my.button.on 'push', -> my.led.toggle() + +.start() diff --git a/examples/raspi_button/raspi_button.js b/examples/raspi_button/raspi_button.js new file mode 100644 index 0000000..6465949 --- /dev/null +++ b/examples/raspi_button/raspi_button.js @@ -0,0 +1,14 @@ +var Cylon = require('../..'); + +Cylon.robot({ + connection: { name: 'raspi', adaptor: 'raspi' }, + + devices: [ + { name: 'led', driver: 'led', pin: 11 }, + { name: 'button', driver: 'button', pin: 7 } + ], + + work: function(my) { + my.button.on('push', function() { my.led.toggle(); }); + } +}).start(); diff --git a/examples/salesforce.coffee b/examples/salesforce.coffee deleted file mode 100644 index 4e2b48a..0000000 --- a/examples/salesforce.coffee +++ /dev/null @@ -1,32 +0,0 @@ -Cylon = require('..') - -Cylon.robot - 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' - - work: (me) -> - - me.salesforce.on('start', () -> - me.salesforce.subscribe('/topic/SpheroMsgOutbound', (data) -> - Logger.info "Sphero: #{ data.sobject.Sphero_Name__c }, Bucks: #{ data.sobject.Bucks__c }, SM_Id: #{ data.sobject.Id }" - ) - ) - - i = 0 - every 2.seconds(), () -> - # push(apexPath, method, body) - toSend = "{ \"spheroName\" :\"#{ me.name }\", \"bucks\": \"#{ i }\" }" - me.salesforce.push('SpheroController', 'POST', toSend) - -.start() diff --git a/examples/salesforce/salesforce.coffee b/examples/salesforce/salesforce.coffee new file mode 100644 index 0000000..ff8753d --- /dev/null +++ b/examples/salesforce/salesforce.coffee @@ -0,0 +1,31 @@ +Cylon = require '../..' + +Cylon.robot + 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' } + + work: (me) -> + me.salesforce.on 'start', () -> + me.salesforce.subscribe '/topic/SpheroMsgOutbound', (data) -> + msg = "Sphero: #{data.sobject.Sphero_Name__c}," + msg += "Bucks: #{data.sobject.Bucks__c}," + msg += "SM_Id: #{data.sobject.Id}" + + Logger.info msg + + i = 0 + + every 2.seconds(), () -> + data = JSON.stringify { spheroName: "#{me.name}", bucks: "#{i}" } + me.salesforce.push 'SpheroController', 'POST', data + +.start() diff --git a/examples/salesforce/salesforce.js b/examples/salesforce/salesforce.js new file mode 100644 index 0000000..776d9fd --- /dev/null +++ b/examples/salesforce/salesforce.js @@ -0,0 +1,40 @@ +var Cylon = require('../..'); + +Cylon.robot({ + 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' }, + + work: function(me) { + me.salesforce.on('start', function() { + me.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) { + var msg = "Sphero: " + data.sobject.Sphero_Name__c + ","; + msg += "Bucks: " + data.sobject.Bucks__c + ","; + msg += "SM_Id: " + data.sobject.Id; + + Logger.info(msg); + }); + }); + + var i = 0; + + every((2).seconds(), function() { + var data = JSON.stringify({ + spheroName: "" + me.name, + bucks: "" + i + }); + + me.salesforce.push('SpheroController', 'POST', data); + }); + } +}).start(); diff --git a/examples/servo.coffee b/examples/servo.coffee deleted file mode 100644 index e31eeb3..0000000 --- a/examples/servo.coffee +++ /dev/null @@ -1,20 +0,0 @@ -Cylon = require('..') - -# Initialize the robot -Cylon.robot - connection: - name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' - - device: - name: 'servo', driver: 'servo', pin: 3 - - work: (my) -> - angle = 0 - increment = 90 - every 1.seconds(), -> - angle += increment - my.servo.angle(angle) - console.log("Current Angle => #{ my.servo.currentAngle() }") - increment = -increment if (angle is 0) or (angle is 180) - -.start() diff --git a/examples/servo/servo.coffee b/examples/servo/servo.coffee new file mode 100644 index 0000000..c225995 --- /dev/null +++ b/examples/servo/servo.coffee @@ -0,0 +1,19 @@ +Cylon = require '../..' + +Cylon.robot + connection: { name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' } + device: { name: 'servo', driver: 'servo', pin: 3 } + + work: (my) -> + angle = 0 + increment = 90 + + every 1.seconds(), -> + angle += increment + my.servo.angle angle + + Logger.info "Current Angle: #{my.servo.currentAngle()}" + + increment = -increment if (angle is 0) or (angle is 180) + +.start() diff --git a/examples/servo/servo.js b/examples/servo/servo.js new file mode 100644 index 0000000..1aecb02 --- /dev/null +++ b/examples/servo/servo.js @@ -0,0 +1,20 @@ +var Cylon = require('../..'); + +Cylon.robot({ + connection: { name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' }, + device: { name: 'servo', driver: 'servo', pin: 3 }, + + work: function(my) { + var angle = 0; + var increment = 90; + + every(1..seconds(), function() { + angle += increment; + my.servo.angle(angle); + + Logger.info("Current Angle: " + (my.servo.currentAngle())); + + if ((angle === 0) || (angle === 180)) { increment = -increment; } + }); + } +}).start(); diff --git a/examples/sf-client.coffee b/examples/sf-client.coffee deleted file mode 100644 index e8eb342..0000000 --- a/examples/sf-client.coffee +++ /dev/null @@ -1,52 +0,0 @@ -'use strict'; - -nforce = require('nforce') -faye = require('faye') - -namespace = require('node-namespace') - -namespace 'SF', -> - class @SFClient - constructor: (opts) -> - @client = null - @outboundMessages = [] - @sfuser = opts.sfuser - @sfpass = opts.sfpass - @orgCreds = opts.orgCredentials - @org = nforce.createConnection(@orgCreds) - - _processOutboundMessages: () -> - # Do work here - - _handleStreamingAPI: (outboundCB) -> - client = new faye.Client(@oauth.instance_url + '/cometd/28.0') - client.setHeader("Authorization", "OAuth #{ @oauth.access_token }") - - subscription = client.subscribe('/topic/SpheroMsgOutbound', outboundCB) - console.log("Streaming API Connected...") - - authenticate: (outboundCB) -> - @org.authenticate({ username: @sfuser, password: @sfpass}, (err, _oauth) => - if(err) - console.error('unable to authenticate to sfdc') - console.log(err) - process.exit(code=0) - else - console.log("authenticated") - @oauth = _oauth - @_handleStreamingAPI(outboundCB) - @_processOutboundMessages() - ) - - push: (msg) -> - #jsonBody = JSON.parse(msg) - #jsonString = JSON.stringify(msg) - jsonString = msg - console.log("SpheroController post msg:") - console.log(msg) - @org.apexRest({uri:'SpheroController', method: 'POST', body: jsonString}, @oauth, (err,resp) => - if(err) - console.log(err) - else - console.log(resp) - ) diff --git a/examples/sf-client/sf-client.coffee b/examples/sf-client/sf-client.coffee new file mode 100644 index 0000000..af34048 --- /dev/null +++ b/examples/sf-client/sf-client.coffee @@ -0,0 +1,48 @@ +'use strict' + +namespace = require 'node-namespace' +nforce = require 'nforce' +Faye = require 'faye' + +namespace 'SF', -> + class @SFClient + constructor: (opts) -> + @client = null + @outboundMessages = [] + @sfuser = opts.sfuser + @sfpass = opts.sfpass + @orgCreds = opts.orgCredentials + @org = nforce.createConnection @orgCreds + + _processOutboundMessages: () -> + # Do work here + + _handleStreamingAPI: (outboundCB) -> + client = new Faye.Client("#{@oauth.instance_url}/cometd/28.0") + client.setHeader "Authorization", "OAuth #{@oauth.access_token}" + + subscription = client.subscribe '/topic/SpheroMsgOutbound', outboundCB + console.log "Streaming API Connected..." + + authenticate: (outboundCB) -> + @org.authenticate {username: @sfuser, password: @sfpass}, (err, _oauth) => + if err + console.log 'unable to authenticate to sfdc' + console.log err + process.exit 1 + else + console.log "authenticated" + @oauth = _oauth + @_handleStreamingAPI outboundCB + @_processOutboundMessages() + + push: (msg) -> + jsonString = msg + + console.log "SpheroController post msg:" + console.log msg + + methodData = {uri:'SpheroController', method: 'POST', body: jsonString} + + @org.apexRest methodData, @oauth, (err, resp) => + console.log if err then err else resp diff --git a/examples/sf-client/sf-client.js b/examples/sf-client/sf-client.js new file mode 100644 index 0000000..36d8b80 --- /dev/null +++ b/examples/sf-client/sf-client.js @@ -0,0 +1,67 @@ +'use strict'; + +var namespace = require('node-namespace'); +var nforce = require('nforce'); +var Faye = require('faye'); + +namespace('SF', function() { + this.SFClient = (function() { + + function SFClient(opts) { + this.client = null; + this.outboundMessages = []; + this.sfuser = opts.sfuser; + this.sfpass = opts.sfpass; + this.orgCreds = opts.orgCredentials; + this.org = nforce.createConnection(this.orgCreds); + } + + SFClient.prototype._processOutboundMessages = function() {}; + + SFClient.prototype._handleStreamingAPI = function(outboundCB) { + var client, subscription; + client = new Faye.Client("" + this.oauth.instance_url + "/cometd/28.0"); + client.setHeader("Authorization", "OAuth " + this.oauth.access_token); + subscription = client.subscribe('/topic/SpheroMsgOutbound', outboundCB); + console.log("Streaming API Connected..."); + }; + + SFClient.prototype.authenticate = function(outboundCB) { + var _this = this; + this.org.authenticate({ + username: this.sfuser, + password: this.sfpass + }, function(err, _oauth) { + if (err) { + console.log('unable to authenticate to sfdc'); + console.log(err); + process.exit(1); + } else { + console.log("authenticated"); + _this.oauth = _oauth; + _this._handleStreamingAPI(outboundCB); + _this._processOutboundMessages(); + } + }); + }; + + SFClient.prototype.push = function(msg) { + var jsonString, methodData, + _this = this; + jsonString = msg; + console.log("SpheroController post msg:"); + console.log(msg); + methodData = { + uri: 'SpheroController', + method: 'POST', + body: jsonString + }; + this.org.apexRest(methodData, this.oauth, function(err, resp) { + console.log(err ? err : resp); + }); + }; + + return SFClient; + + })(); +}); diff --git a/examples/sf-sphero.coffee b/examples/sf-sphero.coffee deleted file mode 100644 index ed65fb0..0000000 --- a/examples/sf-sphero.coffee +++ /dev/null @@ -1,68 +0,0 @@ -Cylon = require('..') - -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' - - 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) - ) - ) - ) - -class SpheroRobot - totalBucks: 0 - - connection: - name: 'sphero', adaptor: 'sphero' - - device: - name: 'sphero', driver: 'sphero' - - react: (robot) => - robot.setRGB(0x00FF00) - robot.roll 90, Math.floor(Math.random() * 360) - - 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.sphero.on 'collision', (data) -> - me.sphero.setRGB(0x0000FF, me) - me.sphero.stop() - 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 - -spheroRobot = new SpheroRobot() -spheroRobot.name = 'ROY' -spheroRobot.connection.port = '/dev/rfcomm0' -Cylon.robot spheroRobot - -Cylon.start() diff --git a/examples/sf-sphero/sf-sphero.coffee b/examples/sf-sphero/sf-sphero.coffee new file mode 100644 index 0000000..cf1e681 --- /dev/null +++ b/examples/sf-sphero/sf-sphero.coffee @@ -0,0 +1,67 @@ +Cylon = require '../..' + +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' } + + work: (me) -> + me.salesforce.on 'start', () -> + me.salesforce.subscribe '/topic/SpheroMsgOutbound', (data) -> + msg = "Sphero: #{data.sobject.Sphero_Name__c}," + msg += "Bucks: #{data.sobject.Bucks__c}," + msg += "SM_Id: #{data.sobject.Id}" + + Logger.info msg + + me.master.findRobot data.sobject.Sphero_Name__c, (err, spheroBot) -> + spheroBot.react spheroBot.devices.sphero + +class SpheroRobot + totalBucks: 0 + + connection: { name: 'sphero', adaptor: 'sphero' } + + device: { name: 'sphero', driver: 'sphero' } + + react: (robot) => + robot.setRGB 0x00FF00 + robot.roll 90, Math.floor(Math.random() * 360) + + 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.sphero.on 'collision', (data) -> + me.sphero.setRGB 0x0000FF, me + me.sphero.stop() + + data = JSON.stringify + spheroName: "#{me.name}" + bucks: "#{me.totalBucks++}" + + me.master.findRobot 'salesforce', (err, sf) -> + sf.devices.salesforce.push 'SpheroController', 'POST', data + +sfRobot = new SalesforceRobot() +sfRobot.name = "salesforce" +Cylon.robot sfRobot + +spheroRobot = new SpheroRobot() +spheroRobot.name = 'ROY' +spheroRobot.connection.port = '/dev/rfcomm0' +Cylon.robot spheroRobot + +Cylon.start() diff --git a/examples/sf-sphero/sf-sphero.js b/examples/sf-sphero/sf-sphero.js new file mode 100644 index 0000000..c24a63c --- /dev/null +++ b/examples/sf-sphero/sf-sphero.js @@ -0,0 +1,92 @@ +var __bind = function(fn, me) { + return function() { return fn.apply(me, arguments); }; +}; + +var Cylon = require('../..'); + +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.work = function(me) { + me.salesforce.on('start', function() { + me.salesforce.subscribe('/topic/SpheroMsgOutbound', function(data) { + var msg; + msg = "Sphero: " + data.sobject.Sphero_Name__c + ","; + msg += "Bucks: " + data.sobject.Bucks__c + ","; + msg += "SM_Id: " + data.sobject.Id; + Logger.info(msg); + me.master.findRobot(data.sobject.Sphero_Name__c, function(err, spheroBot) { + spheroBot.react(spheroBot.devices.sphero); + }); + }); + }); + }; + + return SalesforceRobot; + +})(); + +var SpheroRobot = (function() { + function SpheroRobot() { + this.react = __bind(this.react, this); + } + + SpheroRobot.prototype.totalBucks = 0; + + SpheroRobot.prototype.connection = { name: 'sphero', adaptor: 'sphero' }; + SpheroRobot.prototype.device = { name: 'sphero', driver: 'sphero' }; + + SpheroRobot.prototype.react = function(robot) { + robot.setRGB(0x00FF00); + robot.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.sphero.on('collision', function(data) { + me.sphero.setRGB(0x0000FF, me); + me.sphero.stop(); + 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 sfRobot = new SalesforceRobot(); +sfRobot.name = "salesforce"; +Cylon.robot(sfRobot); + +var spheroRobot = new SpheroRobot(); +spheroRobot.name = 'ROY'; +spheroRobot.connection.port = '/dev/rfcomm0'; +Cylon.robot(spheroRobot); + +Cylon.start(); diff --git a/examples/sf-test.coffee b/examples/sf-test.coffee deleted file mode 100644 index 12daa8f..0000000 --- a/examples/sf-test.coffee +++ /dev/null @@ -1,29 +0,0 @@ -require('../examples/sf-client') - -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' -} - -sf = new Cylon.SF.SFClient(sfuser: sfuser, sfpass: sfpass, orgCredentials: orgCreds) - -cc = 0 -sf.authenticate((msg) -> - simpleMessageString = "^ Sphero Name: #{msg.sobject.Sphero_Name__c}, Msg Content:#{ msg.sobject.Content__c }, SM Id:#{ msg.sobject.Id }" - console.log("Printed from callback in client program.program") - console.log(simpleMessageString + "\n") -) - -myId = null -message = "hello" -detail = "Some Stuff for details" - -setInterval(() => - cc++ - myId = cc - toSend = "{ \"identifier\" :\"run3#{ myId }\", \"msg\": \"#{ message }\" }" - sf.push(toSend) -, 1000) diff --git a/examples/sf-test/sf-test.coffee b/examples/sf-test/sf-test.coffee new file mode 100644 index 0000000..e589499 --- /dev/null +++ b/examples/sf-test/sf-test.coffee @@ -0,0 +1,36 @@ +require '../examples/sf-client' + +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' + +sf = new Cylon.SF.SFClient + sfuser: sfuser + sfpass: sfpass + orgCredentials: orgCreds + +cc = 0 + +sf.authenticate (msg) -> + string = "^ Sphero Name: #{msg.sobject.Sphero_Name__c}," + string += " Msg Content:#{ msg.sobject.Content__c }" + string += ", SM Id:#{ msg.sobject.Id }" + console.log "Printed from callback in client program.program" + console.log string + "\n" + +myId = null +message = "hello" +detail = "Some Stuff for details" + +setInterval () => + cc++ + myId = cc + + data = JSON.stringify { identifier: "run3#{myId}", msg: message } + + sf.push data +, 1000 diff --git a/examples/sf-test/sf-test.js b/examples/sf-test/sf-test.js new file mode 100644 index 0000000..6e18c00 --- /dev/null +++ b/examples/sf-test/sf-test.js @@ -0,0 +1,38 @@ +require('../examples/sf-client'); + +var sfuser = process.env.SF_USERNAME; +var sfpass = process.env.SF_SECURITY_TOKEN; + +var orgCreds = { + clientId: process.env.SF_CLIENT_ID, + clientSecret: process.env.SF_CLIENT_SECRET, + redirectUri: 'http://localhost:3000/oauth/_callback' +}; + +var sf = new Cylon.SF.SFClient({ + sfuser: sfuser, + sfpass: sfpass, + orgCredentials: orgCreds +}); + +var cc = 0; + +sf.authenticate(function(msg) { + var string; + string = "^ Sphero Name: " + msg.sobject.Sphero_Name__c + ","; + string += " Msg Content:" + msg.sobject.Content__c; + string += ", SM Id:" + msg.sobject.Id; + console.log("Printed from callback in client program.program"); + console.log(string + "\n"); +}); + +var myId = null; +var message = "hello"; +var detail = "Some Stuff for details"; + +setInterval(function() { + cc++; + myId = cc; + var data = JSON.stringify({ identifier: "run3" + myId, msg: message }); + sf.push(data); +}, 1000);