gokul/usb3-sd-hub

Engineering draft: four-layer, computer-powered USB 3 hub with one USB-C host socket, two USB-C and two USB-A accessory sockets, and a full-size SD slot. Board and three schematic previews are available. Routing and production checks are incomplete; not fabrication ready.

Version
0.1.4
License
unset
Stars
0

design/schematic-sheet-layout.ts

import { Board } from "tscircuit";

const originalBoardLayout = Board.prototype.doInitialSchematicLayout;
Board.prototype.doInitialSchematicLayout = function () {
	if (this.name !== "USB_HUB_BOARD" || this.root?.schematicDisabled) {
		originalBoardLayout.call(this);
		return;
	}
	const db = this.root?.db;
	if (!db) return;
	for (const sheet of db.schematic_sheet.list()) {
		const chips = db.schematic_component
			.list()
			.filter((chip) => chip.schematic_sheet_id === sheet.schematic_sheet_id);
		// Put the controllers and connectors first, then the smaller support parts.
		// Positions are derived from rendered symbol sizes, not authored schX/Y.
		chips.sort((first, second) => second.size.height - first.size.height);
		const placements: Array<{
			chip: (typeof chips)[number];
			x: number;
			y: number;
		}> = [];
		const rowWidth = 84;
		let cursorX = 0;
		let cursorY = 0;
		let rowHeight = 0;
		for (const chip of chips) {
			const reservedWidth = Math.max(8, chip.size.width + 6);
			const reservedHeight = Math.max(3.5, chip.size.height + 3);
			if (cursorX > 0 && cursorX + reservedWidth > rowWidth) {
				cursorY -= rowHeight;
				cursorX = 0;
				rowHeight = 0;
			}
			placements.push({
				chip,
				x: cursorX + reservedWidth / 2,
				y: cursorY - reservedHeight / 2,
			});
			cursorX += reservedWidth;
			rowHeight = Math.max(rowHeight, reservedHeight);
		}
		const drawingHeight = -cursorY + rowHeight;
		for (const placement of placements) {
			const center = {
				x: placement.x - rowWidth / 2,
				y: placement.y + drawingHeight / 2,
			};
			const deltaX = center.x - placement.chip.center.x;
			const deltaY = center.y - placement.chip.center.y;
			db.schematic_component.update(placement.chip.schematic_component_id, {
				center,
			});
			for (const port of db.schematic_port.list({
				schematic_component_id: placement.chip.schematic_component_id,
			})) {
				db.schematic_port.update(port.schematic_port_id, {
					center: { x: port.center.x + deltaX, y: port.center.y + deltaY },
				});
			}
			for (const text of db.schematic_text.list({
				schematic_component_id: placement.chip.schematic_component_id,
			})) {
				db.schematic_text.update(text.schematic_text_id, {
					position: {
						x: text.position.x + deltaX,
						y: text.position.y + deltaY,
					},
				});
			}
		}
	}
};