Skip to content

Commit

Permalink
Accelerometer: 100% coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information
rwaldron committed Jul 12, 2016
1 parent b92855d commit 3e37161
Show file tree
Hide file tree
Showing 2 changed files with 466 additions and 50 deletions.
83 changes: 48 additions & 35 deletions lib/accelerometer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ var sum = Fn.sum;
var toFixed = Fn.toFixed;

var priv = new Map();
var rad2deg = 180 / Math.PI;
var calibrationSize = 10;
var axes = ["x", "y", "z"];

var aX = "x";
var aY = "y";
var aZ = "z";
var axes = [aX, aY, aZ];

function analogInitialize(opts, dataHandler) {
var state = priv.get(this);
Expand Down Expand Up @@ -252,10 +255,11 @@ var Controllers = {
value: function(opts, dataHandler) {
var state = priv.get(this);

/* istanbul ignore else */
if (opts.sleepPin !== undefined) {
state.sleepPin = opts.sleepPin;
this.board.pinMode(state.sleepPin, 1);
this.board.digitalWrite(state.sleepPin, 1);
this.io.pinMode(state.sleepPin, 1);
this.io.digitalWrite(state.sleepPin, 1);
}

analogInitialize.call(this, opts, dataHandler);
Expand All @@ -268,8 +272,9 @@ var Controllers = {
value: function(value) {
var state = priv.get(this);

/* istanbul ignore else */
if (state.sleepPin !== undefined) {
this.board.digitalWrite(state.sleepPin, value ? 1 : 0);
this.io.digitalWrite(state.sleepPin, value ? 1 : 0);
}
}
}
Expand Down Expand Up @@ -515,6 +520,7 @@ var Controllers = {
this.io.i2cRead(address, this.REGISTER.STATUS, 7, function(data) {
var status = (data.shift() & 0x08) >>> 3;

/* istanbul ignore else */
if (status) {
// Page 9 (AN4076)
//
Expand All @@ -531,14 +537,17 @@ var Controllers = {
var status = data[0];
var tap = status & 0x7F;

/* istanbul ignore else */
if (status & 0x80) {
this.emit("tap");

// Single Tap
/* istanbul ignore else */
if ((tap >> 2) & 0x01) {
this.emit("tap:single");

// Double Tap (must be both S and D bits)
/* istanbul ignore else */
if ((tap >> 3) & 0x01) {
this.emit("tap:double");
}
Expand Down Expand Up @@ -667,6 +676,7 @@ var Controllers = {
16: 3
})[opts.range || 4];

/* istanbul ignore if */
if (range === undefined) {
range = 1;
}
Expand All @@ -678,6 +688,7 @@ var Controllers = {
1365,
][range];

/* istanbul ignore if */
if (divider === undefined) {
divider = 1;
}
Expand All @@ -689,6 +700,7 @@ var Controllers = {
10,
][range];

/* istanbul ignore if */
if (threshold === undefined) {
threshold = 10;
}
Expand Down Expand Up @@ -808,6 +820,7 @@ var Controllers = {
lastEmitTime = thisEmitTime - 101;
}

/* istanbul ignore if */
if (thisEmitTime < (lastEmitTime + 100)) {
return;
}
Expand All @@ -816,6 +829,7 @@ var Controllers = {
return;
}

/* istanbul ignore if */
if (!(status & 0x30)) {
return;
}
Expand Down Expand Up @@ -945,6 +959,7 @@ function Accelerometer(opts) {

priv.set(this, state);

/* istanbul ignore else */
if (typeof this.initialize === "function") {
this.initialize(opts, function(data) {
var isChange = false;
Expand All @@ -966,7 +981,7 @@ function Accelerometer(opts) {
}

state.zeroV[axisIndex] = sum(sensor.calibration) / sensor.calibration.length;
if (axis === "z") {
if (axis === aZ) {
state.zeroV[axisIndex] -= state.sensitivity;
}
}
Expand Down Expand Up @@ -1022,7 +1037,9 @@ function Accelerometer(opts) {

Object.defineProperties(this, {
hasAxis: {
writable: IS_TEST_MODE ? true : /* istanbul ignore next */ false,
value: function(axis) {
/* istanbul ignore next */
return state[axis] ? state[axis].stash.length > 0 : false;
}
},
Expand Down Expand Up @@ -1052,18 +1069,14 @@ function Accelerometer(opts) {
*/
pitch: {
get: function() {
var x, y, z, rads;

x = this.x;
y = this.y;
z = this.z;


rads = this.hasAxis("z") ?
var x = this.x;
var y = this.y;
var z = this.z;
var rads = this.hasAxis(aZ) ?
Math.atan2(x, Math.hypot(y, z)) :
Math.asin(constrain(x, -1, 1));

return toFixed(rads * rad2deg, 2);
return toFixed(rads * Fn.RAD_TO_DEG, 2);
}
},
/**
Expand All @@ -1073,33 +1086,30 @@ function Accelerometer(opts) {
*/
roll: {
get: function() {
var x, y, z, rads;

x = this.x;
y = this.y;
z = this.z;

rads = this.hasAxis("z") ?
var x = this.x;
var y = this.y;
var z = this.z;
var rads = this.hasAxis(aZ) ?
Math.atan2(y, Math.hypot(x, z)) :
Math.asin(constrain(y, -1, 1));

return toFixed(rads * rad2deg, 2);
return toFixed(rads * Fn.RAD_TO_DEG, 2);
}
},
x: {
get: function() {
return toFixed(this.toGravity(state.x.value, "x"), 2);
return toFixed(this.toGravity(state.x.value, aX), 2);
}
},
y: {
get: function() {
return toFixed(this.toGravity(state.y.value, "y"), 2);
return toFixed(this.toGravity(state.y.value, aY), 2);
}
},
z: {
get: function() {
return this.hasAxis("z") ?
toFixed(this.toGravity(state.z.value, "z"), 2) : 0;
return this.hasAxis(aZ) ?
toFixed(this.toGravity(state.z.value, aZ), 2) : 0;
}
},
acceleration: {
Expand All @@ -1113,35 +1123,38 @@ function Accelerometer(opts) {
},
inclination: {
get: function() {
return Math.atan2(this.y, this.x) * rad2deg;
return Math.atan2(this.y, this.x) * Fn.RAD_TO_DEG;
}
},
orientation: {
get: function() {
var abs = Math.abs;
var x = this.x;
var y = this.y;
var z = this.hasAxis(z) ? this.z : 1;
var xAbs = abs(x);
var yAbs = abs(y);
var zAbs = abs(z);
var z = this.hasAxis(aZ) ? this.z : 1;
var absX = abs(x);
var absY = abs(y);
var absZ = abs(z);

if (xAbs < yAbs && xAbs < zAbs) {
if (absX < absY && absX < absZ) {
if (x > 0) {
return 1;
}
return -1;
}
if (yAbs < xAbs && yAbs < zAbs) {
if (absY < absX && absY < absZ) {
if (y > 0) {
return 2;
}
return -2;
}
if (zAbs < xAbs && zAbs < yAbs) {
if (absZ < absX && absZ < absY) {
// TODO: figure out how to test this
/* istanbul ignore else */
if (z > 0) {
return 3;
}
/* istanbul ignore next */
return -3;
}
return 0;
Expand Down
Loading

0 comments on commit 3e37161

Please sign in to comment.