Skip to content

Commit

Permalink
Remove temporal, add toBoolean
Browse files Browse the repository at this point in the history
  • Loading branch information
derekwheee committed May 9, 2015
1 parent 829a5a3 commit 954cf70
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
28 changes: 16 additions & 12 deletions lib/motion.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var Board = require("../lib/board.js"),
events = require("events"),
util = require("util"),
temporal = require("temporal"),
priv = new Map();

var Controllers = {
Expand All @@ -10,20 +9,25 @@ var Controllers = {
value: function (opts, dataHandler) {

var state = priv.get(this);
var calibrationDelay = "calibrationDelay" in opts ? opts.calibrationDelay : 30000;
var calibrationDelay = typeof opts.calibrationDelay !== "undefined" ? opts.calibrationDelay : 2000;

this.io.pinMode(opts.pin, this.io.MODES.INPUT);

temporal.delay(calibrationDelay, function() {
setTimeout(function() {
state.isCalibrated = true;
this.emit("calibrated", null);
}.bind(this));
}.bind(this), calibrationDelay);

this.io.digitalRead(opts.pin, function(data) {
dataHandler.call(this, !!data);
dataHandler.call(this, data);
}.bind(this));

}
},
toBoolean: {
value: function(raw) {
return !!raw;
}
}
},
};
Expand Down Expand Up @@ -72,7 +76,7 @@ function Motion(opts) {
Object.defineProperties(this, controller);

state = {
detectedMotion: false,
value: false,
isCalibrated: false
};

Expand All @@ -86,7 +90,7 @@ function Motion(opts) {
*/
detectedMotion: {
get: function() {
return state.detectedMotion;
return this.toBoolean(state.value);
}
},
/**
Expand All @@ -103,29 +107,29 @@ function Motion(opts) {

if (typeof this.initialize === "function") {
this.initialize(opts, function(data) {
state.detectedMotion = data;
state.value = data;
});
}

setInterval(function() {

var eventData = {
timestamp : Date.now(),
detectedMotion : state.detectedMotion,
detectedMotion : this.detectedMotion,
isCalibrated : state.isCalibrated
};

if (state.isCalibrated && state.detectedMotion && !last) {
if (state.isCalibrated && this.detectedMotion && !last) {
this.emit("motionstart", eventData);
}

if (state.isCalibrated && !state.detectedMotion && last) {
if (state.isCalibrated && !this.detectedMotion && last) {
this.emit("motionend", eventData);
}

this.emit("data", eventData);

last = state.detectedMotion;
last = this.detectedMotion;

}.bind(this), freq);
}
Expand Down
9 changes: 9 additions & 0 deletions test/motion.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ exports["Motion"] = {
test.done();
},

calibrated: function(test) {
var spy = sinon.spy();
test.expect(1);
this.motion.on("calibrated", spy);
this.clock.tick(10);
test.ok(spy.calledOnce);
test.done();
},

data: function(test) {
var spy = sinon.spy();
test.expect(1);
Expand Down

0 comments on commit 954cf70

Please sign in to comment.