Skip to content

Commit

Permalink
added led palette
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Sep 25, 2023
1 parent b68668a commit b3d7904
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
11 changes: 11 additions & 0 deletions packages/graphics/src/palette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions packages/runtime/src/leddisplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
12 changes: 5 additions & 7 deletions packages/sampleprj/src/mainleddisplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

7 changes: 6 additions & 1 deletion website/docs/devices/esp32/esp32-c3fh4-rgb-examples.mdp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
```

0 comments on commit b3d7904

Please sign in to comment.