Skip to content

Commit

Permalink
Fix for rwaldron#1499
Browse files Browse the repository at this point in the history
If an array of keyFrames are passed to Servos, those keyFrames should be used as values for the whole collection and should not be normalized as a keyFrameSet.
  • Loading branch information
dtex committed Sep 15, 2018
1 parent 210aaf4 commit 2522c85
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 7 deletions.
6 changes: 4 additions & 2 deletions lib/animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ Animation.prototype.tweenedValue = function(indices, progress) {
// Find our progress for the current tween
tween.duration = this.cuePoints[memberIndices.right] - this.cuePoints[memberIndices.left];
tween.progress = (progress - this.cuePoints[memberIndices.left]) / tween.duration;

// Catch divide by zero
if (!Number.isFinite(tween.progress)) {
/* istanbul ignore next */
Expand All @@ -459,7 +459,7 @@ Animation.prototype.tweenedValue = function(indices, progress) {

// Calculate this tween value
var calcValue;

if (right.position) {
// This is a tuple
calcValue = right.position.map(function(value, index) {
Expand All @@ -483,6 +483,8 @@ Animation.prototype.tweenedValue = function(indices, progress) {
return result;
};

Animation.prototype

// Make sure our keyframes conform to a standard
Animation.prototype.normalizeKeyframes = function() {

Expand Down
9 changes: 8 additions & 1 deletion lib/servo.js
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ Collection.installMethodForwarding(
*/
Servos.prototype[Animation.normalize] = function(keyFrameSet) {
return keyFrameSet.map(function(keyFrames, index) {
if (keyFrames !== null) {
if (keyFrames !== null && Array.isArray(keyFrames)) {
var servo = this[index];

// If servo is a servoArray then user servo[0] for default values
Expand All @@ -645,7 +645,14 @@ Servos.prototype[Animation.normalize] = function(keyFrameSet) {

return this[index][Animation.normalize](keyFrames);
}

if (typeof keyFrames.degrees === "number") {
keyFrames.value = keyFrames.degrees;
delete keyFrames.degrees;
}

return keyFrames;

}, this);
};

Expand Down
8 changes: 4 additions & 4 deletions test/extended/servo.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ exports["Servo"] = {

this.servo.on("move:complete", function() {
test.equal(this.servo.position, 0);
test.ok(this.servoWrite.callCount === 101);
test.ok(this.servoWrite.callCount - 100 <= 1);
test.done();
}.bind(this));
},
Expand All @@ -163,7 +163,7 @@ exports["Servo"] = {

this.servo.on("move:complete", function() {
test.equal(this.servo.position, 180);
test.ok(this.servoWrite.callCount === 101);
test.ok(this.servoWrite.callCount - 100 <= 1);
test.done();
}.bind(this));
},
Expand Down Expand Up @@ -217,7 +217,7 @@ exports["Servo"] = {

this.servo.on("move:complete", function() {
test.equal(this.servo.value, 80);
test.equal(this.servoWrite.lastCall.args[1], 70);
test.equal(this.servoWrite.lastCall.args[1], 1300);
test.done();
}.bind(this));

Expand All @@ -237,7 +237,7 @@ exports["Servo"] = {

this.servo.on("move:complete", function() {
test.equal(this.servo.value, 80);
test.equal(this.servoWrite.lastCall.args[1], 110);
test.equal(this.servoWrite.lastCall.args[1], 1700);
test.done();
}.bind(this));

Expand Down
53 changes: 53 additions & 0 deletions test/servo.collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ exports["Servo.Collection"] = {
pin: 9,
board: this.board
});

this.d = new Servo({
pin: 11,
board: this.board
});

this.spies = [
"to", "stop"
Expand Down Expand Up @@ -127,6 +132,54 @@ exports["Servo.Collection"] = {
test.done();
},

"Animation.normalize-nested": function(test) {
test.expect(1);

var group1 = new Servos([
this.a, this.b
]);

var group2 = new Servos([
this.c, this.d
]);

var bothGroups = new Servos([
group1, group2
]);

var normalized = bothGroups[Animation.normalize]([
[
[
null,
10,
]
],
[
[
null,
20,
]
]
]);

test.deepEqual(normalized, [
[
[
{ value: 90, easing: "linear" },
{ step: 10, easing: "linear" }
]
],
[
[
{ value: 90, easing: "linear" },
{ step: 20, easing: "linear" }
]
]
]);

test.done();
},

"Animation.normalize": function(test) {
test.expect(3);

Expand Down

0 comments on commit 2522c85

Please sign in to comment.