rushabhcodes/ESP32-E-Reader

This code defines the hardware layout and schematic connections for an ESP32-based e-reader device, including microcontroller, USB, display, power management, connectors, passives, switches, and associated PCB footprints, placements, and net connections.

Version
1.0.6
License
unset
Stars
0

design-data.ts

/** Static electrical, placement, and sourcing data for the Rev. B board. */

export type SectionName =
	| "battery_usb"
	| "mcu_power"
	| "display_power"
	| "frontlight"
	| "storage"
	| "controls";

export type PcbPlacement = {
	pcbX: number;
	pcbY: number;
	pcbRotation?: number;
	layer?: "top" | "bottom";
};

export type PassiveSpec = PcbPlacement & {
	name: string;
	value: string;
	footprint: string;
	section: SectionName;
};

export type ChipSpec = PcbPlacement & {
	name: string;
	value: string;
	footprint: string;
	pins: readonly string[];
	noConnect?: readonly string[];
	section: SectionName;
	kind?: "chip" | "connector" | "pushbutton";
};

export const roundedBoardOutline = [
	{ x: -26.25, y: -48.08 },
	{ x: 26.25, y: -48.08 },
	{ x: 28.1634, y: -47.6994 },
	{ x: 29.7855, y: -46.6155 },
	{ x: 30.8694, y: -44.9934 },
	{ x: 31.25, y: -43.08 },
	{ x: 31.25, y: 43.08 },
	{ x: 30.8694, y: 44.9934 },
	{ x: 29.7855, y: 46.6155 },
	{ x: 28.1634, y: 47.6994 },
	{ x: 26.25, y: 48.08 },
	{ x: -26.25, y: 48.08 },
	{ x: -28.1634, y: 47.6994 },
	{ x: -29.7855, y: 46.6155 },
	{ x: -30.8694, y: 44.9934 },
	{ x: -31.25, y: 43.08 },
	{ x: -31.25, y: -43.08 },
	{ x: -30.8694, y: -44.9934 },
	{ x: -29.7855, y: -46.6155 },
	{ x: -28.1634, y: -47.6994 },
];

export const topGroundOutline = [
	...roundedBoardOutline.slice(0, 11),
	{ x: 10.5, y: 48.08 },
	{ x: 10.5, y: 40 },
	{ x: -10.5, y: 40 },
	{ x: -10.5, y: 48.08 },
	...roundedBoardOutline.slice(12),
];

export const lcscGroups: Array<[string, string[]]> = [
	["C29936", ["C1", "C11", "C13"]],
	["C96446", ["C5", "C7", "C10", "C14", "C24"]],
	["C14663", ["C2", "C4", "C8", "C9", "C12", "C30"]],
	[
		"C1779",
		["C3", "C15", "C16", "C17", "C18", "C22", "C23", "C26", "C28", "C29"],
	],
	["C3039694", ["C19"]],
	["C28323", ["C21", "C25", "C27"]],
	["C114659", ["R1"]],
	["C98220", ["R4", "R10", "R11", "R18", "R19", "R20", "R21", "R23"]],
	["C14675", ["R12", "R13", "R24", "R25"]],
	["C114669", ["R5", "R14"]],
	["C105578", ["R16"]],
	["C2907006", ["R17"]],
	["C125758", ["R27", "R28"]],
	["C109318", ["R22"]],
	["C105881", ["R26"]],
	["C114627", ["R3"]],
	["C113303", ["R6"]],
	["C105580", ["R7", "R8"]],
	["C520357", ["L1"]],
	["C520308", ["L2"]],
	["C82046", ["D2", "D3", "D4"]],
	["C152519", ["D5"]],
	["C173752", ["BT1"]],
	["C2856831", ["J2"]],
	["C2856827", ["J3"]],
	["C114218", ["J4"]],
	["C2830320", ["Q1"]],
	["C469327", ["Q2"]],
	["C424093", ["U1"]],
	["C7519", ["U2"]],
	["C2927799", ["U3"]],
	["C2934560", ["U4"]],
	["C82942", ["U5"]],
	["C71045", ["U6"]],
	["C388883", ["S1", "S2"]],
	["C139797", ["SW2", "SW3", "SW4", "SW6"]],
	["C49023765", ["SW7"]],
];

export const lcscByRef = new Map(
	lcscGroups.flatMap(([partNumber, references]) =>
		references.map((reference) => [reference, partNumber] as const),
	),
);
export const supplierPartNumbersFor = (reference: string) => {
	const partNumber = lcscByRef.get(reference);
	return partNumber ? { jlcpcb: [partNumber] } : undefined;
};

export const kicad = (path: string) => `kicad:${path}`;

export const FP = {
	c0603: "0603",
	c0805: "0805",
	r0603: "0603",
	r0805: "0805",
} as const;

export const polarizedPinLabels = {
	pin1: "anode",
	pin2: "cathode",
} as const;

