Permalink
Browse files

Initial commits

  • Loading branch information...
0 parents commit 0e8b94cb45524519dc2b12770fd2fdfa5227fc17 @rwaldron committed Mar 30, 2012
Showing with 384 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +1 −0 .npmignore
  3. +22 −0 LICENSE-MIT
  4. +51 −0 README.md
  5. +40 −0 eg/claw.js
  6. +39 −0 grunt.js
  7. +136 −0 lib/board.js
  8. +21 −0 lib/johnny-five.js
  9. +16 −0 lib/repl.js
  10. +38 −0 package.json
  11. +19 −0 test/johnny-five_test.js
@@ -0,0 +1 @@
+node_modules
@@ -0,0 +1 @@
+/node_modules/
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Rick Waldron <waldron.rick@gmail.com>
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,51 @@
+# Johnny 5, is in progress.
+
+[Firmata](https://github.com/jgautier/firmata) powered Arduino programming framework.
+
+# [Video Demo](http://jsfiddle.net/rwaldron/7tdQR/show/light/)
+
+## Getting Started
+Install the module with: `npm install johnny-five`
+
+```javascript
+var five = require("johnny-five"),
+ board, repl;
+
+board = new five.Board({
+ debug: true
+});
+
+board.on("ready", function() {
+
+ // Open an interactive board control session!
+ repl = new five.Repl({
+ board: board
+ });
+
+ // ...Or...
+
+ // Set up an output pin!
+ this.board.pinMode( 9, 1 );
+
+ // Make a servo move on that pin!
+ this.board.servoWrite( 9, 180 );
+
+ // Woo!
+});
+```
+
+## Documentation
+_(Coming soon)_
+
+## Examples
+_(Coming soon)_
+
+## Contributing
+In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [grunt](https://github.com/cowboy/grunt).
+
+## Release History
+_(Nothing yet)_
+
+## License
+Copyright (c) 2012 Rick Waldron <waldron.rick@gmail.com>
+Licensed under the MIT license.
@@ -0,0 +1,40 @@
+var five = require('../lib/johnny-five.js'),
+ board, repl;
+
+board = new five.Board({
+ debug: true
+});
+
+board.on("ready", function() {
+
+ var claw = 9,
+ arm = 11,
+ degrees = 10,
+ incrementer = 10,
+ last;
+
+ this.board.pinMode( claw, 3 );
+ this.board.pinMode( arm, 3 );
+
+ setInterval(function() {
+
+ if ( degrees >= 180 || degrees === 0 ) {
+ incrementer *= -1;
+ }
+
+ degrees += incrementer;
+
+ if ( degrees === 180 ) {
+ if ( !last || last === 90 ) {
+ last = 180;
+ } else {
+ last = 90;
+ }
+
+ this.board.servoWrite( arm, last );
+ }
+
+ this.board.servoWrite( claw, degrees );
+
+ }.bind(this), 50);
+});
@@ -0,0 +1,39 @@
+module.exports = function(grunt) {
+
+ // Project configuration.
+ grunt.initConfig({
+ pkg: '<json:package.json>',
+ test: {
+ files: ['test/**/*.js']
+ },
+ lint: {
+ files: ['grunt.js', 'lib/**/*.js', 'test/**/*.js']
+ },
+ watch: {
+ files: '<config:lint.files>',
+ tasks: 'default'
+ },
+ jshint: {
+ options: {
+ curly: true,
+ eqeqeq: true,
+ immed: true,
+ latedef: true,
+ newcap: true,
+ noarg: true,
+ sub: true,
+ undef: true,
+ boss: true,
+ eqnull: true,
+ node: true
+ },
+ globals: {
+ exports: true
+ }
+ }
+ });
+
+ // Default task.
+ grunt.registerTask('default', 'lint test');
+
+};
@@ -0,0 +1,136 @@
+var events = require("events"),
+ child = require("child_process"),
+ util = require("util"),
+ colors = require("colors"),
+ _ = require("underscore"),
+
+ Firmata = require("firmata").Board,
+ Board, board,
+ Serial;
+
+
+Serial = {
+ detect: function( callback ) {
+ var instance = this;
+
+ this.log( "info", "Board", "Connecting..." );
+
+ child.exec("ls /dev | grep -i usb", function( err, stdout, stderr ) {
+ var found,
+ usb = stdout.slice( 0, -1 ).split("\n").filter(function( val ) {
+ return (/tty/i).test( val );
+ })[ 0 ];
+
+ try {
+ found = new Firmata( "/dev/" + usb, function() {
+ // Execute "ready" callback
+ callback.call( instance, err, "ready", found );
+ });
+ } catch ( error ) {
+ err = error;
+ }
+
+ if ( err ) {
+ err = err.message || err;
+ }
+
+ // Execute "connected" callback
+ callback.call( instance, err, "connected", found );
+ });
+ }
+};
+
+// Board constructor: Firmata Board wrapper
+Board = function( opts ) {
+
+ // Ensure opts is an object
+ opts = opts || {};
+
+ var keys = Object.keys( opts ),
+ key;
+
+ // Copy opts to constructor instance
+ while ( keys.length ) {
+ key = keys.shift();
+
+ this[ key ] = opts[ key ];
+ }
+
+ // Initialize instance property to reference firmata board
+ this.board = null;
+
+ // If no debug flag, default to false
+ if ( !("debug" in this) ) {
+ this.debug = false;
+ }
+
+ Serial.detect.call( this, function( err, type, board ) {
+
+ if ( err ) {
+ this.log( "error", "Board", err );
+ } else {
+ // Assign found board to instance
+ this.board = board;
+ this.log( "info", "Serialport", type );
+ this.log( "info", "Board", type );
+ }
+
+ if ( type === "ready" ) {
+ process.on("SIGINT", function() {
+
+ this.log( "warn", "Board", "Closing connection to board, serialport" );
+
+ setTimeout(function() {
+
+ try {
+ this.board.sp.on("close", function() {
+ process.exit();
+ });
+ this.board.sp.close();
+ } catch (e) {}
+
+ }.bind(this), 500);
+
+ }.bind(this));
+ }
+
+ this.emit( type, err );
+ });
+};
+
+// Inherit event api
+util.inherits( Board, events.EventEmitter );
+
+
+
+Board.prototype.log = function( /* type, module, message [, description] */ ) {
+ var args = [].slice.call( arguments ),
+ type = args.shift(),
+ module = args.shift(),
+ message = args.shift(),
+ color = Board.prototype.log.types[ type ];
+
+ if ( this.debug ) {
+ console.log([
+ // Timestamp
+ String(+new Date()).grey,
+ // Module, color matches type of log
+ module.magenta,
+ // Message
+ message[ color ],
+ // Miscellaneous args
+ args.join(", ")
+ ].join(" "));
+ }
+};
+
+Board.prototype.log.types = {
+ error: "red",
+ fail: "orange",
+ warn: "yellow",
+ info: "cyan"
+};
+
+
+
+module.exports = Board;
@@ -0,0 +1,21 @@
+/*
+ * johnny-five
+ * https://github.com/rwldrn/johnny-five
+ *
+ * Copyright (c) 2012 Rick Waldron <waldron.rick@gmail.com>
+ * Licensed under the MIT license.
+ */
+
+module.exports = {
+ Board: require("../lib/board.js"),
+ Repl: require("../lib/repl.js")
+ // TODO: Port these frum duino/development to Johnny Five
+ // Led: require("./lib/led"),
+ // Piezo: require("./lib/piezo"),
+ // Button: require("./lib/button"),
+ // Servo: require("./lib/servo"),
+ // Sensor: require("./lib/sensor"),
+ // Ping: require("./lib/ping"),
+ // PIR: require("./lib/pir"),
+ // GPS: require("./lib/gps")
+};
@@ -0,0 +1,16 @@
+var repl = require("repl"),
+ Repl;
+
+// Ported from
+// https://github.com/jgautier/firmata
+Repl = function( opts ) {
+ process.stdin.resume();
+ process.stdin.setEncoding("utf8");
+ process.stdin.once( "data", function() {
+ console.log("Successfully Connected");
+ repl.start("firmata>").context.board = opts.board.board;
+ });
+};
+
+
+module.exports = Repl;
@@ -0,0 +1,38 @@
+{
+ "name": "johnny-five",
+ "description": "Firmata based Ardui",
+ "version": "0.1.0",
+ "homepage": "https://github.com/rwldrn/johnny-five",
+ "author": {
+ "name": "Rick Waldron <waldron.rick@gmail.com>",
+ "email": "waldron.rick@gmail.com"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/rwldrn/johnny-five.git"
+ },
+ "bugs": {
+ "url": "https://github.com/rwldrn/johnny-five/issues"
+ },
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "https://github.com/rwldrn/johnny-five/blob/master/LICENSE-MIT"
+ }
+ ],
+ "main": "lib/johnny-five",
+ "engines": {
+ "node": "~0.6.13"
+ },
+ "scripts": {
+ "test": "grunt test"
+ },
+ "dependencies": {
+ "firmata": ">=0.1.3",
+ "underscore": "~1.2.4"
+ },
+ "devDependencies": {
+ "grunt": "~0.3.5"
+ },
+ "keywords": []
+}
@@ -0,0 +1,19 @@
+var five = require("../lib/johnny-five.js"),
+ board = new five.Board({
+ debug: true
+ });
+
+
+board.on("connected", function() {
+
+ exports["board"] = {
+ "board": function(test) {
+
+ // TODO: Figure out a way to run tests without a board connected
+
+ test.expect(1);
+ test.ok(board, "Board instance");
+ test.done();
+ }
+ };
+});

0 comments on commit 0e8b94c

Please sign in to comment.