Permalink
Browse files

progress: write and read-loop

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information...
1 parent 7a173bf commit 702f34f63941cf1d51c0ed803487772b86d94353 @rwaldron rwaldron committed Jan 24, 2014
Showing with 303 additions and 328 deletions.
  1. +3 −3 eg/blink.js
  2. +13 −0 eg/read.js
  3. +3 −3 firmware/firmware.cpp
  4. +54 −82 lib/spark.js
  5. +230 −240 test/spark.js
View
@@ -1,7 +1,7 @@
var Spark = require("../lib/spark");
var board = new Spark({
- token: "YOUR TOKEN HERE",
- deviceId: "YOUR TOKEN HERE"
+ token: "608fd30995205529ffc186d4018a651d253af9a9",
+ deviceId: "53ff6f065067544840551187"
});
board.on("ready", function() {
@@ -13,7 +13,7 @@ board.on("ready", function() {
setInterval(function() {
this.digitalWrite("D7", (byte ^= 1));
- }.bind(this), 100);
+ }.bind(this), 500);
});
board.on("error", function(error) {
View
@@ -0,0 +1,13 @@
+var Spark = require("../lib/spark");
+var board = new Spark({
+ token: "608fd30995205529ffc186d4018a651d253af9a9",
+ deviceId: "53ff6f065067544840551187"
+});
+
+board.on("ready", function() {
+ console.log("CONNECTED");
+
+ this.digitalRead("D7", function(data) {
+ console.log( data );
+ });
+});
View
@@ -85,14 +85,14 @@ void loop() {
// parse and execute commands
int action = client.read();
+
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 won't compile and flash
+ // 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
// int type, speed, len, i;
switch (action) {
View
@@ -15,11 +15,14 @@ function service(deviceId) {
if (interfaces) {
ipAddress = Object.keys(interfaces).reduce(function(accum, name) {
- return interfaces[name].reduce(function(ip, iface) {
- if (!iface.internal && iface.family === "IPv4") {
- return iface.address;
- }
- }, "");
+ if (!accum) {
+ return interfaces[name].reduce(function(ip, iface) {
+ if (!ip && !iface.internal && iface.family === "IPv4") {
+ return iface.address;
+ }
+ return ip;
+ }, "");
+ }
}, null);
}
@@ -65,15 +68,14 @@ function Spark(opts) {
}
var state = {
- isConnected: true,
+ isConnected: false,
+ isReading: false,
deviceId: opts.deviceId,
token: opts.token,
service: service(opts.deviceId),
- port: opts.port || 9000,
+ port: opts.port || 8001,
server: null,
- socket: null,
- timers: {},
- interval: 20
+ socket: null
};
this.name = "spark-io";
@@ -91,13 +93,6 @@ function Spark(opts) {
return i;
});
- // Needs to happen because es6-collections is a fucking
- // busted piece of shit and doesn't implement Map.prototype
- // methods on the prototype.
- if (!priv) {
-
- }
-
// Store private state
priv.set(this, state);
@@ -107,39 +102,44 @@ function Spark(opts) {
action: "connect",
pin: ipAddress + ":" + state.port,
handler: function(error, data) {
- if (error === null) {
- this.emit("ready");
- } else {
+ console.log( "connect -> command -> handler" );
+ if (error !== null) {
this.emit("error", error);
+ } else {
+ // Set ready state bit
+ this.isReady = true;
+
+ this.emit("ready");
}
}.bind(this)
});
}.bind(this);
- Spark.Server.create(this, function() {
- process.nextTick(connect);
- });
+ Spark.Server.create(this, connect);
}
Spark.Server = {
- create: function(spark, onCreated) {
+ create: function(spark, afterCreate) {
if (!(spark instanceof Spark)) {
throw new Error("Expected instance of Spark");
}
var state = priv.get(spark);
- var server = net.createServer(function(socket) {
+ state.server = net.createServer({}, function(socket) {
socket.setKeepAlive(true);
- // Set ready state bit
- spark.isReady = true;
+ spark.emit("connected");
// Store socket and server references
state.socket = socket;
- state.server = server;
- onCreated();
+ state.socket.on("data", function() {
+ console.log( "SOCKET DATA: ", arguments );
+ });
});
- server.listen(state.port);
+
+ state.server.listen(state.port);
+
+ process.nextTick(afterCreate);
}
};
@@ -190,18 +190,7 @@ Spark.prototype.command = function(opts) {
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;
- }
+ console.log( url + action, outbound );
request = method === "get" ?
rest.get(url + "?" + querystring.encode(outbound)) :
@@ -224,12 +213,17 @@ Spark.prototype.command = function(opts) {
return this;
};
+Spark.prototype.pinMode = function(pin, mode) {
+ var state = priv.get(this);
+ var offset = pin[0] === "A" ? 10 : 0;
+ var pinInt = (pin.replace(/A|D/, "") | 0) + offset;
+
+ this.pins[pinInt].mode = mode;
-// TODO: Replace this crap with something that isn"t horrible.
-Spark.prototype.command.isTest = false;
-Spark.prototype.command.stub = function() {};
+ state.socket.write(new Buffer([ 0x00, pinInt, mode ]));
-// replace this.command with socket.write changes.
+ return this;
+};
["analogWrite", "digitalWrite"].forEach(function(fn) {
var isAnalog = fn === "analogWrite";
@@ -245,6 +239,7 @@ Spark.prototype.command.stub = function() {};
buffer[1] = pinInt;
buffer[2] = value;
+ console.log( "writing: ", buffer );
state.socket.write(buffer);
this.pins[pinInt].value = value;
@@ -256,36 +251,25 @@ Spark.prototype.command.stub = function() {};
// TODO: Define protocol for gather this information.
["analogRead", "digitalRead"].forEach(function(fn) {
- var action = fn.toLowerCase();
+ var isAnalog = fn === "analogWrite";
+ var action = isAnalog ? 0x04 : 0x03;
+ var offset = isAnalog ? 10 : 0;
Spark.prototype[fn] = function(pin, handler) {
var state = priv.get(this);
- var key = action + "-" + pin;
- // var timer = state.timers[key];
-
- // TODO: REFACTOR ALL OF THIS CRAP TO SOCKET + BUFFER
- //
- // if (timer) {
- // clearInterval(timer);
- // }
-
- // timer = setInterval(function() {
- // this.command({
- // action: action,
- // pin: pin,
- // handler: handler
- // });
- // }.bind(this), state.interval);
-
- // state.timers[key] = timer;
-
-
+ var buffer = new Buffer(3);
+ var pinInt = (pin.replace(/A|D/i, "") | 0) + offset;
+ var event = fn.slice(0, -4) + "-" + pin;
- // 1. create handler
- // 2. send write request
+ buffer[0] = action;
+ buffer[1] = pinInt;
+ buffer[2] = 1;
- // state
+ // register a handler for
+ this.on(event, handler);
+ // Tell the board we have a new pin to read
+ state.socket.write(buffer);
return this;
};
@@ -306,18 +290,6 @@ Spark.prototype.setSamplingInterval = function(interval) {
return this;
};
-Spark.prototype.pinMode = function(pin, mode) {
- var state = priv.get(this);
- var offset = pin[0] === "A" ? 10 : 0;
- var pinInt = (pin.replace(/A|D/, "") | 0) + offset;
-
- this.pins[pinInt].mode = mode;
-
- state.socket.write(new Buffer([ 0x00, pinInt, mode ]));
-
- return this;
-};
-
Spark.prototype.reset = function() {
return this;
};
Oops, something went wrong.

0 comments on commit 702f34f

Please sign in to comment.