-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (67 loc) · 2.05 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const express = require("express");
const app = express();
const SerialPort = require("serialport");
const Readline = require("@serialport/parser-readline");
const serialPort = new SerialPort("COM3", { baudRate: 115200 });
const parser = new Readline();
var server = require("http").Server(app);
var io = require("socket.io")(server);
const design = require("./js/Design");
let matrixData = new Array(16);
var clientIp = "";
app.use(express.static(__dirname + "/public"));
let PORT = process.env.PORT || 8000;
server.listen(PORT);
console.log("Initialized Server on port: " + PORT);
io.set("origins", "*:*");
io.on("connection", function(socket) {
clientIp = socket.request.connection.remoteAddress;
console.log(
"Connection request from " +
clientIp +
":" +
socket.request.connection.remotePort
);
socket.on("connectToServer", function(obj) {
socket.emit("currentDesign", matrixData)
socket.emit("designs", design.LoadDesignsFromFile());
socket.emit("start", { message: "Welcome " + clientIp });
console.log("Connected Successfully");
});
socket.on("clear", function() {
serialPort.write("clear\n");
});
function drawPageLoop() {
for(let i=0;i<16;i++){
for(let j=0;j<16;j++){
serialPort.write(i + "," + j + matrixData[i][j] + "\n");
console.log("a"+i + "," + j + matrixData[i][j])
}
}
}
socket.on("drawPage", function(data) {
matrixData = data;
drawPageLoop();
});
socket.on("saveDesign", function(data) {
design.SaveDesign(clientIp, data);
});
socket.on("writeOne", function(x,y,data) {
serialPort.write(x.toString(16)+y.toString(16)+data + "\n");
matrixData[y][x] = data
});
});
serialPort.pipe(parser);
serialPort.on("open", () => {
console.log("Serial Port Open");
for (let i = 0; i < 16; i++) {
matrixData[i] = new Array(16);
for (let j = 0; j < 16; j++) {
matrixData[i][j] = "000000";
serialPort.write(i.toString(16)+j.toString(16)+matrixData[i][j] + "\n");
}
}
});
parser.on("data", data => {
console.log(data);
});