blinkstick-ts
Version:
BlinkStick TypeScript implementation
177 lines (176 loc) • 7.48 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PulseAnimator = exports.MorphAnimator = exports.BlinkAnimator = void 0;
const promises_1 = require("timers/promises");
const color_1 = require("./color");
/**
* Blinks specified RGB color.
*
* Function supports the following overloads:
*
* @example
* //Available overloads
* blink(red, green, blue, [options], [callback]); // use [0..255] ranges for intensity
*
* blink(color, [options], [callback]); // use '#rrggbb' format
*
* blink(color_name, [options], [callback]); // use 'random', 'red', 'green', 'yellow' and other CSS supported names
*
* Options can contain the following parameters for object:
*
* - channel=0: Channel is represented as 0=R, 1=G, 2=B
* - index=0: The index of the LED
* - repeats=1: How many times to blink
* - delay=1: Delay between on/off cycles in milliseconds
*
* @param {Led} led The led to blink
* @param {Color} color The Color to blink
* @param {Number} [repeats] How many times to blink
* @param {Number} [delay] Delay between on/off cycles in milliseconds, defaults to 10
*/
class BlinkAnimator {
constructor(led, color, repeats = 1, delay = 10) {
this.enabled = false;
this.count = 0;
this.led = led;
this.color = color;
this.repeats = repeats;
this.delay = delay;
}
animate() {
return __awaiter(this, void 0, void 0, function* () {
var _a, e_1, _b, _c;
try {
for (var _d = true, _e = __asyncValues((0, promises_1.setInterval)(this.delay)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
_c = _f.value;
_d = false;
const startTime = _c;
const color = this.count % 1 == 1 ? color_1.Colors.black : this.color;
this.led.setColor(color);
if (!this.enabled || this.count == this.repeats - 1) {
return;
}
this.count++;
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
}
finally { if (e_1) throw e_1.error; }
}
});
}
}
exports.BlinkAnimator = BlinkAnimator;
;
/**
* Morphs to specified RGB color from current color.
*
* Function supports the following overloads:
*
* @example
* //Available overloads
* morph(red, green, blue, [options], [callback]); // use [0..255] ranges for intensity
*
* morph(color, [options], [callback]); // use '#rrggbb' format
*
* morph(color_name, [options], [callback]); // use 'random', 'red', 'green', 'yellow' and other CSS supported names
*
* @param {Led} led The led to morph
* @param {Color} color The Color to morph to
* @param {Number} [duration] How long should the morph animation last in milliseconds, defaults to 1000
* @param {Number} [steps] How many steps for color changes, defaults to 50
*/
class MorphAnimator {
constructor(led, color, duration = 1000, steps = 50) {
this.enabled = false;
this.count = 0;
this.led = led;
this.color = color;
this.duration = duration;
this.steps = steps;
}
animate() {
return __awaiter(this, void 0, void 0, function* () {
var _a, e_2, _b, _c;
const startColor = this.led.getColor();
try {
for (var _d = true, _e = __asyncValues((0, promises_1.setInterval)(this.duration / this.steps)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
_c = _f.value;
_d = false;
const startTime = _c;
const factor = this.steps * this.count;
const color = new color_1.Color(startColor.red + (this.color.red - startColor.red) / factor, startColor.green + (this.color.green - startColor.green) / factor, startColor.blue + (this.color.blue - startColor.blue) / factor);
this.led.setColor(color);
if (!this.enabled || this.count >= this.steps) {
return;
}
this.count++;
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
}
finally { if (e_2) throw e_2.error; }
}
});
}
}
exports.MorphAnimator = MorphAnimator;
;
/**
* Pulses specified RGB color.
*
* Function supports the following overloads:
*
* @example
* //Available overloads
* pulse(red, green, blue, [options], [callback]); // use [0..255] ranges for intensity
*
* pulse(color, [options], [callback]); // use '#rrggbb' format
*
* pulse(color_name, [options], [callback]); // use 'random', 'red', 'green', 'yellow' and other CSS supported names
*
* @param {Led} led The led to pulse
* @param {Color} color The Color to pulse to first
* @param {Color} color The Color to pulse to afterwards, defaults to black
* @param {Number} [duration] How long should the pulse animation last in milliseconds, defaults to 1000
* @param {Number} [steps] How many steps for color changes, defaults to 50
*/
class PulseAnimator {
constructor(led, color1, color2 = color_1.Colors.black, duration = 1000, steps = 50) {
this.led = led;
this.color1 = color1;
this.color2 = color2;
this.duration = duration;
this.steps = steps;
}
animate() {
return __awaiter(this, void 0, void 0, function* () {
yield new MorphAnimator(this.led, this.color1, this.duration / 2, this.steps).animate();
yield new MorphAnimator(this.led, this.color2, this.duration / 2, this.steps).animate();
});
}
}
exports.PulseAnimator = PulseAnimator;
;