-
Notifications
You must be signed in to change notification settings - Fork 0
/
ledmap.ts
86 lines (76 loc) · 2.06 KB
/
ledmap.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
class LEDMap {
map: number[][];
constructor(map: number[][] | null) {
if (map == null || !this.isValidMap(map)) {
map = this.generateEmptyMap();
}
this.map = map;
}
show() {
for (let x = 0; x < LED_NUMBER; x++) {
for (let y = 0; y < LED_NUMBER; y++) {
led.plotBrightness(x, y, this.map[x][y]);
}
}
}
plotBrightness(x: number, y: number, brightness: number) {
if (
0 <= x && x < LED_NUMBER &&
0 <= y && y < LED_NUMBER
) {
this.map[x][y] = brightness;
}
}
plot(x: number, y: number) {
this.plotBrightness(x, y, 255);
}
toggle(x: number, y:number) {
if (this.map[x][y] == 0) {
this.plot(x, y);
} else {
this.plotBrightness(x, y, 0);
}
}
clear() {
this.map = this.generateEmptyMap();
}
stack(newLEDMap: LEDMap) {
for (let x = 0; x < LED_NUMBER; x++) {
for (let y = 0; y < LED_NUMBER; y++) {
this.map[x][y] += newLEDMap.map[x][y];
}
}
}
changeBrightness(brightness: number) {
for (let x = 0; x < LED_NUMBER; x++) {
for (let y = 0; y < LED_NUMBER; y++) {
this.map[x][y]
= this.map[x][y] == 0 ? 0 : brightness;
}
}
}
private isValidMap(map: number[][]): boolean {
let x = 0;
if (map.length == LED_NUMBER) {
for (; x < LED_NUMBER; x++) {
if (map[x].length != LED_NUMBER) {
break;
}
}
}
if (x == LED_NUMBER) {
return true;
}
return false;
}
private generateEmptyMap(): number[][] {
let emptyMap: number[][] = [];
for (let x = 0; x < LED_NUMBER; x++) {
emptyMap.push([]);
for (let y = 0; y < LED_NUMBER; y++) {
emptyMap[x].push(0);
}
}
return emptyMap;
}
}