Led module prototype

Change-type: patch
This commit is contained in:
Alexis Svinartchouk 2019-06-26 14:59:18 +02:00
parent 78cebdb7a4
commit 5f38cca60c
3 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,70 @@
import { delay } from 'bluebird';
import { Gpio } from 'pigpio';
class Led {
private gpio: Gpio;
constructor(gpioNumber: number) {
this.gpio = new Gpio(gpioNumber, { mode: Gpio.OUTPUT });
}
public set intensity(value: number) {
// TODO: check that 0 <= value <= 1
this.gpio.pwmWrite(Math.round(value * 255));
}
}
export type Color = [number, number, number];
export type AnimationFunction = (t: number) => Color;
export class RGBLed {
private leds: [Led, Led, Led];
private currentAnimation?: AnimationFunction;
private static animations: Map<string, AnimationFunction> = new Map();
constructor(gpioNumbers: [number, number, number], public frequency = 60) {
this.leds = gpioNumbers.map(n => new Led(n)) as [Led, Led, Led];
}
private async loop() {
while (this.currentAnimation !== undefined) {
this.$setColor(...this.currentAnimation(new Date().getTime()));
await delay(1000 / this.frequency);
}
}
private $setColor(red: number, green: number, blue: number) {
this.leds[0].intensity = red;
this.leds[1].intensity = green;
this.leds[2].intensity = blue;
}
public setColor(red: number, green: number, blue: number) {
// stop any running animation
this.setAnimation();
this.$setColor(red, green, blue);
}
public static registerAnimation(name: string, animation: AnimationFunction) {
RGBLed.animations.set(name, animation);
}
public setAnimation(name?: string) {
const hadAnimation = this.currentAnimation !== undefined;
this.currentAnimation = name ? RGBLed.animations.get(name) : undefined;
// Don't launch the loop a second time
if (!hadAnimation) {
this.loop();
}
}
}
RGBLed.registerAnimation('breathe-white', (t: number) => {
const intensity = Math.sin(t / 1000);
return [intensity, intensity, intensity];
});
RGBLed.registerAnimation('blink-white', (t: number) => {
const intensity = Math.floor(t / 1000) % 2;
return [intensity, intensity, intensity];
});

9
npm-shrinkwrap.json generated
View File

@ -1085,6 +1085,15 @@
"integrity": "sha1-qIc1gLOoS2msHmhzI7Ffu+uQR5o=",
"dev": true
},
"@types/pigpio": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/@types/pigpio/-/pigpio-1.2.1.tgz",
"integrity": "sha512-R0czS/QbWS/vU7rSp6MMcRu+zCZENcrYo/aDNDkF6RNSaKax6J+H45E9183AQtjJVb2N9VOCGrUfnHgyZ19WvQ==",
"dev": true,
"requires": {
"@types/node": "*"
}
},
"@types/prettier": {
"version": "1.16.4",
"resolved": "https://registry.npmjs.org/@types/prettier/-/prettier-1.16.4.tgz",

View File

@ -36,6 +36,7 @@
"license": "Apache-2.0",
"platformSpecificDependencies": [
"fsevents",
"pigpio",
"winusb-driver-generator"
],
"dependencies": {
@ -63,6 +64,7 @@
"nan": "^2.9.2",
"node-ipc": "^9.1.1",
"path-is-inside": "^1.0.2",
"pigpio": "^1.2.3",
"pretty-bytes": "^1.0.4",
"prop-types": "^15.5.9",
"react": "^16.8.5",
@ -88,6 +90,7 @@
"@babel/preset-react": "^7.0.0",
"@types/debug": "^4.1.4",
"@types/node": "^10.14.9",
"@types/pigpio": "^1.2.1",
"@types/react-dom": "^16.8.4",
"@types/request": "^2.48.1",
"@types/tmp": "^0.1.0",