Skip to content

Commit 712d979

Browse files
author
Martin Schuhfuss
committed
add index-mapping functionality
1 parent dc0e6cc commit 712d979

File tree

1 file changed

+90
-19
lines changed

1 file changed

+90
-19
lines changed

lib/ws281x-native.js

Lines changed: 90 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,96 @@
1-
// the native module might even be harmful (or won't work in the best case)
2-
// in the wrong environment, so we make sure that at least everything we can
3-
// test for matches the raspberry-pi before loading the native-module
4-
if(process.arch === 'arm' || process.platform === 'linux') {
5-
var fs = require('fs');
1+
function getNativeBindings() {
2+
// the native module might even be harmful (or won't work in the best case)
3+
// in the wrong environment, so we make sure that at least everything we can
4+
// test for matches the raspberry-pi before loading the native-module
5+
if(process.arch === 'arm' || process.platform === 'linux') {
6+
var fs = require('fs');
67

7-
// we are doing stuff here that will only work on the Broadcom BCM
8-
var isBCM2708 = (function() {
9-
var cpuInfo = fs.readFileSync('/proc/cpuinfo').toString();
8+
// will only work on the Broadcom BCM2708
9+
var isBCM2708 = (function() {
10+
var cpuInfo = fs.readFileSync('/proc/cpuinfo').toString();
1011

11-
return /hardware\s*:\s*bcm2708/i.test(cpuInfo);
12-
} ());
12+
return /hardware\s*:\s*bcm2708/i.test(cpuInfo);
13+
} ());
1314

14-
if(isBCM2708) {
15-
module.exports = require('./binding/rpi_ws281x.node');
15+
if(isBCM2708) {
16+
return require('./binding/rpi_ws281x.node');
17+
}
18+
}
1619

17-
return;
18-
}
20+
return null;
1921
}
2022

21-
process.stderr.write(
22-
'[rpi-ws281x-native] it looks like you are not running the module on a raspberry pi. ' +
23-
'As it won\'t work and even might be dangerous, we stop it right here.\n'
24-
);
25-
process.exit(-1);
23+
24+
var ws281xNative = getNativeBindings();
25+
if(!ws281xNative) {
26+
process.stderr.write(
27+
'[rpi-ws281x-native] it looks like you are not running the module ' +
28+
'on a raspberry pi. Will only return stubs for native ' +
29+
'interface-functions.\n'
30+
);
31+
32+
ws281xNative = {
33+
init: function() {},
34+
render: function() {},
35+
reset: function() {}
36+
};
37+
}
38+
39+
40+
41+
var _numLeds = 0,
42+
_indexMapping = null;
43+
44+
45+
function remap(src, indexMapping) {
46+
var dest = new Uint32Array(src.length);
47+
48+
for(var i=0; i<src.length; i++) {
49+
dest[i] = src[indexMapping[i]];
50+
}
51+
52+
return dest;
53+
}
54+
55+
56+
module.exports = {
57+
/**
58+
*
59+
* @param numLeds
60+
* @param options
61+
*/
62+
init: function(numLeds, options) {
63+
_numLeds = numLeds;
64+
65+
ws281xNative.init(numLeds, options);
66+
},
67+
68+
/**
69+
* registers an index-mapping.
70+
* Index: destination-index, Value: Source-Index
71+
*
72+
* @param {Array.<Number>} mapping
73+
*/
74+
setIndexMapping: function(mapping) {
75+
_indexMapping = mapping;
76+
},
77+
78+
/**
79+
*
80+
* @param data
81+
*/
82+
render: function(data) {
83+
if(_indexMapping) {
84+
data = remap(data, _indexMapping);
85+
}
86+
87+
ws281xNative.render(data);
88+
},
89+
90+
/**
91+
*
92+
*/
93+
reset: function() {
94+
ws281xNative.reset();
95+
}
96+
};

0 commit comments

Comments
 (0)