Skip to content

Commit

Permalink
Accelerometer: MMA7660 support
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Sep 7, 2015
1 parent efe2402 commit ff2ab60
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
51 changes: 51 additions & 0 deletions lib/accelerometer.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,57 @@ var Controllers = {
}
}
},
MMA7660: {
ADDRESSES: {
value: [0x4C]
},
REGISTER: {
value: {
READREGISTER: 0x00,
RATE: 0x08,
MODE: 0x07,
}
},
initialize: {
value: function(opts, dataHandler) {
var READLENGTH = 3;
var address = opts.address || this.ADDRESSES[0];
var state = priv.get(this);

state.sensitivity = 21.33;

this.io.i2cConfig(opts);

// http://www.freescale.com.cn/files/sensors/doc/data_sheet/MMA7660FC.pdf?fpsp=1
//
// Standby mode
this.io.i2cWrite(address, this.REGISTER.MODE, 0x00);

// Sample Rate ()
this.io.i2cWrite(address, this.REGISTER.RATE, 0x07);

// Active Mode
this.io.i2cWrite(address, this.REGISTER.MODE, 0x01);

this.io.i2cRead(address, this.REGISTER.READREGISTER, READLENGTH, function(data) {
dataHandler.call(this, {
// Shift off the sign bits
// This solution is used in
// https://github.com/intel-iot-devkit/upm/blob/master/src/mma7660/mma7660.cxx
x: (data[0] << 2) / 4,
y: (data[1] << 2) / 4,
z: (data[2] << 2) / 4,
});
}.bind(this));
},
},
toGravity: {
value: function(raw) {
var state = priv.get(this);
return raw / state.sensitivity;
}
}
},
ESPLORA: {
DEFAULTS: {
value: {
Expand Down
85 changes: 85 additions & 0 deletions test/accelerometer.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,91 @@ exports["Accelerometer -- MMA7361"] = {
}
};

exports["Accelerometer -- MMA7660"] = {

setUp: function(done) {
this.board = newBoard();
this.clock = sinon.useFakeTimers();
this.i2cConfig = sinon.spy(MockFirmata.prototype, "i2cConfig");
this.i2cWrite = sinon.spy(MockFirmata.prototype, "i2cWrite");
this.i2cRead = sinon.spy(MockFirmata.prototype, "i2cRead");
this.accel = new Accelerometer({
controller: "MMA7660",
board: this.board
});

done();
},

tearDown: function(done) {
Board.purge();
restore(this);
done();
},

fwdOptionsToi2cConfig: function(test) {
test.expect(3);

this.i2cConfig.reset();

new Accelerometer({
controller: "MMA7660",
address: 0xff,
bus: "i2c-1",
board: this.board
});

var forwarded = this.i2cConfig.lastCall.args[0];

test.equal(this.i2cConfig.callCount, 1);
test.equal(forwarded.address, 0xff);
test.equal(forwarded.bus, "i2c-1");

test.done();
},

data: function(test) {
var read, dataSpy = sinon.spy(), changeSpy = sinon.spy();

// test.expect(12);
this.accel.on("data", dataSpy);
this.accel.on("change", changeSpy);

read = this.i2cRead.args[0][3];
read([
0x01, 0x01, 0x01
]);

test.ok(this.i2cConfig.calledOnce);

test.ok(this.i2cWrite.calledThrice);
test.deepEqual(this.i2cWrite.getCall(0).args, [ 76, 7, 0 ]);
test.deepEqual(this.i2cWrite.getCall(1).args, [ 76, 8, 7 ]);
test.deepEqual(this.i2cWrite.getCall(2).args, [ 76, 7, 1 ]);

test.ok(this.i2cRead.calledOnce);
test.deepEqual(this.i2cRead.getCall(0).args.slice(0, 3), [ 76, 0, 3 ]);

this.clock.tick(100);

test.ok(dataSpy.calledOnce);
test.deepEqual(dataSpy.args[0], [{
x: 1,
y: 1,
z: 1
}]);

test.ok(changeSpy.calledOnce);
test.deepEqual(changeSpy.args[0], [{
x: 0.047,
y: 0.047,
z: 0.047
}]);

test.done();
}
};

exports["Accelerometer -- ESPLORA"] = {
setUp: function(done) {
this.board = newBoard();
Expand Down

0 comments on commit ff2ab60

Please sign in to comment.