Permalink
Browse files

Improves processInput routine

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information...
1 parent b0e58fc commit 059f4f415ea516d09437de02392a009afed51560 @rwaldron rwaldron committed Jan 24, 2014
Showing with 17 additions and 21 deletions.
  1. +17 −21 lib/spark.js
View
@@ -70,32 +70,28 @@ function processReceived(spark, data) {
length = spark.buffer.length;
if (length >= 3) {
- action = spark.buffer[0];
- pin = spark.buffer[1];
- value = spark.buffer[2];
+ 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 === DIGITAL_READ ||
+ action === ANALOG_READ) {
- if (action === ANALOG_READ) {
- pin = "A" + (pin - 10);
- }
+ if (action === ANALOG_READ) {
+ pin = "A" + (pin - 10);
+ }
- if (action === DIGITAL_READ) {
- pin = "D" + pin;
- }
+ if (action === DIGITAL_READ) {
+ pin = "D" + pin;
+ }
- event = "read-" + pin;
+ event = "read-" + pin;
- spark.emit(event, value);
- }
+ spark.emit(event, value);
+ }
- if (length === 3) {
- // If the buffer was exactly 3 bytes...
- spark.buffer.length = 0;
- } else {
- // If the buffer was 3 or more...
- spark.buffer = spark.buffer.slice(3);
+ length = spark.buffer.length;
}
}
}
@@ -150,7 +146,7 @@ function Spark(opts) {
this.emit("ready");
- if (state.socket) {
+ if (state.socket && !state.isReading) {
state.isReading = true;
state.socket.on("data", function(data) {
processReceived(this, data);

0 comments on commit 059f4f4

Please sign in to comment.