periph 1.1.0 → 1.2.1
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/README.md +61 -12
- package/examples/accelerometer/adxl345/complete.js +52 -0
- package/examples/accelerometer/adxl345/demo.js +34 -0
- package/examples/accelerometer/adxl345/minimal.js +18 -0
- package/examples/accelerometer/adxl362/complete.js +57 -0
- package/examples/accelerometer/adxl362/demo.js +51 -0
- package/examples/accelerometer/adxl362/minimal.js +21 -0
- package/examples/adc_dac/ad7705/complete.js +32 -0
- package/examples/adc_dac/ad7705/demo.js +42 -0
- package/examples/adc_dac/ad7705/minimal.js +16 -0
- package/examples/adc_dac/ad7706/complete.js +35 -0
- package/examples/adc_dac/ad7706/demo.js +44 -0
- package/examples/adc_dac/ad7706/minimal.js +16 -0
- package/examples/adc_dac/hx710a/complete.js +45 -0
- package/examples/adc_dac/hx710a/demo.js +52 -0
- package/examples/adc_dac/hx710a/minimal.js +16 -0
- package/examples/adc_dac/hx710b/complete.js +45 -0
- package/examples/adc_dac/hx710b/demo.js +64 -0
- package/examples/adc_dac/hx710b/minimal.js +16 -0
- package/examples/adc_dac/hx711/complete.js +7 -7
- package/examples/adc_dac/hx711/demo.js +6 -6
- package/examples/adc_dac/hx711/minimal.js +7 -7
- package/examples/adc_dac/mcp4725/complete.js +19 -17
- package/examples/adc_dac/mcp4725/demo.js +5 -5
- package/examples/adc_dac/mcp4725/minimal.js +8 -6
- package/examples/adc_dac/mcp4728/complete.js +24 -22
- package/examples/adc_dac/mcp4728/demo.js +22 -22
- package/examples/adc_dac/mcp4728/minimal.js +8 -6
- package/examples/adc_dac/pcf8591/complete.js +41 -0
- package/examples/adc_dac/pcf8591/demo.js +39 -0
- package/examples/adc_dac/pcf8591/minimal.js +19 -0
- package/examples/comms/mcp2515/complete.js +75 -0
- package/examples/comms/mcp2515/demo.js +66 -0
- package/examples/comms/mcp2515/minimal.js +27 -0
- package/examples/comms/rda5807m/complete.js +55 -0
- package/examples/comms/rda5807m/demo.js +68 -0
- package/examples/comms/rda5807m/minimal.js +15 -0
- package/examples/comms/rfm9x/complete.js +50 -0
- package/examples/comms/rfm9x/demo.js +55 -0
- package/examples/comms/rfm9x/minimal.js +20 -0
- package/examples/display/pcf8576/complete.js +18 -15
- package/examples/display/pcf8576/demo.js +6 -6
- package/examples/display/pcf8576/minimal.js +12 -10
- package/examples/environmental/aht21/complete.js +15 -13
- package/examples/environmental/aht21/demo.js +8 -6
- package/examples/environmental/aht21/minimal.js +5 -5
- package/examples/environmental/bme280/complete.js +23 -20
- package/examples/environmental/bme280/demo.js +14 -14
- package/examples/environmental/bme280/minimal.js +13 -11
- package/examples/environmental/bme680/complete.js +25 -22
- package/examples/environmental/bme680/demo.js +38 -36
- package/examples/environmental/bme680/minimal.js +14 -12
- package/examples/gas/ens160/complete.js +29 -27
- package/examples/gas/ens160/demo.js +39 -37
- package/examples/gas/ens160/minimal.js +17 -15
- package/examples/gnss/neo6/complete.js +49 -0
- package/examples/gnss/neo6/demo.js +54 -0
- package/examples/gnss/neo6/minimal.js +30 -0
- package/examples/gyroscope/l3g4200d/complete.js +56 -0
- package/examples/gyroscope/l3g4200d/demo.js +49 -0
- package/examples/gyroscope/l3g4200d/minimal.js +19 -0
- package/examples/gyroscope/l3gd20h/complete.js +42 -0
- package/examples/gyroscope/l3gd20h/demo.js +29 -0
- package/examples/gyroscope/l3gd20h/minimal.js +13 -0
- package/examples/humidity/dht11/complete.js +22 -0
- package/examples/humidity/dht11/demo.js +38 -0
- package/examples/humidity/dht11/minimal.js +18 -0
- package/examples/imu/mpu6050/complete.js +27 -25
- package/examples/imu/mpu6050/demo.js +29 -26
- package/examples/imu/mpu6050/minimal.js +6 -6
- package/examples/imu/mpu9250/complete.js +65 -0
- package/examples/imu/mpu9250/demo.js +53 -0
- package/examples/imu/mpu9250/minimal.js +21 -0
- package/examples/imu/mpu9255/complete.js +73 -0
- package/examples/imu/mpu9255/demo.js +73 -0
- package/examples/imu/mpu9255/minimal.js +21 -0
- package/examples/io_expander/mcp23017/complete.js +70 -77
- package/examples/io_expander/mcp23017/demo.js +37 -33
- package/examples/io_expander/mcp23017/minimal.js +15 -11
- package/examples/io_expander/pcf8574/complete.js +60 -65
- package/examples/io_expander/pcf8574/demo.js +30 -25
- package/examples/io_expander/pcf8574/minimal.js +15 -11
- package/examples/io_expander/pcf8575/complete.js +24 -20
- package/examples/io_expander/pcf8575/demo.js +27 -23
- package/examples/io_expander/pcf8575/minimal.js +16 -12
- package/examples/io_expander/tpic6b595/complete.js +57 -0
- package/examples/io_expander/tpic6b595/demo.js +75 -0
- package/examples/io_expander/tpic6b595/minimal.js +26 -0
- package/examples/led/apa102/complete.js +72 -0
- package/examples/led/apa102/demo.js +92 -0
- package/examples/led/apa102/minimal.js +21 -0
- package/examples/led/sk6812rgbw/complete.js +42 -40
- package/examples/led/sk6812rgbw/demo.js +16 -16
- package/examples/led/sk6812rgbw/minimal.js +15 -17
- package/examples/led/ws2812b/complete.js +40 -38
- package/examples/led/ws2812b/demo.js +16 -16
- package/examples/led/ws2812b/minimal.js +13 -14
- package/examples/led/ws2814/complete.js +71 -0
- package/examples/led/ws2814/demo.js +82 -0
- package/examples/led/ws2814/minimal.js +23 -0
- package/examples/light/apds-9930/complete.js +46 -0
- package/examples/light/apds-9930/demo.js +31 -0
- package/examples/light/apds-9930/minimal.js +20 -0
- package/examples/light/apds9960/complete.js +45 -43
- package/examples/light/apds9960/demo.js +11 -9
- package/examples/light/apds9960/minimal.js +5 -5
- package/examples/magnetometer/as5600/complete.js +31 -29
- package/examples/magnetometer/as5600/demo.js +35 -33
- package/examples/magnetometer/as5600/minimal.js +7 -7
- package/examples/magnetometer/hmc5883l/complete.js +47 -0
- package/examples/magnetometer/hmc5883l/demo.js +50 -0
- package/examples/magnetometer/hmc5883l/minimal.js +15 -0
- package/examples/memory/24aa02uid/complete.js +23 -21
- package/examples/memory/24aa02uid/demo.js +32 -30
- package/examples/memory/24aa02uid/minimal.js +5 -5
- package/examples/motor/drv8830/complete.js +50 -0
- package/examples/motor/drv8830/demo.js +57 -0
- package/examples/motor/drv8830/minimal.js +23 -0
- package/examples/other/mpr121/complete.js +50 -0
- package/examples/other/mpr121/demo.js +30 -0
- package/examples/other/mpr121/minimal.js +17 -0
- package/examples/power/ade7953/complete.js +44 -0
- package/examples/power/ade7953/demo.js +28 -0
- package/examples/power/ade7953/minimal.js +15 -0
- package/examples/power/ina219/complete.js +20 -18
- package/examples/power/ina219/demo.js +15 -13
- package/examples/power/ina219/minimal.js +8 -8
- package/examples/power/ina226/complete.js +24 -22
- package/examples/power/ina226/demo.js +22 -20
- package/examples/power/ina226/minimal.js +5 -5
- package/examples/power/ina3221/complete.js +51 -49
- package/examples/power/ina3221/demo.js +13 -13
- package/examples/power/ina3221/minimal.js +8 -8
- package/examples/pressure/bmp085/complete.js +29 -0
- package/examples/pressure/bmp085/demo.js +34 -0
- package/examples/pressure/bmp085/minimal.js +19 -0
- package/examples/pressure/bmp180/complete.js +20 -17
- package/examples/pressure/bmp180/demo.js +24 -22
- package/examples/pressure/bmp180/minimal.js +13 -11
- package/examples/pressure/bmp280/complete.js +23 -20
- package/examples/pressure/bmp280/demo.js +44 -41
- package/examples/pressure/bmp280/minimal.js +13 -11
- package/examples/pressure/bmp384/complete.js +29 -0
- package/examples/pressure/bmp384/demo.js +40 -0
- package/examples/pressure/bmp384/minimal.js +18 -0
- package/examples/pressure/bmp581/complete.js +34 -0
- package/examples/pressure/bmp581/demo.js +56 -0
- package/examples/pressure/bmp581/minimal.js +19 -0
- package/examples/pressure/lps22df/complete.js +50 -0
- package/examples/pressure/lps22df/demo.js +41 -0
- package/examples/pressure/lps22df/minimal.js +19 -0
- package/examples/pressure/lps28dfw/complete.js +37 -0
- package/examples/pressure/lps28dfw/demo.js +33 -0
- package/examples/pressure/lps28dfw/minimal.js +19 -0
- package/examples/pressure/lps33hw/complete.js +43 -0
- package/examples/pressure/lps33hw/demo.js +43 -0
- package/examples/pressure/lps33hw/minimal.js +19 -0
- package/examples/rfid/mfrc522/complete.js +44 -0
- package/examples/rfid/mfrc522/demo.js +70 -0
- package/examples/rfid/mfrc522/minimal.js +17 -0
- package/examples/rtc/ds3231/complete.js +77 -0
- package/examples/rtc/ds3231/demo.js +60 -0
- package/examples/rtc/ds3231/minimal.js +23 -0
- package/examples/rtc/pcf8523/complete.js +66 -0
- package/examples/rtc/pcf8523/demo.js +65 -0
- package/examples/rtc/pcf8523/minimal.js +20 -0
- package/examples/temperature/mcp9808/complete.js +89 -0
- package/examples/temperature/mcp9808/demo.js +62 -0
- package/examples/temperature/mcp9808/minimal.js +21 -0
- package/examples/temperature/tmp117/complete.js +90 -0
- package/examples/temperature/tmp117/demo.js +73 -0
- package/examples/temperature/tmp117/minimal.js +21 -0
- package/examples/tof/vl53l0x/complete.js +118 -0
- package/examples/tof/vl53l0x/demo.js +83 -0
- package/examples/tof/vl53l0x/minimal.js +25 -0
- package/examples/tof/vl53l1x/complete.js +151 -0
- package/examples/tof/vl53l1x/demo.js +100 -0
- package/examples/tof/vl53l1x/minimal.js +25 -0
- package/package.json +5 -1
- package/src/chips/accelerometer/adxl345.js +482 -0
- package/src/chips/accelerometer/adxl362.js +594 -0
- package/src/chips/adc_dac/ad7705.js +345 -0
- package/src/chips/adc_dac/ad7706.js +331 -0
- package/src/chips/adc_dac/hx710a.js +184 -0
- package/src/chips/adc_dac/hx710b.js +186 -0
- package/src/chips/adc_dac/hx711.js +15 -15
- package/src/chips/adc_dac/mcp4725.js +48 -40
- package/src/chips/adc_dac/mcp4728.js +57 -44
- package/src/chips/adc_dac/pcf8591.js +196 -0
- package/src/chips/comms/mcp2515.js +659 -0
- package/src/chips/comms/rda5807m.js +393 -0
- package/src/chips/comms/rfm9x.js +590 -0
- package/src/chips/display/pcf8576.js +42 -33
- package/src/chips/environmental/aht21.js +52 -37
- package/src/chips/environmental/bme280.js +85 -66
- package/src/chips/environmental/bme680.js +110 -87
- package/src/chips/gas/ens160.js +82 -64
- package/src/chips/gnss/neo6.js +438 -0
- package/src/chips/gyroscope/l3g4200d.js +366 -0
- package/src/chips/gyroscope/l3gd20h.js +314 -0
- package/src/chips/humidity/dht11.js +135 -0
- package/src/chips/imu/mpu6050.js +89 -66
- package/src/chips/imu/mpu9250.js +394 -0
- package/src/chips/imu/mpu9255.js +447 -0
- package/src/chips/io_expander/mcp23017.js +240 -157
- package/src/chips/io_expander/pcf8574.js +149 -124
- package/src/chips/io_expander/pcf8575.js +164 -135
- package/src/chips/io_expander/tpic6b595.js +242 -0
- package/src/chips/led/_NeoPixelRGBBase.js +204 -0
- package/src/chips/led/_NeoPixelRGBWBase.js +218 -0
- package/src/chips/led/apa102.js +236 -0
- package/src/chips/led/sk6812rgbw.js +15 -149
- package/src/chips/led/ws2812b.js +14 -139
- package/src/chips/led/ws2814.js +49 -0
- package/src/chips/light/apds-9930.js +342 -0
- package/src/chips/light/apds9960.js +158 -123
- package/src/chips/magnetometer/as5600.js +102 -82
- package/src/chips/magnetometer/hmc5883l.js +222 -0
- package/src/chips/memory/_24aa02uid.js +32 -29
- package/src/chips/motor/drv8830.js +239 -0
- package/src/chips/other/mpr121.js +375 -0
- package/src/chips/power/ade7953.js +427 -0
- package/src/chips/power/ina219.js +45 -40
- package/src/chips/power/ina226.js +50 -45
- package/src/chips/power/ina3221.js +95 -76
- package/src/chips/pressure/bmp085.js +251 -0
- package/src/chips/pressure/bmp180.js +54 -37
- package/src/chips/pressure/bmp280.js +72 -53
- package/src/chips/pressure/bmp384.js +410 -0
- package/src/chips/pressure/bmp581.js +489 -0
- package/src/chips/pressure/lps22df.js +341 -0
- package/src/chips/pressure/lps28dfw.js +314 -0
- package/src/chips/pressure/lps33hw.js +255 -0
- package/src/chips/rfid/mfrc522.js +674 -0
- package/src/chips/rtc/ds3231.js +504 -0
- package/src/chips/rtc/pcf8523.js +539 -0
- package/src/chips/temperature/mcp9808.js +488 -0
- package/src/chips/temperature/tmp117.js +471 -0
- package/src/chips/tof/_vl53_base.js +160 -0
- package/src/chips/tof/vl53l0x.js +816 -0
- package/src/chips/tof/vl53l1x.js +816 -0
- package/src/connection/connection.js +111 -0
- package/src/connection/dhtxx.js +138 -0
- package/src/connection/dhtxx_mock.js +39 -0
- package/src/{transport → connection}/hx711.js +49 -22
- package/src/connection/hx711_mock.js +52 -0
- package/src/{transport → connection}/i2c.js +13 -9
- package/src/connection/i2c_mock.js +91 -0
- package/src/connection/input_pin.js +156 -0
- package/src/connection/neo6_mock.js +45 -0
- package/src/{transport → connection}/neopixel.js +23 -4
- package/src/connection/output_pin.js +41 -0
- package/src/connection/sipo.js +159 -0
- package/src/{transport → connection}/smbus.js +14 -10
- package/src/{transport → connection}/spi.js +12 -8
- package/src/connection/spi_mock.js +151 -0
- package/src/{transport → connection}/uart.js +33 -26
- package/examples/gnss/.gitkeep +0 -0
- package/examples/humidity/.gitkeep +0 -0
- package/examples/motor/.gitkeep +0 -0
- package/examples/temperature/.gitkeep +0 -0
- package/examples/tof/.gitkeep +0 -0
- package/src/chips/gnss/.gitkeep +0 -0
- package/src/chips/motor/.gitkeep +0 -0
- package/src/chips/temperature/.gitkeep +0 -0
- package/src/chips/tof/.gitkeep +0 -0
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* APA102 addressable RGB LED strip — minimal interface.
|
|
5
|
+
*
|
|
6
|
+
* Drives a chain of n APA102 pixels over an SPI connection (Mode 0, MSB first).
|
|
7
|
+
* Maintains an internal BGR+brightness buffer; fill() writes every pixel and
|
|
8
|
+
* transmits the full frame (start + pixels + end) immediately.
|
|
9
|
+
*
|
|
10
|
+
* The APA102 frame format is:
|
|
11
|
+
* start frame: 4 zero-bytes (0x00 × 4)
|
|
12
|
+
* pixel data: n × 4 bytes [0xE0|brightness, B, G, R] (BGR wire order)
|
|
13
|
+
* end frame: max(4, (n+15)//16) bytes of 0xFF
|
|
14
|
+
*
|
|
15
|
+
* @param {import('../../connection/spi').SPIConnection} connection - Configured SPI connection (Mode 0, MSB first).
|
|
16
|
+
* @param {number} n - Number of pixels in the strip.
|
|
17
|
+
*/
|
|
18
|
+
class APA102Minimal {
|
|
19
|
+
/**
|
|
20
|
+
* @param {import('../../connection/spi').SPIConnection} connection - Configured SPI connection (Mode 0, MSB first).
|
|
21
|
+
* @param {number} n - Number of pixels in the strip.
|
|
22
|
+
*/
|
|
23
|
+
constructor(connection, n) {
|
|
24
|
+
this._conn = connection;
|
|
25
|
+
this._n = n;
|
|
26
|
+
this._buf = Buffer.alloc(n * 4);
|
|
27
|
+
// Initialize buffer with hardware brightness=31, all channels off
|
|
28
|
+
for (let i = 0; i < n; i++) {
|
|
29
|
+
this._buf[i * 4] = 0xE0 | 31; // brightness byte (3 high bits = 1)
|
|
30
|
+
this._buf[i * 4 + 1] = 0; // blue
|
|
31
|
+
this._buf[i * 4 + 2] = 0; // green
|
|
32
|
+
this._buf[i * 4 + 3] = 0; // red
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Fill every pixel with one colour and send to the strip immediately.
|
|
38
|
+
*
|
|
39
|
+
* Clamps each channel to [0, 255]. Stores brightness/B/G/R in the
|
|
40
|
+
* internal buffer (BGR wire order with hardware brightness byte first),
|
|
41
|
+
* then transmits the full APA102 frame.
|
|
42
|
+
*
|
|
43
|
+
* @param {number} r - Red channel (0–255).
|
|
44
|
+
* @param {number} g - Green channel (0–255).
|
|
45
|
+
* @param {number} b - Blue channel (0–255).
|
|
46
|
+
* @returns {Promise<void>}
|
|
47
|
+
*/
|
|
48
|
+
async fill(r, g, b) {
|
|
49
|
+
r = Math.max(0, Math.min(255, r | 0));
|
|
50
|
+
g = Math.max(0, Math.min(255, g | 0));
|
|
51
|
+
b = Math.max(0, Math.min(255, b | 0));
|
|
52
|
+
for (let i = 0; i < this._n; i++) {
|
|
53
|
+
this._buf[i * 4] = 0xE0 | 31; // hardware brightness = 31 (max)
|
|
54
|
+
this._buf[i * 4 + 1] = b; // blue
|
|
55
|
+
this._buf[i * 4 + 2] = g; // green
|
|
56
|
+
this._buf[i * 4 + 3] = r; // red
|
|
57
|
+
}
|
|
58
|
+
await this._sendFrame();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Turn off all pixels (fill with black and send).
|
|
63
|
+
*
|
|
64
|
+
* Equivalent to fill(0, 0, 0).
|
|
65
|
+
* @returns {Promise<void>}
|
|
66
|
+
*/
|
|
67
|
+
async off() {
|
|
68
|
+
await this.fill(0, 0, 0);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Send the full APA102 frame (start + pixel buffer + end).
|
|
73
|
+
* @returns {Promise<void>}
|
|
74
|
+
* @private
|
|
75
|
+
*/
|
|
76
|
+
async _sendFrame() {
|
|
77
|
+
const endBytes = Math.max(4, Math.floor((this._n + 15) / 16));
|
|
78
|
+
const frame = Buffer.alloc(4 + this._n * 4 + endBytes);
|
|
79
|
+
// Start frame: 4 zero bytes
|
|
80
|
+
frame[0] = frame[1] = frame[2] = frame[3] = 0x00;
|
|
81
|
+
// Pixel data
|
|
82
|
+
this._buf.copy(frame, 4);
|
|
83
|
+
// End frame: 0xFF bytes
|
|
84
|
+
for (let i = 0; i < endBytes; i++) {
|
|
85
|
+
frame[4 + this._n * 4 + i] = 0xFF;
|
|
86
|
+
}
|
|
87
|
+
await this._conn.write(frame);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* APA102 full interface — extends APA102Minimal with per-pixel control.
|
|
93
|
+
*
|
|
94
|
+
* Adds individual pixel addressing with per-pixel hardware brightness,
|
|
95
|
+
* explicit show(), global software brightness scaling, buffer rotation,
|
|
96
|
+
* and HSV fill. Call set_pixel() / set_pixels() to update the buffer,
|
|
97
|
+
* then show() to transmit; or use the inherited fill() for an immediate
|
|
98
|
+
* all-same-colour update.
|
|
99
|
+
*
|
|
100
|
+
* @param {import('../../connection/spi').SPIConnection} connection - Configured SPI connection (Mode 0, MSB first).
|
|
101
|
+
* @param {number} n - Number of pixels in the strip.
|
|
102
|
+
*/
|
|
103
|
+
class APA102Full extends APA102Minimal {
|
|
104
|
+
/**
|
|
105
|
+
* @param {import('../../connection/spi').SPIConnection} connection - Configured SPI connection (Mode 0, MSB first).
|
|
106
|
+
* @param {number} n - Number of pixels in the strip.
|
|
107
|
+
*/
|
|
108
|
+
constructor(connection, n) {
|
|
109
|
+
super(connection, n);
|
|
110
|
+
this._brightness = 255; // global software brightness (0–255)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Global software brightness scalar applied at show() time (0–255).
|
|
115
|
+
* @type {number}
|
|
116
|
+
*/
|
|
117
|
+
get brightness() { return this._brightness; }
|
|
118
|
+
set brightness(value) {
|
|
119
|
+
this._brightness = Math.max(0, Math.min(255, value | 0));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Set one pixel in the buffer without sending.
|
|
124
|
+
*
|
|
125
|
+
* Index is clamped to [0, n-1]; each RGB channel is clamped to [0, 255];
|
|
126
|
+
* pixel_brightness is clamped to [0, 31]. Call show() to transmit.
|
|
127
|
+
*
|
|
128
|
+
* @param {number} index - Zero-based pixel index.
|
|
129
|
+
* @param {number} r - Red channel (0–255).
|
|
130
|
+
* @param {number} g - Green channel (0–255).
|
|
131
|
+
* @param {number} b - Blue channel (0–255).
|
|
132
|
+
* @param {number} [pixel_brightness=31] - Per-pixel hardware brightness 0–31.
|
|
133
|
+
*/
|
|
134
|
+
set_pixel(index, r, g, b, pixel_brightness = 31) {
|
|
135
|
+
index = Math.max(0, Math.min(this._n - 1, index | 0));
|
|
136
|
+
r = Math.max(0, Math.min(255, r | 0));
|
|
137
|
+
g = Math.max(0, Math.min(255, g | 0));
|
|
138
|
+
b = Math.max(0, Math.min(255, b | 0));
|
|
139
|
+
pixel_brightness = Math.max(0, Math.min(31, pixel_brightness | 0));
|
|
140
|
+
this._buf[index * 4] = 0xE0 | pixel_brightness;
|
|
141
|
+
this._buf[index * 4 + 1] = b;
|
|
142
|
+
this._buf[index * 4 + 2] = g;
|
|
143
|
+
this._buf[index * 4 + 3] = r;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Set multiple pixels from an array of [r, g, b] or [r, g, b, pixel_brightness] arrays.
|
|
148
|
+
*
|
|
149
|
+
* Each element is [r, g, b] or [r, g, b, pixel_brightness]. Missing brightness
|
|
150
|
+
* defaults to 31. Extra entries beyond the strip length are ignored.
|
|
151
|
+
* Does not transmit — call show() afterwards.
|
|
152
|
+
*
|
|
153
|
+
* @param {Array<number[]>} colors - Array of [r, g, b] or [r, g, b, pixel_brightness] arrays.
|
|
154
|
+
*/
|
|
155
|
+
set_pixels(colors) {
|
|
156
|
+
for (let i = 0; i < colors.length && i < this._n; i++) {
|
|
157
|
+
const [r, g, b, pixel_brightness = 31] = colors[i];
|
|
158
|
+
this._buf[i * 4] = 0xE0 | Math.max(0, Math.min(31, pixel_brightness | 0));
|
|
159
|
+
this._buf[i * 4 + 1] = Math.max(0, Math.min(255, b | 0));
|
|
160
|
+
this._buf[i * 4 + 2] = Math.max(0, Math.min(255, g | 0));
|
|
161
|
+
this._buf[i * 4 + 3] = Math.max(0, Math.min(255, r | 0));
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Transmit the current buffer to the strip, applying software brightness scaling.
|
|
167
|
+
*
|
|
168
|
+
* Each RGB channel value is scaled: sent = stored * brightness / 255.
|
|
169
|
+
* The per-pixel hardware brightness byte is NOT scaled.
|
|
170
|
+
* @returns {Promise<void>}
|
|
171
|
+
*/
|
|
172
|
+
async show() {
|
|
173
|
+
const bri = this._brightness;
|
|
174
|
+
const endBytes = Math.max(4, Math.floor((this._n + 15) / 16));
|
|
175
|
+
const pixelDataLen = this._n * 4;
|
|
176
|
+
const totalLen = 4 + pixelDataLen + endBytes;
|
|
177
|
+
|
|
178
|
+
const frame = Buffer.alloc(totalLen);
|
|
179
|
+
frame[0] = frame[1] = frame[2] = frame[3] = 0x00;
|
|
180
|
+
|
|
181
|
+
if (bri === 255) {
|
|
182
|
+
this._buf.copy(frame, 4);
|
|
183
|
+
} else {
|
|
184
|
+
// Scale RGB channels, leave hardware brightness byte unchanged
|
|
185
|
+
for (let i = 0; i < this._n; i++) {
|
|
186
|
+
const base = i * 4;
|
|
187
|
+
frame[4 + base] = this._buf[base]; // hardware brightness
|
|
188
|
+
frame[4 + base + 1] = (this._buf[base + 1] * bri / 255) | 0; // blue
|
|
189
|
+
frame[4 + base + 2] = (this._buf[base + 2] * bri / 255) | 0; // green
|
|
190
|
+
frame[4 + base + 3] = (this._buf[base + 3] * bri / 255) | 0; // red
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
for (let i = 0; i < endBytes; i++) {
|
|
195
|
+
frame[4 + pixelDataLen + i] = 0xFF;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
await this._conn.write(frame);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Shift the pixel buffer left by steps whole-pixel positions (wraps around).
|
|
203
|
+
*
|
|
204
|
+
* Each step shifts 4 bytes (one BGR+brightness pixel). Does not transmit —
|
|
205
|
+
* call show() afterwards.
|
|
206
|
+
*
|
|
207
|
+
* @param {number} [steps=1] - Number of pixel positions to shift left.
|
|
208
|
+
*/
|
|
209
|
+
rotate(steps = 1) {
|
|
210
|
+
steps = ((steps % this._n) + this._n) % this._n;
|
|
211
|
+
if (steps === 0) return;
|
|
212
|
+
const bytes = steps * 4;
|
|
213
|
+
const head = Buffer.from(this._buf.slice(0, bytes));
|
|
214
|
+
this._buf.copyWithin(0, bytes);
|
|
215
|
+
head.copy(this._buf, this._buf.length - bytes);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Fill every pixel with one HSV colour and send to the strip immediately.
|
|
220
|
+
*
|
|
221
|
+
* Converts HSV to RGB then calls fill() at hardware brightness 31.
|
|
222
|
+
*
|
|
223
|
+
* @param {number} h - Hue (0.0–1.0).
|
|
224
|
+
* @param {number} s - Saturation (0.0–1.0).
|
|
225
|
+
* @param {number} v - Value/brightness (0.0–1.0).
|
|
226
|
+
* @returns {Promise<void>}
|
|
227
|
+
*/
|
|
228
|
+
async fill_hsv(h, s, v) {
|
|
229
|
+
const [r, g, b] = _hsvToRgb(h, s, v);
|
|
230
|
+
await this.fill(r, g, b);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const { _hsvToRgb } = require('./_color');
|
|
235
|
+
|
|
236
|
+
module.exports = { APA102Minimal, APA102Full };
|
|
@@ -1,183 +1,49 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const { NeoPixelRGBWMinimal, NeoPixelRGBWFull } = require('./_NeoPixelRGBWBase');
|
|
4
|
+
|
|
5
|
+
const RESET_BYTES = 24;
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
8
|
* SK6812RGBW addressable RGBW LED strip — minimal interface.
|
|
7
9
|
*
|
|
8
|
-
* Drives a chain of n SK6812RGBW pixels over a NeoPixel
|
|
10
|
+
* Drives a chain of n SK6812RGBW pixels over a NeoPixel connection.
|
|
9
11
|
* Maintains an internal GRBW buffer; fill() writes every pixel and
|
|
10
12
|
* transmits immediately. Each pixel has four channels: red, green,
|
|
11
13
|
* blue, and white (dedicated white LED element).
|
|
12
14
|
*
|
|
13
|
-
* @param {
|
|
15
|
+
* @param {import('../../connection/neopixel').NeoPixelConnection} connection - Configured NeoPixel connection.
|
|
14
16
|
* @param {number} n - Number of pixels in the strip.
|
|
15
17
|
*/
|
|
16
|
-
class SK6812RGBWMinimal {
|
|
18
|
+
class SK6812RGBWMinimal extends NeoPixelRGBWMinimal {
|
|
17
19
|
/**
|
|
18
|
-
* @param {
|
|
20
|
+
* @param {import('../../connection/neopixel').NeoPixelConnection} connection - Configured NeoPixel connection.
|
|
19
21
|
* @param {number} n - Number of pixels in the strip.
|
|
20
22
|
*/
|
|
21
|
-
constructor(
|
|
22
|
-
|
|
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);
|
|
23
|
+
constructor(connection, n) {
|
|
24
|
+
super(connection, n, [1, 0, 2, 3], RESET_BYTES);
|
|
59
25
|
}
|
|
60
26
|
}
|
|
61
27
|
|
|
62
28
|
/**
|
|
63
|
-
* SK6812RGBW full interface —
|
|
29
|
+
* SK6812RGBW full interface — per-pixel control, brightness, rotation, HSV fill.
|
|
64
30
|
*
|
|
65
31
|
* Adds individual pixel addressing, explicit show(), global brightness
|
|
66
32
|
* scaling, buffer rotation, and HSV fill. Call set_pixel() / set_pixels()
|
|
67
33
|
* to update the buffer, then show() to transmit; or use the inherited
|
|
68
34
|
* fill() for an immediate all-same-colour update.
|
|
69
35
|
*
|
|
70
|
-
* @param {
|
|
36
|
+
* @param {import('../../connection/neopixel').NeoPixelConnection} connection - Configured NeoPixel connection.
|
|
71
37
|
* @param {number} n - Number of pixels in the strip.
|
|
72
38
|
*/
|
|
73
|
-
class SK6812RGBWFull extends
|
|
39
|
+
class SK6812RGBWFull extends NeoPixelRGBWFull {
|
|
74
40
|
/**
|
|
75
|
-
* @param {
|
|
41
|
+
* @param {import('../../connection/neopixel').NeoPixelConnection} connection - Configured NeoPixel connection.
|
|
76
42
|
* @param {number} n - Number of pixels in the strip.
|
|
77
43
|
*/
|
|
78
|
-
constructor(
|
|
79
|
-
super(
|
|
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);
|
|
44
|
+
constructor(connection, n) {
|
|
45
|
+
super(connection, n, [1, 0, 2, 3], RESET_BYTES);
|
|
178
46
|
}
|
|
179
47
|
}
|
|
180
48
|
|
|
181
|
-
const { _hsvToRgb } = require('./_color');
|
|
182
|
-
|
|
183
49
|
module.exports = { SK6812RGBWMinimal, SK6812RGBWFull };
|
package/src/chips/led/ws2812b.js
CHANGED
|
@@ -1,171 +1,46 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const { NeoPixelRGBMinimal, NeoPixelRGBFull } = require('./_NeoPixelRGBBase');
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
6
|
* WS2812B addressable RGB LED strip — minimal interface.
|
|
5
7
|
*
|
|
6
|
-
* Drives a chain of n WS2812B pixels over a NeoPixel
|
|
8
|
+
* Drives a chain of n WS2812B pixels over a NeoPixel connection.
|
|
7
9
|
* Maintains an internal GRB buffer; fill() writes every pixel and
|
|
8
10
|
* transmits immediately.
|
|
9
11
|
*
|
|
10
|
-
* @param {
|
|
12
|
+
* @param {import('../../connection/neopixel').NeoPixelConnection} connection - Configured NeoPixel connection.
|
|
11
13
|
* @param {number} n - Number of pixels in the strip.
|
|
12
14
|
*/
|
|
13
|
-
class WS2812BMinimal {
|
|
15
|
+
class WS2812BMinimal extends NeoPixelRGBMinimal {
|
|
14
16
|
/**
|
|
15
|
-
* @param {
|
|
17
|
+
* @param {import('../../connection/neopixel').NeoPixelConnection} connection - Configured NeoPixel connection.
|
|
16
18
|
* @param {number} n - Number of pixels in the strip.
|
|
17
19
|
*/
|
|
18
|
-
constructor(
|
|
19
|
-
|
|
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);
|
|
20
|
+
constructor(connection, n) {
|
|
21
|
+
super(connection, n, [1, 0, 2], 0);
|
|
53
22
|
}
|
|
54
23
|
}
|
|
55
24
|
|
|
56
25
|
/**
|
|
57
|
-
* WS2812B full interface —
|
|
26
|
+
* WS2812B full interface — per-pixel control, brightness, rotation, HSV fill.
|
|
58
27
|
*
|
|
59
28
|
* Adds individual pixel addressing, explicit show(), global brightness
|
|
60
29
|
* scaling, buffer rotation, and HSV fill. Call set_pixel() / set_pixels()
|
|
61
30
|
* to update the buffer, then show() to transmit; or use the inherited
|
|
62
31
|
* fill() for an immediate all-same-colour update.
|
|
63
32
|
*
|
|
64
|
-
* @param {
|
|
33
|
+
* @param {import('../../connection/neopixel').NeoPixelConnection} connection - Configured NeoPixel connection.
|
|
65
34
|
* @param {number} n - Number of pixels in the strip.
|
|
66
35
|
*/
|
|
67
|
-
class WS2812BFull extends
|
|
36
|
+
class WS2812BFull extends NeoPixelRGBFull {
|
|
68
37
|
/**
|
|
69
|
-
* @param {
|
|
38
|
+
* @param {import('../../connection/neopixel').NeoPixelConnection} connection - Configured NeoPixel connection.
|
|
70
39
|
* @param {number} n - Number of pixels in the strip.
|
|
71
40
|
*/
|
|
72
|
-
constructor(
|
|
73
|
-
super(
|
|
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);
|
|
41
|
+
constructor(connection, n) {
|
|
42
|
+
super(connection, n, [1, 0, 2], 0);
|
|
166
43
|
}
|
|
167
44
|
}
|
|
168
45
|
|
|
169
|
-
const { _hsvToRgb } = require('./_color');
|
|
170
|
-
|
|
171
46
|
module.exports = { WS2812BMinimal, WS2812BFull };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { NeoPixelRGBWMinimal, NeoPixelRGBWFull } = require('./_NeoPixelRGBWBase');
|
|
4
|
+
|
|
5
|
+
const RESET_BYTES = 90;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* WS2814 addressable RGBW LED strip — minimal interface.
|
|
9
|
+
*
|
|
10
|
+
* Drives a chain of n WS2814 pixels over a NeoPixel connection.
|
|
11
|
+
* Maintains an internal RGBW buffer (identity wire order — no reorder
|
|
12
|
+
* needed); fill() writes every pixel and transmits immediately. Each
|
|
13
|
+
* pixel has four channels: red, green, blue, and white.
|
|
14
|
+
*
|
|
15
|
+
* @param {import('../../connection/neopixel').NeoPixelConnection} connection - Configured NeoPixel connection.
|
|
16
|
+
* @param {number} n - Number of pixels in the strip.
|
|
17
|
+
*/
|
|
18
|
+
class WS2814Minimal extends NeoPixelRGBWMinimal {
|
|
19
|
+
/**
|
|
20
|
+
* @param {import('../../connection/neopixel').NeoPixelConnection} connection - Configured NeoPixel connection.
|
|
21
|
+
* @param {number} n - Number of pixels in the strip.
|
|
22
|
+
*/
|
|
23
|
+
constructor(connection, n) {
|
|
24
|
+
super(connection, n, [0, 1, 2, 3], RESET_BYTES);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* WS2814 full interface — per-pixel control, brightness, rotation, HSV fill.
|
|
30
|
+
*
|
|
31
|
+
* Adds individual pixel addressing, explicit show(), global brightness
|
|
32
|
+
* scaling, buffer rotation, and HSV fill. Call set_pixel() / set_pixels()
|
|
33
|
+
* to update the buffer, then show() to transmit; or use the inherited
|
|
34
|
+
* fill() for an immediate all-same-colour update.
|
|
35
|
+
*
|
|
36
|
+
* @param {import('../../connection/neopixel').NeoPixelConnection} connection - Configured NeoPixel connection.
|
|
37
|
+
* @param {number} n - Number of pixels in the strip.
|
|
38
|
+
*/
|
|
39
|
+
class WS2814Full extends NeoPixelRGBWFull {
|
|
40
|
+
/**
|
|
41
|
+
* @param {import('../../connection/neopixel').NeoPixelConnection} connection - Configured NeoPixel connection.
|
|
42
|
+
* @param {number} n - Number of pixels in the strip.
|
|
43
|
+
*/
|
|
44
|
+
constructor(connection, n) {
|
|
45
|
+
super(connection, n, [0, 1, 2, 3], RESET_BYTES);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
module.exports = { WS2814Minimal, WS2814Full };
|