Skip to content

Commit

Permalink
Merge fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Williams committed Jan 24, 2014
2 parents bf1db90 + 4d1aa3c commit 72a8180
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 37 deletions.
10 changes: 7 additions & 3 deletions firmware/voodoospark.cpp
Expand Up @@ -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));
}
}
Expand Down Expand Up @@ -357,6 +360,7 @@ void loop() {
break;



case 0x40:
pin = client.read();
servos[pin].attach(pin);
Expand Down
14 changes: 12 additions & 2 deletions lib/spark.js
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions 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
Expand Down
48 changes: 16 additions & 32 deletions test/spark.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 72a8180

Please sign in to comment.