From 6b2abf5ca0473d7b3ea1572466cc30d807429313 Mon Sep 17 00:00:00 2001 From: foomoon Date: Thu, 8 Feb 2024 22:34:28 -0600 Subject: [PATCH] fromOctet() --- dist/lib/frame.d.ts | 1 + dist/lib/frame.js | 10 ++++++++++ dist/lib/led.d.ts | 1 + dist/lib/led.js | 5 +++++ dist/lib/movie.d.ts | 1 + dist/lib/movie.js | 17 +++++++++++++++++ src/lib/frame.ts | 10 ++++++++++ src/lib/led.ts | 6 ++++++ src/lib/movie.ts | 19 +++++++++++++++++++ 9 files changed, 70 insertions(+) diff --git a/dist/lib/frame.d.ts b/dist/lib/frame.d.ts index 3c98007..d61ec2d 100644 --- a/dist/lib/frame.d.ts +++ b/dist/lib/frame.d.ts @@ -21,6 +21,7 @@ export declare class Frame { * @returns {Uint8Array} */ toOctet(): Uint8Array; + fromOctet(octet: Uint8Array): void; /** * Get the number of LEDs in this frame * diff --git a/dist/lib/frame.js b/dist/lib/frame.js index 26b5d40..d10c149 100644 --- a/dist/lib/frame.js +++ b/dist/lib/frame.js @@ -1,3 +1,4 @@ +import { Led } from "./led.js"; /** * A frame of LEDs, used when you wish to set color pixel by pixel * @@ -30,6 +31,15 @@ export class Frame { }); return output; } + fromOctet(octet) { + let offset = 0; + this.leds = Array.from({ length: octet.length / 3 }, () => { + const led = new Led(0, 0, 0); + led.fromOctet(octet.slice(offset, offset + 3)); + offset += 3; + return led; + }); + } /** * Get the number of LEDs in this frame * diff --git a/dist/lib/led.d.ts b/dist/lib/led.d.ts index 54e804d..631c884 100644 --- a/dist/lib/led.d.ts +++ b/dist/lib/led.d.ts @@ -23,6 +23,7 @@ export declare class Led { * @returns {Uint8Array} The RGB values in a Uint8Array format. */ toOctet(): Uint8Array; + fromOctet(octet: Uint8Array): void; /** * Checks if the LED color is turned on (non-zero). * diff --git a/dist/lib/led.js b/dist/lib/led.js index 764c9f6..ac6131d 100644 --- a/dist/lib/led.js +++ b/dist/lib/led.js @@ -26,6 +26,11 @@ export class Led { toOctet() { return new Uint8Array([this.red, this.green, this.blue]); } + fromOctet(octet) { + this.red = octet[0]; + this.green = octet[1]; + this.blue = octet[2]; + } /** * Checks if the LED color is turned on (non-zero). * diff --git a/dist/lib/movie.d.ts b/dist/lib/movie.d.ts index dde310b..5e8dc98 100644 --- a/dist/lib/movie.d.ts +++ b/dist/lib/movie.d.ts @@ -20,5 +20,6 @@ export declare class Movie { fps: number; }; toOctet(): Uint8Array; + fromOctet(octet: Uint8Array): void; size(isCompressed?: boolean): number; } diff --git a/dist/lib/movie.js b/dist/lib/movie.js index e078b2f..b022352 100644 --- a/dist/lib/movie.js +++ b/dist/lib/movie.js @@ -1,3 +1,4 @@ +import { Frame } from "./frame.js"; export class Movie { constructor(data) { // Required for toOctet() @@ -52,6 +53,22 @@ export class Movie { // this.frameData = output; return output; //.buffer; } + fromOctet(octet) { + const nFrames = this.frames_number; + const nLeds = this.leds_per_frame; + const nBytes = nFrames * nLeds * 3; + if (octet.length != nBytes) + throw new Error(`Octet length (${octet.length}) does not match expected length (${nBytes})`); + const frames = []; + for (let i = 0; i < nFrames; i++) { + const offset = i * nLeds * 3; + const frameOctet = octet.slice(offset, offset + nLeds * 3); + const frame = new Frame([]); + frame.fromOctet(frameOctet); + frames.push(frame); + } + this.frameData = frames; + } size(isCompressed = false) { let nBytes = 0; // for each frame, determine number of leds diff --git a/src/lib/frame.ts b/src/lib/frame.ts index 453c0c2..93d3713 100644 --- a/src/lib/frame.ts +++ b/src/lib/frame.ts @@ -36,6 +36,16 @@ export class Frame { return output; } + fromOctet(octet: Uint8Array) { + let offset = 0; + this.leds = Array.from({ length: octet.length / 3 }, () => { + const led = new Led(0, 0, 0); + led.fromOctet(octet.slice(offset, offset + 3)); + offset += 3; + return led; + }); + } + /** * Get the number of LEDs in this frame * diff --git a/src/lib/led.ts b/src/lib/led.ts index d998a63..12aa838 100644 --- a/src/lib/led.ts +++ b/src/lib/led.ts @@ -34,6 +34,12 @@ export class Led { return new Uint8Array([this.red, this.green, this.blue]); } + fromOctet(octet: Uint8Array) { + this.red = octet[0]; + this.green = octet[1]; + this.blue = octet[2]; + } + /** * Checks if the LED color is turned on (non-zero). * diff --git a/src/lib/movie.ts b/src/lib/movie.ts index 2dfb0e8..1d4b9b4 100644 --- a/src/lib/movie.ts +++ b/src/lib/movie.ts @@ -70,6 +70,25 @@ export class Movie { // this.frameData = output; return output; //.buffer; } + fromOctet(octet: Uint8Array) { + const nFrames = this.frames_number; + const nLeds = this.leds_per_frame; + const nBytes = nFrames * nLeds * 3; + if (octet.length != nBytes) + throw new Error( + `Octet length (${octet.length}) does not match expected length (${nBytes})` + ); + + const frames = []; + for (let i = 0; i < nFrames; i++) { + const offset = i * nLeds * 3; + const frameOctet = octet.slice(offset, offset + nLeds * 3); + const frame = new Frame([]); + frame.fromOctet(frameOctet); + frames.push(frame); + } + this.frameData = frames; + } size(isCompressed: boolean = false) { let nBytes = 0; // for each frame, determine number of leds