-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
62 lines (53 loc) · 1.17 KB
/
index.js
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
const { PIO, ASM, StateMachine } = require("rp2");
class NeoPixel {
constructor(pin, len, options = {}) {
this.pin = pin;
this.length = len;
this.smId = options.sm ?? StateMachine.getAvailableId();
this.hz = options.hz || 800000;
this.buf = new Uint32Array(this.length);
this.buf.fill(0);
const asm = new ASM({ sideset: 1 });
asm
.label("bitloop")
.out("x", 1)
.side(0)
.delay(2)
.jmp("!x", "do_zero")
.side(1)
.delay(1)
.label("do_one")
.jmp("bitloop")
.side(1)
.delay(4)
.label("do_zero")
.nop()
.side(0)
.delay(4);
this.sm = new StateMachine(this.smId, asm, {
freq: this.hz * 10,
autopull: true,
pullThreshold: 24,
fifoJoin: PIO.FIFO_JOIN_TX,
sidesetBase: this.pin,
outShiftDir: PIO.SHIFT_LEFT,
});
this.sm.active(true);
}
color(r, g, b) {
return (g << 24) | (r << 16) | (b << 8);
}
setPixel(index, color) {
this.buf[index] = color;
}
getPixel(index) {
return this.buf[index];
}
clear() {
this.buf.fill(0);
}
show() {
this.sm.put(this.buf);
}
}
exports.NeoPixel = NeoPixel;