Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions packages/core/src/device/profiles/p12.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const p12Profile: DeviceProfile = {
packetSize: 90,
flowControl: {
initialCredits: 4,
packetDelayMs: 30,
},
defaults: { density: 2, paperType: "gap" },
namePrefixes: ["P12", "LP90", "P11"],
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/device/profiles/p15.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const p15Profile: DeviceProfile = {
packetSize: 95,
flowControl: {
initialCredits: 4,
packetDelayMs: 30,
},
defaults: { density: 2, paperType: "gap" },
namePrefixes: [
Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,14 @@ export class Printer {
} else if (response.type === "mtu") {
const mtu = response.value as number;
if (mtu > 3) {
debugLog("BLE", `mtu=${mtu} packet=${mtu - 3}`);
this.flowController.setPacketSize(mtu - 3);
const mtuPacketSize = mtu - 3;
// Cap at profile-specified size — the profile knows what the device
// firmware can handle; a higher BLE MTU doesn't mean the printer's
// application-level buffer can keep up with larger writes.
const cap = this.profile.packetSize;
const effective = cap ? Math.min(mtuPacketSize, cap) : mtuPacketSize;
debugLog("BLE", `mtu=${mtu} packet=${effective}${cap && mtuPacketSize > cap ? ` (capped from ${mtuPacketSize})` : ""}`);
this.flowController.setPacketSize(effective);
}
}

Expand Down
13 changes: 11 additions & 2 deletions packages/core/src/transport/flow-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { debugLog } from "../debug-log.js";

const DEFAULT_OPTIONS: FlowControlOptions = {
initialCredits: 4,
starvationTimeoutMs: 30,
starvationTimeoutMs: 1000,
timerIntervalMs: 5,
packetDelayMs: 0,
};
Expand Down Expand Up @@ -35,7 +35,10 @@ export class FlowController {
/** Reset credits to initial state before a new print job. */
reset(): void {
this.credits = this.options.initialCredits;
this.hasRealCredits = false;
// Preserve hasRealCredits — it reflects whether this device supports
// credit-based flow control, which is a connection-level property.
// Clearing it would let starvation recovery bypass flow control at the
// start of every print job.
this.lastCreditTime = Date.now();
debugLog("FC", `reset, credits=${this.credits}`);
}
Expand All @@ -61,9 +64,15 @@ export class FlowController {
let offset = 0;
debugLog("TX", `sending ${data.length}B in ${Math.ceil(data.length / this.packetSize)} packets, credits=${this.credits}`);

const { packetDelayMs } = this.options;

while (offset < data.length) {
await this.waitForCredit();

if (packetDelayMs > 0 && offset > 0) {
await new Promise((r) => setTimeout(r, packetDelayMs));
}

const remaining = data.length - offset;
const chunkSize = Math.min(remaining, this.packetSize);
const chunk = data.subarray(offset, offset + chunkSize);
Expand Down
Loading