diff --git a/lib/led/rgb.js b/lib/led/rgb.js index e6c0c837d..ae35843d4 100644 --- a/lib/led/rgb.js +++ b/lib/led/rgb.js @@ -215,6 +215,7 @@ class RGB { } }, update: { + writable: true, value(colors) { const state = priv.get(this); @@ -282,6 +283,9 @@ class RGB { if (!this.isOn) { /* istanbul ignore next */ this.update(state.prev); + } else if (state.isRunning) { + this.stop(); + this.update(state.prev); } return this; @@ -486,7 +490,9 @@ class RGB { */ [Animation.render](frames) { - return this.update(frames[0]); + const state = priv.get(this); + state.value = frames[0]; + return this.update(state.value); } diff --git a/package.json b/package.json index 7e72496ef..f5b057888 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@code-dot-org/johnny-five", "description": "Code.org fork of rwaldron/johnny-five: The JavaScript Robotics and Hardware Programming Framework. Use with: Arduino (all models), Electric Imp, Beagle Bone, Intel Galileo & Edison, Linino One, Pinoccio, pcDuino3, Raspberry Pi, Particle/Spark Core & Photon, Tessel 2, TI Launchpad and more!", - "version": "2.1.0-cdo.1", + "version": "2.1.0-cdo.2", "homepage": "https://johnny-five.io", "author": "Rick Waldron ", "keywords": [ diff --git a/test/rgb.collection.js b/test/rgb.collection.js index 1b8ba59c5..da4267e7e 100644 --- a/test/rgb.collection.js +++ b/test/rgb.collection.js @@ -291,22 +291,18 @@ exports["RGB.Collection"] = { "Animation.render": function(test) { - test.expect(1); - - const rgbs = new RGB.Collection([ - [1, 2, 3], - [4, 5, 6], - ]); - - this.color = this.sandbox.stub(RGB.prototype, "color"); + test.expect(3); + this.render = this.sandbox.stub(RGB.prototype, "@@render"); + const rgbs = new RGB.Collection([ this.a, this.b ]); rgbs[Animation.render]([ { red: 0xff, green: 0x00, blue: 0x00 }, { red: 0x00, green: 0xff, blue: 0x00 }, ]); - test.equal(this.color.callCount, 2); + test.equal(this.render.callCount, 2); + test.deepEqual(this.render.firstCall.args[0], [ { red: 255, green: 0, blue: 0 } ]); + test.deepEqual(this.render.secondCall.args[0], [ { red: 0, green: 255, blue: 0 } ]); test.done(); }, }; - diff --git a/test/rgb.js b/test/rgb.js index 536dcbdd4..43abe92f1 100644 --- a/test/rgb.js +++ b/test/rgb.js @@ -12,6 +12,8 @@ const rgbProtoProperties = [{ name: "strobe" }, { name: "blink" +}, { + name: "pulse" }, { name: "stop" }]; @@ -44,7 +46,7 @@ exports["RGB"] = { }); this.write = this.sandbox.spy(this.rgb, "write"); - + this.enqueue = this.sandbox.stub(Animation.prototype, "enqueue"); done(); }, @@ -469,6 +471,72 @@ exports["RGB"] = { test.done(); }, + pulse(test) { + test.expect(1); + this.rgb.color("#0000ff"); + this.rgb.pulse(); + test.equal(this.enqueue.callCount, 1); + test.done(); + }, + + pulseDuration(test) { + test.expect(2); + + this.rgb.pulse(1010); + + test.equal(this.enqueue.callCount, 1); + + const duration = this.enqueue.lastCall.args[0].duration; + + test.equal(duration, 1010); + test.done(); + }, + + pulseCallback(test) { + test.expect(2); + + const spy = this.sandbox.spy(); + + this.rgb.pulse(spy); + + test.equal(this.enqueue.callCount, 1); + + const onloop = this.enqueue.lastCall.args[0].onloop; + + onloop(); + + test.equal(spy.callCount, 1); + test.done(); + }, + + pulseDurationCallback(test) { + test.expect(3); + + const spy = this.sandbox.spy(); + + this.rgb.pulse(1010, spy); + + test.equal(this.enqueue.callCount, 1); + + const duration = this.enqueue.lastCall.args[0].duration; + const onloop = this.enqueue.lastCall.args[0].onloop; + + onloop(); + + test.equal(duration, 1010); + test.equal(spy.callCount, 1); + test.done(); + }, + + pulseObject(test) { + test.expect(1); + + this.rgb.color("#0000ff"); + this.rgb.pulse({}); + test.equal(this.enqueue.callCount, 1); + test.done(); + }, + toggle(test) { test.expect(7); @@ -749,9 +817,9 @@ exports["RGB"] = { "Animation.render"(test) { test.expect(1); - this.color = this.sandbox.stub(this.rgb, "color"); + this.update = this.sandbox.stub(this.rgb, "update"); this.rgb[Animation.render]([0]); - test.equal(this.color.callCount, 1); + test.equal(this.update.callCount, 1); test.done(); }, @@ -876,11 +944,14 @@ exports["RGB - Cycling Operations"] = { }, rgbCallsStopBeforeNextCyclingOperation(test) { - test.expect(1); + test.expect(2); this.rgb.blink(); + this.rgb.pulse(); - test.equal(this.stop.callCount, 1); + test.equal(this.stop.callCount, 2); + // pulse is an animation + test.equal(this.enqueue.callCount, 1); // Ensure that the interval is cleared. this.rgb.stop();