NEWS
TPM2.NET-Skript
-
Hallo,
ich mache gerade meine ersten Versuche, meine LED-Stripes mittels JS per TPM2.NET zu steuern.
Bisher nicht viel, aber vielleicht hilft es dem einen oder anderen oder jemand möchte mich unterstützen etc.
Bin kein Pro darin, also bitte nicht meckern
! ```
`var Buffer = require('buffer').Buffer;
var dgram = require('dgram');
var matrix;
! function tpm2net(host, port, rows, cols) {
this._host = host;
this._port = port;
this._rows = rows;
this._cols = cols;
this._socket = dgram.createSocket("udp4");
! // 1. Byte: Block Start Byte
// 0xC9 fuer TPM2.Serial, 0x9C fuer TPM2.NET
this.HEADER = [0x9C];// 2\. Byte: Block Typ // 0xDA fuer Daten (DAta), // 0xC0 fuer Kommando (C0mmand), // 0xAC fuer Bestaetigung (ACknowledge), // 0xAD fuer Antwort mit Daten (Answer with Data) this.TYPE = [0xDA]; this.ENDBYTE = [0x36];
! this.node = this;
! matrix = new Array(rows);
for(row = 0; row < rows; row++) {
matrix[row] = new Array(cols);
for(col = 0; col < cols; col++) {
matrix[row][col] = [0,255,255];
}
}
}
! function flattenDeep(arr1) {
return arr1.reduce((acc, val) => Array.isArray(val) ? acc.concat(flattenDeep(val)) : acc.concat(val), []);
}
! Object.defineProperty(Array.prototype, 'chunk', {
value: function(chunkSize) {
var R = [];
for (var i=0; i <this.length; 65536/i+="chunkSize)" r.push(this.slice(i,i+chunksize));/return/r;/}/});/tpm2net.prototype.sendmatrix="function(matrix)" {/max./nutzdaten:/bytes/var/flatmatrix="flattenDeep(matrix);" beruecksichtigt/keine/snake-verkabelung!!/payloadarrays="flatMatrix.chunk(65536);" numberofpackets="payloadArrays.length;" for(i="0;" i/</payloadarrays.length;/i++)/payload="payloadArrays[i];" lengthhighbyte="Math.floor(payload.length" 256);/lengthlowbyte="payload.length" %/256;/data="this.HEADER.concat(this.TYPE).concat([lengthHighByte," lengthlowbyte,(i+1),numberofpackets]).concat(payload).concat(this.endbyte);/buffer="Buffer.from(data);" this._socket.send(buffer,/0,/buffer.length,/this._port,/this._host,/function(){});/tpm2net.prototype.send="function(payload)" lengthlowbyte,0,0]).concat(payload).concat(this.endbyte);/tpm2net.prototype.sendcommand="function(data)" type="0xC0;" data2="this.HEADER.concat(type).concat([lengthHighByte,lengthLowByte,0,0]).concat(data).concat(this.ENDBYTE);" tpm2net.prototype.close="function(){" this._socket.close();/};/client="new" tpm2net("10.10.100.111",/65506,/1,/150);/blink="function(data)" +/1)/2;/if(i="=" client.send([0x0,0x0,0x0]);/else/client.send(data);/tpm2net.prototype.blink="function(interval," r,/g,/b)/pixel="['0x'+r.toString(16)," '0x'+g.tostring(16),/'0x'+b.tostring(16)];/console.log(pixel)/to="setInterval(blink," interval,/pixel);/fi="0;" dir="0;" to;/start="new" array(3)/fade="function(fps," speed,/time1,/time2)/step="[(start[0]">0)?start[0]/255:0,(start[1] > 0)?start[1]/255:0,(start[2]>0)?start[2]/255: 0];
var timeout = (dir===0)?time1:time2;
var sr = Math.round(fistep[0]);
var sg = Math.round(fistep[1]);
var sb = Math.round(fistep[2]);
! if(dir === 0) {
r = (start[0]-sr)<0?0:start[0]-sr;
g = (start[1]-sg)<0?0:start[1]-sg;
b = (start[2]-sb)<0?0:start[2]-sb;
} else {
r = (sr>255)?255:sr;
g = (sg>255)?255:sg;
b = (sb>255)?255:sb;
}
! for(row = 0; row < matrix.length; row++) {
for(col = 0; col < matrix[row].length; col++) {
matrix[row][col] = [r,g,b];
}
}
console.log(fi, dir,r,g,b, timeout);
client.sendMatrix(matrix);
! if(fi == 255) dir = (dir + 1) % 2;
! if(fi == 255) {
clearInterval(to);
setTimeout(function() {
to = setInterval(fade,(1000/fps)(1/speed),fps, speed, time1,time2);
}, timeout)
}
! fi = (fi+1)%256;
! // data = [fi, 0, 0];
! }
! tpm2net.prototype.fade = function(fps, speed, time1, time2, r, g, b) {
// var pixel = [r.toString(16), g.toString(16), b.toString(16)];
start = [r,g,b];
to = setInterval(fade,(1000/fps)*(1/speed),fps,speed,time1,time2); // (1000/25)/interval);
}
! tpm2net.prototype.setColor = function(r,g,b) {
var data = [0,0,0];
for(index = 0; index < 450; index=index+3) {
data[index] = r;
data[index+1] = g;
data[index+2] = b;
}
client.send(data);
}
! tpm2net.prototype.clear = function() {
client.setColor(0,0,0);
}
! strobo = function(r, g, b) {
! }
! tpm2net.prototype.strobo = function(interval, r, g, b) {
! }
! client.fade(25, 20, 0, 2000, 127, 255, 0);
! // client.setColor(0 ,20,0);
// client.sendCommand([1, 0x0a, 10]);
// client.clear();
// client.blink(1000, 255,0,255);
// var client = new tpm2net("10.10.100.111", 65506);
// var data = [100,0,0,0,0,0];
// client.send(data);
// client.sendMatrix(matrix);</this.length;>` [/i]