-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
89 lines (72 loc) · 2.4 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
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(express.static(`${__dirname}/public`));
var tessel = require('tessel');
var accel = require('accel-mma84').use(tessel.port['A']);
var av = require('tessel-av');
var camera = new av.Camera();
app.get('/stream', (request, response) => {
response.redirect(camera.url);
});
// var calib = [0, 0]; // calibration shift
console.log('Accelerometer is initializing...');
accel.on('ready', function() {
// Stream accelerometer data
console.log('Accelerometer is ready...');
io.on('connection', function(socket) {
console.log('A user is connected...');
// Initialize the accelerometer.
var blinkID;
function ledBlink() {
blinkID = setInterval(blink, 100);
}
function blink() {
tessel.led[2].toggle();
// console.log('blink!');
}
accel.on('data', function(xyz) {
socket.emit('newdata', {
x: xyz[0],
y: xyz[1],
z: xyz[2],
});
// console.log('x:', xyz[0] - calib[0], 'y:', xyz[1] - calib[1], 'z:', xyz[2]);
});
accel.on('error', function(err) {
// console.log('Error:', err);
});
// socket.on('newcalib', function(newcalib) {
// calib[0] = newcalib[0];
// calib[1] = newcalib[1];
// // console.log('New calibration => x: ' + calib[0] + ', y: ' + calib[1]);
// });
socket.on('ledon', function() {
tessel.led[3].on();
// console.log('Led is on.');
});
socket.on('ledoff', function() {
tessel.led[3].off();
// console.log('Led is off.');
});
socket.on('greenon', function() {
clearInterval(blinkID);
tessel.led[2].on();
// console.log('Green light is on.');
});
socket.on('greenoff', function() {
clearInterval(blinkID);
tessel.led[2].off();
// console.log('Green light is off.');
});
socket.on('greenblink', function() {
clearInterval(blinkID);
ledBlink();
// console.log('Green light is blinking.');
});
});
});
http.listen(3000, function() {
console.log('listening on *:3000');
});