export const capacitors: PassiveSpec[] = [
	{
		name: "C1",
		value: "1uF",
		footprint: FP.c0603,
		section: "mcu_power",
		pcbX: -11.150000000000006,
		pcbY: 37.68,
		pcbRotation: 180,
		layer: "top",
	},
	{
		name: "C2",
		value: "100nF",
		footprint: FP.c0603,
		section: "battery_usb",
		pcbX: -19.171300000000002,
		pcbY: 22.952200000000005,
		pcbRotation: 0,
		layer: "top",
	},
	{
		name: "C3",
		value: "4.7uF",
		footprint: FP.c0805,
		section: "battery_usb",
		pcbX: -19.1113,
		pcbY: 24.682199999999995,
		pcbRotation: 0,
		layer: "top",
	},
	{
		name: "C4",
		value: "100nF",
		footprint: FP.c0603,
		section: "battery_usb",
		pcbX: 20.57499999999999,
		pcbY: 40.9675,
		pcbRotation: 90,
		layer: "top",
	},
	{
		name: "C5",
		value: "10uF",
		footprint: FP.c0603,
		section: "battery_usb",
		pcbX: 18.724999999999994,
		pcbY: 40.9675,
		pcbRotation: 90,
		layer: "top",
	},
	{
		name: "C6",
		value: "10uF",
		footprint: FP.c0603,
		section: "battery_usb",
		pcbX: -26.525599999999997,
		pcbY: 39.012,
		pcbRotation: 90,
		layer: "top",
	},
	{
		name: "C7",
		value: "10uF",
		footprint: FP.c0603,
		section: "mcu_power",
		pcbX: -11.150000000000006,
		pcbY: 39.18,
		pcbRotation: 180,
		layer: "top",
	},
	{
		name: "C8",
		value: "100nF",
		footprint: FP.c0603,
		section: "mcu_power",
		pcbX: -11.150000000000006,
		pcbY: 40.69,
		pcbRotation: 180,
		layer: "top",
	},
	{
		name: "C9",
		value: "100nF",
		footprint: FP.c0603,
		section: "mcu_power",
		pcbX: -13.370000000000005,
		pcbY: 16.3985,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "C10",
		value: "10uF",
		footprint: FP.c0603,
		section: "mcu_power",
		pcbX: -16.580000000000013,
		pcbY: 16.40599999999999,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "C11",
		value: "1uF",
		footprint: FP.c0603,
		section: "mcu_power",
		pcbX: -14.969999999999999,
		pcbY: 16.3985,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "C12",
		value: "100nF",
		footprint: FP.c0603,
		section: "mcu_power",
		pcbX: -7.530000000000001,
		pcbY: 15.64,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "C13",
		value: "1uF",
		footprint: FP.c0603,
		section: "mcu_power",
		pcbX: -5.930000000000007,
		pcbY: 15.64,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "C14",
		value: "10uF",
		footprint: FP.c0603,
		section: "mcu_power",
		pcbX: -4.310000000000002,
		pcbY: 15.64,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "C15",
		value: "4.7uF",
		footprint: FP.c0805,
		section: "display_power",
		pcbX: -20.2825,
		pcbY: 6.420000000000002,
		pcbRotation: 0,
		layer: "top",
	},
	{
		name: "C16",
		value: "4.7uF",
		footprint: FP.c0805,
		section: "display_power",
		pcbX: -10.300000000000011,
		pcbY: 1.3100000000000023,
		pcbRotation: 90,
		layer: "top",
	},
	{
		name: "C17",
		value: "4.7uF",
		footprint: FP.c0805,
		section: "display_power",
		pcbX: -15.280000000000001,
		pcbY: 4.75,
		pcbRotation: 180,
		layer: "top",
	},
	{
		name: "C18",
		value: "4.7uF",
		footprint: FP.c0805,
		section: "frontlight",
		pcbX: 19.620000000000005,
		pcbY: -1.0700000000000074,
		pcbRotation: 0,
		layer: "top",
	},
	{
		name: "C19",
		value: "10uF",
		footprint: FP.c0805,
		section: "display_power",
		pcbX: 4.599999999999994,
		pcbY: -10.657499999999999,
		pcbRotation: 90,
		layer: "top",
	},
	{
		name: "C20",
		value: "100nF",
		footprint: FP.r0805,
		section: "display_power",
		pcbX: 2.4499999999999886,
		pcbY: -10.657499999999999,
		pcbRotation: 90,
		layer: "top",
	},
	{
		name: "C21",
		value: "1uF",
		footprint: FP.c0805,
		section: "display_power",
		pcbX: 0.4000000000000057,
		pcbY: -10.657499999999999,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "C22",
		value: "4.7uF",
		footprint: FP.c0805,
		section: "display_power",
		pcbX: -3.8000000000000114,
		pcbY: -10.657499999999999,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "C23",
		value: "4.7uF",
		footprint: FP.c0805,
		section: "display_power",
		pcbX: -8,
		pcbY: -10.657499999999999,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "C24",
		value: "10uF",
		footprint: FP.c0603,
		section: "storage",
		pcbX: -9.761599999999987,
		pcbY: -27.8408,
		pcbRotation: 0,
		layer: "top",
	},
	{
		name: "C25",
		value: "1uF",
		footprint: FP.c0805,
		section: "display_power",
		pcbX: -1.6999999999999886,
		pcbY: -10.657499999999999,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "C26",
		value: "4.7uF",
		footprint: FP.c0805,
		section: "frontlight",
		pcbX: 20.19999999999999,
		pcbY: -10.070000000000007,
		pcbRotation: 90,
		layer: "top",
	},
	{
		name: "C27",
		value: "1uF",
		footprint: FP.c0805,
		section: "display_power",
		pcbX: -10.099999999999994,
		pcbY: -10.657499999999999,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "C28",
		value: "4.7uF",
		footprint: FP.c0805,
		section: "display_power",
		pcbX: -5.900000000000006,
		pcbY: -10.657499999999999,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "C29",
		value: "4.7uF",
		footprint: FP.c0805,
		section: "display_power",
		pcbX: 6.6200000000000045,
		pcbY: -10.657499999999999,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "C30",
		value: "100nF",
		footprint: FP.c0603,
		section: "storage",
		pcbX: -9.761599999999987,
		pcbY: -29.370800000000003,
		pcbRotation: 0,
		layer: "top",
	},
	{
		name: "C31",
		value: "100nF",
		footprint: FP.c0805,
		section: "frontlight",
		pcbX: 16.879999999999995,
		pcbY: -10.079999999999998,
		pcbRotation: 90,
		layer: "top",
	},
	{
		name: "C32",
		value: "100nF",
		footprint: FP.c0805,
		section: "display_power",
		pcbX: -15.240000000000009,
		pcbY: -4.340000000000003,
		pcbRotation: 0,
		layer: "top",
	},
];

