-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
74 lines (57 loc) · 1.38 KB
/
main.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
66
67
68
69
70
71
72
73
74
"use strict";
const five = require("johnny-five");
const express = require("express");
const app = express();
const server = require("http").createServer(app);
const io = require("socket.io")(server);
let strip = null;
app.use(express.static(__dirname + "/public"))
app.get("/", function(req, res) {
res.sendFile(__dirname + "/index.html")
});
five.Board({
port: process.argv[2] || "COM9"
}).on("ready", function() {
console.log("Arduino connected!");
let state = {
red: 0,
green: 0,
blue: 0
};
strip = new five.Led.RGB({
pins: {
red: 6,
green: 9,
blue: 3
}
});
let setColors = function(state) {
strip.color({
red: state.red,
blue: state.blue,
green: state.green
});
};
io.on("connection", function(client) {
client.on("join", function(handshake) {
console.log(handshake);
});
setColors(state);
client.on("rgb", function(data) {
if (!data.get) {
state.red = data.red;
state.green = data.green;
state.blue = data.blue;
setColors(state);
}
client.emit("rgb", state);
client.broadcast.emit("rgb", state);
});
});
this.repl.inject({
strip: strip
});
});
const port = process.env.PORT || 3000;
server.listen(port);
console.log("Server listening on http://localhost:" + port);