-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathneomatrix.js
More file actions
59 lines (56 loc) · 1.47 KB
/
neomatrix.js
File metadata and controls
59 lines (56 loc) · 1.47 KB
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
import NeoPixel from 'neopixel'
const TIMING_WS2812B = {
mark: { level0: 1, duration0: 900, level1: 0, duration1: 350 },
space: { level0: 1, duration0: 350, level1: 0, duration1: 900 },
reset: { level0: 0, duration0: 100, level1: 0, duration1: 100 }
}
Object.freeze(TIMING_WS2812B)
Object.freeze(TIMING_WS2812B.mark)
Object.freeze(TIMING_WS2812B.space)
Object.freeze(TIMING_WS2812B.reset)
export class NeoMatrix {
constructor ({ height, width, pin, timing, order, brightness }) {
this.length = height * width
this.height = height
this.width = width
this.timing = timing || TIMING_WS2812B
this.neoPixel = new NeoPixel({
length: this.length,
pin,
timing: this.timing,
order
})
this.neoPixel.brightness = brightness || 32
}
setPixel (x, y, color) {
const a = x * this.width
const b = x & 1 ? this.height - y - 1 : y
const i = a + b
this.neoPixel.setPixel(i, color)
}
fill (color, index, count) {
if (index == null) {
this.neoPixel.fill(color)
} else if (count == null) {
this.neoPixel.fill(color, index)
} else {
this.neoPixel.fill(color, index, count)
}
}
update () {
this.neoPixel.update()
}
set brightness (b) {
this.neoPixel.brightness = b
}
makeRGB (r, g, b, w) {
return this.neoPixel.makeRGB(r, g, b, w)
}
makeHSB (h, s, b, w) {
return this.neoPixel.makeHSB(h, s, b, w)
}
close () {
this.neoPixel.close()
}
}
Object.freeze(NeoMatrix.prototype)