export const resistors: PassiveSpec[] = [
	{
		name: "R1",
		value: "12k",
		footprint: FP.r0603,
		section: "controls",
		pcbX: 13,
		pcbY: -44.989999999999995,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "R3",
		value: "5.6k",
		footprint: FP.r0603,
		section: "controls",
		pcbX: -0.9499999999999886,
		pcbY: -44.82000000000001,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "R4",
		value: "10k",
		footprint: FP.r0603,
		section: "mcu_power",
		pcbX: -11.159999999999997,
		pcbY: 36.14,
		pcbRotation: 180,
		layer: "top",
	},
	{
		name: "R5",
		value: "470",
		footprint: FP.r0603,
		section: "battery_usb",
		pcbX: -28.919999999999987,
		pcbY: 30.39,
		pcbRotation: 0,
		layer: "top",
	},
	{
		name: "R6",
		value: "2.2k",
		footprint: FP.r0603,
		section: "controls",
		pcbX: 28.02000000000001,
		pcbY: -44.36,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "R7",
		value: "5.1k",
		footprint: FP.r0603,
		section: "battery_usb",
		pcbX: 19.2825,
		pcbY: 29.86,
		pcbRotation: 180,
		layer: "top",
	},
	{
		name: "R8",
		value: "5.1k",
		footprint: FP.r0603,
		section: "battery_usb",
		pcbX: 19.2825,
		pcbY: 31.539999999999992,
		pcbRotation: 180,
		layer: "top",
	},
	{
		name: "R9",
		value: "2.5k",
		footprint: FP.r0603,
		section: "battery_usb",
		pcbX: -15.320400000000006,
		pcbY: 27.9122,
		pcbRotation: 90,
		layer: "top",
	},
	{
		name: "R10",
		value: "10k",
		footprint: FP.r0603,
		section: "controls",
		pcbX: -25.54249999999999,
		pcbY: 17.069999999999993,
		pcbRotation: 0,
		layer: "top",
	},
	{
		name: "R11",
		value: "10k",
		footprint: FP.r0603,
		section: "controls",
		pcbX: -17.27000000000001,
		pcbY: -44.83,
		pcbRotation: 90,
		layer: "top",
	},
	{
		name: "R12",
		value: "100k",
		footprint: FP.r0603,
		section: "mcu_power",
		pcbX: 19.257499999999993,
		pcbY: 27.629999999999995,
		pcbRotation: 180,
		layer: "top",
	},
	{
		name: "R13",
		value: "100k",
		footprint: FP.r0603,
		section: "mcu_power",
		pcbX: 19.24000000000001,
		pcbY: 26.03,
		pcbRotation: 0,
		layer: "top",
	},
	{
		name: "R14",
		value: "470",
		footprint: FP.r0603,
		section: "battery_usb",
		pcbX: -18.295999999999992,
		pcbY: 35.608399999999996,
		pcbRotation: 90,
		layer: "top",
	},
	{
		name: "R15",
		value: "2.7k",
		footprint: FP.r0603,
		section: "battery_usb",
		pcbX: -19.870800000000003,
		pcbY: 35.608399999999996,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "R16",
		value: "1M",
		footprint: FP.r0603,
		section: "display_power",
		pcbX: -18.507499999999993,
		pcbY: -9.409999999999997,
		pcbRotation: 0,
		layer: "top",
	},
	{
		name: "R17",
		value: "2.2",
		footprint: FP.r0805,
		section: "display_power",
		pcbX: -15.680000000000007,
		pcbY: -8.019999999999996,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "R18",
		value: "10k",
		footprint: FP.r0603,
		section: "display_power",
		pcbX: 3.710000000000008,
		pcbY: -1.269999999999996,
		pcbRotation: 90,
		layer: "top",
	},
	{
		name: "R19",
		value: "10k",
		footprint: FP.r0603,
		section: "display_power",
		pcbX: 6.889999999999986,
		pcbY: -1.269999999999996,
		pcbRotation: 90,
		layer: "top",
	},
	{
		name: "R20",
		value: "10k",
		footprint: FP.r0603,
		section: "storage",
		pcbX: -9.761599999999987,
		pcbY: -25.910399999999996,
		pcbRotation: 180,
		layer: "top",
	},
	{
		name: "R21",
		value: "10k",
		footprint: FP.r0603,
		section: "display_power",
		pcbX: 5.27000000000001,
		pcbY: -1.269999999999996,
		pcbRotation: 90,
		layer: "top",
	},
	{
		name: "R22",
		value: "10",
		footprint: FP.r0603,
		section: "frontlight",
		pcbX: 24.439999999999998,
		pcbY: -7.814999999999998,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "R23",
		value: "10k",
		footprint: FP.r0603,
		section: "storage",
		pcbX: -9.761599999999987,
		pcbY: -31.34599999999999,
		pcbRotation: 180,
		layer: "top",
	},
	{
		name: "R24",
		value: "100k",
		footprint: FP.r0603,
		section: "storage",
		pcbX: -9.761599999999987,
		pcbY: -24.386399999999995,
		pcbRotation: 180,
		layer: "top",
	},
	{
		name: "R25",
		value: "100k",
		footprint: FP.r0603,
		section: "storage",
		pcbX: -9.761599999999987,
		pcbY: -32.81920000000001,
		pcbRotation: 180,
		layer: "top",
	},
	{
		name: "R26",
		value: "330",
		footprint: FP.r0603,
		section: "frontlight",
		pcbX: 24.210000000000008,
		pcbY: 1.0900000000000034,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "R27",
		value: "20",
		footprint: FP.r0603,
		section: "mcu_power",
		pcbX: -11.150000000000006,
		pcbY: 31.769999999999996,
		pcbRotation: 0,
		layer: "top",
	},
	{
		name: "R28",
		value: "20",
		footprint: FP.r0603,
		section: "mcu_power",
		pcbX: 9.474999999999994,
		pcbY: 25.629999999999995,
		pcbRotation: 90,
		layer: "top",
	},
];

