Skip to content

Commit

Permalink
fromOctet()
Browse files Browse the repository at this point in the history
  • Loading branch information
foomoon committed Feb 9, 2024
1 parent 71ee959 commit 6b2abf5
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions dist/lib/frame.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export declare class Frame {
* @returns {Uint8Array}
*/
toOctet(): Uint8Array;
fromOctet(octet: Uint8Array): void;
/**
* Get the number of LEDs in this frame
*
Expand Down
10 changes: 10 additions & 0 deletions dist/lib/frame.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Led } from "./led.js";
/**
* A frame of LEDs, used when you wish to set color pixel by pixel
*
Expand Down Expand Up @@ -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
*
Expand Down
1 change: 1 addition & 0 deletions dist/lib/led.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*
Expand Down
5 changes: 5 additions & 0 deletions dist/lib/led.js
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*
Expand Down
1 change: 1 addition & 0 deletions dist/lib/movie.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ export declare class Movie {
fps: number;
};
toOctet(): Uint8Array;
fromOctet(octet: Uint8Array): void;
size(isCompressed?: boolean): number;
}
17 changes: 17 additions & 0 deletions dist/lib/movie.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Frame } from "./frame.js";
export class Movie {
constructor(data) {
// Required for toOctet()
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/lib/frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
6 changes: 6 additions & 0 deletions src/lib/led.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*
Expand Down
19 changes: 19 additions & 0 deletions src/lib/movie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6b2abf5

Please sign in to comment.