Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 14 commits
  • 10 files changed
  • 0 commit comments
  • 2 contributors
Commits on Jan 24, 2014
@rwaldron rwaldron Enable tests
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
52ba5c3
@rwaldron rwaldron Continuous read: analog and digital
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
003129a
@voodootikigod firmware.cpp report() fixes
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
917b999
@rwaldron rwaldron firmware.cpp: Reset reading and previous values on connect
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
c6d7f6d
@rwaldron rwaldron removes spark.local.js
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
42d4882
@rwaldron rwaldron Rename firmware in honor of its creator :) "voodoospark"
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
27db394
@rwaldron rwaldron Updates to readme: instructions for flashing voodoospark firmware
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
75391a5
@rwaldron rwaldron Remove tokens and add local versions to gitignore
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
ad2bd21
@rwaldron rwaldron Adds Chris Williams as first contributor
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
f2ce341
@rwaldron rwaldron Update dependencies
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
2bcb3b4
@rwaldron rwaldron v0.1.0
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
de38397
@rwaldron rwaldron Adds travis and package.json keywords
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
b0e58fc
@rwaldron rwaldron Improves processInput routine
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
059f4f4
@voodootikigod merges. 4eddaf4
View
6 .travis.yml
@@ -0,0 +1,6 @@
+language: node_js
+node_js:
+ - "0.10"
+before_script:
+ - npm install -g grunt-cli
+compiler: clang
View
2  eg/.gitignore
@@ -1 +1,3 @@
blink.local.js
+write.local.js
+read.local.js
View
4 eg/blink.js
@@ -1,7 +1,7 @@
var Spark = require("../lib/spark");
var board = new Spark({
- token: "608fd30995205529ffc186d4018a651d253af9a9",
- deviceId: "53ff6f065067544840551187"
+ token: "{{ YOUR TOKEN }}",
+ deviceId: "{{ YOUR DEVICE ID }}"
});
board.on("ready", function() {
View
9 eg/read.js
@@ -1,13 +1,14 @@
var Spark = require("../lib/spark");
var board = new Spark({
- token: "608fd30995205529ffc186d4018a651d253af9a9",
- deviceId: "53ff6f065067544840551187"
+ token: "{{ YOUR TOKEN }}",
+ deviceId: "{{ YOUR DEVICE ID }}"
});
board.on("ready", function() {
console.log("CONNECTED");
- this.digitalRead("D7", function(data) {
- console.log( data );
+ this.analogRead("A0", function(data) {
+ console.log( "A0", data );
});
+
});
View
66 firmware/firmware.cpp → firmware/voodoospark.cpp
@@ -30,6 +30,9 @@ int connectToMyServer(String params) {
ipArrayFromString(serverAddress, ip);
int serverPort = port.toInt();
if (client.connect(serverAddress, serverPort)) {
+
+ reset();
+
if (DEBUG)
Serial.println("Connected to server: "+ip+":"+port);
return 1; // successfully connected
@@ -48,6 +51,12 @@ void setup() {
}
+void reset() {
+ for (int i = 0; i < 20; i++) {
+ reading[i] = 0;
+ previous[i] = 0;
+ }
+}
void send(int action, int pin, int value) {
if (previous[pin] != value) {
@@ -87,55 +96,6 @@ void loop() {
if(DEBUG)
Serial.println("Action received: "+('0'+action));
int pin, mode, val, type, speed, address, stop, len, i;
- switch (action) {
- case 0x00: // pinMode
- pin = client.read();
- mode = client.read();
- //mode is modeled after Standard Firmata
- if (mode == 0x00) {
- pinMode(pin, INPUT);
- } else if (mode == 0x02) {
- pinMode(pin, INPUT_PULLUP);
- } else if (mode == 0x03) {
- pinMode(pin, INPUT_PULLDOWN);
- } else if (mode == 0x01) {
- pinMode(pin, OUTPUT);
- }
- break;
- case 0x01: // digitalWrite
- pin = client.read();
- val = client.read();
- digitalWrite(pin, val);
- break;
- case 0x02: // analogWrite
- pin = client.read();
- val = client.read();
- analogWrite(pin, val);
- break;
- case 0x03: // digitalRead
- pin = client.read();
- val = digitalRead(pin);
- client.write(0x03);
- client.write(pin);
- client.write(val);
- break;
- case 0x04: // analogRead
- pin = client.read();
- val = analogRead(pin);
- client.write(0x04);
- client.write(pin);
- client.write(val);
- break;
- case 0x05: // set always send bit
- pin = client.read();
- val = client.read();
- reading[pin] = val;
- break;
-
- if(DEBUG)
- Serial.println("Action received: "+('0'+action));
-
- int pin, mode, val;
// These are used in the commented code below there are warnings there that need to be resolved
// otherwise spark.io will not compile and flash
@@ -358,16 +318,8 @@ void loop() {
client.write(val);
break;
-
-
-
-
-
-
-
default: // noop
break;
- }
}
}
}
View
165 lib/spark.js
@@ -1,5 +1,4 @@
var es6 = require("es6-shim");
-var querystring = require("querystring");
var os = require("os");
var net = require("net");
var Emitter = require("events").EventEmitter;
@@ -9,10 +8,6 @@ var interfaces = os.networkInterfaces();
var ipAddress;
-function service(deviceId) {
- return "https://api.spark.io/v1/devices/" + deviceId + "/";
-}
-
if (interfaces) {
ipAddress = Object.keys(interfaces).reduce(function(accum, name) {
if (!accum) {
@@ -26,9 +21,6 @@ if (interfaces) {
}, null);
}
-// console.log(ipAddress);
-// process.exit(1);
-
var pins = [
{ id: "D0", modes: [0, 1, 3, 4] },
{ id: "D1", modes: [0, 1, 3, 4] },
@@ -60,6 +52,51 @@ var modes = Object.freeze({
SERVO: 4
});
+var DIGITAL_READ = 0x03;
+var ANALOG_READ = 0x04;
+
+function service(deviceId) {
+ return "https://api.spark.io/v1/devices/" + deviceId + "/";
+}
+
+function processReceived(spark, data) {
+ var dlength = data.length;
+ var length, action, pin, value, event;
+
+ for (var i = 0; i < dlength; i++) {
+ spark.buffer.push(data.readUInt8(i));
+ }
+
+ length = spark.buffer.length;
+
+ if (length >= 3) {
+ while (length && (length % 3) === 0) {
+ action = spark.buffer.shift();
+ pin = spark.buffer.shift();
+ value = spark.buffer.shift();
+
+ if (action === DIGITAL_READ ||
+ action === ANALOG_READ) {
+
+ if (action === ANALOG_READ) {
+ pin = "A" + (pin - 10);
+ }
+
+ if (action === DIGITAL_READ) {
+ pin = "D" + pin;
+ }
+
+ event = "read-" + pin;
+
+ spark.emit(event, value);
+ }
+
+ length = spark.buffer.length;
+ }
+ }
+}
+
+
function Spark(opts) {
Emitter.call(this);
@@ -79,6 +116,7 @@ function Spark(opts) {
};
this.name = "spark-io";
+ this.buffer = [];
this.isReady = false;
this.pins = pins.map(function(pin) {
@@ -96,26 +134,29 @@ function Spark(opts) {
// Store private state
priv.set(this, state);
- var connect = function() {
- this.command({
- method: "post",
- action: "connect",
- pin: ipAddress + ":" + state.port,
- handler: function(error, data) {
- console.log( "connect -> command -> handler" );
- if (error !== null) {
- this.emit("error", error);
- } else {
- // Set ready state bit
- this.isReady = true;
-
- this.emit("ready");
+ var connector = function() {
+ this.connect(function(error, data) {
+ // console.log( "connect -> connect -> handler" );
+ if (error !== null) {
+ this.emit("error", error);
+ } else {
+ state.isConnected = true;
+ // Set ready state bit
+ this.isReady = true;
+
+ this.emit("ready");
+
+ if (state.socket && !state.isReading) {
+ state.isReading = true;
+ state.socket.on("data", function(data) {
+ processReceived(this, data);
+ }.bind(this));
}
- }.bind(this)
- });
+ }
+ }.bind(this));
}.bind(this);
- Spark.Server.create(this, connect);
+ Spark.Server.create(this, connector);
}
Spark.Server = {
@@ -124,17 +165,10 @@ Spark.Server = {
throw new Error("Expected instance of Spark");
}
var state = priv.get(spark);
- state.server = net.createServer({}, function(socket) {
+ state.server = net.createServer(function(socket) {
socket.setKeepAlive(true);
-
- spark.emit("connected");
-
- // Store socket and server references
+ // spark.emit("connected");
state.socket = socket;
-
- state.socket.on("data", function() {
- console.log( "SOCKET DATA: ", arguments );
- });
});
state.server.listen(state.port);
@@ -148,9 +182,7 @@ Spark.prototype = Object.create(Emitter.prototype, {
value: Spark
},
MODES: {
- get: function() {
- return modes;
- }
+ value: modes
},
HIGH: {
value: 1
@@ -160,43 +192,27 @@ Spark.prototype = Object.create(Emitter.prototype, {
}
});
-Spark.prototype.command = function(opts) {
+Spark.prototype.connect = function(handler) {
var state = priv.get(this);
var url = state.service;
- var outbound = {};
- var method = opts.method;
- var action = opts.action;
- var pin = opts.pin;
- var handler = opts.handler;
- var value = opts.value;
+ var action = "connect";
+ var outbound = {
+ access_token: state.token,
+ params: ipAddress + ":" + state.port
+ };
var request;
- // Allow omission of "method", default to "post"
- if (method === undefined) {
- method = "post";
- }
-
- if (typeof pin === "undefined" && action !== "query") {
- // error?
- }
-
- outbound.access_token = state.token;
-
- if (action !== "query") {
- outbound.params = pin;
+ if (state.isConnected) {
+ return this;
}
- if (value !== undefined) {
- outbound.params = [pin, value].join();
- }
+ handler = handler.bind(this);
- console.log( url + action, outbound );
+ // console.log( url + action, outbound );
- request = method === "get" ?
- rest.get(url + "?" + querystring.encode(outbound)) :
- rest.post(url + action, {
- data: outbound
- });
+ request = rest.post(url + action, {
+ data: outbound
+ });
request.on("complete", function(data) {
var err = null;
@@ -206,9 +222,9 @@ Spark.prototype.command = function(opts) {
}
if (handler) {
- handler.call(this, err, data);
+ handler(err, data);
}
- }.bind(this));
+ });
return this;
};
@@ -239,7 +255,7 @@ Spark.prototype.pinMode = function(pin, mode) {
buffer[1] = pinInt;
buffer[2] = value;
- console.log( "writing: ", buffer );
+ // console.log( "writing: ", buffer );
state.socket.write(buffer);
this.pins[pinInt].value = value;
@@ -251,7 +267,7 @@ Spark.prototype.pinMode = function(pin, mode) {
// TODO: Define protocol for gather this information.
["analogRead", "digitalRead"].forEach(function(fn) {
- var isAnalog = fn === "analogWrite";
+ var isAnalog = fn === "analogRead";
var action = isAnalog ? 0x04 : 0x03;
var offset = isAnalog ? 10 : 0;
@@ -259,7 +275,7 @@ Spark.prototype.pinMode = function(pin, mode) {
var state = priv.get(this);
var buffer = new Buffer(3);
var pinInt = (pin.replace(/A|D/i, "") | 0) + offset;
- var event = fn.slice(0, -4) + "-" + pin;
+ var event = "read-" + pin;
buffer[0] = action;
buffer[1] = pinInt;
@@ -268,6 +284,13 @@ Spark.prototype.pinMode = function(pin, mode) {
// register a handler for
this.on(event, handler);
+ if (!state.isReading) {
+ state.isReading = true;
+ state.socket.on("data", function(data) {
+ processReceived(this, data);
+ }.bind(this));
+ }
+
// Tell the board we have a new pin to read
state.socket.write(buffer);
View
348 lib/spark.local.js
@@ -1,348 +0,0 @@
-var os = require('os');
-var net = require('net');
-var Emitter = require("events").EventEmitter;
-var rest = require("restler");
-var querystring = require("querystring");
-var es6 = require("es6-collections");
-var priv = new Map();
-
-var ipAddress = null;
-
-function gatherIPAddress() {
- // gather ip Address
- var interfaces = os.networkInterfaces();
- for (var ifName in os.networkInterfaces()) {
- if (!ipAddress) {
- interfaces[ifName].forEach(function (iface){
- if (!ipAddress && !iface.internal && 'IPv4' === iface.family) {
- ipAddress = iface.address;
- }
- });
- }
- }
-}
-
-var pins = [
- { id: "D0", modes: [0, 1, 3, 4] },
- { id: "D1", modes: [0, 1, 3, 4] },
- { id: "D2", modes: [0, 1] },
- { id: "D3", modes: [0, 1] },
- { id: "D4", modes: [0, 1] },
- { id: "D5", modes: [0, 1] },
- { id: "D6", modes: [0, 1] },
- { id: "D7", modes: [0, 1] },
-
- { id: "A0", modes: [0, 1, 2, 3, 4] },
- { id: "A1", modes: [0, 1, 2, 3, 4] },
- { id: "A2", modes: [0, 1, 2] },
- { id: "A3", modes: [0, 1, 2] },
- { id: "A4", modes: [0, 1, 2] },
- { id: "A5", modes: [0, 1, 2, 3, 4] },
- { id: "A6", modes: [0, 1, 2, 3, 4] },
- { id: "A7", modes: [0, 1, 2, 3, 4] }
-];
-
-var modes = Object.freeze({
- INPUT: 0,
- OUTPUT: 1,
- ANALOG: 2,
- PWM: 3,
- SERVO: 4
-});
-
-
-
-function service(deviceId) {
- return "https://api.spark.io/v1/devices/" + deviceId + "/";
-}
-
-function Spark(opts) {
-
- Emitter.call(this);
- if (!(this instanceof Spark)) {
- return new Spark(opts);
- }
-
-
-
- var state = {
- isConnected: true,
- deviceId: opts.deviceId,
- token: opts.token,
- service: service(opts.deviceId),
- timers: {},
- interval: 20
- };
-
-
- this.name = "spark-io";
- this.isReady = false;
-
- this.localServer = null;
- // this is odd as we could have a pool of sockets really with many devices..
- // TODO: Consider a pooling and identification method for sending commands.
- this.localSocket = null;
-
- this.localPort = opts.port || 9000;
-
-
-
- this.pins = pins.map(function(pin) {
- return {
- supportedModes: pin.modes,
- mode: pin.modes[0],
- value: 0
- };
- });
-
- this.analogPins = this.pins.slice(8).map(function(pin, i) {
- return i;
- });
-
-
- var bindLocalTCPServer = function() {
- var that = this;
- var localServer = net.createServer(function(socket){
- socket.setKeepAlive(true);
- that.isReady = true;
- that.emit("ready", socket.remoteAddress);
- that.localSocket = socket;
- });
- localServer.listen(this.localPort);
- this.localServer = localServer;
- }.bind(this);
-
- var connectSparkCore = function() {
- this.command({
- method: "post",
- action: "connect",
- pin: ipAddress+":"+this.localPort,
- handler: function(error, data) {
- // wait for the socket to connect.
- if (error != null) {
- console.log("Handler error");
- console.log(arguments);
- this.emit("error", error);
- } else {
- // this.emit("ready", data);
- }
- }.bind(this)
- });
- }.bind(this);
-
-
- if (!ipAddress)
- gatherIPAddress();
- bindLocalTCPServer();
-
- // coalese CPU
- process.nextTick(connectSparkCore);
-
-
- // Store private state
- priv.set(this, state);
-
-}
-
-
-Spark.prototype = Object.create(Emitter.prototype, {
- constructor: {
- value: Spark
- },
- MODES: {
- get: function() {
- return modes;
- }
- },
- HIGH: {
- value: 1
- },
- LOW: {
- value: 0
- }
-});
-
-
-
-
-Spark.prototype.command = function(opts) {
- var state = priv.get(this);
- var url = state.service;
- var outbound = {};
- var method = opts.method;
- var action = opts.action;
- var pin = opts.pin;
- var handler = opts.handler;
- var value = opts.value;
- var request;
-
- // Allow omission of "method", default to "post"
- if (method === undefined) {
- method = "post";
- }
-
- if (typeof pin === "undefined" && action !== "query") {
- // error?
- }
-
- outbound.access_token = state.token;
-
- if (action !== "query") {
- outbound.params = pin;
- }
-
- if (value !== undefined) {
- outbound.params = [pin, value].join();
- }
-
- if (Spark.prototype.command.isTest) {
- Spark.prototype.command.stub({
- action: action,
- handler: handler,
- method: method,
- pin: pin,
- value: value,
- outbound: outbound
- });
-
- return this;
- }
-
- request = method === "get" ?
- rest.get(url + "?" + querystring.encode(outbound)) :
- rest.post(url + action, {
- data: outbound
- });
-
- request.on("complete", function(data) {
- var err = null;
-
- if (data.error) {
- err = "ERROR: " + data.code + " " + data.error_description;
- }
-
- if (handler) {
- handler.call(this, err, data);
- }
- }.bind(this));
-
- return this;
-};
-
-
-// TODO: Replace this crap with something that isn't horrible.
-Spark.prototype.command.isTest = false;
-Spark.prototype.command.stub = function() {};
-
-
-
-Spark.prototype.close = function () {
- this.localSocket.close();
- this.localServer.close();
-};
-
-
-
-
-
-
-// replace this.command with socket.write changes.
-
-["analogWrite", "digitalWrite"].forEach(function(fn) {
- var action = fn.toLowerCase();
- var offset = action === "analogwrite" ? 8 : 0;
-
- Spark.prototype[fn] = function(pin, value) {
- var buff = new Buffer(4);
-
- if (fn === "digitalWrite") {
- buff[0] = 0x01;
- if (value !== 1 && value !== 0 &&
- value !== "HIGH" && value !== "LOW") {
-
- throw new Error(
- "Unexpected digitalWrite value"
- );
- }
- if (value == "HIGH") {
- value = 1;
- } else if (value == "LOW") {
- value = 0;
- }
- } else {
- buff[0] = 0x02;
- }
-
- // write out pin
- pin = ""+pin; // coerce to string so we don't care about input format
- buff[1] = ((pin[0] == 'A' || pin[0] == 'a') ? 'A' : 'D').charCodeAt();
- buff[2] = parseInt(pin.replace(/A|D/i, ""), 10);
- // write value
- buff[3] = value;
- this.localSocket.write(buff);
- };
-});
-
-
-// TODO: Define protocol for gather this information.
-["analogRead", "digitalRead"].forEach(function(fn) {
- var action = fn.toLowerCase();
-
- Spark.prototype[fn] = function(pin, handler) {
- var state = priv.get(this);
- var key = action + "-" + pin;
- var timer = state.timers[key];
-
- if (timer) {
- clearInterval(timer);
- }
-
- timer = setInterval(function() {
- this.command({
- action: action,
- pin: pin,
- handler: handler
- });
- }.bind(this), state.interval);
-
- state.timers[key] = timer;
-
- return this;
- };
-});
-
-Spark.prototype.servoWrite = Spark.prototype.analogWrite;
-
-/**
- * Compatibility Shimming
- */
-Spark.prototype.setSamplingInterval = function(interval) {
- // This does not send a value to the board
- var safeint = interval < 10 ?
- 10 : (interval > 65535 ? 65535 : interval);
-
- priv.get(this).interval = safeint;
-
- return this;
-};
-
-Spark.prototype.pinMode = function(pin, mode) {
- // This does not send a value to the board and
- // does not affect the mode of a given pin on
- // the board. `pinMode` is managed by
- // https://github.com/spark/core-firmware/blob/master/src/application.cpp
- var offset = pin[0] === "A" ? 8 : 0;
- var index = (pin.replace(/A|D/, "") | 0) + offset;
-
- this.pins[index].mode = mode;
-
- return this;
-};
-
-Spark.prototype.reset = function() {
- return this;
-};
-
-module.exports = Spark;
-
-
View
15 package.json
@@ -1,14 +1,18 @@
{
"name": "spark-io",
"description": "Spark Core IO",
- "version": "0.0.5",
+ "version": "0.1.0",
"homepage": "https://github.com/rwaldron/spark-io",
"author": {
"name": "Rick Waldron <waldron.rick@gmail.com>",
"email": "waldron.rick@gmail.com"
},
- "contributors": [],
- "keywords": [],
+ "contributors": [
+ { "name": "Chris Williams", "email": "<chris@iterativedesigns.com>" }
+ ],
+ "keywords": [
+ "spark", "io", "arduino", "firmata", "johnny-five"
+ ],
"repository": {
"type": "git",
"url": "git://github.com/rwaldron/spark-io.git"
@@ -27,10 +31,9 @@
"node": ">=0.10.0"
},
"dependencies": {
- "es6-collections": ">=0.0.1",
+ "es6-shim": "~0.9.2",
"nanotimer": "0.3.0",
- "restler": "~3.1.0",
- "querystring": "~0.2.0"
+ "restler": "~3.1.0"
},
"devDependencies": {
"sinon": "~1.7.3",
View
15 readme.md
@@ -1,6 +1,19 @@
# Spark-io
-Spark-io is a Firmata-compatibility IO class for writing node programs that interact with [Spark devices](http://docs.spark.io/) that have the default [Tinker Firmware](http://docs.spark.io/#/start/tinkering-with-tinker-the-tinker-api) installed.
+Spark-io is a Firmata-compatibility IO class for writing node programs that interact with [Spark devices](http://docs.spark.io/).
+
+### Getting Started
+
+With your Spark device connected to the correct Wifi network:
+
+1. Open the [Spark.io Editor](https://www.spark.io/build). (Be sure to "claim" your Spark core device).
+2. Copy and paste the entire contents of `firmware/voodoospark.cpp` into the editor window.
+3. Click "Verify"
+4. Click "Flash"
+
+Once the flashing process is complete, close the Spark.io Editor.
+
+### Usage
This module can be used a substitute IO layer with [Johnny-Five](https://github.com/rwaldron/johnny-five).
View
394 test/spark.js
@@ -4,33 +4,33 @@ var Spark = require("../lib/spark");
var Emitter = require("events").EventEmitter;
var sinon = require("sinon");
+
+function State() {
+ this.isConnected = false;
+ this.isReading = false;
+ this.deviceId = "deviceId";
+ this.token = "token";
+ this.service = "service";
+ this.port = 9000;
+ this.server = {};
+ this.socket = new Emitter();
+ this.socket.write = function() {};
+}
+
sinon.stub(Spark.Server, "create", function(spark, onCreated) {
process.nextTick(onCreated);
});
-// function command(stub) {
-// Spark.prototype.command.isTest = true;
-// if (Spark.prototype.command.stub.restore) {
-// Spark.prototype.command.stub.restore();
-// }
-// sinon.stub(Spark.prototype.command, "stub", stub);
-// }
-
-// command.reset = function() {
-// if (Spark.prototype.command.isTest) {
-// Spark.prototype.command.isTest = false;
-// Spark.prototype.command.stub.restore();
-// }
-// };
-
exports["Spark"] = {
setUp: function(done) {
this.clock = sinon.useFakeTimers();
- // Spark.prototype.command = function() {};
- this.command = sinon.stub(Spark.prototype, "command", function(opts) {
- opts.handler(null);
+ this.state = new State();
+ this.map = sinon.stub(Map.prototype, "get").returns(this.state);
+ this.socketwrite = sinon.spy(this.state.socket, "write");
+ this.connect = sinon.stub(Spark.prototype, "connect", function(handler) {
+ handler(null);
});
this.spark = new Spark({
@@ -38,13 +38,6 @@ exports["Spark"] = {
deviceId: "deviceId"
});
- // command(function(received) {
- // received.handler.call(this.spark, null, {
- // connected: true
- // });
- // });
-
-
this.proto = {};
this.proto.functions = [{
@@ -58,7 +51,7 @@ exports["Spark"] = {
}, {
name: "servoWrite"
}, {
- name: "command"
+ name: "connect"
}];
this.proto.objects = [{
@@ -80,7 +73,9 @@ exports["Spark"] = {
done();
},
tearDown: function(done) {
- this.command.restore();
+ this.connect.restore();
+ this.map.restore();
+ this.socketwrite.restore();
this.clock.restore();
done();
},
@@ -153,255 +148,204 @@ exports["Spark"] = {
this.spark.on("ready", function() {
test.ok(true);
- console.log( "message" );
test.done();
});
}
};
-// [
-// 'analogWrite',
-// 'digitalWrite',
-// 'analogRead',
-// 'digitalRead'
-// ].forEach(function(fn) {
-// var entry = "Spark.prototype." + fn;
-// var action = fn.toLowerCase();
-
-// exports[entry] = {
-// setUp: function(done) {
-
-// this.clock = sinon.useFakeTimers();
-
-// var state = {
-// isConnected: true,
-// deviceId: "deviceId",
-// token: "token",
-// service: "service",
-// port: 9000,
-// server: {},
-// socket: {
-// write: function() {}
-// },
-// timers: {},
-// interval: 20
-// };
+[
+ 'analogWrite',
+ 'digitalWrite',
+ 'analogRead',
+ 'digitalRead'
+].forEach(function(fn) {
+ var entry = "Spark.prototype." + fn;
+ var action = fn.toLowerCase();
+ var isAnalog = action === "analogwrite" || action === "analogread";
-// this.map = sinon.stub(Map.prototype, "get").returns(state);
+ var index = isAnalog ? 10 : 0;
+ var pin = isAnalog ? "A0" : "D0";
+ var value = isAnalog ? 255 : 1;
+ var sent = isAnalog ? [2, 10, 255] : [1, 0, 1];
+ var receiving = new Buffer(isAnalog ? [4, 10, 4095] : [3, 0, 1]);
-// this.socketwrite = sinon.spy(state.socket, "write");
+ exports[entry] = {
+ setUp: function(done) {
+ this.clock = sinon.useFakeTimers();
-// // command(function(received) {
-// // received.handler(null, {
-// // connected: true
-// // });
-// // });
+ this.state = new State();
+ this.map = sinon.stub(Map.prototype, "get").returns(this.state);
+ this.socketwrite = sinon.spy(this.state.socket, "write");
+ this.connect = sinon.stub(Spark.prototype, "connect", function(handler) {
+ handler(null);
+ });
-// this.spark = new Spark({
-// token: "token",
-// deviceId: "deviceId"
-// });
+ this.spark = new Spark({
+ token: "token",
+ deviceId: "deviceId"
+ });
-// done();
-// },
-// tearDown: function(done) {
-// // command.reset();
+ done();
+ },
+ tearDown: function(done) {
+ this.connect.restore();
+ this.map.restore();
+ this.socketwrite.restore();
+ this.clock.restore();
-// this.map.restore();
-// this.socketwrite.restore();
-// this.clock.restore();
+ done();
+ }
+ };
-// done();
-// }
-// };
+ // *Read Tests
+ if (/read/.test(action)) {
-// // *Read Tests
-// if (/read/.test(action)) {
-// // exports[entry].command = function(test) {
-// // test.expect(2);
+ exports[entry].data = function(test) {
+ test.expect(1);
-// // var handler = function(value) {
-// // test.equal(value, 1);
-// // test.done();
-// // };
+ var handler = function(value) {
+ test.equal(value, receiving[2]);
+ test.done();
+ };
-// // command(function(received) {
+ this.spark[fn](pin, handler);
-// // test.deepEqual(received, {
-// // action: action,
-// // handler: handler,
-// // method: "post",
-// // pin: "A0",
-// // value: undefined,
-// // outbound: {
-// // access_token: "token",
-// // params: "A0"
-// // }
-// // });
-
-// // received.handler(1);
-// // });
-
-// // this.spark[fn]("A0", handler);
-
-// // this.clock.tick(100);
-// // };
+ this.state.socket.emit("data", receiving);
-// // exports[entry].interval = function(test) {
-// // test.expect(1);
-
-// // var calls = 0;
-
-// // command(function(received) {
-// // received.handler();
-// // });
-
-
-// // this.spark[fn]("A0", function() {
-// // calls++;
-
-// // if (calls === 5) {
-// // test.ok(true);
-// // test.done();
-// // }
-// // });
-
-// // this.clock.tick(100);
-// // };
-// } else {
-
-// // *Write Tests
-// var index = action === "analogwrite" ? 10 : 0;
-// var pin = action === "analogwrite" ? "A0" : "D0";
-// var value = action === "analogwrite" ? 255 : 1;
-// var sent = action === "analogwrite" ? [2, 10, 255]: [1, 0, 1];
+ // this.clock.tick(100);
+ };
+ // exports[entry].interval = function(test) {
+ // test.expect(1);
-// exports[entry].write = function(test) {
-// test.expect(4);
+ // var calls = 0;
-// this.spark[fn](pin, value);
+ // connect(function(received) {
+ // received.handler();
+ // });
-// test.ok(this.socketwrite.calledOnce);
-// var buffer = this.socketwrite.args[0][0];
+ // this.spark[fn]("A0", function() {
+ // calls++;
-// for (var i = 0; i < sent.length; i++) {
-// test.equal(sent[i], buffer.readUInt8(i));
-// }
+ // if (calls === 5) {
+ // test.ok(true);
+ // test.done();
+ // }
+ // });
-// test.done();
-// };
+ // this.clock.tick(100);
+ // };
+ } else {
-// exports[entry].stored = function(test) {
-// test.expect(1);
+ // *Write Tests
-// this.spark[fn](pin, value);
-// test.equal(this.spark.pins[index].value, value);
+ exports[entry].write = function(test) {
+ test.expect(4);
-// test.done();
-// };
-// }
-// });
+ this.spark[fn](pin, value);
+ test.ok(this.socketwrite.calledOnce);
-// exports["Spark.prototype.servoWrite"] = {
-// setUp: function(done) {
-// done();
-// },
-// tearDown: function(done) {
-// done();
-// },
-// alias: function(test) {
-// test.expect(1);
-// test.equal(
-// Spark.prototype.servoWrite,
-// Spark.prototype.analogWrite
-// );
-// test.done();
-// }
-// };
+ var buffer = this.socketwrite.args[0][0];
+ for (var i = 0; i < sent.length; i++) {
+ test.equal(sent[i], buffer.readUInt8(i));
+ }
-// exports["Spark.prototype.pinMode"] = {
-// setUp: function(done) {
+ test.done();
+ };
-// this.clock = sinon.useFakeTimers();
+ exports[entry].stored = function(test) {
+ test.expect(1);
-// var state = {
-// isConnected: true,
-// deviceId: "deviceId",
-// token: "token",
-// service: "service",
-// port: 9000,
-// server: {},
-// socket: {
-// write: function() {}
-// },
-// timers: {},
-// interval: 20
-// };
+ this.spark[fn](pin, value);
-// this.map = sinon.stub(Map.prototype, "get").returns(state);
+ test.equal(this.spark.pins[index].value, value);
-// this.socketwrite = sinon.spy(state.socket, "write");
+ test.done();
+ };
+ }
+});
-// // command(function(received) {
-// // received.handler(null, {
-// // connected: true
-// // });
-// // });
-// this.spark = new Spark({
-// token: "token",
-// deviceId: "deviceId"
-// });
+exports["Spark.prototype.servoWrite"] = {
+ setUp: function(done) {
+ done();
+ },
+ tearDown: function(done) {
+ done();
+ },
+ alias: function(test) {
+ test.expect(1);
+ test.equal(
+ Spark.prototype.servoWrite,
+ Spark.prototype.analogWrite
+ );
+ test.done();
+ }
+};
-// done();
-// },
-// tearDown: function(done) {
-// // command.reset();
-// this.map.restore();
-// this.socketwrite.restore();
-// this.clock.restore();
+exports["Spark.prototype.pinMode"] = {
+ setUp: function(done) {
-// done();
-// },
-// analog: function(test) {
-// test.expect(4);
+ this.clock = sinon.useFakeTimers();
+ this.state = new State();
+ this.map = sinon.stub(Map.prototype, "get").returns(this.state);
+ this.socketwrite = sinon.spy(this.state.socket, "write");
+ this.connect = sinon.stub(Spark.prototype, "connect", function(handler) {
+ handler(null);
+ });
-// var sent = [0, 11, 1];
+ this.spark = new Spark({
+ token: "token",
+ deviceId: "deviceId"
+ });
-// this.spark.pinMode("A1", 1);
+ done();
+ },
+ tearDown: function(done) {
+ this.connect.restore();
+ this.map.restore();
+ this.socketwrite.restore();
+ this.clock.restore();
+
+ done();
+ },
+ analog: function(test) {
+ test.expect(4);
+
+ var sent = [0, 11, 1];
-// test.ok(this.socketwrite.calledOnce);
+ this.spark.pinMode("A1", 1);
-// var buffer = this.socketwrite.args[0][0];
+ test.ok(this.socketwrite.calledOnce);
+ var buffer = this.socketwrite.args[0][0];
-// for (var i = 0; i < sent.length; i++) {
-// test.equal(sent[i], buffer.readUInt8(i));
-// }
-// test.done();
-// },
-// digital: function(test) {
-// test.expect(4);
+ for (var i = 0; i < sent.length; i++) {
+ test.equal(sent[i], buffer.readUInt8(i));
+ }
+ test.done();
+ },
+ digital: function(test) {
+ test.expect(4);
-// var sent = [0, 0, 1];
+ var sent = [0, 0, 1];
-// this.spark.pinMode("D0", 1);
+ this.spark.pinMode("D0", 1);
-// test.ok(this.socketwrite.calledOnce);
+ test.ok(this.socketwrite.calledOnce);
-// var buffer = this.socketwrite.args[0][0];
+ var buffer = this.socketwrite.args[0][0];
-// for (var i = 0; i < sent.length; i++) {
-// test.equal(sent[i], buffer.readUInt8(i));
-// }
-// test.done();
-// }
-// };
+ for (var i = 0; i < sent.length; i++) {
+ test.equal(sent[i], buffer.readUInt8(i));
+ }
+ test.done();
+ }
+};

No commit comments for this range

Something went wrong with that request. Please try again.