export const inductors: PassiveSpec[] = [
	{
		name: "L1",
		value: "47uH",
		footprint: kicad("Inductor_SMD/L_Panasonic_PCC-M0540M"),
		section: "display_power",
		pcbX: -22.377499999999998,
		pcbY: 0.20250000000000057,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "L2",
		value: "10uH",
		footprint: kicad("Inductor_SMD/L_Bourns-SRN4018"),
		section: "frontlight",
		pcbX: 18.590000000000003,
		pcbY: -5.049999999999997,
		pcbRotation: -90,
		layer: "top",
	},
];

export const diodes: PassiveSpec[] = [
	{
		name: "D2",
		value: "MBR0530",
		footprint: kicad("Diode_SMD/D_SOD-123"),
		section: "display_power",
		pcbX: -15.75,
		pcbY: -2.1899999999999977,
		pcbRotation: 180,
		layer: "top",
	},
	{
		name: "D3",
		value: "MBR0530",
		footprint: kicad("Diode_SMD/D_SOD-123"),
		section: "display_power",
		pcbX: -15.737500000000011,
		pcbY: 0.18999999999999773,
		pcbRotation: 0,
		layer: "top",
	},
	{
		name: "D4",
		value: "MBR0530",
		footprint: kicad("Diode_SMD/D_SOD-123"),
		section: "display_power",
		pcbX: -15.900000000000006,
		pcbY: 2.569999999999993,
		pcbRotation: 180,
		layer: "top",
	},
	{
		name: "D5",
		value: "NSR0240",
		footprint: kicad("Diode_SMD/D_SOD-323_HandSoldering"),
		section: "frontlight",
		pcbX: 22.28,
		pcbY: -9.430000000000007,
		pcbRotation: 90,
		layer: "top",
	},
];

