diff --git a/packages/graphics/src/palette.ts b/packages/graphics/src/palette.ts index d01571e6ee..66840b3cfd 100644 --- a/packages/graphics/src/palette.ts +++ b/packages/graphics/src/palette.ts @@ -16,6 +16,17 @@ export class Palette { `) } + /** + * A better color palette for LED strips + * @returns + */ + static leds() { + return new Palette(hex` + 000000 ffffff ff0000 00ff00 0000ff ffff00 ff00ff 00ffff + ffa500 adff2f 008080 800080 ffc0cb 4b0082 ffd8a8 e0e0ff + `) + } + /** * Allocates a 1bpp monochrome palette * @returns diff --git a/packages/runtime/src/leddisplay.ts b/packages/runtime/src/leddisplay.ts index 9e25afe68d..0330aa415f 100644 --- a/packages/runtime/src/leddisplay.ts +++ b/packages/runtime/src/leddisplay.ts @@ -15,7 +15,7 @@ export async function startLedDisplay( if (!palette) { const waveLength = await led.waveLength.read() if (waveLength) palette = Palette.monochrome() - else palette = Palette.arcade() + else palette = Palette.leds() } const buffer = await led.buffer() @@ -27,15 +27,14 @@ export async function startLedDisplay( const image = Image.alloc(width, height, bpp) - const init = async () => { } + const init = async () => {} const show = async () => { for (let x = 0; x < width; ++x) { for (let y = 0; y < height; ++y) { const ci = image.get(x, y) const c = palette.at(ci) - if (c !== undefined) - buffer.setAt(y * width + x, c) + if (c !== undefined) buffer.setAt(y * width + x, c) } } await led.show() diff --git a/packages/sampleprj/src/mainleddisplay.ts b/packages/sampleprj/src/mainleddisplay.ts index 47d924cb4e..1f213c843b 100644 --- a/packages/sampleprj/src/mainleddisplay.ts +++ b/packages/sampleprj/src/mainleddisplay.ts @@ -5,12 +5,10 @@ const board = new Esp32C3FH4RGB() const led = await board.startLed() await led.intensity.write(0.05) const display = await startLedDisplay(led) -display.palette.setAt(1, 0xff0000) -display.palette.setAt(2, 0x00ff00) -display.palette.setAt(3, 0x0000ff) -for(let x = 0; x < 5; ++x) -for(let y = 0; y < 5; ++y) - await display.image.set(x, y, (x + y) % 4) +const n = display.palette.length +let k = 0 +for (let y = 0; y < 5; ++y) + for (let x = 0; x < 5; ++x) + await display.image.set(x, y, k++ % n) await display.show() - diff --git a/website/docs/devices/esp32/esp32-c3fh4-rgb-examples.mdp b/website/docs/devices/esp32/esp32-c3fh4-rgb-examples.mdp index 4bf82946ce..405ea43d4a 100644 --- a/website/docs/devices/esp32/esp32-c3fh4-rgb-examples.mdp +++ b/website/docs/devices/esp32/esp32-c3fh4-rgb-examples.mdp @@ -8,8 +8,13 @@ import { startLedDisplay } from "@devicescript/runtime" const board = new Esp32C3FH4RGB() const led = await board.startLed() +await led.intensity.write(0.05) const display = await startLedDisplay(led) -await display.image.fill(0x00ff00) +const n = display.palette.length +let k = 0 +for (let y = 0; y < 5; ++y) + for (let x = 0; x < 5; ++x) + await display.image.set(x, y, k++ % n) await display.show() ``` \ No newline at end of file