Skip to content

Commit 8dc380a

Browse files
author
Martin Schuhfuss
committed
restructured module and implmented event-listener interface
1 parent 0bab66a commit 8dc380a

File tree

1 file changed

+54
-37
lines changed

1 file changed

+54
-37
lines changed

lib/ws281x-native.js

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var EventEmitter = require('events').EventEmitter;
2+
13
function getNativeBindings() {
24
// the native module might even be harmful (or won't work in the best case)
35
// in the wrong environment, so we make sure that at least everything we can
@@ -21,6 +23,7 @@ function getNativeBindings() {
2123
}
2224

2325

26+
// fallback to empty stub-functions if not on RPi.
2427
var ws281xNative = getNativeBindings();
2528
if(!ws281xNative) {
2629
process.stderr.write(
@@ -53,44 +56,58 @@ function remap(src, indexMapping) {
5356
}
5457

5558

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-
}
59+
var ws281x = new EventEmitter();
60+
61+
/**
62+
* configures PWM and DMA for sending data to the LEDs
63+
*
64+
* @param {Number} numLeds number of LEDs to be controlled
65+
* @param {?Object} options (acutally only tested with default-values)
66+
* intialization-options for the library
67+
* (PWM frequency, DMA channel, GPIO, Brightness)
68+
*/
69+
ws281x.init = function(numLeds, options) {
70+
_numLeds = numLeds;
8671

87-
ws281xNative.render(data);
88-
},
72+
ws281xNative.init(numLeds, options);
73+
};
74+
75+
/**
76+
* register a mapping to manipulate array-indices within the
77+
* data-array before rendering.
78+
*
79+
* @param {Array.<Number>} map the mapping, indexed by destination.
80+
*/
81+
ws281x.setIndexMapping = function(mapping) {
82+
_indexMapping = mapping;
83+
};
8984

90-
/**
91-
*
92-
*/
93-
reset: function() {
94-
ws281xNative.reset();
85+
/**
86+
* send data to the LED-strip.
87+
*
88+
* @param {Uint32Array} data the pixel-data, 24bit per pixel in
89+
* RGB-format (0xff0000 is red).
90+
* @return {Uint32Array} data as it was sent to the LED-strip
91+
*/
92+
ws281x.render = function(data) {
93+
this.emit('beforeRender', data);
94+
95+
if(_indexMapping) {
96+
data = remap(data, _indexMapping);
9597
}
98+
99+
ws281xNative.render(data);
100+
this.emit('render', data);
101+
102+
return data;
96103
};
104+
105+
/**
106+
* clears all LEDs, resets the PWM and DMA-parts and deallocates
107+
* all internal structures.
108+
*/
109+
ws281x.reset = function() {
110+
ws281xNative.reset();
111+
};
112+
113+
module.exports = ws281x;

0 commit comments

Comments
 (0)