Skip to content

Commit

Permalink
Motion: finalize GP2Y0D805Z0F support!
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed May 13, 2015
1 parent 3150c14 commit f6952a1
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 43 deletions.
32 changes: 12 additions & 20 deletions lib/motion.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,31 @@ var Controllers = {
},
GP2Y0D805Z0F: {
initialize: {
value: function (opts, dataHandler) {

value: function(opts, dataHandler) {
var address = opts.address || 0x26;
var state = priv.get(this);

setTimeout(function () {
// This is meaningless for GP2Y0D805Z0F.
// The event is implemented for consistency
// with the digital passive infrared sensor
setTimeout(function() {
state.isCalibrated = true;
this.emit("calibrated", null);
this.emit("calibrated");
}.bind(this), 10);

// Set up I2C data connection
this.io.i2cConfig();

// Enumerate and write each set of setup instructions
[0x3, 0xFE].forEach(function(byteArray) {
this.io.i2cWrite(address, byteArray);
}, this);

// Read Request Loop
setInterval(function() {
// Set pointer to X most signficant byte/register
this.io.i2cWrite(address, [0x0]);

// Read from register
this.io.i2cReadOnce(address, 1, dataHandler);

}.bind(this), opts.freq || 25);

this.io.i2cWriteReg(address, 0x03, 0xFE);
this.io.i2cWrite(address, [0x00]);
this.io.i2cRead(address, 1, function(data) {
dataHandler(data[0] & 0x02);
});
}
},
toBoolean: {
value: function(raw) {
return raw[0] === 1;
return raw === 0;
}
}
}
Expand Down
73 changes: 50 additions & 23 deletions test/motion.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,10 @@ exports["Motion - PIR"] = {
exports["Motion - GP2Y0D805Z0F"] = {
setUp: function(done) {
this.clock = sinon.useFakeTimers();
this.i2cReadOnce = sinon.spy(board.io, "i2cReadOnce");
this.i2cWrite = sinon.spy(board.io, "i2cWrite");
this.i2cConfig = sinon.spy(board.io, "i2cConfig");
this.i2cRead = sinon.spy(MockFirmata.prototype, "i2cRead");
this.i2cWrite = sinon.spy(MockFirmata.prototype, "i2cWrite");
this.i2cWriteReg = sinon.spy(MockFirmata.prototype, "i2cWriteReg");
this.i2cConfig = sinon.spy(MockFirmata.prototype, "i2cConfig");
this.motion = new Motion({
controller: "GP2Y0D805Z0F",
calibrationDelay: 10,
Expand All @@ -143,28 +144,24 @@ exports["Motion - GP2Y0D805Z0F"] = {
},

tearDown: function(done) {
this.motion.removeAllListeners();
this.clock.restore();
this.i2cReadOnce.restore();
this.i2cWrite.restore();
this.i2cConfig.restore();
restore(this);
done();
},

initialize: function(test) {
test.expect(5);
test.expect(9);

test.ok(this.i2cConfig.called);
test.ok(this.i2cWrite.calledTwice);
test.ok(this.i2cWriteReg.calledOnce);
test.ok(this.i2cWrite.calledOnce);
test.ok(this.i2cRead.calledOnce);

test.deepEqual(this.i2cConfig.args[0], []);
test.deepEqual(
this.i2cWrite.firstCall.args, [0x26, 0x3]
);
test.deepEqual(
this.i2cWrite.secondCall.args, [0x26, 0xFE]
);
test.deepEqual(this.i2cConfig.firstCall.args, []);
test.deepEqual(this.i2cWriteReg.firstCall.args, [ 0x26, 0x03, 0xFE ]);
test.deepEqual(this.i2cWrite.firstCall.args, [0x26, [0x00]]);

test.equal(this.i2cRead.firstCall.args[0], 0x26);
test.equal(this.i2cRead.firstCall.args[1], 1);
test.done();
},

Expand All @@ -186,12 +183,43 @@ exports["Motion - GP2Y0D805Z0F"] = {
test.done();
},

motionstart: function(test) {
change: function(test) {
var spy = sinon.spy();
var callback = this.i2cRead.firstCall.args[2];
test.expect(1);
this.motion.on("change", spy);
callback([0x00]);
this.clock.tick(25);
callback([0x03]);
this.clock.tick(25);

this.clock.tick(250);
test.ok(spy.calledTwice);
test.done();
},

var callback = this.i2cReadOnce.args[0][2];
noChange: function(test) {
test.expect(1);
var spy = sinon.spy();
var callback = this.i2cRead.firstCall.args[2];
this.motion.on("change", spy);

this.clock.tick(25);
callback([0x03]);
this.clock.tick(25);
callback([0x03]);
this.clock.tick(25);
callback([0x03]);
this.clock.tick(25);

test.ok(spy.notCalled);
test.done();
},

motionstart: function(test) {

// this.clock.tick(250);
var spy = sinon.spy();
var callback = this.i2cRead.firstCall.args[2];

test.expect(1);
this.motion.on("motionstart", spy);
Expand All @@ -206,10 +234,9 @@ exports["Motion - GP2Y0D805Z0F"] = {

motionend: function(test) {

this.clock.tick(250);

var callback = this.i2cReadOnce.args[0][2];
// this.clock.tick(250);
var spy = sinon.spy();
var callback = this.i2cRead.firstCall.args[2];

test.expect(1);
this.motion.on("motionend", spy);
Expand Down

0 comments on commit f6952a1

Please sign in to comment.