-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpticalServer.js
381 lines (305 loc) · 9.53 KB
/
OpticalServer.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
(function(){
var CHAR_TO_MORSE = {
"A": ".-",
"B": "-...",
"C": "-.-.",
"D": "-..",
"E": ".",
"F": "..-.",
"G": "--.",
"H": "....",
"I": "..",
"J": ".---",
"K": "-.-",
"L": ".-..",
"M": "--",
"N": "-.",
"O": "---",
"P": ".--.",
"Q": "--.-",
"R": ".-.",
"S": "...",
"T": "-",
"U": "..-",
"V": "...-",
"W": ".--",
"X": "-..-",
"Y": "-.--",
"Z": "--..",
"1": ".----",
"2": "..---",
"3": "...--",
"4": "....-",
"5": ".....",
"6": "-....",
"7": "--...",
"8": "---..",
"9": "----.",
"0": "-----"
};
// build reverse mapping Morse code to alpha-numeric
var MORSE_TO_CHAR = {};
for (var key in CHAR_TO_MORSE){
MORSE_TO_CHAR[CHAR_TO_MORSE[key]] = key;
}
function OpticalServer(params) {
params = params || {};
this.wpm = params.wpm || 10; // words per minute
this.unit = 1200 / this.wpm; // duration of dot (milliseconds) in Morse Code
this.callbacks = {};
this.times = {
// light
dot: this.unit,
dash: this.unit * 3,
// dark
break: this.unit,
charBreak: this.unit * 3,
wordBreak: this.unit * 7,
}
this.modes = {
DARK: "dark",
LIGHT: "light"
}
this.buffer = []; // temp array of morse code symbols as they are decoded
this.mode; // current mode, one of "light" or "dark"
this.isRunning = false; // flag for monitoring light variation as morse code
this.lastTime; // UTC time (milliseconds) when mode last changed between "dark" and "light"
this.lux = 0; // last known lux value from 'devicelight' event
this.luxThreshold; // min lux value considered as light signal to be setup automatically
this.wordBreakCount = 0; // count of consecutive workd breaks
this.wordBreakCountLimit = 3 // number of consecutive word breaks after which to pause monitoring
this.timeout;
var sampler = DeviceLightSampler();
sampler.average;
// Calibrate lux threshold
setTimeout(function(){
var limit = Math.max(sampler.average * 1.25, sampler.average + 500);
this.luxThreshold = limit;
console.log('avg: ', sampler.average, 'threshold: ', limit);
sampler.stop();
window.addEventListener('devicelight', this.onLightChange.bind(this));
}.bind(this), 500);
}
/*
Event handler for 'devicelight' event.
Store the latest lux value of the light intensity.
It the server isn't already running, start monitoring for optical morse code
patterns if the lux value is above the threshold to qualify for a light pulse.
*/
OpticalServer.prototype.onLightChange = function(e){
this.lux = e.value;
if (this.isRunning == false && this.lux >= this.luxThreshold){
this.start();
console.warn('Monitoring ON!')
}
};
OpticalServer.prototype.on = function(event, callback) {
if (event === "character"){
this.callbacks.character = callback;
}
if (event === "signal"){
this.callbacks.signal = callback;
}
}
OpticalServer.prototype.fire = function(callback, arg) {
if (typeof(callback) === 'function') {
callback(arg);
}
}
OpticalServer.prototype.start = function() {
this.isRunning = true;
this.lastTime = Date.now();
this.mode = this.modes.LIGHT;
this.wordBreakCount = 0;
this.buffer.length = 0;
this.loop();
}
OpticalServer.prototype.stop = function() {
console.warn('Monitoring OFF!')
this.isRunning = false;
this.timeout = clearTimeout(this.timeout);
}
OpticalServer.prototype.loop = function() {
// console.log('Looping:', this.lux);
var modeNow = (this.lux >= this.luxThreshold) ? this.modes.LIGHT : this.modes.DARK;
var duration;
// TODO: add timeout for flushing elapsed time to decode()
if (modeNow !== this.mode){
duration = Date.now() - this.lastTime;
console.warn(this.mode, duration);
this.decode(this.mode, duration);
// Swap mode and set new time reference;
this.mode = modeNow;
this.lastTime = Date.now();
this.timeout = clearTimeout(this.timeout);
}
if (this.isRunning && this.timeout === undefined){
/*
If mode stays unchanged for too long, flush the current buffer.
Mode monitoring stops after a number of consecutive dark pauses (see: wordBreakCountLimit)
*/
this.timeout = setTimeout(function(){
var duration = Date.now() - this.lastTime;
this.decode(this.mode, duration);
this.lastTime = Date.now();
this.timeout = clearTimeout(this.timeout);
}.bind(this), this.times.wordBreak);
}
if (this.isRunning){
_raf(this.loop.bind(this))
}
}
OpticalServer.prototype.decode = function(mode, duration) {
var op = function(){}; // placeholder for operation to run according to mode and duration
switch (mode){
case this.modes.DARK:
// fallback
op = function(){ console.warn('noop DARK, duration: ', duration, 'times: ', this.times) }
// FIXME: remove because it's not used
// break within character
if (duration >= this.times.break * 0.75) {
op = function() {
// used for testing
return "OP_BREAK";
}
}
// break between characters
if (duration >= this.times.charBreak * 0.5) {
op = function(){
var char = this.morseToChar(this.buffer.join(''));
if (!char) {
console.error('Unknown character for code:', this.buffer.join(''));
} else {
this.fire(this.callbacks.character, char);
// reset consecutive word break count
this.wordBreakCount = 0;
}
// reset morse code symbols buffer;
this.buffer.length = 0;
// used for testing
return "OP_CHAR_BREAK"
}
}
// break between words
if (duration >= this.times.wordBreak * 0.75) {
op = function(){
// increment consecutive word break count;
this.wordBreakCount++;
// console.warn(this.wordBreakCount, 'buffer: ', this.buffer.join(''))
if (this.buffer.length){
var char = this.morseToChar(this.buffer.join(''));
if (!char) {
console.error('Unknown character for code:', this.buffer.join(''));
} else {
// include space after last char of word
char = char + " ";
this.fire(this.callbacks.character, char);
this.wordBreakCount = 0;
}
this.buffer.length = 0;
}
if (this.wordBreakCount >= this.wordBreakCountLimit) {
this.stop();
}
// used for testing
return "OP_WORD_BREAK"
}
}
break;
// be more leninent with checking light durations
case this.modes.LIGHT:
// fallback
op = function(){ console.warn('noop LIGHT, duration: ', duration, 'times: ', this.times) }
if (duration <= this.times.dot * 1.25) {
op = function() {
this.buffer.push('.');
this.fire(this.callbacks.signal, '.');
return "OP_DOT";
}
}
if (duration >= this.times.dash * 0.75) {
op = function() {
this.buffer.push('-');
this.fire(this.callbacks.signal, '-');
return "OP_DASH";
}
}
break;
}
// run prevailing operation; returns string value for testing
return op.call(this);
}
OpticalServer.prototype.charToMorse = function(char) {
return CHAR_TO_MORSE[char.toUpperCase()];
}
OpticalServer.prototype.morseToChar = function(morse) {
return MORSE_TO_CHAR[morse];
}
function DeviceLightSampler(params){
if (!(this instanceof DeviceLightSampler)){
return new DeviceLightSampler();
}
params = params || {};
var maxLength = params.bufferLength || 240;
var callbacks = {};
var buffer = [];
var loop;
var lux;
var scope = this;
this.average = 50;
function _onLightChange(e){
lux = event.value;
}
function _sample(){
if (lux){
buffer.push(lux);
}
if (buffer.length >= maxLength){
buffer.splice(0, 1);
}
if (typeof callbacks.sample == 'function'){
callbacks.sample.call(scope, lux, buffer);
}
_calculateAverage()
loop = window.requestAnimationFrame(_sample);
}
function _calculateAverage(){
var len = buffer.length;
var sum = 0;
while (len--){
sum += buffer[len];
}
scope.average = sum / buffer.length || scope.average;
}
this.on = function(event, callback){
if (event === 'sample'){
callbacks.sample = callback;
}
}
this.stop = function(){
buffer.length = 0;
loop = window.cancelAnimationFrame(loop);
this.average = 50;
window.removeEventListener('devicelight', _onLightChange);
}
this.start = function(){
_sample();
window.addEventListener('devicelight', _onLightChange);
}
this.start();
}
/**
* A request animation frame shortcut. This one is intended to work even in
* background pages of an extension.
*/
function _raf(callback) {
var isCrx = !!(window.chrome && chrome.extension);
if (isCrx) {
setTimeout(callback, 1000/60);
} else {
requestAnimationFrame(callback);
}
};
// expose global
window.OpticalServer = OpticalServer;
})();