-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
182 lines (165 loc) · 3.62 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
171
172
173
174
175
176
177
178
179
180
181
182
var Cylon = require('cylon');
var request = require("request");
var express = require('express');
var fs = require('fs');
var app = express();
var bodyParser = require('body-parser')
var bb8_uuid = 'db82c92b4d824143b1b8223d55a21ad4';
(function (httpserver, bb8server) {
console.log("start servers");
bb8server(function (ollie) {
httpserver(function (msg) {
executeOrder(ollie, msg.command);
});
});
})(httpserver, bb8server);
function httpserver(callback) {
app.use(express.static('www'));
app.use(bodyParser.json())
app.post('/api/send', function (request, response) {
console.log("/api/send", request.body);
callback(request.body);
response.sendStatus(200);
});
app.listen(8080);
}
function bb8server(callback) {
Cylon.robot({
connections: {
bluetooth: {
adaptor: 'central',
uuid: bb8_uuid,
module: 'cylon-ble'
}
},
devices: {
ollie: {
driver: 'ollie'
}
},
work: function (my) {
callback(my.ollie);
my.ollie.start(function () {
my.ollie.detectCollisions(function () {
console.log("Aie !!!!");
})
});
}
}).start();
}
var speed = 0;
var angle = 0;
var lastTwitterId = 0;
var lastWebId = 0;
var setColorFromSpeed = function (ollie, speed) {
if (speed <= 0) {
ollie.color(0x00FF00);
} else if (speed <= 20) {
ollie.color(0x00FFEE);
} else if (speed <= 40) {
ollie.color(0x0077FF);
} else if (speed <= 60) {
ollie.color(0x0000FF);
} else if (speed <= 80) {
ollie.color(0x9900FF);
} else if (speed <= 100) {
ollie.color(0xFF00BB);
} else {
ollie.color(0xFF0000);
}
}
var rollf = function (ollie, angle, speed) {
console.log("Going " + speed + "mph at " + angle + "degrees");
ollie.roll(speed, angle);
setColorFromSpeed(ollie, speed);
}
var hex2rgb = function (hex) {
// long version
r = hex.match(/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);
if (r) {
h = r.slice(1, 4).map(function (x) {
return parseInt(x, 16);
});
return h[2] + 256 * h[1] + 65536 * h[0];
}
// short version
r = hex.match(/^#([0-9a-f])([0-9a-f])([0-9a-f])$/i);
if (r) {
h = r.slice(1, 4).map(function (x) {
return 0x11 * parseInt(x, 16);
});
return h[2] + 256 * h[1] + 65536 * h[0];
}
return null;
}
var executeOrder = function (ollie, order) {
if (order.charAt(0) == '#') {
var color = hex2rgb(order.toLowerCase());
console.log(color);
ollie.color(color);
return;
}
switch (order) {
case "left":
angle -= 30;
rollf(ollie, angle, speed);
break;
case "right":
angle += 30;
rollf(ollie, angle, speed);
break;
case "west":
angle = 270;
rollf(ollie, angle, speed);
break;
case "south":
angle = 180;
rollf(ollie, angle, speed);
break;
case "north":
angle = 0;
rollf(ollie, angle, speed);
break;
case "east":
angle = 90;
rollf(ollie, angle, speed);
break;
case "slow":
if (speed > 0) {
speed -= 20;
rollf(ollie, angle, speed);
}
break;
case "fast":
if (speed < 120) {
speed += 20;
rollf(ollie, angle, speed);
}
break;
case "stop":
speed = 0;
rollf(ollie, angle, speed);
break;
case "blue":
ollie.color(0x0000FF);
break;
case "green":
ollie.color(0x00FF00);
break;
case "red":
ollie.color(0xFF0000);
break;
case "pink":
case "purple":
ollie.color(0xFF00FF);
break;
case "yellow":
ollie.color(0xFFFF00);
break;
case "cyan":
ollie.color(0x00FFFF);
break;
default:
console.log("Unknown command: '" + order + "'");
}
}