diff --git a/README.md b/README.md index 2345e90..3ee2422 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ light.animationSheet(null, 50) The bitmap format should be specified as follows. All values are little endian. -* magic number ``0x2e0a21``, 4 bytes +* magic number ``0x2e0a2188``, 4 bytes * palette size, 1 byte (``npalette``) * reserved padding, 1 byte * palette, ``npalette`` x 24bit RGB colors diff --git a/main.ts b/main.ts index d32757e..cf97a6c 100644 --- a/main.ts +++ b/main.ts @@ -8,7 +8,7 @@ namespace light { return animation; } - const AnimationSheetMagicNumber = 0x2e0a21; + const AnimationSheetMagicNumber = 0x2e0a2188; class BufferAnimation extends NeoPixelAnimation { private bitmap: Buffer; @@ -30,22 +30,22 @@ namespace light { } // check magic number - if (this.bitmap.getNumber(NumberFormat.UInt32LE, 0) != AnimationSheetMagicNumber) - return; + // if (this.bitmap.getNumber(NumberFormat.UInt32LE, 0) != AnimationSheetMagicNumber) + // return; // npalette const npalette = this.bitmap[4]; // extract palette and frames - const palette = this.bitmap.slice(6, npalette * 3); - const frames = this.bitmap.slice(6 + palette.length); + const opalette = 6; + const oframes = opalette + npalette * 3; - const n = strip.length(); + const n = strip.length(); // the image is written row by row - let offset = this.step * n; - if (offset + n > frames.length) { + let offset = oframes + this.step * n; + if (offset + n > this.bitmap.length) { // last frame is missing images, reset step this.step = 0; - offset = 0; + offset = oframes; } const bf = strip.buffered(); @@ -53,9 +53,14 @@ namespace light { // scan colors starting at offset, lookup color and set in neopixel for (let i = 0; i < n; i++) { const k = i + offset; - const ci = frames[k]; - const c = rgb(palette[ci], palette[ci + 1], palette[ci + 2]); - strip.setPixelColor(i, c); + const ci = this.bitmap[k]; + if (ci < ncolors) { + const c = rgb( + this.bitmap[opalette + ci * 3], + this.bitmap[opalette + ci * 3 + 1], + this.bitmap[opalette + ci * 3 + 2]); + strip.setPixelColor(i, c); + } } strip.show(); strip.setBuffered(bf); @@ -64,7 +69,7 @@ namespace light { loops.pause(this.interval); // increment step - this.step = this.step % n; + this.step = (this.step + 1) % n; } } } \ No newline at end of file diff --git a/tests.ts b/tests.ts index 1b67112..571b9cf 100644 --- a/tests.ts +++ b/tests.ts @@ -1,27 +1,32 @@ + const strip = light.pixels; const nleds = strip.length(); const ncolors = 3; -const nframes = 10; +const nframes = 2; const sheet = pins.createBuffer(6 + ncolors * 3 + nleds * nframes); -const palette = sheet.slice(6, ncolors * 3); -const frames = sheet.slice(6 + palette.length); +const opalette = 6; +const oframes = opalette + ncolors * 3; // magic number -sheet.setNumber(0, NumberFormat.UInt32LE, 0x2e0a21); +//sheet.setNumber(0, NumberFormat.UInt32BE, 0x2e0a2188); // palette sheet[4] = ncolors; -palette[0] = 0xff; // red -palette[4] = 0xff; // green -palette[8] = 0xff; // blue +sheet[opalette + 0] = 0xff; // red +sheet[opalette + 4] = 0xff; // green +sheet[opalette + 8] = 0xff; // blue // filing up colors let k = 0; for (let i = 0; i < nleds; ++i) { for (let j = 0; j < nframes; ++j) { - frames[k++] = i + j % ncolors; + sheet[oframes + k] = k % ncolors; + k++; } } +for (let k = 0; k < sheet.length; ++k) + serial.writeLine("sheet[" + k + "] = " + sheet[k]) let anim = light.animationSheet(sheet, 50); -loops.forever(() => { - strip.showAnimation(anim, 10000); -}) \ No newline at end of file +strip.showAnimationFrame(anim); +input.buttonA.onEvent(ButtonEvent.Down, () => { + strip.showAnimationFrame(anim); +})