Permalink
Browse files

Merge fix.

  • Loading branch information...
2 parents bf1db90 + 4d1aa3c commit 72a818040445a780327d69a7d5e6d91f2ca13301 @voodootikigod committed Jan 24, 2014
Showing with 37 additions and 37 deletions.
  1. +7 −3 firmware/voodoospark.cpp
  2. +12 −2 lib/spark.js
  3. +2 −0 readme.md
  4. +16 −32 test/spark.js
View
10 firmware/voodoospark.cpp
@@ -108,13 +108,16 @@ void send(int action, int pin, int value) {
void report() {
for (int i = 0; i < 20; i++) {
if (reading[i]) {
- if (i < 10 && (reading[i] & 1)) {
+ int dr = (reading[i] & 1);
+ int ar = (reading[i] & 2);
+
+ if (i < 10 && dr) {
send(0x03, i, digitalRead(i));
} else {
- if (reading[i] & 1) {
+ if (dr) {
send(0x03, i, digitalRead(i));
} else {
- if (reading[i] & 2) {
+ if (ar) {
send(0x04, i, analogRead(i));
}
}
@@ -357,6 +360,7 @@ void loop() {
break;
+
case 0x40:
pin = client.read();
servos[pin].attach(pin);
View
14 lib/spark.js
@@ -59,6 +59,12 @@ function service(deviceId) {
return "https://api.spark.io/v1/devices/" + deviceId + "/";
}
+function scale(x, fromLow, fromHigh, toLow, toHigh) {
+ return (x - fromLow) * (toHigh - toLow) /
+ (fromHigh - fromLow) + toLow;
+}
+
+
function processReceived(spark, data) {
var dlength = data.length;
var length, action, pin, value, event;
@@ -80,6 +86,7 @@ function processReceived(spark, data) {
if (action === ANALOG_READ) {
pin = "A" + (pin - 10);
+ value = scale(value, 0, 255, 0, 1024);
}
if (action === DIGITAL_READ) {
@@ -268,8 +275,11 @@ Spark.prototype.pinMode = function(pin, mode) {
// TODO: Define protocol for gather this information.
["analogRead", "digitalRead"].forEach(function(fn) {
var isAnalog = fn === "analogRead";
- var action = isAnalog ? 0x04 : 0x03;
+ // Use 0x05 to get a continuous read.
+ var action = 0x05;
+ // var action = isAnalog ? 0x04 : 0x03;
var offset = isAnalog ? 10 : 0;
+ var value = isAnalog ? 2 : 1;
Spark.prototype[fn] = function(pin, handler) {
var state = priv.get(this);
@@ -279,7 +289,7 @@ Spark.prototype.pinMode = function(pin, mode) {
buffer[0] = action;
buffer[1] = pinInt;
- buffer[2] = 1;
+ buffer[2] = value;
// register a handler for
this.on(event, handler);
View
2 readme.md
@@ -1,5 +1,7 @@
# Spark-io
+[![Build Status](https://travis-ci.org/rwaldron/spark-io.png?branch=master)](https://travis-ci.org/rwaldron/spark-io)
+
Spark-io is a Firmata-compatibility IO class for writing node programs that interact with [Spark devices](http://docs.spark.io/).
### Getting Started
View
48 test/spark.js
@@ -165,9 +165,8 @@ exports["Spark"] = {
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]);
+ var sent, value;
exports[entry] = {
setUp: function(done) {
@@ -200,48 +199,33 @@ exports["Spark"] = {
// *Read Tests
if (/read/.test(action)) {
+ value = isAnalog ? 1024 : 1;
+ sent = isAnalog ?
+ [5, 10, 2] : // continuous, analog 0, analog
+ [5, 0, 1]; // continuous, digital 0, digital
exports[entry].data = function(test) {
- test.expect(1);
+ test.expect(4);
- var handler = function(value) {
- test.equal(value, receiving[2]);
+ var handler = function(data) {
+ test.equal(data, value);
test.done();
};
this.spark[fn](pin, handler);
- this.state.socket.emit("data", receiving);
-
- // this.clock.tick(100);
- };
-
- // exports[entry].interval = function(test) {
- // test.expect(1);
-
- // var calls = 0;
-
- // connect(function(received) {
- // received.handler();
- // });
-
-
- // this.spark[fn]("A0", function() {
- // calls++;
+ var buffer = this.socketwrite.args[0][0];
- // if (calls === 5) {
- // test.ok(true);
- // test.done();
- // }
- // });
+ for (var i = 0; i < sent.length; i++) {
+ test.equal(sent[i], buffer.readUInt8(i));
+ }
- // this.clock.tick(100);
- // };
+ this.state.socket.emit("data", receiving);
+ };
} else {
-
// *Write Tests
-
-
+ value = isAnalog ? 255 : 1;
+ sent = isAnalog ? [2, 10, 255] : [1, 0, 1];
exports[entry].write = function(test) {
test.expect(4);

0 comments on commit 72a8180

Please sign in to comment.