export const chips: ChipSpec[] = [
	{
		name: "BT1",
		value: "LiPo Cell",
		kind: "connector",
		footprint: kicad("Connector_JST/JST_PH_S2B-PH-K_1x02_P2.00mm_Horizontal"),
		pins: ["1", "2"],
		section: "battery_usb",
		pcbX: -21.05000000000001,
		pcbY: 40.629999999999995,
		pcbRotation: 180,
		layer: "top",
	},
	{
		name: "J1",
		value: "USB-C",
		kind: "connector",
		footprint: "usb-c-local",
		pins: Array.from({ length: 16 }, (_, i) => `${i + 1}`),
		noConnect: ["6", "7"],
		section: "battery_usb",
		pcbX: 27.555000000000007,
		pcbY: 36.26,
		pcbRotation: 90,
		layer: "top",
	},
	{
		name: "J2",
		value: "4.2in E-Paper",
		kind: "connector",
		footprint: kicad(
			"Connector_FFC-FPC/Hirose_FH12-24S-0.5SH_1x24-1MP_P0.50mm_Horizontal",
		),
		pins: Array.from({ length: 24 }, (_, i) => `${i + 1}`),
		noConnect: ["1", "4", "6", "7", "19"],
		section: "display_power",
		pcbX: -0.75,
		pcbY: -17.477599999999995,
		pcbRotation: 0,
		layer: "top",
	},
	{
		name: "J3",
		value: "E-Paper Frontlight",
		kind: "connector",
		footprint: kicad(
			"Connector_FFC-FPC/Hirose_FH12-6S-0.5SH_1x06-1MP_P0.50mm_Horizontal",
		),
		pins: ["1", "2", "3", "4", "5", "6"],
		noConnect: ["1", "2", "3", "4"],
		section: "frontlight",
		pcbX: 18.5,
		pcbY: -17.92,
		pcbRotation: 0,
		layer: "top",
	},
	{
		name: "J4",
		value: "Micro SD",
		kind: "connector",
		footprint: kicad("Connector_Card/microSD_HC_Hirose_DM3AT-SF-PEJM5"),
		pins: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
		noConnect: ["10"],
		section: "storage",
		pcbX: -22.349999999999994,
		pcbY: -29.820000000000007,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "Q1",
		value: "FS8205A",
		footprint: kicad("Package_TO_SOT_SMD/SOT-23-6_Handsoldering"),
		pins: ["1", "2", "3", "4", "5", "6"],
		noConnect: ["2", "5"],
		section: "battery_usb",
		pcbX: -23.274400000000014,
		pcbY: 31.646,
		pcbRotation: 90,
		layer: "top",
	},
	{
		name: "Q2",
		value: "Si1308EDL",
		footprint: kicad("Package_TO_SOT_SMD/SOT-323_SC-70"),
		pins: ["1", "2", "3"],
		section: "display_power",
		pcbX: -18.810000000000002,
		pcbY: -5.560000000000002,
		pcbRotation: 90,
		layer: "top",
	},
	{
		name: "U1",
		value: "MCP73831-2",
		footprint: kicad("Package_TO_SOT_SMD/SOT-23-5"),
		pins: ["1", "2", "3", "4", "5"],
		section: "battery_usb",
		pcbX: -19.428799999999995,
		pcbY: 27.765299999999996,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "U2",
		value: "USBLC6-2SC6",
		footprint: kicad("Package_TO_SOT_SMD/SOT-23-6"),
		pins: ["1", "2", "3", "4", "5", "6"],
		noConnect: ["1", "4"],
		section: "battery_usb",
		pcbX: 19.919999999999987,
		pcbY: 36.4075,
		pcbRotation: 90,
		layer: "top",
	},
	{
		name: "U3",
		value: "DW01A",
		footprint: kicad("Package_TO_SOT_SMD/SOT-23-6"),
		pins: ["1", "2", "3", "4", "5", "6"],
		noConnect: ["4"],
		section: "battery_usb",
		pcbX: -23.24940000000001,
		pcbY: 36.6244,
		pcbRotation: 90,
		layer: "top",
	},
	{
		name: "U4",
		value: "ESP32-C3-WROOM-02",
		footprint: kicad("RF_Module/ESP32-C3-WROOM-02"),
		pins: Array.from({ length: 19 }, (_, i) => `${i + 1}`),
		section: "mcu_power",
		pcbX: 0.25,
		pcbY: 34.78,
		pcbRotation: 0,
		layer: "top",
	},
	{
		name: "U5",
		value: "ME6211C33M5",
		footprint: kicad("Package_TO_SOT_SMD/SOT-23-5"),
		pins: ["1", "2", "3", "4", "5"],
		noConnect: ["4"],
		section: "mcu_power",
		pcbX: -10.462500000000006,
		pcbY: 15.549999999999997,
		pcbRotation: 0,
		layer: "top",
	},
	{
		name: "U6",
		value: "TPS61169DCKR",
		footprint: kicad("Package_TO_SOT_SMD/SOT-353_SC-70-5"),
		pins: ["1", "2", "3", "4", "5"],
		section: "frontlight",
		pcbX: 23.55000000000001,
		pcbY: -4.112499999999997,
		pcbRotation: 90,
		layer: "top",
	},
	{
		name: "S1",
		value: "EN",
		kind: "pushbutton",
		footprint: kicad("Button_Switch_SMD/SW_SPST_EVQP7C"),
		pins: ["1", "2"],
		section: "controls",
		pcbX: -29.125,
		pcbY: -15.920000000000002,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "S2",
		value: "POWER",
		kind: "pushbutton",
		footprint: kicad("Button_Switch_SMD/SW_SPST_EVQP7C"),
		pins: ["1", "2"],
		section: "controls",
		pcbX: -29.07499999999999,
		pcbY: 23.08,
		pcbRotation: -90,
		layer: "top",
	},
	{
		name: "SW2",
		value: "BACK",
		kind: "pushbutton",
		footprint: kicad("Button_Switch_SMD/SW_SPST_CK_KXT3"),
		pins: ["1", "2", "3", "4"],
		noConnect: ["2", "3"],
		section: "controls",
		pcbX: 7.75,
		pcbY: -44.92,
		pcbRotation: 180,
		layer: "bottom",
	},
	{
		name: "SW3",
		value: "CONFIRM",
		kind: "pushbutton",
		footprint: kicad("Button_Switch_SMD/SW_SPST_CK_KXT3"),
		pins: ["1", "2", "3", "4"],
		noConnect: ["2", "3"],
		section: "controls",
		pcbX: -7.25,
		pcbY: -44.92,
		pcbRotation: 180,
		layer: "bottom",
	},
	{
		name: "SW4",
		value: "LEFT",
		kind: "pushbutton",
		footprint: kicad("Button_Switch_SMD/SW_SPST_CK_KXT3"),
		pins: ["1", "2", "3", "4"],
		noConnect: ["2", "3"],
		section: "controls",
		pcbX: 22.75,
		pcbY: -44.92,
		pcbRotation: 180,
		layer: "bottom",
	},
	{
		name: "SW6",
		value: "RIGHT",
		kind: "pushbutton",
		footprint: kicad("Button_Switch_SMD/SW_SPST_CK_KXT3"),
		pins: ["1", "2", "3", "4"],
		noConnect: ["2", "3"],
		section: "controls",
		pcbX: -22.25,
		pcbY: -44.92,
		pcbRotation: 180,
		layer: "bottom",
	},
	{
		name: "SW7",
		value: "FRONTLIGHT",
		footprint: kicad("Button_Switch_SMD/SW_SPDT_CK_JS102011SAQN"),
		pins: ["1", "2", "3"],
		noConnect: ["3"],
		section: "frontlight",
		pcbX: 27.349999999999994,
		pcbY: 14.105000000000004,
		pcbRotation: 90,
		layer: "top",
	},
	{
		name: "JP1",
		value: "BOOT",
		footprint: kicad("Jumper/SolderJumper-2_P1.3mm_Open_RoundedPad1.0x1.5mm"),
		pins: ["1", "2"],
		section: "mcu_power",
		pcbX: -20.531200000000013,
		pcbY: 18.285600000000002,
		pcbRotation: 0,
		layer: "top",
	},
];

