From 4534ac8e9204f3c0e69552d4040905e2e419805b Mon Sep 17 00:00:00 2001 From: Edwin Eefting Date: Sat, 28 Dec 2024 18:47:42 +0100 Subject: [PATCH] pixelflut --- ledder/Color.ts | 11 ++++ ledder/DisplayPixelflut.ts | 132 +++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 ledder/DisplayPixelflut.ts diff --git a/ledder/Color.ts b/ledder/Color.ts index 6747347..f8cad98 100644 --- a/ledder/Color.ts +++ b/ledder/Color.ts @@ -100,6 +100,17 @@ export default class Color implements ColorInterface { } + // toHexRGB() + // { + // return `${this.r.toString(16).padStart(2, '0')}${this.g.toString(16).padStart(2, '0')}${this.b.toString(16).padStart(2, '0')}`; + // } + // + // toHexRGBA() + // { + // const a_255=~~(this.a*255) + // return `${this.r.toString(16).padStart(2, '0')}${this.g.toString(16).padStart(2, '0')}${this.b.toString(16).padStart(2, '0')}${a_255.toString(16).padStart(2, '0')}`; + // } + } diff --git a/ledder/DisplayPixelflut.ts b/ledder/DisplayPixelflut.ts new file mode 100644 index 0000000..525224b --- /dev/null +++ b/ledder/DisplayPixelflut.ts @@ -0,0 +1,132 @@ +import Display from "./Display.js" +import ColorInterface from "./ColorInterface" +import * as net from "node:net" + +const encoder = new TextEncoder() + + +export default class DisplayPixelflut extends Display { + client: net.Socket + pixelSize: number + gridSize: number + frameBuff: Uint8Array + + bytesSend: number + + + constructor(width, height, host, port) { + super(width, height) + + this.gridSize = 15 + this.pixelSize = 10 + this.bytesSend = 0 + + + //create static pixelbuffer + let buff = "" + for (let y = 0; y < height; y++) { + for (let x = 0; x < width; x++) { + const xScaled = ~~x * this.gridSize + const yScaled = ~~y * this.gridSize + + for (let thisX = xScaled; thisX < xScaled + this.pixelSize; thisX++) + for (let thisY = yScaled; thisY < yScaled + this.pixelSize; thisY++) { + + buff = buff + `PX ${thisX},${thisY} ffffff\n` + } + + } + } + + this.frameBuff = new Uint8Array(buff.length) + this.frameBuff.set(encoder.encode(buff)) + + this.client = new net.Socket() + this.client.setNoDelay(true) + + this.client.connect(port, host) + + + this.client.on('connect', () => { + this.client.write('OFFSET 1600,300\n') + this.fillSendbuffer() + + + }) + + this.client.on('drain', () => { + + this.fillSendbuffer() + + }) + + setInterval(() => { + if (this.bytesSend===0) + console.log('PixelFlut: idle') + else + console.log(`PixelFlut: ${~~(this.bytesSend/1000000)} MB/s `) + this.bytesSend = 0 + }, 1000) + + this.client.on('error', (e)=>{ + console.error(`PixelFLut: ${e}`) + }) + } + + fillSendbuffer() { + this.bytesSend = this.bytesSend + this.frameBuff.length + while (this.client.write(this.frameBuff)) { + this.bytesSend = this.bytesSend + this.frameBuff.length + + } + + } + + frame(displayTimeMicros: number) { + + // console.log(this.client.write(this.frameBuff)) + + } + + clearBuff() { + + //fill with spaces + // this.frameOffset = 0 + // this.frameBuff.fill(' '.charCodeAt(0)) + } + + // addBuff(stringOrNum) { + // const result = encoder.encodeInto( + // stringOrNum, + // this.frameBuff.subarray(this.frameOffset), + // ) + // this.frameOffset = this.frameOffset + result.written + // + // if (this.frameOffset >= this.frameBuff.length) { + // console.log("full") + // } + // } + + setPixel(x: number, y: number, color: ColorInterface) { + // const rgb = `${(~~color.r).toString(16).padStart(2, '0')}${(~~color.g).toString(16).padStart(2, '0')}${(~~color.b).toString(16).padStart(2, '0')}` + // + // const xScaled = ~~x * this.pixelSize + // const yScaled = ~~y * this.pixelSize + // + // for (let thisX = xScaled; thisX < xScaled + this.pixelSize; thisX++) + // for (let thisY = yScaled; thisY < yScaled + this.pixelSize; thisY++) { + // + // this.addBuff(PX) + // + // this.addBuff(thisX.toString()) + // this.frameOffset = this.frameOffset + 1 + // + // this.addBuff(thisY.toString()) + // this.frameOffset = this.frameOffset + 1 + // + // this.addBuff(rgb) + // + // } + } + +} \ No newline at end of file