From f7c649554cba4652a0bbf387f0bf3f77dd676470 Mon Sep 17 00:00:00 2001 From: Kirill Zhumarin Date: Sun, 5 Oct 2025 19:43:05 +0000 Subject: [PATCH] Refactor BFC getImageBuffer + small fixes --- package.json | 7 ++--- pnpm-lock.yaml | 6 ++--- src/BFC.ts | 71 ++++++++++++++++++++++++++++++++++++-------------- src/DWD.ts | 13 ++++++--- src/io.ts | 4 +-- 5 files changed, 70 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 8a843ea..1463ecd 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,6 @@ "./dist/src/*" ], "dependencies": { - "debug": "^4.4.3", "sprintf-js": "^1.1.3" }, "devDependencies": { @@ -33,15 +32,17 @@ "@types/node": "^22.18.8", "@types/sprintf-js": "^1.1.4", "@types/throttle-debounce": "^5.0.2", + "debug": "^4.4.3", "serialport": "^13.0.0", "serialport-bindings-socket": "^1.0.0", "tsx": "^4.20.6", "typescript": "^5.9.3", "vitest": "^3.2.4" }, - "packageManager": "pnpm@10.5.2+sha512.da9dc28cd3ff40d0592188235ab25d3202add8a207afbedc682220e4a0029ffbff4562102b9e6e46b4e3f9e8bd53e6d05de48544b0c57d4b0179e22c76d1199b", + "packageManager": "pnpm@10.18.0+sha512.e804f889f1cecc40d572db084eec3e4881739f8dec69c0ff10d2d1beff9a4e309383ba27b5b750059d7f4c149535b6cd0d2cb1ed3aeb739239a4284a68f40cfa", "peerDependencies": { - "@serialport/stream": "^13.0.0" + "@serialport/stream": "^13.0.0", + "debug": "^4.4.3" }, "pnpm": { "patchedDependencies": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 51a3507..d748449 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -13,9 +13,6 @@ importers: .: dependencies: - debug: - specifier: ^4.4.3 - version: 4.4.3 sprintf-js: specifier: ^1.1.3 version: 1.1.3 @@ -47,6 +44,9 @@ importers: '@types/throttle-debounce': specifier: ^5.0.2 version: 5.0.2 + debug: + specifier: ^4.4.3 + version: 4.4.3 serialport: specifier: ^13.0.0 version: 13.0.0 diff --git a/src/BFC.ts b/src/BFC.ts index 1e4037b..3727728 100644 --- a/src/BFC.ts +++ b/src/BFC.ts @@ -93,6 +93,8 @@ export type BfcDisplayInfo = { clientId: number; }; +export type BfcDisplayBufferType = 'wb' | 'bgr233' | 'bgra4444' | 'bgr565' | 'bgr888' | 'bgra8888' | 'bgra8888p' | 'bgra8888mask'; + export type BfcDisplayBufferInfo = { clientId: number; width: number; @@ -104,9 +106,14 @@ export type BfcDisplayBufferInfo = { }; export type BfcDisplayBufferData = IoReadResult & { - mode: string; + type: BfcDisplayBufferType; + displayWidth: number; + displayHeight: number; + x: number; + y: number; width: number; height: number; + bpp: number; }; export class BFC extends BaseSerialProtocol { @@ -613,34 +620,60 @@ export class BFC extends BaseSerialProtocol { async getDisplayBuffer(displayId: number, options: IoReadWriteOptions = {}): Promise { const displayInfo = await this.getDisplayInfo(displayId); + debug(sprintf(`DISPLAY #%d: %dx%d @ client=%d`, displayId - 1, displayInfo.width, displayInfo.height, displayInfo.clientId)); + const displayBufferInfo = await this.getDisplayBufferInfo(displayInfo.clientId); + if (!displayBufferInfo.type) + throw new Error(`Display #${displayId - 1} not found!`); - const modes: Record = { + const modes: Record = { 1: 'wb', - 2: 'rgb332', - 3: 'rgba4444', - 4: 'rgb565', - 5: 'rgb888', - 9: 'rgb8888', + 2: 'bgr233', + 3: 'bgra4444', + 4: 'bgr565', + 5: 'bgra8888', + 9: 'bgra8888mask', }; - const mode2bpp: Record number> = { - 'wb': (w, h) => Math.floor(Math.floor((w + 7) / 8) * h), - 'rgb332': (w, h) => w * h, - 'rgba4444': (w, h) => w * h * 2, - 'rgb565be': (w, h) => w * h * 2, - 'rgb565': (w, h) => w * h * 2, - 'rgb888': (w, h) => w * h * 3, - 'rgb8888': (w, h) => w * h * 4, + const typeToBytesPerPixel: Record number> = { + 'wb': (w, h) => Math.floor(Math.floor((w + 7) / 8) * h), + 'bgr233': (w, h) => w * h, + 'bgra4444': (w, h) => w * h * 2, + 'bgr565': (w, h) => w * h * 2, + 'bgr888': (w, h) => w * h * 3, + 'bgra8888': (w, h) => w * h * 4, + 'bgra8888p': (w, h) => w * h * 4, + 'bgra8888mask': (w, h) => w * h * 4, }; - const rgbMode = modes[displayBufferInfo.type]; - if (!rgbMode) + const type = modes[displayBufferInfo.type]; + if (!type) throw new Error(`Unknown display buffer type=${displayBufferInfo.type}`); - const bytes = mode2bpp[rgbMode](displayBufferInfo.width, displayBufferInfo.height); + debug(sprintf( + `BUFFER: 0x%08X (%d, %d, %dx%d, %s)`, + displayBufferInfo.addr, + displayBufferInfo.x, displayBufferInfo.y, + displayBufferInfo.width, displayBufferInfo.height, + type + )); + + const bpp = typeToBytesPerPixel[type](1, 1); + const bytes = typeToBytesPerPixel[type](displayBufferInfo.width, displayBufferInfo.height); + debug(sprintf(`BUFFER SIZE: %d bytes [%d bpp]`, bytes, bpp)); + const response = await this.readMemory(displayBufferInfo.addr, bytes, options); - return { mode: rgbMode, width: displayBufferInfo.width, height: displayBufferInfo.height, ...response }; + return { + type: type, + bpp, + displayWidth: displayInfo.width, + displayHeight: displayInfo.height, + width: displayBufferInfo.width, + height: displayBufferInfo.height, + x: displayBufferInfo.x, + y: displayBufferInfo.y, + ...response + }; } async sendAT(cmd: string, timeout: number): Promise { diff --git a/src/DWD.ts b/src/DWD.ts index 2240097..ab4f74b 100644 --- a/src/DWD.ts +++ b/src/DWD.ts @@ -197,20 +197,22 @@ export class DWD extends BaseSerialProtocol { for (let i = 0; i < 4; i++) { let ADDRSEL: number = 0; let BUSCON: number = 0; + let agen: number = 0; if (EBU_REV < 8) { ADDRSEL = (await this.readMemory(0xF0000080 + i * 8, 4)).buffer.readUInt32LE(0); BUSCON = (await this.readMemory(0xF00000C0 + i * 8, 4)).buffer.readUInt32LE(0); + agen = (BUSCON & 0x70000000) >>> 28; } else { - ADDRSEL = (await this.readMemory(0xF0000020 + i * 4, 4)).buffer.readUInt32LE(0); - BUSCON = (await this.readMemory(0xF0000060 + i * 4, 4)).buffer.readUInt32LE(0); + ADDRSEL = (await this.readMemory(0xF0000010 + i * 4, 4)).buffer.readUInt32LE(0); + BUSCON = (await this.readMemory(0xF0000020 + i * 4, 4)).buffer.readUInt32LE(0); + agen = (BUSCON & 0x70000000) >>> 28; } const addr = (ADDRSEL & 0xFFFFF000) >>> 0; const mask = (ADDRSEL & 0x000000F0) >>> 4; const size = (1 << (27 - mask)); const enabled = (ADDRSEL & 1) !== 0; - const agen = (BUSCON & 0x70000000) >>> 28; debug(sprintf("CS%d: ADDRSEL=%08X, BUSCON=%08X", i, ADDRSEL, BUSCON)); @@ -223,8 +225,11 @@ export class DWD extends BaseSerialProtocol { // AGEN=SDRAM access type 0 or SDRAM access type 1 memoryRegions.push({ name: "RAM", addr, size }); debug(sprintf(" RAM %08X %08X", addr, size)); + } else if (agen == 3 || agen == 4) { + // AGEN=SDRAM access type 0 or SDRAM access type 1 + debug(sprintf(" NAND %08X %08X", addr, size)); } else { - debug(sprintf(" UNKNOWN %08X %08X", addr, size)); + debug(sprintf(" UNKNOWN %08X %08X [agen=%d]", addr, size, agen)); } } } diff --git a/src/io.ts b/src/io.ts index 807ddae..84bba68 100644 --- a/src/io.ts +++ b/src/io.ts @@ -94,7 +94,7 @@ export function ioProgressTracker(options: IoProgressOptions) { const reportProgressDirect = () => { const elapsed = Date.now() - start; - const speed = effectiveSpeed ? effectiveSpeed : currentCursor / (elapsed / 1000); + const speed = effectiveSpeed || currentCursor == 0 ? effectiveSpeed : currentCursor / (elapsed / 1000); const remaining = (options.total - currentCursor) / speed; options.onProgress && options.onProgress({ @@ -124,7 +124,7 @@ export function ioProgressTracker(options: IoProgressOptions) { lastSpeedCheck = Date.now(); } - if (!options.progressInterval || (Date.now() - lastProgressCalled >= options.progressInterval && cursor > 0)) { + if (!options.progressInterval || (Date.now() - lastProgressCalled >= options.progressInterval)) { reportProgressDirect(); progressCalled = true; } else {