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 11, 2018
1 parent 210aaf4 commit bff5d8a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 8 deletions.
8 changes: 4 additions & 4 deletions lib/servo.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ Servo.prototype.to = function(degrees, time, rate) {
*/

Servo.prototype[Animation.normalize] = function(keyFrames) {

var last = this.last ? this.last.target : this.startAt;

// If user passes null as the first element in keyFrames use current position
Expand Down Expand Up @@ -618,9 +618,9 @@ 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
if (servo instanceof Servos) {
servo = servo[0];
Expand All @@ -642,7 +642,7 @@ Servos.prototype[Animation.normalize] = function(keyFrameSet) {
};
}
}

return this[index][Animation.normalize](keyFrames);
}
return keyFrames;
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 bff5d8a

Please sign in to comment.