Skip to content

Commit

Permalink
added pixel matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Oct 2, 2023
1 parent 11d1f3d commit ce2734c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export * from "./leddisplay"
export * from "./palette"
export * from "./bargraph"
export * from "./worker"
export * from "./number"
export * from "./number"
export * from "./pixelmatrix"
15 changes: 13 additions & 2 deletions packages/runtime/src/pixelbuffer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as ds from "@devicescript/core"
import { blendRgb, hsv, rgb } from "./colors"
import { PixelMatrix } from "./pixelmatrix"

/**
* A buffer of RGB colors
Expand Down Expand Up @@ -44,7 +45,7 @@ export class PixelBuffer {
*/
setAt(pixeloffset: number, color: number) {
pixeloffset = pixeloffset | 0
while (pixeloffset < 0) pixeloffset += this.length
if (pixeloffset < 0) pixeloffset += this.length
const i = this.start + pixeloffset
if (i < this.start || i >= this.start + this.length) return
const bi = i * 3
Expand Down Expand Up @@ -74,7 +75,7 @@ export class PixelBuffer {
*/
at(pixeloffset: number): number {
pixeloffset = pixeloffset | 0
while (pixeloffset < 0) pixeloffset += this.length
if (pixeloffset < 0) pixeloffset += this.length
const i = this.start + pixeloffset
if (i < this.start || i >= this.start + this.length) return undefined
const bi = i * 3
Expand Down Expand Up @@ -144,6 +145,16 @@ export class PixelBuffer {
return new PixelBuffer(this.buffer, rangeStart, rangeLength)
}

/**
* Returns a matrix view of the pixel buffer
* @param width number of LEDs in each row
* @returns a matrix view of the pixel buffer
*/
viewAsMatrix(width: number) {
if (width <= 1) throw new RangeError("invalid width")
return new PixelMatrix(this, width)
}

/**
* Applies a inplace gamma correction to the pixel colors
* @param pixels
Expand Down
56 changes: 56 additions & 0 deletions packages/runtime/src/pixelmatrix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { PixelBuffer } from "./pixelbuffer"

/**
* A 2D matrix of LED pixels
*/
export class PixelMatrix {
/**
* Number of pixels
*/
readonly length: number
/**
* Height of matrix
*/
readonly height: number

constructor(
/**
* The underlying pixel buffer
*/
readonly buffer: PixelBuffer,
/**
* Width of matrix
*/
readonly width: number
) {
this.length = buffer.length
this.height = (this.length / this.width) | 0
if (this.length !== this.width * this.height)
throw new RangeError("Invalid buffer size")
}

/**
* Sets the color of the pixel at the specified coordinates.
* If the coordinates are out of bounds, the method does nothing.
* @param x - The x-coordinate of the pixel.
* @param y - The y-coordinate of the pixel.
* @param color - The 24bit RGB color to set the pixel to.
*/
setAt(x: number, y: number, color: number) {
if (x < 0) x += this.width
if (y < 0) y += this.height
if (x < 0 || x >= this.width || y < 0 || y >= this.height) return

this.buffer.setAt(y * this.width + x, color)
}

/**
* Returns the value at the specified coordinates in the pixel matrix buffer.
* @param x The x-coordinate of the pixel to retrieve.
* @param y The y-coordinate of the pixel to retrieve.
* @returns The 24bit RGB color of the pixel at the specified coordinates.
*/
at(x: number, y: number) {
return this.buffer.at(y * this.width + x)
}
}

0 comments on commit ce2734c

Please sign in to comment.