-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
183 lines (165 loc) · 5.04 KB
/
Copy pathindex.js
File metadata and controls
183 lines (165 loc) · 5.04 KB
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
/**
* LED Control for DMX512 LED Tubes
*
* This module provides control for DMX512 LED tubes connected via T-790K controller
* Works with Raspberry Pi running Node.js
*/
const dmx = require('dmx');
const artnet = require('artnet');
class LEDController {
constructor(options = {}) {
this.protocol = options.protocol || 'artnet'; // 'artnet' or 'dmx'
this.host = options.host || '192.168.1.100'; // Controller IP address
this.universe = options.universe || 1;
this.pixelsPerMeter = options.pixelsPerMeter || 24;
this.tubeLength = options.tubeLength || 1; // meters
this.totalPixels = this.pixelsPerMeter * this.tubeLength;
this.channelsPerPixel = 3; // RGB
// Art-Net configuration (primary method for T-790K controller)
if (this.protocol === 'artnet') {
this.artnetClient = artnet({
host: this.host,
sendAll: true
});
} else {
// DMX configuration (requires DMX interface)
this.dmx = new dmx();
this.dmxUniverse = this.dmx.addUniverse('leds', 'enttec-usb-dmx-pro', '/dev/ttyUSB0');
}
// LED buffer (RGB values for each pixel)
this.ledBuffer = new Uint8Array(this.totalPixels * this.channelsPerPixel);
console.log(`LED Controller initialized:`);
console.log(` Protocol: ${this.protocol}`);
console.log(` Host: ${this.host}`);
console.log(` Total Pixels: ${this.totalPixels}`);
console.log(` Total Channels: ${this.totalPixels * this.channelsPerPixel}`);
}
/**
* Set a single pixel color
* @param {number} pixelIndex - Pixel index (0 to totalPixels-1)
* @param {number} r - Red value (0-255)
* @param {number} g - Green value (0-255)
* @param {number} b - Blue value (0-255)
*/
setPixel(pixelIndex, r, g, b) {
if (pixelIndex < 0 || pixelIndex >= this.totalPixels) {
console.warn(`Pixel index ${pixelIndex} out of range`);
return;
}
const offset = pixelIndex * this.channelsPerPixel;
this.ledBuffer[offset] = Math.max(0, Math.min(255, r));
this.ledBuffer[offset + 1] = Math.max(0, Math.min(255, g));
this.ledBuffer[offset + 2] = Math.max(0, Math.min(255, b));
}
/**
* Set all pixels to the same color
* @param {number} r - Red value (0-255)
* @param {number} g - Green value (0-255)
* @param {number} b - Blue value (0-255)
*/
setAll(r, g, b) {
for (let i = 0; i < this.totalPixels; i++) {
this.setPixel(i, r, g, b);
}
}
/**
* Clear all LEDs (set to black)
*/
clear() {
this.setAll(0, 0, 0);
this.update();
}
/**
* Update the LED strip with current buffer values
*/
update() {
if (this.protocol === 'artnet') {
// Send via Art-Net
this.artnetClient.set(this.universe, this.ledBuffer);
} else {
// Send via DMX
const dmxData = {};
for (let i = 0; i < this.ledBuffer.length; i++) {
dmxData[i + 1] = this.ledBuffer[i]; // DMX channels start at 1
}
if (this.dmxUniverse) {
this.dmxUniverse.update(dmxData);
}
}
}
/**
* Create a rainbow effect
* @param {number} offset - Offset for animation (0-255)
*/
rainbow(offset = 0) {
for (let i = 0; i < this.totalPixels; i++) {
const hue = ((i * 255 / this.totalPixels) + offset) % 255;
const rgb = this.hsvToRgb(hue / 255, 1, 1);
this.setPixel(i, rgb.r, rgb.g, rgb.b);
}
this.update();
}
/**
* Create a chasing light effect
* @param {number} position - Position of the chase (0 to totalPixels)
* @param {number} width - Width of the chase
* @param {number} r - Red value
* @param {number} g - Green value
* @param {number} b - Blue value
*/
chase(position, width = 5, r = 255, g = 255, b = 255) {
this.clear();
for (let i = 0; i < width; i++) {
const pixelIndex = (position + i) % this.totalPixels;
const brightness = 1 - (i / width);
this.setPixel(
pixelIndex,
Math.floor(r * brightness),
Math.floor(g * brightness),
Math.floor(b * brightness)
);
}
this.update();
}
/**
* Convert HSV to RGB
* @param {number} h - Hue (0-1)
* @param {number} s - Saturation (0-1)
* @param {number} v - Value (0-1)
* @returns {Object} RGB object with r, g, b values
*/
hsvToRgb(h, s, v) {
let r, g, b;
const i = Math.floor(h * 6);
const f = h * 6 - i;
const p = v * (1 - s);
const q = v * (1 - f * s);
const t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0: r = v; g = t; b = p; break;
case 1: r = q; g = v; b = p; break;
case 2: r = p; g = v; b = t; break;
case 3: r = p; g = q; b = v; break;
case 4: r = t; g = p; b = v; break;
case 5: r = v; g = p; b = q; break;
}
return {
r: Math.floor(r * 255),
g: Math.floor(g * 255),
b: Math.floor(b * 255)
};
}
/**
* Cleanup and close connections
*/
close() {
this.clear();
if (this.artnetClient) {
this.artnetClient.close();
}
if (this.dmx) {
// DMX cleanup if needed
}
}
}
module.exports = LEDController;