Skip to content

Commit 0dd155c

Browse files
committed
Basic project setup files with package.json and gulp build system
1 parent 1c16014 commit 0dd155c

7 files changed

+226
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

gulpfile.coffee

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
gulp = require('gulp')
2+
watch = require('gulp-watch')
3+
plumber = require('gulp-plumber')
4+
coffee = require('gulp-coffee')
5+
6+
gulp.task('default', ->
7+
watch(glob: 'src/**/*.coffee', verbose: true)
8+
.pipe(plumber()) # This will keeps pipes working after error event
9+
.pipe(coffee(bare: yes))
10+
.pipe(gulp.dest('lib'))
11+
)

gulpfile.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require('coffee-script-redux/register');
2+
require('./gulpfile.coffee')

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('lib/controller')

lib/controller.js

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
var doesProtocolMatch, helper, protocols, sortIndices;
2+
3+
helper = require('./helper');
4+
5+
protocols = ['weather1', 'weather2', 'switch1', 'switch2', 'switch3', 'switch4'];
6+
7+
protocols = protocols.map((function(_this) {
8+
return function(p) {
9+
return require("../protocols/" + p + ".coffee")(helper);
10+
};
11+
})(this));
12+
13+
doesProtocolMatch = function(pulseLengths, pulses, protocol) {
14+
var i, maxDelta;
15+
if (pulses.length !== protocol.pulseCount) {
16+
return false;
17+
}
18+
if (pulseLengths.length !== protocol.pulseLengths.length) {
19+
return false;
20+
}
21+
i = 0;
22+
while (i < pulseLengths.length) {
23+
maxDelta = pulseLengths[i] * 0.25;
24+
if (Math.abs(pulseLengths[i] - protocol.pulseLengths[i]) > maxDelta) {
25+
return false;
26+
}
27+
i++;
28+
}
29+
return true;
30+
};
31+
32+
sortIndices = function(array) {
33+
var e, i, indices, j, tuple, tuples, _i, _j, _len, _len1;
34+
tuples = new Array(array.length);
35+
for (i = _i = 0, _len = array.length; _i < _len; i = ++_i) {
36+
e = array[i];
37+
tuples[i] = [e, i];
38+
}
39+
tuples.sort(function(left, right) {
40+
if (left[0] < right[0]) {
41+
return -1;
42+
} else {
43+
return 1;
44+
}
45+
});
46+
indices = new Array(array.length);
47+
for (j = _j = 0, _len1 = tuples.length; _j < _len1; j = ++_j) {
48+
tuple = tuples[j];
49+
indices[tuple[1]] = j;
50+
}
51+
return indices;
52+
};
53+
54+
module.exports = {
55+
compressTimings: function(timings) {
56+
var bucket, buckets, counts, hasMatch, i, j, pulses, sums, timing, _i, _j, _k, _len, _len1, _len2;
57+
pulses = '';
58+
buckets = [];
59+
sums = [];
60+
counts = [];
61+
for (i = _i = 0, _len = timings.length; _i < _len; i = ++_i) {
62+
timing = timings[i];
63+
hasMatch = false;
64+
for (j = _j = 0, _len1 = buckets.length; _j < _len1; j = ++_j) {
65+
bucket = buckets[j];
66+
if (Math.abs(bucket - timing) < bucket * 0.5) {
67+
pulses += j;
68+
sums[j] += timing;
69+
counts[j]++;
70+
hasMatch = true;
71+
}
72+
}
73+
if (!hasMatch) {
74+
pulses += buckets.length;
75+
buckets.push(timing);
76+
sums.push(timing);
77+
counts.push(1);
78+
}
79+
}
80+
for (j = _k = 0, _len2 = buckets.length; _k < _len2; j = ++_k) {
81+
bucket = buckets[j];
82+
buckets[j] = Math.round(sums[j] / counts[j]);
83+
}
84+
return {
85+
buckets: buckets,
86+
pulses: pulses
87+
};
88+
},
89+
prepareCompressedPulses: function(input) {
90+
var parts, pulseLengths, pulses;
91+
parts = input.split(' ');
92+
pulseLengths = parts.slice(0, 8);
93+
pulses = parts[8];
94+
pulseLengths = pulseLengths.filter(function(puls) {
95+
return puls !== '0';
96+
}).map(function(puls) {
97+
return parseInt(puls, 10);
98+
});
99+
sortIndices = sortIndices(pulseLengths);
100+
pulseLengths.sort(function(l, r) {
101+
return l - r;
102+
});
103+
pulses = helper.mapByArray(pulses, sortIndices);
104+
return {
105+
pulseLengths: pulseLengths,
106+
pulses: pulses
107+
};
108+
},
109+
parsePulseSquence: function(pulseLengths, pulses) {
110+
var p, results, values, _i, _len;
111+
results = [];
112+
for (_i = 0, _len = protocols.length; _i < _len; _i++) {
113+
p = protocols[_i];
114+
if (doesProtocolMatch(pulseLengths, pulses, p)) {
115+
values = p.parse(pulses);
116+
results.push({
117+
protocol: p.name,
118+
values: values
119+
});
120+
}
121+
}
122+
return results;
123+
}
124+
};

