This repository was archived by the owner on Sep 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDotstarStripController.ts
87 lines (78 loc) · 2.07 KB
/
DotstarStripController.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { IStripController } from "./IStripController";
import { Dotstar } from "dotstar";
const DOT = require("dotstar");
const SPI = require("pi-spi");
/**
* Controller that takes all calls to a Dotstar-Strip and forwards them to the Library
*/
export default class DotstarStripController implements IStripController {
dotstar: Dotstar;
spi;
/**
* Controller that takes all calls to a Dotstar-Strip and forwards them to the Library
* @param params Parameters passed to main application
*/
constructor(params: Array<string>) {
if (!params["spi"]) {
const err : Error = new Error(`When using DotstarStripController you need to specify a "spi" parameter pointing to the SPI-Device and Bus you want to use`);
err["type"] = "parameter";
throw err;
}
this.spi = SPI.initialize(params["spi"]);
this.dotstar = new DOT.Dotstar(this.spi, {length: params["ledcount"]});
console.log('DotstarStripController initialized at: %s', params["spi"]);
}
/**
* Sets the whole strip
* @param r Red
* @param g Green
* @param b Blue
* @param a Alpha
*/
all(r: number, g: number, b: number, a: number): void {
this.dotstar.all(r, g, b, a);
}
/**
* Sets a specific LED
* @param led Index of LED
* @param r Red
* @param g Green
* @param b Blue
* @param a Alpha
*/
set(led: number, r: number, g: number, b: number, a: number): void {
this.dotstar.set(led, r, g, b, a);
}
/**
* Applies changes to LED-Strip
*/
sync(): void {
this.dotstar.sync();
}
/**
* Clears color buffer (all off)
*/
clear(): void {
this.dotstar.clear();
}
/**
* Clears the LED-Strip while retaining the colors in buffer
* kinda like pausing the LEDs
*/
off(): void {
this.dotstar.off();
}
/**
* Shuts down all Strip-Controller relevant connections like spi
*/
shutdown(callback: Function): void {
this.spi.close(callback);
}
/**
* Returns the length of the LED-Strip
* @returns {number} Length of LED-Strip
*/
getLength(): number {
return this.dotstar.length;
}
}