-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
170 lines (148 loc) · 3.54 KB
/
server.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
var SerialPort = require("serialport").SerialPort
var serialPortButtons = new SerialPort("/dev/ttyACM2", {
baudrate: 9600,
dataBits: 8,
parity: 'none',
stopBits: 1,
flowControl: false
});
var serialPortCube = new SerialPort("/dev/ttyACM3", {
baudrate: 9600,
dataBits: 8,
parity: 'none',
stopBits: 1,
flowControl: false
});
var serialPortPlayer1 = new SerialPort("/dev/ttyACM4", {
baudrate: 9600,
dataBits: 8,
parity: 'none',
stopBits: 1,
flowControl: false
});
var serialPortPlayer2 = new SerialPort("/dev/ttyACM5", {
baudrate: 9600,
dataBits: 8,
parity: 'none',
stopBits: 1,
flowControl: false
});
var led_value = 0;
var led_value = 0;
var score_player1 = 1;
var score_player2 = 1;
var blinkstate = 0;
var won = 0;
serialPortButtons.on("open", function(){
console.log('loaded buttons');
setTimeout(mainLoop, 3000);
});
serialPortCube.on('open', function(){
console.log('loaded cube');
});
serialPortPlayer1.on('open', function(){
console.log('loaded player 1');
});
serialPortPlayer2.on('open', function(){
console.log('loaded player 2');
});
function mainLoop() {
console.log('open');
serialPortPlayer1.write(""+score_player1);
serialPortPlayer2.write(""+score_player2);
start_led_countdown();
serialPortButtons.on('data', function(data) {
receivedData = data.toString();
// console.log(receivedData);
if(score_player1>6){
reset();
return;
}
if(score_player2>6){
reset();
return;
}
if(led_value == 1){
led_off();
start_led_countdown();
if(receivedData == "1"){
score_player1 += 1;
} else {
score_player2 += 1;
}
} else {
if(receivedData == "1"){
if(score_player1>1){
score_player1 -= 1;
}
} else {
if(score_player2>1){
score_player2 -= 1;
}
}
console.log('CHEATER!');
}
console.log("Score player 1: ", score_player1);
console.log("Score player 2: ", score_player2);
serialPortPlayer1.write(""+score_player1);
serialPortPlayer2.write(""+score_player2);
if(score_player1>6){
won = 1;
blink_leds1()
return;
}
if(score_player2>6){
won = 1;
blink_leds2()
return;
}
});
}
function blink_leds1(){
if(blinkstate == 0){
serialPortPlayer1.write(""+blinkstate);
} else {
serialPortPlayer1.write(""+7);
}
blinkstate = (blinkstate + 1) % 2
if(won==1){
setTimeout(blink_leds1, 500);
}
}
function blink_leds2(){
if(blinkstate == 0){
serialPortPlayer2.write(""+blinkstate);
} else {
serialPortPlayer2.write(""+7);
}
blinkstate = (blinkstate + 1) % 2
if(won==1){
setTimeout(blink_leds2, 500);
}
}
function led_off(){
console.log('led off');
// serialPortButtons.write("0E");
serialPortCube.write("x");
led_value = 0;
}
function led_on(){
console.log('led on');
// serialPortButtons.write("1E");
serialPortCube.write("z");
led_value = 1;
}
function start_led_countdown(){
var min_milliseconds = 2000,
max_milliseconds = 7000,
milliseconds = Math.random()*(max_milliseconds - min_milliseconds) + min_milliseconds;
setTimeout(led_on, milliseconds);
}
function reset(){
won = 0;
serialPortPlayer1.write(""+1);
serialPortPlayer2.write(""+1);
score_player1=1;
score_player2=1;
start_led_countdown();
}