periph 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/examples/accelerometer/.gitkeep +0 -0
- package/examples/adc_dac/.gitkeep +0 -0
- package/examples/adc_dac/mcp4725/complete.js +34 -0
- package/examples/adc_dac/mcp4725/demo.js +43 -0
- package/examples/adc_dac/mcp4725/minimal.js +14 -0
- package/examples/color/.gitkeep +0 -0
- package/examples/comms/.gitkeep +0 -0
- package/examples/display/.gitkeep +0 -0
- package/examples/environmental/.gitkeep +0 -0
- package/examples/gas/.gitkeep +0 -0
- package/examples/gnss/.gitkeep +0 -0
- package/examples/gpio/.gitkeep +0 -0
- package/examples/gyroscope/.gitkeep +0 -0
- package/examples/humidity/.gitkeep +0 -0
- package/examples/imu/.gitkeep +0 -0
- package/examples/io_expander/mcp23017/complete.js +80 -0
- package/examples/io_expander/mcp23017/demo.js +59 -0
- package/examples/io_expander/mcp23017/minimal.js +16 -0
- package/examples/io_expander/pcf8574/complete.js +68 -0
- package/examples/io_expander/pcf8574/demo.js +41 -0
- package/examples/io_expander/pcf8574/minimal.js +16 -0
- package/examples/io_expander/pcf8575/complete.js +31 -0
- package/examples/io_expander/pcf8575/demo.js +33 -0
- package/examples/io_expander/pcf8575/minimal.js +20 -0
- package/examples/led/.gitkeep +0 -0
- package/examples/led/sk6812rgbw/complete.js +69 -0
- package/examples/led/sk6812rgbw/demo.js +85 -0
- package/examples/led/sk6812rgbw/minimal.js +25 -0
- package/examples/led/ws2812b/complete.js +64 -0
- package/examples/led/ws2812b/demo.js +101 -0
- package/examples/led/ws2812b/minimal.js +22 -0
- package/examples/light/.gitkeep +0 -0
- package/examples/magnetometer/.gitkeep +0 -0
- package/examples/magnetometer/as5600/complete.js +43 -0
- package/examples/magnetometer/as5600/demo.js +49 -0
- package/examples/magnetometer/as5600/minimal.js +16 -0
- package/examples/memory/.gitkeep +0 -0
- package/examples/motor/.gitkeep +0 -0
- package/examples/other/.gitkeep +0 -0
- package/examples/power/ina219/complete.js +30 -0
- package/examples/power/ina219/demo.js +26 -0
- package/examples/power/ina219/minimal.js +17 -0
- package/examples/power/ina226/complete.js +33 -0
- package/examples/power/ina226/demo.js +30 -0
- package/examples/power/ina226/minimal.js +11 -0
- package/examples/power/ina3221/complete.js +65 -0
- package/examples/power/ina3221/demo.js +56 -0
- package/examples/power/ina3221/minimal.js +16 -0
- package/examples/pressure/.gitkeep +0 -0
- package/examples/pressure/bmp180/complete.js +26 -0
- package/examples/pressure/bmp180/demo.js +32 -0
- package/examples/pressure/bmp180/minimal.js +17 -0
- package/examples/pressure/bmp280/complete.js +28 -0
- package/examples/pressure/bmp280/demo.js +34 -0
- package/examples/pressure/bmp280/minimal.js +17 -0
- package/examples/rfid/.gitkeep +0 -0
- package/examples/rtc/.gitkeep +0 -0
- package/examples/temperature/.gitkeep +0 -0
- package/examples/tof/.gitkeep +0 -0
- package/package.json +20 -0
- package/src/chips/accelerometer/.gitkeep +0 -0
- package/src/chips/adc_dac/.gitkeep +0 -0
- package/src/chips/adc_dac/mcp4725.js +161 -0
- package/src/chips/color/.gitkeep +0 -0
- package/src/chips/comms/.gitkeep +0 -0
- package/src/chips/display/.gitkeep +0 -0
- package/src/chips/environmental/.gitkeep +0 -0
- package/src/chips/gas/.gitkeep +0 -0
- package/src/chips/gnss/.gitkeep +0 -0
- package/src/chips/gpio/.gitkeep +0 -0
- package/src/chips/gyroscope/.gitkeep +0 -0
- package/src/chips/humidity/.gitkeep +0 -0
- package/src/chips/imu/.gitkeep +0 -0
- package/src/chips/io_expander/mcp23017.js +396 -0
- package/src/chips/io_expander/pcf8574.js +298 -0
- package/src/chips/io_expander/pcf8575.js +295 -0
- package/src/chips/led/.gitkeep +0 -0
- package/src/chips/led/_color.js +25 -0
- package/src/chips/led/sk6812rgbw.js +183 -0
- package/src/chips/led/ws2812b.js +171 -0
- package/src/chips/light/.gitkeep +0 -0
- package/src/chips/magnetometer/as5600.js +293 -0
- package/src/chips/memory/.gitkeep +0 -0
- package/src/chips/motor/.gitkeep +0 -0
- package/src/chips/other/.gitkeep +0 -0
- package/src/chips/power/ina219.js +154 -0
- package/src/chips/power/ina226.js +208 -0
- package/src/chips/power/ina3221.js +341 -0
- package/src/chips/pressure/.gitkeep +0 -0
- package/src/chips/pressure/bmp180.js +234 -0
- package/src/chips/pressure/bmp280.js +328 -0
- package/src/chips/rfid/.gitkeep +0 -0
- package/src/chips/rtc/.gitkeep +0 -0
- package/src/chips/temperature/.gitkeep +0 -0
- package/src/chips/tof/.gitkeep +0 -0
- package/src/index.js +2 -0
- package/src/transport/i2c.js +67 -0
- package/src/transport/neopixel.js +37 -0
- package/src/transport/smbus.js +117 -0
- package/src/transport/spi.js +72 -0
- package/src/transport/uart.js +145 -0
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* PCF8575 16-bit quasi-bidirectional I/O port expander — minimal interface.
|
|
5
|
+
*
|
|
6
|
+
* Exposes all 16 pins (P00–P07, P10–P17) as GPIO objects via the pin() factory.
|
|
7
|
+
* Direction is implicit: writeSync(1) puts a pin in input mode (weak pull-up);
|
|
8
|
+
* writeSync(0) drives it low. Two shadow registers track the output latches.
|
|
9
|
+
*
|
|
10
|
+
* Initialises all pins to input mode (shadow = [0xFF, 0xFF]) at construction.
|
|
11
|
+
*
|
|
12
|
+
* @param {object} transport - Configured I²C transport (write, read, writeRead).
|
|
13
|
+
* @param {number} [addr=0x20] - 7-bit I²C device address.
|
|
14
|
+
*/
|
|
15
|
+
class Pcf8575Minimal {
|
|
16
|
+
/**
|
|
17
|
+
* @param {object} transport - Configured I²C transport.
|
|
18
|
+
* @param {number} [addr=0x20] - 7-bit I²C device address.
|
|
19
|
+
*/
|
|
20
|
+
constructor(transport, addr = 0x20) {
|
|
21
|
+
this._transport = transport;
|
|
22
|
+
this._addr = addr;
|
|
23
|
+
this._shadow = [0xFF, 0xFF];
|
|
24
|
+
this._writeBoth();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
_writeBoth() {
|
|
28
|
+
this._transport.write(Buffer.from([this._shadow[0], this._shadow[1]]));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
_readPort() {
|
|
32
|
+
return this._transport.read(2);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
_setPin(n, value) {
|
|
36
|
+
const portIdx = Math.floor(n / 8);
|
|
37
|
+
const bit = n % 8;
|
|
38
|
+
if (value) this._shadow[portIdx] |= (1 << bit);
|
|
39
|
+
else this._shadow[portIdx] &= ~(1 << bit);
|
|
40
|
+
this._writeBoth();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Return a Pin proxy object for pin n (0–15).
|
|
45
|
+
* @param {number} n - Pin index (0 = P00, 15 = P17).
|
|
46
|
+
* @param {string} [direction='in'] - Initial direction: 'in' or 'out'.
|
|
47
|
+
* @returns {_Pin} Pin proxy implementing the onoff Gpio subset.
|
|
48
|
+
*/
|
|
49
|
+
pin(n, direction = 'in') {
|
|
50
|
+
const p = new _Pin(this, n, direction);
|
|
51
|
+
if (direction === 'out') this._setPin(n, 0);
|
|
52
|
+
else this._setPin(n, 1);
|
|
53
|
+
return p;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Read all 8 pins of the given port as a bitmask.
|
|
58
|
+
* @param {number} [port=0] - Port index (0 = P00–P07, 1 = P10–P17).
|
|
59
|
+
* @returns {number} 8-bit bitmask of actual pin logic levels.
|
|
60
|
+
*/
|
|
61
|
+
readPort(port = 0) {
|
|
62
|
+
return this._readPort()[port];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Write all 8 pins of the given port at once and update the shadow register.
|
|
67
|
+
* @param {number} [port=0] - Port index (0 or 1).
|
|
68
|
+
* @param {number} mask - 8-bit output mask; 1 = input mode, 0 = drive low.
|
|
69
|
+
*/
|
|
70
|
+
writePort(port = 0, mask = 0xFF) {
|
|
71
|
+
this._shadow[port] = mask & 0xFF;
|
|
72
|
+
this._writeBoth();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* GPIO proxy for a single PCF8575 pin — onoff Gpio interface subset.
|
|
78
|
+
*
|
|
79
|
+
* Obtain via Pcf8575Minimal.pin(n). Do not instantiate directly.
|
|
80
|
+
*
|
|
81
|
+
* @param {Pcf8575Minimal} chip - Parent driver instance.
|
|
82
|
+
* @param {number} n - Pin index (0–15).
|
|
83
|
+
* @param {string} direction - 'in' or 'out'.
|
|
84
|
+
*/
|
|
85
|
+
class _Pin {
|
|
86
|
+
constructor(chip, n, direction) {
|
|
87
|
+
this._chip = chip;
|
|
88
|
+
this._n = n;
|
|
89
|
+
this._direction = direction;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** @type {string} Current pin direction ('in' or 'out'). */
|
|
93
|
+
get direction() { return this._direction; }
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Read pin synchronously.
|
|
97
|
+
* @returns {number} 0 or 1.
|
|
98
|
+
*/
|
|
99
|
+
readSync() {
|
|
100
|
+
const port = Math.floor(this._n / 8);
|
|
101
|
+
const bit = this._n % 8;
|
|
102
|
+
return (this._chip._readPort()[port] >> bit) & 1;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Write pin synchronously.
|
|
107
|
+
* @param {number} value - 0 (drive low) or 1 (release to quasi-high input).
|
|
108
|
+
*/
|
|
109
|
+
writeSync(value) {
|
|
110
|
+
this._chip._setPin(this._n, value ? 1 : 0);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Read pin asynchronously.
|
|
115
|
+
* @param {function} callback - Node-style callback(err, value).
|
|
116
|
+
*/
|
|
117
|
+
read(callback) {
|
|
118
|
+
try { callback(null, this.readSync()); } catch (e) { callback(e); }
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Write pin asynchronously.
|
|
123
|
+
* @param {number} value - 0 or 1.
|
|
124
|
+
* @param {function} callback - Node-style callback(err).
|
|
125
|
+
*/
|
|
126
|
+
write(value, callback) {
|
|
127
|
+
try { this.writeSync(value); callback(null); } catch (e) { callback(e); }
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Set pin direction.
|
|
132
|
+
* @param {string} direction - 'in' or 'out'.
|
|
133
|
+
* @param {function} callback - Node-style callback(err).
|
|
134
|
+
*/
|
|
135
|
+
setDirection(direction, callback) {
|
|
136
|
+
try {
|
|
137
|
+
this._direction = direction;
|
|
138
|
+
this._chip._setPin(this._n, direction === 'in' ? 1 : 0);
|
|
139
|
+
if (callback) callback(null);
|
|
140
|
+
} catch (e) { if (callback) callback(e); }
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Release the pin (no-op; shadow state preserved). */
|
|
144
|
+
unexport() {}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* PCF8575 full interface — extends Pcf8575Minimal with interrupt support.
|
|
149
|
+
*
|
|
150
|
+
* Adds configureInterrupt() to attach a callback to the chip's INT line
|
|
151
|
+
* and clearInterrupt() to return the 16-bit changed-pin bitmask.
|
|
152
|
+
* Pin objects gain watch() / unwatch() for per-pin interrupt handlers.
|
|
153
|
+
*
|
|
154
|
+
* @param {object} transport - Configured I²C transport.
|
|
155
|
+
* @param {number} [addr=0x20] - 7-bit I²C device address.
|
|
156
|
+
*/
|
|
157
|
+
class Pcf8575Full extends Pcf8575Minimal {
|
|
158
|
+
/**
|
|
159
|
+
* @param {object} transport - Configured I²C transport.
|
|
160
|
+
* @param {number} [addr=0x20] - 7-bit I²C device address.
|
|
161
|
+
*/
|
|
162
|
+
constructor(transport, addr = 0x20) {
|
|
163
|
+
super(transport, addr);
|
|
164
|
+
const raw = this._readPort();
|
|
165
|
+
this._prev = [raw[0], raw[1]];
|
|
166
|
+
this._callback = null;
|
|
167
|
+
this._pollTimer = null;
|
|
168
|
+
this._watchers = {};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Return a Full pin proxy for pin n (0–15).
|
|
173
|
+
* @param {number} n - Pin index.
|
|
174
|
+
* @param {string} [direction='in'] - Initial direction.
|
|
175
|
+
* @returns {_FullPin} Full pin proxy with watch/unwatch support.
|
|
176
|
+
*/
|
|
177
|
+
pin(n, direction = 'in') {
|
|
178
|
+
const p = new _FullPin(this, n, direction);
|
|
179
|
+
if (direction === 'out') this._setPin(n, 0);
|
|
180
|
+
else this._setPin(n, 1);
|
|
181
|
+
return p;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Attach a callback to the chip's INT output.
|
|
186
|
+
*
|
|
187
|
+
* On Linux, uses a 5 ms polling interval; pass intGpioPath to the
|
|
188
|
+
* sysfs value file for edge-based delivery via epoll. Pass null to use polling.
|
|
189
|
+
*
|
|
190
|
+
* @param {string|null} intGpioPath - Sysfs GPIO value file path, or null for polling.
|
|
191
|
+
* @param {function} callback - Called with 16-bit changed bitmask on any input change.
|
|
192
|
+
*/
|
|
193
|
+
configureInterrupt(intGpioPath, callback) {
|
|
194
|
+
this._callback = callback;
|
|
195
|
+
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
196
|
+
if (intGpioPath) {
|
|
197
|
+
try {
|
|
198
|
+
const fs = require('fs');
|
|
199
|
+
const ep = require('epoll').Epoll;
|
|
200
|
+
const fd = fs.openSync(intGpioPath, 'r');
|
|
201
|
+
const poll = new ep((err, fd2) => {
|
|
202
|
+
fs.readSync(fd2, Buffer.alloc(1), 0, 1, 0);
|
|
203
|
+
const changed = this.clearInterrupt();
|
|
204
|
+
if (changed) this._dispatch(changed);
|
|
205
|
+
});
|
|
206
|
+
poll.add(fd, ep.EPOLLPRI);
|
|
207
|
+
} catch (_) {
|
|
208
|
+
this._startPolling();
|
|
209
|
+
}
|
|
210
|
+
} else {
|
|
211
|
+
this._startPolling();
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
_startPolling() {
|
|
216
|
+
this._pollTimer = setInterval(() => {
|
|
217
|
+
const changed = this.clearInterrupt();
|
|
218
|
+
if (changed) this._dispatch(changed);
|
|
219
|
+
}, 5);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
_dispatch(changed) {
|
|
223
|
+
if (this._callback) this._callback(changed);
|
|
224
|
+
for (const [n, handlers] of Object.entries(this._watchers)) {
|
|
225
|
+
if ((changed >> n) & 1) {
|
|
226
|
+
handlers.forEach(h => h(null, this.readSync ? undefined : 0));
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Read current pin states and return 16-bit bitmask of pins that changed.
|
|
233
|
+
* Also clears the chip's INT line.
|
|
234
|
+
* @returns {number} 16-bit changed-pin bitmask (bits 0–7 = Port 0, bits 8–15 = Port 1).
|
|
235
|
+
*/
|
|
236
|
+
clearInterrupt() {
|
|
237
|
+
const current = this._readPort();
|
|
238
|
+
const changed0 = current[0] ^ this._prev[0];
|
|
239
|
+
const changed1 = current[1] ^ this._prev[1];
|
|
240
|
+
this._prev = [current[0], current[1]];
|
|
241
|
+
return changed0 | (changed1 << 8);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Full GPIO proxy — adds watch/unwatch for interrupt-driven input.
|
|
247
|
+
*
|
|
248
|
+
* @param {Pcf8575Full} chip - Parent full driver instance.
|
|
249
|
+
* @param {number} n - Pin index.
|
|
250
|
+
* @param {string} direction - 'in' or 'out'.
|
|
251
|
+
*/
|
|
252
|
+
class _FullPin extends _Pin {
|
|
253
|
+
constructor(chip, n, direction) {
|
|
254
|
+
super(chip, n, direction);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Register a handler for pin state changes.
|
|
259
|
+
* Requires configureInterrupt() to have been called on the driver.
|
|
260
|
+
* @param {function} handler - Called with (err, value) when pin changes.
|
|
261
|
+
*/
|
|
262
|
+
watch(handler) {
|
|
263
|
+
const n = this._n;
|
|
264
|
+
if (!this._chip._watchers[n]) this._chip._watchers[n] = [];
|
|
265
|
+
this._chip._watchers[n].push((err) => {
|
|
266
|
+
handler(err, this.readSync());
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Remove a previously registered handler.
|
|
272
|
+
* @param {function} handler - The handler to remove.
|
|
273
|
+
*/
|
|
274
|
+
unwatch(handler) {
|
|
275
|
+
const n = this._n;
|
|
276
|
+
if (this._chip._watchers[n]) {
|
|
277
|
+
this._chip._watchers[n] = this._chip._watchers[n].filter(h => h !== handler);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** Remove all handlers for this pin. */
|
|
282
|
+
unwatchAll() {
|
|
283
|
+
this._chip._watchers[this._n] = [];
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Invert active-low polarity for this pin.
|
|
288
|
+
* @param {boolean} invert - true to invert read/write values.
|
|
289
|
+
*/
|
|
290
|
+
setActiveLow(invert) {
|
|
291
|
+
this._activeLow = !!invert;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
module.exports = { Pcf8575Minimal, Pcf8575Full };
|
|
File without changes
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/** Convert HSV (each 0.0–1.0) to [r, g, b] integers 0–255. */
|
|
4
|
+
function _hsvToRgb(h, s, v) {
|
|
5
|
+
if (s === 0) {
|
|
6
|
+
const c = Math.round(v * 255);
|
|
7
|
+
return [c, c, c];
|
|
8
|
+
}
|
|
9
|
+
const i = Math.floor(h * 6);
|
|
10
|
+
const f = h * 6 - i;
|
|
11
|
+
const p = Math.round(v * (1 - s) * 255);
|
|
12
|
+
const q = Math.round(v * (1 - s * f) * 255);
|
|
13
|
+
const t = Math.round(v * (1 - s * (1 - f)) * 255);
|
|
14
|
+
const vv = Math.round(v * 255);
|
|
15
|
+
switch (i % 6) {
|
|
16
|
+
case 0: return [vv, t, p];
|
|
17
|
+
case 1: return [q, vv, p];
|
|
18
|
+
case 2: return [p, vv, t];
|
|
19
|
+
case 3: return [p, q, vv];
|
|
20
|
+
case 4: return [t, p, vv];
|
|
21
|
+
default: return [vv, p, q];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
module.exports = { _hsvToRgb };
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const RESET = Buffer.alloc(24);
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* SK6812RGBW addressable RGBW LED strip — minimal interface.
|
|
7
|
+
*
|
|
8
|
+
* Drives a chain of n SK6812RGBW pixels over a NeoPixel transport.
|
|
9
|
+
* Maintains an internal GRBW buffer; fill() writes every pixel and
|
|
10
|
+
* transmits immediately. Each pixel has four channels: red, green,
|
|
11
|
+
* blue, and white (dedicated white LED element).
|
|
12
|
+
*
|
|
13
|
+
* @param {object} transport - Configured NeoPixel transport.
|
|
14
|
+
* @param {number} n - Number of pixels in the strip.
|
|
15
|
+
*/
|
|
16
|
+
class SK6812RGBWMinimal {
|
|
17
|
+
/**
|
|
18
|
+
* @param {object} transport - Configured NeoPixel transport.
|
|
19
|
+
* @param {number} n - Number of pixels in the strip.
|
|
20
|
+
*/
|
|
21
|
+
constructor(transport, n) {
|
|
22
|
+
this._transport = transport;
|
|
23
|
+
this._n = n;
|
|
24
|
+
this._buf = Buffer.alloc(n * 4);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Fill every pixel with one colour and send to the strip immediately.
|
|
29
|
+
*
|
|
30
|
+
* Clamps each channel to [0, 255]. Stores G, R, B, W (GRBW wire order),
|
|
31
|
+
* then appends 24 reset bytes and calls transport.write().
|
|
32
|
+
*
|
|
33
|
+
* @param {number} r - Red channel (0–255).
|
|
34
|
+
* @param {number} g - Green channel (0–255).
|
|
35
|
+
* @param {number} b - Blue channel (0–255).
|
|
36
|
+
* @param {number} [w=0] - White channel (0–255).
|
|
37
|
+
*/
|
|
38
|
+
fill(r, g = 0, b = 0, w = 0) {
|
|
39
|
+
r = Math.max(0, Math.min(255, r | 0));
|
|
40
|
+
g = Math.max(0, Math.min(255, g | 0));
|
|
41
|
+
b = Math.max(0, Math.min(255, b | 0));
|
|
42
|
+
w = Math.max(0, Math.min(255, w | 0));
|
|
43
|
+
for (let i = 0; i < this._n; i++) {
|
|
44
|
+
this._buf[i * 4] = g;
|
|
45
|
+
this._buf[i * 4 + 1] = r;
|
|
46
|
+
this._buf[i * 4 + 2] = b;
|
|
47
|
+
this._buf[i * 4 + 3] = w;
|
|
48
|
+
}
|
|
49
|
+
this._transport.write(Buffer.concat([this._buf, RESET]));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Turn off all pixels (fill with all zeros and send).
|
|
54
|
+
*
|
|
55
|
+
* Equivalent to fill(0, 0, 0, 0).
|
|
56
|
+
*/
|
|
57
|
+
off() {
|
|
58
|
+
this.fill(0, 0, 0, 0);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* SK6812RGBW full interface — extends SK6812RGBWMinimal with per-pixel control.
|
|
64
|
+
*
|
|
65
|
+
* Adds individual pixel addressing, explicit show(), global brightness
|
|
66
|
+
* scaling, buffer rotation, and HSV fill. Call set_pixel() / set_pixels()
|
|
67
|
+
* to update the buffer, then show() to transmit; or use the inherited
|
|
68
|
+
* fill() for an immediate all-same-colour update.
|
|
69
|
+
*
|
|
70
|
+
* @param {object} transport - Configured NeoPixel transport.
|
|
71
|
+
* @param {number} n - Number of pixels in the strip.
|
|
72
|
+
*/
|
|
73
|
+
class SK6812RGBWFull extends SK6812RGBWMinimal {
|
|
74
|
+
/**
|
|
75
|
+
* @param {object} transport - Configured NeoPixel transport.
|
|
76
|
+
* @param {number} n - Number of pixels in the strip.
|
|
77
|
+
*/
|
|
78
|
+
constructor(transport, n) {
|
|
79
|
+
super(transport, n);
|
|
80
|
+
this._brightness = 255;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Global brightness scalar applied at show() time (0–255).
|
|
85
|
+
* @type {number}
|
|
86
|
+
*/
|
|
87
|
+
get brightness() { return this._brightness; }
|
|
88
|
+
set brightness(value) {
|
|
89
|
+
this._brightness = Math.max(0, Math.min(255, value | 0));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Set one pixel in the buffer without sending.
|
|
94
|
+
*
|
|
95
|
+
* Index is clamped to [0, n-1]; each channel to [0, 255].
|
|
96
|
+
* Call show() to transmit.
|
|
97
|
+
*
|
|
98
|
+
* @param {number} index - Zero-based pixel index.
|
|
99
|
+
* @param {number} r - Red channel (0–255).
|
|
100
|
+
* @param {number} g - Green channel (0–255).
|
|
101
|
+
* @param {number} b - Blue channel (0–255).
|
|
102
|
+
* @param {number} [w=0] - White channel (0–255).
|
|
103
|
+
*/
|
|
104
|
+
set_pixel(index, r, g, b, w = 0) {
|
|
105
|
+
index = Math.max(0, Math.min(this._n - 1, index | 0));
|
|
106
|
+
this._buf[index * 4] = Math.max(0, Math.min(255, g | 0));
|
|
107
|
+
this._buf[index * 4 + 1] = Math.max(0, Math.min(255, r | 0));
|
|
108
|
+
this._buf[index * 4 + 2] = Math.max(0, Math.min(255, b | 0));
|
|
109
|
+
this._buf[index * 4 + 3] = Math.max(0, Math.min(255, w | 0));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Write a sequence of [r, g, b] or [r, g, b, w] arrays into the buffer
|
|
114
|
+
* starting at pixel 0.
|
|
115
|
+
*
|
|
116
|
+
* Extra entries beyond the strip length are ignored. Call show() to transmit.
|
|
117
|
+
*
|
|
118
|
+
* @param {Array<number[]>} colors - Array of [r, g, b] or [r, g, b, w] arrays.
|
|
119
|
+
*/
|
|
120
|
+
set_pixels(colors) {
|
|
121
|
+
for (let i = 0; i < colors.length && i < this._n; i++) {
|
|
122
|
+
const [r, g, b, w = 0] = colors[i];
|
|
123
|
+
this._buf[i * 4] = Math.max(0, Math.min(255, g | 0));
|
|
124
|
+
this._buf[i * 4 + 1] = Math.max(0, Math.min(255, r | 0));
|
|
125
|
+
this._buf[i * 4 + 2] = Math.max(0, Math.min(255, b | 0));
|
|
126
|
+
this._buf[i * 4 + 3] = Math.max(0, Math.min(255, w | 0));
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Transmit the current buffer to the strip, applying brightness scaling.
|
|
132
|
+
*
|
|
133
|
+
* Each channel is scaled: sent = stored * brightness / 255.
|
|
134
|
+
* Appends 24 reset bytes before transmission.
|
|
135
|
+
*/
|
|
136
|
+
show() {
|
|
137
|
+
const bri = this._brightness;
|
|
138
|
+
let pixels;
|
|
139
|
+
if (bri === 255) {
|
|
140
|
+
pixels = this._buf;
|
|
141
|
+
} else {
|
|
142
|
+
pixels = Buffer.alloc(this._buf.length);
|
|
143
|
+
for (let i = 0; i < this._buf.length; i++) {
|
|
144
|
+
pixels[i] = (this._buf[i] * bri / 255) | 0;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
this._transport.write(Buffer.concat([pixels, RESET]));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Shift the pixel buffer left by steps positions (wraps around).
|
|
152
|
+
*
|
|
153
|
+
* Does not transmit — call show() afterwards.
|
|
154
|
+
*
|
|
155
|
+
* @param {number} [steps=1] - Number of pixel positions to shift left.
|
|
156
|
+
*/
|
|
157
|
+
rotate(steps = 1) {
|
|
158
|
+
steps = ((steps % this._n) + this._n) % this._n;
|
|
159
|
+
if (steps === 0) return;
|
|
160
|
+
const bytes = steps * 4;
|
|
161
|
+
const head = Buffer.from(this._buf.slice(0, bytes));
|
|
162
|
+
this._buf.copyWithin(0, bytes);
|
|
163
|
+
head.copy(this._buf, this._buf.length - bytes);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Fill every pixel with one HSV colour and send to the strip immediately.
|
|
168
|
+
*
|
|
169
|
+
* Converts HSV to RGB (white=0) then calls fill().
|
|
170
|
+
*
|
|
171
|
+
* @param {number} h - Hue (0.0–1.0).
|
|
172
|
+
* @param {number} s - Saturation (0.0–1.0).
|
|
173
|
+
* @param {number} v - Value/brightness (0.0–1.0).
|
|
174
|
+
*/
|
|
175
|
+
fill_hsv(h, s, v) {
|
|
176
|
+
const [r, g, b] = _hsvToRgb(h, s, v);
|
|
177
|
+
this.fill(r, g, b, 0);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const { _hsvToRgb } = require('./_color');
|
|
182
|
+
|
|
183
|
+
module.exports = { SK6812RGBWMinimal, SK6812RGBWFull };
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* WS2812B addressable RGB LED strip — minimal interface.
|
|
5
|
+
*
|
|
6
|
+
* Drives a chain of n WS2812B pixels over a NeoPixel transport.
|
|
7
|
+
* Maintains an internal GRB buffer; fill() writes every pixel and
|
|
8
|
+
* transmits immediately.
|
|
9
|
+
*
|
|
10
|
+
* @param {object} transport - Configured NeoPixel transport.
|
|
11
|
+
* @param {number} n - Number of pixels in the strip.
|
|
12
|
+
*/
|
|
13
|
+
class WS2812BMinimal {
|
|
14
|
+
/**
|
|
15
|
+
* @param {object} transport - Configured NeoPixel transport.
|
|
16
|
+
* @param {number} n - Number of pixels in the strip.
|
|
17
|
+
*/
|
|
18
|
+
constructor(transport, n) {
|
|
19
|
+
this._transport = transport;
|
|
20
|
+
this._n = n;
|
|
21
|
+
this._buf = Buffer.alloc(n * 3);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Fill every pixel with one colour and send to the strip immediately.
|
|
26
|
+
*
|
|
27
|
+
* Clamps each channel to [0, 255]. Stores G, R, B (GRB wire order),
|
|
28
|
+
* then calls transport.write().
|
|
29
|
+
*
|
|
30
|
+
* @param {number} r - Red channel (0–255).
|
|
31
|
+
* @param {number} g - Green channel (0–255).
|
|
32
|
+
* @param {number} b - Blue channel (0–255).
|
|
33
|
+
*/
|
|
34
|
+
fill(r, g, b) {
|
|
35
|
+
r = Math.max(0, Math.min(255, r | 0));
|
|
36
|
+
g = Math.max(0, Math.min(255, g | 0));
|
|
37
|
+
b = Math.max(0, Math.min(255, b | 0));
|
|
38
|
+
for (let i = 0; i < this._n; i++) {
|
|
39
|
+
this._buf[i * 3] = g;
|
|
40
|
+
this._buf[i * 3 + 1] = r;
|
|
41
|
+
this._buf[i * 3 + 2] = b;
|
|
42
|
+
}
|
|
43
|
+
this._transport.write(this._buf);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Turn off all pixels (fill with black and send).
|
|
48
|
+
*
|
|
49
|
+
* Equivalent to fill(0, 0, 0).
|
|
50
|
+
*/
|
|
51
|
+
off() {
|
|
52
|
+
this.fill(0, 0, 0);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* WS2812B full interface — extends WS2812BMinimal with per-pixel control.
|
|
58
|
+
*
|
|
59
|
+
* Adds individual pixel addressing, explicit show(), global brightness
|
|
60
|
+
* scaling, buffer rotation, and HSV fill. Call set_pixel() / set_pixels()
|
|
61
|
+
* to update the buffer, then show() to transmit; or use the inherited
|
|
62
|
+
* fill() for an immediate all-same-colour update.
|
|
63
|
+
*
|
|
64
|
+
* @param {object} transport - Configured NeoPixel transport.
|
|
65
|
+
* @param {number} n - Number of pixels in the strip.
|
|
66
|
+
*/
|
|
67
|
+
class WS2812BFull extends WS2812BMinimal {
|
|
68
|
+
/**
|
|
69
|
+
* @param {object} transport - Configured NeoPixel transport.
|
|
70
|
+
* @param {number} n - Number of pixels in the strip.
|
|
71
|
+
*/
|
|
72
|
+
constructor(transport, n) {
|
|
73
|
+
super(transport, n);
|
|
74
|
+
this._brightness = 255;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Global brightness scalar applied at show() time (0–255).
|
|
79
|
+
* @type {number}
|
|
80
|
+
*/
|
|
81
|
+
get brightness() { return this._brightness; }
|
|
82
|
+
set brightness(value) {
|
|
83
|
+
this._brightness = Math.max(0, Math.min(255, value | 0));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Set one pixel in the buffer without sending.
|
|
88
|
+
*
|
|
89
|
+
* Index is clamped to [0, n-1]; each channel to [0, 255].
|
|
90
|
+
* Call show() to transmit.
|
|
91
|
+
*
|
|
92
|
+
* @param {number} index - Zero-based pixel index.
|
|
93
|
+
* @param {number} r - Red channel (0–255).
|
|
94
|
+
* @param {number} g - Green channel (0–255).
|
|
95
|
+
* @param {number} b - Blue channel (0–255).
|
|
96
|
+
*/
|
|
97
|
+
set_pixel(index, r, g, b) {
|
|
98
|
+
index = Math.max(0, Math.min(this._n - 1, index | 0));
|
|
99
|
+
this._buf[index * 3] = Math.max(0, Math.min(255, g | 0));
|
|
100
|
+
this._buf[index * 3 + 1] = Math.max(0, Math.min(255, r | 0));
|
|
101
|
+
this._buf[index * 3 + 2] = Math.max(0, Math.min(255, b | 0));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Write a sequence of [r, g, b] arrays into the buffer starting at pixel 0.
|
|
106
|
+
*
|
|
107
|
+
* Extra entries beyond the strip length are ignored. Call show() to transmit.
|
|
108
|
+
*
|
|
109
|
+
* @param {Array<number[]>} colors - Array of [r, g, b] arrays (0–255 each).
|
|
110
|
+
*/
|
|
111
|
+
set_pixels(colors) {
|
|
112
|
+
for (let i = 0; i < colors.length && i < this._n; i++) {
|
|
113
|
+
const [r, g, b] = colors[i];
|
|
114
|
+
this._buf[i * 3] = Math.max(0, Math.min(255, g | 0));
|
|
115
|
+
this._buf[i * 3 + 1] = Math.max(0, Math.min(255, r | 0));
|
|
116
|
+
this._buf[i * 3 + 2] = Math.max(0, Math.min(255, b | 0));
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Transmit the current buffer to the strip, applying brightness scaling.
|
|
122
|
+
*
|
|
123
|
+
* Each channel is scaled: sent = stored * brightness / 255.
|
|
124
|
+
*/
|
|
125
|
+
show() {
|
|
126
|
+
const bri = this._brightness;
|
|
127
|
+
if (bri === 255) {
|
|
128
|
+
this._transport.write(this._buf);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const scaled = Buffer.alloc(this._buf.length);
|
|
132
|
+
for (let i = 0; i < this._buf.length; i++) {
|
|
133
|
+
scaled[i] = (this._buf[i] * bri / 255) | 0;
|
|
134
|
+
}
|
|
135
|
+
this._transport.write(scaled);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Shift the pixel buffer left by steps positions (wraps around).
|
|
140
|
+
*
|
|
141
|
+
* Does not transmit — call show() afterwards.
|
|
142
|
+
*
|
|
143
|
+
* @param {number} [steps=1] - Number of pixel positions to shift left.
|
|
144
|
+
*/
|
|
145
|
+
rotate(steps = 1) {
|
|
146
|
+
steps = ((steps % this._n) + this._n) % this._n;
|
|
147
|
+
if (steps === 0) return;
|
|
148
|
+
const bytes = steps * 3;
|
|
149
|
+
const head = Buffer.from(this._buf.slice(0, bytes));
|
|
150
|
+
this._buf.copyWithin(0, bytes);
|
|
151
|
+
head.copy(this._buf, this._buf.length - bytes);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Fill every pixel with one HSV colour and send to the strip immediately.
|
|
156
|
+
*
|
|
157
|
+
* Converts HSV to RGB then calls fill().
|
|
158
|
+
*
|
|
159
|
+
* @param {number} h - Hue (0.0–1.0).
|
|
160
|
+
* @param {number} s - Saturation (0.0–1.0).
|
|
161
|
+
* @param {number} v - Value/brightness (0.0–1.0).
|
|
162
|
+
*/
|
|
163
|
+
fill_hsv(h, s, v) {
|
|
164
|
+
const [r, g, b] = _hsvToRgb(h, s, v);
|
|
165
|
+
this.fill(r, g, b);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const { _hsvToRgb } = require('./_color');
|
|
170
|
+
|
|
171
|
+
module.exports = { WS2812BMinimal, WS2812BFull };
|
|
File without changes
|