This repository was archived by the owner on Aug 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
166 lines (140 loc) · 5.04 KB
/
Copy pathserver.js
File metadata and controls
166 lines (140 loc) · 5.04 KB
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
//// Node.js server ////
//require('look').start();
// var memwatch = require('memwatch');
// var agent = require('webkit-devtools-agent');
// agent.start()
var controller = require('./controller.js');
var drawers1D = require('./drawers1D.js');
var alienblob = require('./alienblob.js');
var bzr = require('./bzr.js');
var video = require('./video.js');
var off = require('./off.js');
var gradient = require('./gradient.js');
var leds = require('./leds.js');
var palette = require('./palette.js');
var camera = require('./camera.js');
var allBaseColors = require('./kuler.js').allBaseColors;
var fs = require('fs');
var express = require('express');
var app = express();
var http = require('http').Server(app);
var socketIO = require('socket.io');
var io = socketIO(http);
var path = require('path');
//// Server config variables ////
// *** These must match settings on the Teensy ***
var WIDTH = 64;
var HEIGHT = 32;
// *** End settings match ***
var DEPTH = 24; // bit depth: 24 or 48
var NUM_COLORS = 1<<12; // colors in the gradient of each palette
var FPS = 60;
var CAMERA_FPS = 10;
var START_DRAWER = 'Bzr';
var DRAWER_CHANGE_INTERVAL = 10000;
var CAM_SIZE = [1280, 960];//[640, 480];
var layoutLeftToRight = false; // only used for serial port connections
var ENABLE_AUDIO = false;
var ENABLE_CAMERA = false;
var UPDATE_IMAGE_INTERVAL = 0;
//// End Server config variables ///
//// Global variables ////
var device;
if (process.argv.length > 2) {
device = process.argv[2];
} else {
device = "/dev/ttyACM0"; // for direct serial access to Teensy
// device = "ws://localhost:7890"; // for fadecandy
}
// start the camera used by face detection and the VideoDrawer
if (ENABLE_CAMERA) {
var cam = new camera.Camera(CAM_SIZE, CAMERA_FPS);
}
var allDrawers =
[new drawers1D.GradientDrawer(),
new drawers1D.WipeDrawer(),
new drawers1D.WaveDrawer(),
new drawers1D.SparkleDrawer(),
new drawers1D.PulseDrawer(),
new alienblob.AlienBlobDrawer(WIDTH, HEIGHT, NUM_COLORS),
new bzr.BzrDrawer(WIDTH, HEIGHT, NUM_COLORS),
new video.VideoDrawer(WIDTH, HEIGHT, NUM_COLORS, cam),
// new gradient.GradientDrawer(WIDTH, HEIGHT, NUM_COLORS),
new off.OffDrawer(WIDTH, HEIGHT, NUM_COLORS)
];
var availableDrawers = {}; // drawers that can be selected from the UI
for (var i = 0; i < allDrawers.length; i++) {
if ((HEIGHT == 1 && allDrawers[i].type().indexOf("1D") > -1) ||
(HEIGHT > 1 && allDrawers[i].type().indexOf("2D") > -1)) {
availableDrawers[allDrawers[i].name] = allDrawers[i];
}
}
//// Start the patterns ////
console.log('start');
var paletteMgr = new palette.PaletteManager(allBaseColors, NUM_COLORS);
var leds = new leds.LEDs(WIDTH, HEIGHT, DEPTH, device, layoutLeftToRight, UPDATE_IMAGE_INTERVAL);
var control = new controller.Controller(leds, paletteMgr, availableDrawers,
START_DRAWER, DRAWER_CHANGE_INTERVAL, cam, ENABLE_AUDIO);
function func() {
control.loop();
};
setInterval(func, 1000 / FPS);
//// Start the http server ///
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.get('/image', function(req, res) {
var image = "data:image/png;base64," + leds.pngData.toString('base64');
//leds.pngData = '';
res.send(image);
});
app.use(express.static(path.join(__dirname, 'public')));
http.listen(8080, function(){
console.log('listening on *:8080');
});
//// Handle the websockets connection to the client ////
io.sockets.on('connection', function (socket) {
// get the allowed programs
socket.on('get drawers', function (data, fn) {
console.log('get drawers: ' + JSON.stringify(data));
var settings = control.getSettings();
fn({active: {name: control.currDrawer.name, ranges: settings.ranges, values: settings.values},
allNames: Object.keys(availableDrawers)});
});
// set the running program, return it's settings
// plus the palette
socket.on('set drawer', function (drawerName, fn) {
console.log('set drawer: ' + drawerName);
control.changeDrawer(availableDrawers[drawerName]);
var settings = control.getSettings();
fn({drawer: drawerName, ranges: settings.ranges, values: settings.values});
});
// update the settings on the server
socket.on('set settings', function (settingVals, fn) {
console.log('set settings: ' + JSON.stringify(settingVals));
control.setSettings(settingVals);
//fn();
});
// randomize the settings on the server
socket.on('randomize settings', function (data, fn) {
console.log('randomize settings: ' + data);
control.randomizeSettings();
fn(control.getSettings().values);
});
});
function cleanup() {
if (ENABLE_CAMERA)
cam.stop();
leds.stop(function(err) {
if (err) console.log("Error when stopping leds: " + err);
process.exit();
});
}
process.on('SIGINT', function() {
console.log("Caught sigint");
cleanup();
});
process.on('SIGTERM', function() {
console.log("Caught sigterm");
cleanup();
});