export const testpoints = [
	{
		name: "TP1",
		label: "BATT",
		section: "battery_usb" as const,
		pcbX: -16.6704,
		pcbY: 43.431599999999996,
		pcbRotation: 0,
		layer: "top",
	},
	{
		name: "TP2",
		label: "3V3",
		section: "mcu_power" as const,
		pcbX: -3.360800000000012,
		pcbY: 18.691999999999993,
		pcbRotation: 0,
		layer: "top",
	},
	{
		name: "TP3",
		label: "VBUS",
		section: "battery_usb" as const,
		pcbX: 23.817200000000014,
		pcbY: 44.1428,
		pcbRotation: 0,
		layer: "top",
	},
	{
		name: "TP4",
		label: "VGL",
		section: "display_power" as const,
		pcbX: -9.710800000000006,
		pcbY: 8.430399999999992,
		pcbRotation: 0,
		layer: "top",
	},
	{
		name: "TP5",
		label: "VGH",
		section: "display_power" as const,
		pcbX: -12.961999999999989,
		pcbY: 8.481200000000001,
		pcbRotation: 0,
		layer: "top",
	},
	{
		name: "TP6",
		label: "BAT_MEASURE",
		section: "mcu_power" as const,
		pcbX: 19.042,
		pcbY: 22.1464,
		pcbRotation: 0,
		layer: "top",
	},
	{
		name: "TP7",
		label: "LED",
		section: "frontlight" as const,
		pcbX: 15.389999999999986,
		pcbY: 1.9399999999999977,
		pcbRotation: 0,
		layer: "top",
	},
];

export const sectionOrigins: Record<SectionName, { x: number; y: number }> = {
	battery_usb: { x: -28, y: 20 },
	mcu_power: { x: 0, y: 20 },
	display_power: { x: 28, y: 20 },
	storage: { x: -28, y: -7 },
	controls: { x: 0, y: -7 },
	frontlight: { x: 28, y: -7 },
};

export const schematicSpecs = [
	...capacitors,
	...resistors,
	...inductors,
	{ name: "D1", section: "battery_usb" as const },
	...diodes,
	...chips,
	...testpoints,
];

export const schematicPlacementByReference = (() => {
	const sectionIndexes: Partial<Record<SectionName, number>> = {};

	return new Map(
		schematicSpecs.map(({ name, section }) => {
			const index = sectionIndexes[section] ?? 0;
			sectionIndexes[section] = index + 1;
			const origin = sectionOrigins[section];

			return [
				name,
				{
					schX: origin.x + (index % 5) * 4.4,
					schY: origin.y - Math.floor(index / 5) * 4.4,
					schSectionName: section,
				},
			] as const;
		}),
	);
})();

export const schematicPlacementOverrides: Record<
	string,
	{ schX?: number; schY?: number }
> = {
	R4: { schY: 21.201 },
	J3: { schX: 36.53 },
	U6: { schX: 41.47 },
};

export const schematicPlacementFor = (reference: string) => {
	const placement = schematicPlacementByReference.get(reference);
	if (!placement)
		throw new Error(`Missing schematic placement for ${reference}`);
	return { ...placement, ...schematicPlacementOverrides[reference] };
};

export const aliasForPin = (pin: string) =>
	`P_${pin.replace(/[^A-Za-z0-9_]/g, "_")}`;
export const pinLabelsFor = (pins: readonly string[]) =>
	Object.fromEntries(pins.map((pin) => [pin, aliasForPin(pin)]));

export const endpoint = (ref: string, pin: string) => `.${ref} > .pin${pin}`;

/** Trace widths retained from the Rev. B KiCad design. */
export const traceThicknessByNet: Record<string, string> = {
	BATT_N_RAW: "0.5mm",
	BATT_P: "0.5mm",
	EPD_PUMP_NEG: "0.6mm",
	EPD_PUMP_SWITCH: "0.5mm",
	FL_INPUT: "0.4mm",
	FL_LED_NEG: "0.3mm",
	FL_LED_POS: "0.3mm",
	FL_SWITCH: "0.6mm",
	PREVGL: "0.4mm",
	PROTECT_OC: "0.6mm",
	PROTECT_OD: "0.6mm",
	PROTECT_VCC: "0.3mm",
	USB_VBUS: "0.5mm",
};

