-
Notifications
You must be signed in to change notification settings - Fork 101
/
ws281x-native.js
245 lines (211 loc) · 5.67 KB
/
ws281x-native.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
const bindings = require('./get-native-bindings')();
const {stripType, stripTypeIds, paramCodes} = require('./constants');
const MAX_CHANNELS = 2;
const DEFAULT_DMA = 5;
const DEFAULT_FREQ = 800000;
const DEFAULT_STRIP_TYPE = stripType.WS2812;
// for GPIO-numbers see for example https://pinout.xyz/
const CHANNEL_DEFAULTS = [
{
count: 0,
gpio: 18,
invert: 0,
brightness: 255,
stripType: DEFAULT_STRIP_TYPE
},
{
count: 0,
gpio: 13,
invert: 0,
brightness: 255,
stripType: DEFAULT_STRIP_TYPE
}
];
const channels = Array(2);
// private members for channel-instances
const _id = Symbol('_id');
const _params = Symbol('_params');
const _update = Symbol('_update');
const _init = Symbol('_init');
const _reset = Symbol('_reset');
class Channel {
/**
* @param {number} channelId
* @param {ChannelParams} params
*/
constructor(channelId, params) {
const ledCount = params.count;
// private properties
this[_id] = channelId;
this[_params] = params;
// public/readonly properties
Object.defineProperties(this, {
count: {
value: params.count,
writable: false,
enumerable: true,
configurable: false
},
stripType: {
value: params.stripType,
writable: false,
enumerable: true,
configurable: false
},
invert: {
value: params.invert,
writable: false,
enumerable: true,
configurable: false
},
gpio: {
value: params.gpio,
writable: false,
enumerable: true,
configurable: false
}
});
// public properties
this.brightness = params.brightness;
this.buffer = null;
this.array = null;
if (ledCount > 0) {
const arrayBuffer = new ArrayBuffer(ledCount * 4); // 0xWWRRGGBB
// buffer and array are different ways to access the same data
this.buffer = Buffer.from(arrayBuffer);
this.array = new Uint32Array(arrayBuffer);
}
}
/**
* Resets this channel, setting all LEDs to black.
*/
[_reset]() {
if (!this.array) {
return;
}
this.array.fill(0);
}
/**
* Sends values for all parameters to the library.
* @private
*/
[_init]() {
Object.keys(this[_params]).forEach(paramName => {
const value = this[_params][paramName];
const code = paramCodes[paramName];
bindings.setChannelParam(this[_id], code, value);
});
}
/**
* Sends the current state of the buffer and the current brightness-value
* to the library.
* @private
*/
[_update]() {
if (this.buffer === null) {
return;
}
bindings.setChannelParam(this[_id], paramCodes.brightness, this.brightness);
bindings.setChannelData(this[_id], this.buffer);
}
}
/**
* Initializes the library.
* @param {FullParams} params
* @return {Channel[]} the initialized channel-instances
*/
function init(params) {
// FIXME: validate params
const {
dma = DEFAULT_DMA,
freq = DEFAULT_FREQ,
channels: channelsConfig = []
} = params;
// send global parameter values
bindings.setParam(paramCodes.dma, dma);
bindings.setParam(paramCodes.freq, freq);
// setup channels
for (let channelId = 0; channelId < MAX_CHANNELS; channelId++) {
const userParams = channelsConfig[channelId];
const defaults = CHANNEL_DEFAULTS[channelId];
const params = Object.assign({}, defaults, userParams);
if (typeof params.stripType === 'string') {
params.stripType = stripTypeIds[params.stripType];
}
channels[channelId] = new Channel(channelId, params);
}
channels.forEach(channel => channel[_init]());
bindings.init();
return channels;
}
/**
* Submits the current state of the channel-buffers to the driver for rendering.
*/
function render() {
channels.forEach(channel => channel[_update]());
bindings.render();
}
/**
* resets all color-values of all channels and renders.
*/
function reset() {
channels.forEach(channel => channel[_reset]());
render();
}
/**
* Shuts down the library, freeing allocated memory and resources.
* This should always be called when terminating the program.
*/
function finalize() {
bindings.finalize();
}
/**
* Simple initializer for single-channel usage.
* @param {number} numLeds number of LEDs
* @param {CombinedParams} options additional options.
* @return {Channel}
*/
module.exports = function(numLeds, options = {}) {
const {
dma = DEFAULT_DMA,
freq = DEFAULT_FREQ,
gpio = 18,
invert = false,
brightness = 255,
stripType = stripType.WS2812
} = options;
const channelOptions = {count: numLeds, gpio, invert, brightness, stripType};
const [channel] = init({dma, freq, channels: [channelOptions]});
// for convenience, make methods available via the channel-instance
channel.render = render;
channel.finalize = finalize;
return channel;
};
Object.assign(module.exports, {
init,
render,
reset,
finalize,
stripType
});
/**
* @typedef {object} FullParams
* @property {number} dma the DMA-number
* @property {number} freq the PWM-frequency in Hz
* @property {ChannelParams[]} channels The channel configurations.
*/
/**
* @typedef {object} ChannelParams
* @property {number} count number of LEDs on this channel
* @property {number} gpio the GPIO port-number the LEDs connect to
* @property {boolean} invert true to invert the output-signal (if you are
* using an inverting level-shifter for example)
* @property {number} brightness initial brightness for the channel
* @property {number|string} stripType the strip-type (see ./constants.js)
*/
/**
* @typedef {object} CombinedParams
* @extends ChannelParams
* @property {number} dma
* @property {number} freq
*/