Permalink
Browse files

basic javascript wrapper for the native stuff finished

  • Loading branch information...
1 parent 29566a1 commit ab6f638c1116833cb5cded5604ba0d5318a8ebbb @RussTheAerialist committed May 12, 2012
Showing with 144 additions and 21 deletions.
  1. +13 −0 LICENSE
  2. +1 −0 package.json
  3. +86 −0 spi.js
  4. +44 −21 test.js
View
13 LICENSE
@@ -0,0 +1,13 @@
+Copyright (c) 2012, Russell Hay <me@russellhay.com>
+
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
View
1 package.json
@@ -7,6 +7,7 @@
"type": "git",
"url": "git://github.com/RussTheAerialist/node-spi.git"
},
+ "main": "./spi",
"dependencies": {
"bindings": "*"
},
View
86 spi.js
@@ -0,0 +1,86 @@
+/*
+ Copyright (c) 2012, Russell Hay <me@russellhay.com>
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+"use strict";
+
+var _spi = require('bindings')('_spi.node');
+
+var MODE = [
+ _spi.SPI_MODE_0,
+ _spi.SPI_MODE_1,
+ _spi.SPI_MODE_2,
+ _spi.SPI_MODE_3
+];
+
+var CS = {
+ 'high': _spi.SPI_CS_HIGH,
+ 'low': 0,
+ 'none': _spi.SPI_NO_CS
+};
+
+function isFunction(object) {
+ return object && getClass.call(object) == '[object Function]';
+}
+
+var Spi = function(device, options, callback) {
+ this._spi = new _spi._spi();
+
+ if (callback == undefined) {
+ callback = options;
+ options = {};
+ }
+
+ options = options || { }; // Default to an empty object
+
+ for(var attrname in options) {
+ var value = options[attrname];
+ if (attrname in this._spi) {
+ console.trace("Setting " + attrname + "=" + value);
+ this._spi[attrname](value);
+ }
+ }
+
+ this._spi.open(device);
+
+ callback(this); // TODO: Update once open is async;
+}
+
+Spi.prototype.write = function(buf, callback) {
+ this._spi.transfer(buf, null);
+
+ if (callback !== undefined) {
+ callback(this, buf); // TODO: Update once transfer is async;
+ }
+}
+
+Spi.prototype.read = function(buf, callback) {
+ this._spi.transfer(null, buf);
+
+ if (callback !== undefined) {
+ callback(this, buf); // TODO: Update once transfer is async;
+ }
+}
+Spi.prototype.transfer = function(wrbuf, rdbuf, callback) {
+ this._spi.transfer(wrbuf, rdbuf);
+
+ if (callback !== undefined) {
+ callback(this, rdbuf); // TODO: Update once transfer is async;
+ }
+}
+
+module.exports.MODE = MODE;
+module.exports.CS = CS;
+module.exports.Spi = Spi;
View
65 test.js
@@ -1,23 +1,46 @@
-var spi = require('./build/Release/_spi');
-console.log(spi);
-var x = new spi._spi();
-console.log(x.mode());
-x.mode(spi.SPI_MODE_3);
-console.log(x.mode());
-x.mode(spi.SPI_MODE_0);
-x.chipSelect(spi.SPI_CS_HIGH);
-console.log(x.mode());
-x.chipSelect(spi.SPI_NO_CS);
-console.log(x.mode());
-x.chipSelect(0);
-console.log(x.mode());
-console.log(x.bitsPerWord());
-x.bitsPerWord(10);
-console.log(x.bitsPerWord());
-console.log(x.maxSpeed());
+//var spi = require('./build/Release/_spi');
+//console.log(spi);
+//var x = new spi._spi();
+//console.log(x.mode());
+//x.mode(spi.SPI_MODE_3);
+//console.log(x.mode());
+//x.mode(spi.SPI_MODE_0);
+//x.chipSelect(spi.SPI_CS_HIGH);
+//console.log(x.mode());
+//x.chipSelect(spi.SPI_NO_CS);
+//console.log(x.mode());
+//x.chipSelect(0);
+//console.log(x.mode());
+//console.log(x.bitsPerWord());
+//x.bitsPerWord(10);
+//console.log(x.bitsPerWord());
+//console.log(x.maxSpeed());
// x.bitsPerWord(-1); // Error Case: Should throw RangeError
-console.log(x.open("/dev/spidev1.1"));
-var buff = new Buffer([ 0x56, 0x78, 0x88 ]);
-x.transfer(buff, null);
+//console.log(x.open("/dev/spidev1.1"));
+//var buff = new Buffer([ 0x56, 0x78, 0x88 ]);
+//x.transfer(buff, null);
+//
+//console.log(x.close());
-console.log(x.close());
+var spi = require('./spi');
+
+var rotate = function(spi, buf) {
+ var tmp = buf[0];
+ for(var i=0; i<buf.length; i++) {
+ buf[i] = (i != buf.length-1) ? buf[i+1] : tmp;
+ }
+ spi.write(buf);
+}
+
+var pixelCtrl = new spi.Spi('/dev/spidev1.1', {
+ 'mode': spi.MODE[0], // Make sure that mode is the first option!
+ 'chipSelect': spi.CS['none']
+}, function(spi) {
+ console.log("Callback called");
+ var buf = new Buffer([0xff, 0x00, 0x00]); // Turn on Red;
+ spi.write(buf, function(spi, buf) {
+ setInterval(function() {
+ rotate(spi, buf);
+ }, 500)
+ });
+});

0 comments on commit ab6f638

Please sign in to comment.