export const nets: Record<string, string[]> = {
	V3V3: [
		endpoint("C7", "1"),
		endpoint("C8", "1"),
		endpoint("C12", "1"),
		endpoint("C13", "1"),
		endpoint("C14", "1"),
		endpoint("C15", "1"),
		endpoint("C19", "1"),
		endpoint("C20", "1"),
		endpoint("C21", "2"),
		endpoint("C24", "1"),
		endpoint("C30", "1"),
		endpoint("J2", "15"),
		endpoint("J2", "16"),
		endpoint("J4", "4"),
		endpoint("L1", "1"),
		endpoint("R4", "2"),
		endpoint("R10", "1"),
		endpoint("R11", "1"),
		endpoint("R18", "2"),
		endpoint("R19", "2"),
		endpoint("R20", "1"),
		endpoint("R21", "2"),
		endpoint("R23", "1"),
		endpoint("R24", "1"),
		endpoint("R25", "1"),
		endpoint("U4", "1"),
		endpoint("U5", "5"),
		endpoint("TP2", "1"),
	],
	GND: [
		endpoint("C1", "2"),
		endpoint("C2", "2"),
		endpoint("C3", "2"),
		endpoint("C4", "2"),
		endpoint("C5", "2"),
		endpoint("C7", "2"),
		endpoint("C8", "2"),
		endpoint("C9", "2"),
		endpoint("C10", "2"),
		endpoint("C11", "2"),
		endpoint("C12", "2"),
		endpoint("C13", "2"),
		endpoint("C14", "2"),
		endpoint("C15", "2"),
		endpoint("C17", "2"),
		endpoint("C18", "2"),
		endpoint("C19", "2"),
		endpoint("C20", "2"),
		endpoint("C21", "1"),
		endpoint("C22", "1"),
		endpoint("C23", "1"),
		endpoint("C24", "2"),
		endpoint("C25", "1"),
		endpoint("C26", "2"),
		endpoint("C27", "1"),
		endpoint("C28", "1"),
		endpoint("C29", "1"),
		endpoint("C30", "2"),
		endpoint("C31", "2"),
		endpoint("C32", "2"),
		endpoint("D3", "1"),
		endpoint("J1", "1"),
		endpoint("J1", "12"),
		endpoint("J1", "13"),
		endpoint("J1", "14"),
		endpoint("J1", "15"),
		endpoint("J1", "16"),
		endpoint("J2", "8"),
		endpoint("J2", "17"),
		endpoint("J4", "6"),
		endpoint("J4", "9"),
		endpoint("JP1", "1"),
		endpoint("Q1", "3"),
		endpoint("R1", "2"),
		endpoint("R3", "2"),
		endpoint("R6", "2"),
		endpoint("R7", "2"),
		endpoint("R8", "2"),
		endpoint("R9", "2"),
		endpoint("R13", "2"),
		endpoint("R15", "1"),
		endpoint("R16", "2"),
		endpoint("R17", "2"),
		endpoint("R22", "2"),
		endpoint("S1", "1"),
		endpoint("S1", "3"),
		endpoint("S2", "1"),
		endpoint("S2", "3"),
		endpoint("SW6", "1"),
		endpoint("U1", "2"),
		endpoint("U2", "2"),
		endpoint("U4", "9"),
		endpoint("U4", "19"),
		endpoint("U5", "2"),
		endpoint("U6", "2"),
	],
	BATT_P: [
		endpoint("BT1", "1"),
		endpoint("C9", "1"),
		endpoint("C10", "1"),
		endpoint("C11", "1"),
		endpoint("R12", "1"),
		endpoint("R14", "2"),
		endpoint("SW7", "1"),
		endpoint("U1", "3"),
		endpoint("U5", "1"),
		endpoint("U5", "3"),
		endpoint("TP1", "1"),
	],
	BATT_N_RAW: [
		endpoint("BT1", "2"),
		endpoint("C6", "2"),
		endpoint("Q1", "1"),
		endpoint("U3", "6"),
	],
	ESP32_EN: [
		endpoint("C1", "1"),
		endpoint("R4", "1"),
		endpoint("S1", "2"),
		endpoint("S1", "4"),
		endpoint("U4", "2"),
	],
	USB_VBUS: [
		endpoint("C2", "1"),
		endpoint("C3", "1"),
		endpoint("C4", "1"),
		endpoint("C5", "1"),
		endpoint("D1", "2"),
		endpoint("J1", "2"),
		endpoint("J1", "11"),
		endpoint("U1", "4"),
		endpoint("U2", "5"),
		endpoint("TP3", "1"),
	],
	USB_DP: [
		endpoint("J1", "4"),
		endpoint("J1", "9"),
		endpoint("U2", "6"),
		endpoint("U4", "14"),
	],
	USB_DM: [
		endpoint("J1", "5"),
		endpoint("J1", "8"),
		endpoint("U2", "3"),
		endpoint("U4", "13"),
	],
	USB_CC1: [endpoint("J1", "3"), endpoint("R7", "1")],
	USB_CC2: [endpoint("J1", "10"), endpoint("R8", "1")],
	CHARGE_LED_K: [endpoint("D1", "1"), endpoint("R5", "1")],
	CHARGE_STAT: [endpoint("R5", "2"), endpoint("U1", "1")],
	CHARGE_PROG: [endpoint("R9", "1"), endpoint("U1", "5")],
	PROTECT_VCC: [endpoint("C6", "1"), endpoint("R14", "1"), endpoint("U3", "5")],
	PROTECT_CS: [endpoint("R15", "2"), endpoint("U3", "2")],
	PROTECT_OD: [endpoint("Q1", "6"), endpoint("U3", "1")],
	PROTECT_OC: [endpoint("Q1", "4"), endpoint("U3", "3")],
	BOOT_OPTION: [endpoint("JP1", "2"), endpoint("U4", "8")],
	EPD_DC: [endpoint("J2", "11"), endpoint("U4", "3")],
	EPD_RST: [endpoint("J2", "10"), endpoint("R21", "1"), endpoint("U4", "4")],
	EPD_BUSY: [endpoint("J2", "9"), endpoint("R19", "1"), endpoint("U4", "5")],
	EPD_CS: [endpoint("J2", "12"), endpoint("R18", "1"), endpoint("U4", "12")],
	SPI_MISO: [endpoint("J4", "7"), endpoint("R20", "2"), endpoint("U4", "6")],
	SPI_SCLK: [endpoint("J2", "13"), endpoint("J4", "5"), endpoint("R27", "1")],
	SPI_MOSI: [endpoint("J2", "14"), endpoint("J4", "3"), endpoint("R28", "1")],
	MCU_IO8: [endpoint("R27", "2"), endpoint("U4", "7")],
	MCU_IO10: [endpoint("R28", "2"), endpoint("U4", "10")],
	SD_CS: [endpoint("J4", "2"), endpoint("R23", "2"), endpoint("U4", "16")],
	SD_DAT1: [endpoint("J4", "8"), endpoint("R24", "2")],
	SD_DAT2: [endpoint("J4", "1"), endpoint("R25", "2")],
	POWER_BUTTON: [
		endpoint("R10", "2"),
		endpoint("S2", "2"),
		endpoint("S2", "4"),
		endpoint("U4", "15"),
	],
	BUTTONS_ADC: [
		endpoint("R11", "2"),
		endpoint("SW2", "4"),
		endpoint("SW3", "4"),
		endpoint("SW4", "4"),
		endpoint("SW6", "4"),
		endpoint("U4", "17"),
	],
	BUTTON_BACK_DIV: [endpoint("R1", "1"), endpoint("SW2", "1")],
	BUTTON_CONFIRM_DIV: [endpoint("R3", "1"), endpoint("SW3", "1")],
	BUTTON_LEFT_DIV: [endpoint("R6", "1"), endpoint("SW4", "1")],
	BATT_MEASURE: [
		endpoint("R12", "2"),
		endpoint("R13", "1"),
		endpoint("U4", "18"),
		endpoint("TP6", "1"),
	],
	FL_PWM: [endpoint("R26", "1"), endpoint("U4", "11")],
	FL_CTRL: [endpoint("R26", "2"), endpoint("U6", "4")],
	FL_INPUT: [
		endpoint("C18", "1"),
		endpoint("L2", "1"),
		endpoint("SW7", "2"),
		endpoint("U6", "5"),
	],
	FL_SWITCH: [endpoint("D5", "2"), endpoint("L2", "2"), endpoint("U6", "1")],
	FL_LED_POS: [
		endpoint("C26", "1"),
		endpoint("C31", "1"),
		endpoint("D5", "1"),
		endpoint("J3", "5"),
		endpoint("TP7", "1"),
	],
	FL_LED_NEG: [endpoint("J3", "6"), endpoint("R22", "1"), endpoint("U6", "3")],
	EPD_PUMP_SWITCH: [
		endpoint("C16", "2"),
		endpoint("D4", "2"),
		endpoint("L1", "2"),
		endpoint("Q2", "3"),
	],
	EPD_PUMP_NEG: [
		endpoint("C16", "1"),
		endpoint("D2", "1"),
		endpoint("D3", "2"),
	],
	PREVGH: [
		endpoint("C17", "1"),
		endpoint("D4", "1"),
		endpoint("J2", "21"),
		endpoint("TP5", "1"),
	],
	PREVGL: [
		endpoint("C23", "2"),
		endpoint("C32", "1"),
		endpoint("D2", "2"),
		endpoint("J2", "23"),
		endpoint("TP4", "1"),
	],
	GDR: [endpoint("J2", "2"), endpoint("Q2", "1"), endpoint("R16", "1")],
	RESE: [endpoint("J2", "3"), endpoint("Q2", "2"), endpoint("R17", "1")],
	EPD_VDD: [endpoint("C25", "2"), endpoint("J2", "18")],
	EPD_VSH1: [endpoint("C22", "2"), endpoint("J2", "20")],
	EPD_VSH2: [endpoint("C29", "2"), endpoint("J2", "5")],
	EPD_VSL: [endpoint("C28", "2"), endpoint("J2", "22")],
	VCOM: [endpoint("C27", "2"), endpoint("J2", "24")],
};

export const noConnectAliases = (spec: ChipSpec) =>
	spec.noConnect?.map(aliasForPin);

export const schematicHeights: Partial<Record<string, number>> = {
	Q2: 0.4,
	U1: 0.6,
	U4: 2,
	U5: 0.6,
	U6: 0.6,
	SW7: 0.4,
};