-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
transmitter.js
65 lines (55 loc) · 1.48 KB
/
transmitter.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
63
64
65
var five = require("johnny-five");
var pixel = require("node-pixel");
var board = new five.Board({ port: "/dev/cu.usbmodem1144101" });
const INTERVAL = 100;
var strip = null;
const string = "Hello, world! ";
const strLength = string.length;
board.on("ready", async function () {
strip = new pixel.Strip({
board: this,
controller: "FIRMATA",
strips: [{ pin: 7, length: 7 },],
gamma: 2.8,
});
strip.on("ready", function () {
strip.color('#fff');
strip.show();
});
await delay(3000);
sendBytes(strip);
});
const sendBytes = async (strip) => {
for (var i = 0; i < strLength; i++) {
strip.off();
strip.show();
await delay(INTERVAL);
const bits = convertToBinary(string[i]);
for (var y = 0; y < 8; y++) {
const ledState = bits[y];
if (ledState === '1') {
strip.color('#fff');
strip.show();
} else {
strip.off();
strip.show();
}
await delay(INTERVAL);
}
strip.color('#fff');
strip.show();
await delay(INTERVAL);
}
await delay(INTERVAL);
sendBytes(strip);
}
const convertToBinary = (string) => {
return string.split('').map(function (char) {
return char.charCodeAt(0).toString(2).padStart(8, '0');
}).join(' ');
}
const delay = (ms) => {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}