Skip to content
This repository was archived by the owner on Dec 8, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions compiler/src/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"extends": "../../tsconfig-base.json",
"compilerOptions": {
"declarationDir": "../built/types",
"outDir": "../built"
},
"include": [
"."
]
}
"extends": "../../tsconfig-base.json",
"compilerOptions": {
"declarationDir": "../built/types",
"forceConsistentCasingInFileNames": true,
"outDir": "../built"
},
"include": ["."]
}
1 change: 1 addition & 0 deletions packages/core/src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"isolatedModules": true,
"noImplicitAny": true,
"moduleDetection": "force",
"forceConsistentCasingInFileNames": true,
"types": []
},
"include": [
Expand Down
2 changes: 1 addition & 1 deletion packages/drivers/src/indexedscreen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "@devicescript/core"
import { Display, Image, Palette } from "@devicescript/graphics"

export interface IndexedScreenOptions {}
export interface IndexedScreenOptions { }

class IndexedScreenServer extends Server implements IndexedScreenServerSpec {
readonly display: Display
Expand Down
78 changes: 66 additions & 12 deletions packages/drivers/src/ledserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
PixelBuffer,
fillFade,
pixelBuffer,
correctGamma,
} from "@devicescript/runtime"
import { Server, ServerOptions, startServer } from "@devicescript/server"

Expand All @@ -12,6 +11,7 @@ export interface LedServerOptions {
* Number of LEDs
*/
length: number

/**
* Brightness applied to pixels before being rendered.
* This allocate twice the memory if less than 1 as an additional buffer is needed to compute the color.
Expand All @@ -36,19 +36,26 @@ export interface LedServerOptions {
*/
variant?: ds.LedVariant
/**
* Specify the amount of gamma correction
* Specify the amount of gamma correction. Default is 2.8, set to 1 to cancel correction.
* @see {@link https://cdn-learn.adafruit.com/downloads/pdf/led-tricks-gamma-correction.pdf}
*/
gamma?: number
/**
* Maximum supported power for LED strip
*/
maxPower?: number
}

class LedServer extends Server implements ds.LedServerSpec {
private _intensity: number
private _actualBrightness: number
private _columns: number
private _ledPerPixels: number
private _waveLength: number
private _luminousIntensity: number
private _variant: ds.LedVariant
private _gamma: number
private _maxPower: number

readonly buffer: PixelBuffer

Expand All @@ -62,6 +69,7 @@ class LedServer extends Server implements ds.LedServerSpec {
this._luminousIntensity = options.luminousIntensity
this._variant = options.variant
this._gamma = options.gamma
this._maxPower = options.maxPower
}

pixels(): ds.Buffer {
Expand All @@ -78,7 +86,7 @@ class LedServer extends Server implements ds.LedServerSpec {
this._intensity = Math.constrain(value, 0, 1)
}
actualBrightness(): number {
return this._intensity
return this._actualBrightness ?? this._intensity
}
numPixels(): number {
return this.buffer.length
Expand All @@ -98,20 +106,66 @@ class LedServer extends Server implements ds.LedServerSpec {
variant(): ds.LedVariant {
return this._variant
}
maxPower(): number {
return this._maxPower
}
set_maxPower(value: number): void {
// ignore
}

/**
* Display buffer on hardware
*/
async show(): Promise<void> {
let b = this.render()
b = this.capPower(b)

// TODO: render b to hardware
}

private render() {
let b = this.buffer
// full brightness so we can use the buffer as is
if (this._intensity < 1 || this._gamma) {
const r = b.allocClone()
if (this._intensity < 1) fillFade(r, this._intensity)
if (this._gamma) correctGamma(r, this._gamma)
b = r
const brightness = this.actualBrightness()

// apply fade if needed
if (brightness < 1) {
if (b === this.buffer)
b = b.allocClone()
fillFade(b, this._intensity)
}
// TODO: render b to hardware

// apply gamma
const g = this._gamma
if (g && g !== 1) {
if (b === this.buffer)
b = b.allocClone()
b.correctGamma(this._gamma)
}
return b
}

private capPower(b: PixelBuffer): PixelBuffer {
if (!(this._maxPower > 0)) return b

const power = 0 // TODO
// if power maxed out, cap and recompute
if (power > this._maxPower) {
// compute 80% of max brightness
// gamma?
this._actualBrightness = (this._intensity * this._maxPower / power) * 0.8
this.render()
// power is not maxed, but residual brightness
} else if (this._actualBrightness) {
// update actualbrightness
const alpha = 0.1
const threshold = 0.05
// towards actual brightness
this._actualBrightness = (1 - alpha) * this._actualBrightness + alpha * this._intensity
// when within 10% of brightness, clear flag
if (Math.abs(this._actualBrightness - this._intensity) < threshold)
this._actualBrightness = undefined
}
return b
}
}

Expand All @@ -127,9 +181,9 @@ export async function startLed(
): Promise<ds.Led> {
const { length } = options
const server = new LedServer(options)
const client = new ds.Led(startServer(server))
const client = new ds.Led(startServer(server));

;(client as any)._buffer = server.buffer
; (client as any)._buffer = server.buffer
client.show = async function () {
await server.show()
if (length <= 64) await client.pixels.write(server.buffer.buffer)
Expand Down
11 changes: 5 additions & 6 deletions packages/drivers/src/st7735.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { SPI, spi } from "@devicescript/spi"
import {
Display,
Image,
Palette,
SpiImageFlags,
spiSendImage,
Palette
} from "@devicescript/graphics"
import "@devicescript/gpio"

Expand Down Expand Up @@ -100,7 +100,7 @@ export interface STLikeDisplayOptions extends FourWireOptions {
}

export class FourWireDriver<OPT extends FourWireOptions> {
constructor(public options: OPT) {}
constructor(public options: OPT) { }

protected async sendSeq(seq: Buffer) {
let i = 0
Expand Down Expand Up @@ -158,8 +158,7 @@ export class FourWireDriver<OPT extends FourWireOptions> {

export class STLikeDisplayDriver
extends FourWireDriver<STLikeDisplayOptions>
implements Display
{
implements Display {
public readonly palette: Palette
private rot = 0

Expand Down Expand Up @@ -244,7 +243,7 @@ export class STLikeDisplayDriver
}
}

export interface ST7735Options extends STLikeDisplayOptions {}
export interface ST7735Options extends STLikeDisplayOptions { }

export class ST7735Driver extends STLikeDisplayDriver {
constructor(image: Image, options: ST7735Options) {
Expand All @@ -263,7 +262,7 @@ export class ST7735Driver extends STLikeDisplayDriver {
}
}

export interface ST7789Options extends STLikeDisplayOptions {}
export interface ST7789Options extends STLikeDisplayOptions { }

export class ST7789Driver extends STLikeDisplayDriver {
constructor(image: Image, options: ST7789Options) {
Expand Down
1 change: 1 addition & 0 deletions packages/drivers/src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"isolatedModules": true,
"noImplicitAny": true,
"moduleDetection": "force",
"forceConsistentCasingInFileNames": true,
"types": []
},
"include": ["**/*.ts", "../../core/src/*.ts"]
Expand Down
5 changes: 2 additions & 3 deletions packages/drivers/src/uc8151.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { FourWireDriver, FourWireOptions } from "./st7735"
import {
Display,
Image,
Palette,
SpiImageFlags,
spiSendImage,
Palette
} from "@devicescript/graphics"
import { DriverError } from "./core"

Expand All @@ -27,8 +27,7 @@ export interface UC8151Options extends FourWireOptions {

export class UC8151Driver
extends FourWireDriver<UC8151Options>
implements Display
{
implements Display {
public readonly palette: Palette
private resflag: number

Expand Down
12 changes: 6 additions & 6 deletions packages/graphics/src/dotmatrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ declare module "@devicescript/core" {

/**
* Writes an image to the dots
* @param img
* @param image
*/
writeImage(img: Image): Promise<void>
writeImage(image: Image): Promise<void>
}
}

Expand Down Expand Up @@ -50,20 +50,20 @@ ds.DotMatrix.prototype.readImage = async function () {
return img
}

ds.DotMatrix.prototype.writeImage = async function (img: Image) {
ds.DotMatrix.prototype.writeImage = async function (image: Image) {
const data = await this.dots.read()
const columns = await this.columns.read()
const rows = await this.rows.read()

if (!data || isNaN(columns) || isNaN(rows)) return

const w = Math.min(img.width, columns)
const h = Math.min(img.height, rows)
const w = Math.min(image.width, columns)
const h = Math.min(image.height, rows)
const n = data.length
const columns_size = rows <= 8 ? 1 : (rows + 7) >> 3
for (let row = 0; row < h; ++row) {
for (let col = 0; col < w; ++col) {
const dot = !!img.get(col, row)
const dot = !!image.get(col, row)
const bit = ((col * columns_size + (row >> 3)) << 3) + (row % 8)
data.setBit(bit, dot)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/graphics/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ export * from "./image"
export * from "./text"
export * from "./dotmatrix"
export * from "./image_spi"
export * from "./palette"
export * from "./display"
export * from "./canvas"
export * from "./palette"
13 changes: 10 additions & 3 deletions packages/graphics/src/palette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ export class Palette {
}

/**
* Returns the 24bit RGB color at the given index
* Returns the 24bit RGB color at the given index. Supports negative indices like Array.at
* @param index
* @returns
*/
getAt(index: number) {
if (index < 0 || index >= this.length) return 0
at(index: number) {
index = index >> 0
if (index < 0) index += this.length
if (index < 0 || index >= this.length) return undefined

return (
(this.buffer[3 * index] << 16) |
(this.buffer[3 * index + 1] << 8) |
Expand All @@ -50,6 +53,10 @@ export class Palette {
* @param color 24bit RGB color
*/
setAt(index: number, color: number) {
index = index >> 0
if (index < 0) index += this.length
if (index < 0 || index >= this.length) return

this.buffer[3 * index] = color >> 16
this.buffer[3 * index + 1] = color >> 8
this.buffer[3 * index + 2] = color >> 0
Expand Down
1 change: 1 addition & 0 deletions packages/graphics/src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"isolatedModules": true,
"noImplicitAny": true,
"moduleDetection": "force",
"forceConsistentCasingInFileNames": true,
"types": []
},
"include": ["**/*.ts", "../../core/src/*.ts"]
Expand Down
Loading