lib/helper.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
module.exports = {
2+
map: function(data, mapping) {
3+
var hadMatch, i, replace, result, search;
4+
i = 0;
5+
result = '';
6+
while (i < data.length) {
7+
hadMatch = false;
8+
for (search in mapping) {
9+
replace = mapping[search];
10+
if (data.length - i >= search.length) {
11+
if (data.substr(i, search.length) === search) {
12+
result += replace;
13+
i += search.length;
14+
hadMatch = true;
15+
break;
16+
}
17+
}
18+
}
19+
if (!hadMatch) {
20+
throw new Error("Data did not match mapping");
21+
}
22+
}
23+
return result;
24+
},
25+
mapByArray: function(data, mapping) {
26+
var d, result, _i, _len;
27+
result = '';
28+
for (_i = 0, _len = data.length; _i < _len; _i++) {
29+
d = data[_i];
30+
result += mapping[parseInt(d, 10)];
31+
}
32+
return result;
33+
},
34+
binaryToNumber: function(data, b, e) {
35+
var i, number;
36+
number = 0;
37+
i = b;
38+
while (i <= e) {
39+
number <<= 1;
40+
number |= parseInt(data[i], 10);
41+
i++;
42+
}
43+
return number;
44+
},
45+
binaryToNumberRevert: function(data, b, e) {
46+
var i, number;
47+
number = 0;
48+
i = e;
49+
while (i >= b) {
50+
number <<= 1;
51+
number |= parseInt(data[i], 10);
52+
i--;
53+
}
54+
return number;
55+
},
56+
binaryToBoolean: function(data, i) {
57+
return data[i] === '1';
58+
}
59+
};

package.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "rfcontroljs",
3+
"version": "0.0.0",
4+
"description": "Protocol support for different 433mhz switches and weather stations for the RFControl Arduino library",
5+
"main": "index.js",
6+
"directories": {
7+
"test": "test"
8+
},
9+
"scripts": {
10+
"test": "gulp test"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git://github.com/pimatic/rfcontroljs.git"
15+
},
16+
"author": "Oliver Schneider <[email protected]>",
17+
"license": "GPLv3",
18+
"bugs": {
19+
"url": "https://github.com/pimatic/rfcontroljs/issues"
20+
},
21+
"devDependencies": {
22+
"gulp": "~3.8.6",
23+
"gulp-coffee": "~2.1.1",
24+
"coffee-script-redux": "~2.0.0-beta8",
25+
"gulp-plumber": "~0.6.4",
26+
"gulp-watch": "~0.6.9"
27+
}
28+
}

0 commit comments

Comments
 (0)