Skip to content

Commit

Permalink
progress: write and read-loop
Browse files Browse the repository at this point in the history
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information
rwaldron committed Jan 24, 2014
1 parent 7a173bf commit 702f34f
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 328 deletions.
6 changes: 3 additions & 3 deletions eg/blink.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions eg/read.js
Original file line number Diff line number Diff line change
@@ -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 );
});
});
6 changes: 3 additions & 3 deletions firmware/firmware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
136 changes: 54 additions & 82 deletions lib/spark.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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";
Expand All @@ -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);

Expand All @@ -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);
}
};

Expand Down Expand Up @@ -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)) :
Expand All @@ -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";
Expand All @@ -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;
Expand All @@ -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;
};
Expand All @@ -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;
};
Expand Down
Loading

0 comments on commit 702f34f

Please sign in to comment.