Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
foomoon committed Dec 16, 2023
1 parent fdeacca commit ccba838
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
30 changes: 20 additions & 10 deletions dist/lib/movie.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
export class Movie {
constructor(data) {
this.id = data.id || 0;
// Required for toOctet()
console.log(data);
this.frameData = data.frames || null;
// Check if all frames are the same length
if (this.frameData != null) {
let leds_per_frame = 0;
this.frameData.forEach((frame) => {
if (frame.getNLeds() != leds_per_frame && leds_per_frame != 0)
throw new Error("Frames must all be the same length");
leds_per_frame = frame.getNLeds();
});
}
// Required for export()
this.name = data.name || "xled-js effect";
this.unique_id = data.unique_id || generateUUID();
this.descriptor_type = data.descriptor_type || "rgb_raw";
this.loop_type = data.loop_type || 0;
this.leds_per_frame = data.leds_per_frame || 0;
this.frames_number = data.frames_number || 0;
this.leds_per_frame = data.leds_per_frame || this.frameData[0].getNLeds();
this.frames_number = data.frames_number || this.frameData.length;
this.fps = data.fps || 0;
this.frameData = data.frames || null;
if (this.frameData) {
this.frames_number = this.frameData.length;
this.leds_per_frame = this.frameData[0].getNLeds();
}
// Not used yet
this.id = data.id || 0;
this.loop_type = data.loop_type || 0;
}
export() {
return {
Expand All @@ -30,7 +39,8 @@ export class Movie {
const frames = this.frameData;
this.frames_number = frames.length;
this.leds_per_frame = frames[0].getNLeds();
const output = new Uint8Array(this.frames_number * this.leds_per_frame * 3);
const buffer = new ArrayBuffer(this.frames_number * this.leds_per_frame * 3);
const output = new Uint8Array(buffer);
frames.forEach((frame, index) => {
if (frame.getNLeds() != this.leds_per_frame)
throw new Error("Frames must all be the same length");
Expand Down
1 change: 1 addition & 0 deletions src/lib/light.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class Light {
this.ipaddr = ipaddr;
// this.challenge = randomBytes(256).toString("hex");
// this.challenge = generateRandomHex(256);
this.challenge = ""; // default value, will be set in login()
this.net = axios.create({
baseURL: `http://${this.ipaddr}/xled/v1/`,
timeout: timeout,
Expand Down
37 changes: 26 additions & 11 deletions src/lib/movie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,31 @@ export class Movie {
fps: number;
frameData: Frame[];
constructor(data: any) {
this.id = data.id || 0;
// Required for toOctet()
console.log(data);
this.frameData = data.frames || null;

// Check if all frames are the same length
if (this.frameData != null) {
let leds_per_frame = 0;
this.frameData.forEach((frame) => {
if (frame.getNLeds() != leds_per_frame && leds_per_frame != 0)
throw new Error("Frames must all be the same length");
leds_per_frame = frame.getNLeds();
});
}

// Required for export()
this.name = data.name || "xled-js effect";
this.unique_id = data.unique_id || generateUUID();
this.descriptor_type = data.descriptor_type || "rgb_raw";
this.loop_type = data.loop_type || 0;
this.leds_per_frame = data.leds_per_frame || 0;
this.frames_number = data.frames_number || 0;
this.leds_per_frame = data.leds_per_frame || this.frameData[0].getNLeds();
this.frames_number = data.frames_number || this.frameData.length;
this.fps = data.fps || 0;
this.frameData = data.frames || null;
if (this.frameData) {
this.frames_number = this.frameData.length;
this.leds_per_frame = this.frameData[0].getNLeds();
}

// Not used yet
this.id = data.id || 0;
this.loop_type = data.loop_type || 0;
}
export() {
return {
Expand All @@ -41,7 +53,10 @@ export class Movie {
const frames = this.frameData;
this.frames_number = frames.length;
this.leds_per_frame = frames[0].getNLeds();
const output = new Uint8Array(this.frames_number * this.leds_per_frame * 3);
const buffer = new ArrayBuffer(
this.frames_number * this.leds_per_frame * 3
);
const output = new Uint8Array(buffer);
frames.forEach((frame, index) => {
if (frame.getNLeds() != this.leds_per_frame)
throw new Error("Frames must all be the same length");
Expand All @@ -64,4 +79,4 @@ function generateUUID() {
const v = c === "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
}

0 comments on commit ccba838

Please sign in to comment.