mohan-bee/mini-mp3-player

Portable, battery-powered MP3 player hardware with USB charging, microSD storage, an MCU, VS10xx audio decoder, headphone output, status controls, and regulated power rails.

Version
1.2.0
License
unset
Stars
0

scripts/repair-via-clearances.mjs

// Recorded local repair of the 0.30/0.45 mm via autorouting result.
// All coincident route contacts move with their via. Refuse to save unless DRC passes.
import fs from 'node:fs'
import { runAllChecks } from '@tscircuit/checks'
let circuit=JSON.parse(fs.readFileSync('dist/index/circuit.json','utf8')).filter(e=>!e.type.endsWith('_error')&&!e.type.endsWith('_warning'))
const repairs=[]
const errors=async c=>(await runAllChecks(c)).filter(e=>e.type.endsWith('_error'))
let findings=await errors(circuit)
console.log('Starting errors:',findings.length)
if(!findings.length) { console.log('Already clean; keeping the saved routing.'); process.exit(0) }
function move(c,id,dx,dy) {
 const v=c.find(e=>e.type==='pcb_via'&&e.pcb_via_id===id),x=v.x,y=v.y
 for(const e of c) {
  if(e.type==='pcb_via' && Math.abs(e.x-x)<1e-7&&Math.abs(e.y-y)<1e-7){e.x+=dx;e.y+=dy}
  if(e.type==='pcb_trace')for(const p of e.route)if(Math.abs(p.x-x)<1e-7&&Math.abs(p.y-y)<1e-7){p.x+=dx;p.y+=dy}
 }
 return {id,x,y,dx,dy}
}
const uart=circuit.find(e=>e.type==="pcb_trace"&&e.pcb_trace_id==="source_net_33_0");
const spur=uart.route.findIndex(p=>Math.abs(p.x+13.86572759556314)<1e-7&&Math.abs(p.y-13.0237992519505)<1e-7);
if(spur<0)throw Error("Expected UART routing spur not found");
uart.route.splice(spur,1);
for(const [id,dx,dy] of [["pcb_via_102",-.025,0],["pcb_via_209",-.15,-.05],["pcb_via_93",.07,.07]])repairs.push(move(circuit,id,dx,dy));
for(const p of uart.route)if(p.layer==="top"&&p.x> -13.7&&p.x< -13.58&&p.y>12.5&&p.y<13.1)p.x=-13.575;
circuit.find(e=>e.type==="pcb_keepout"&&e.center.x===16.25&&e.center.y===12).width=1.75;
const ground=circuit.find(e=>e.type==="pcb_trace"&&e.pcb_trace_id==="source_net_7_mst57_0");
ground.route.splice(24,0,{route_type:"wire",x:-10.58,y:13.7,width:.18,layer:"bottom"});
findings=await errors(circuit);
console.log(findings.map(e=>e.message));
fs.writeFileSync('validation/via-clearance-repairs.json',JSON.stringify({repairs,remainingErrors:findings},null,2)+'\n')
fs.writeFileSync('validation/repaired-vias.circuit.json',JSON.stringify(circuit)+'\n')
if(findings.length)throw Error(`${findings.length} unresolved DRC errors`)
const traces=JSON.parse(fs.readFileSync('dist/autorouter-debug/phase-0.output.traces.json','utf8'))
const savedUart=traces.find(t=>t.pcb_trace_id==="source_net_33_0");
savedUart.route=savedUart.route.filter(p=>!(Math.abs(p.x+13.86572759556314)<1e-7&&Math.abs(p.y-13.0237992519505)<1e-7));
for(const p of savedUart.route)if(p.layer==="top"&&p.x> -13.7&&p.x< -13.58&&p.y>12.5&&p.y<13.1)p.x=-13.575;
for(const r of repairs)for(const t of traces)for(const p of t.route)if(Math.abs(p.x-r.x)<1e-7&&Math.abs(p.y-r.y)<1e-7){p.x+=r.dx;p.y+=r.dy}
traces.find(t=>t.pcb_trace_id==="source_net_7_mst57_0").route.splice(7,0,{route_type:"wire",x:-10.58,y:13.7,width:.18,layer:"bottom"});
const input=JSON.parse(fs.readFileSync('dist/autorouter-debug/phase-0.input.simple-route.json','utf8'))
input.obstacles.find(o=>o.center.x===16.25&&o.center.y===12&&o.layers.length===1&&o.layers[0]==="bottom").width=1.75;
fs.writeFileSync('routing/autorouter-result.json',JSON.stringify({input,traces},null,2)+'\n')
console.log('Saved repaired routing; rebuild and check shorts before release.')