imrishabh18/rp2040-motor-controller
An RP2040-based NEMA17 stepper-motor controller with USB-C programming and PD motor-power negotiation, DRV8825 dual H-bridge drive, current/temperature telemetry, protected power filtering, status RGB LED, buzzer alarm, and optional magnetic encoder.
- Version
- 1.0.42
- License
- unset
- Stars
- 3
scripts/snapshot-auto-layout.ts
import { readFileSync, mkdirSync, writeFileSync } from 'node:fs';
import sharp from 'sharp';
const json = JSON.parse(readFileSync('dist/schematic/circuit.json', 'utf8'));
mkdirSync('../schematic-auto-layout', {recursive:true});
for (const sheet of json.filter((e:any) => e.type === 'schematic_sheet')) {
const elements = json.filter((e:any) => e.schematic_sheet_id === sheet.schematic_sheet_id);
const points = elements.flatMap((e:any) => {
if (e.type === 'schematic_component') return [{x:e.center.x-e.size.width/2,y:e.center.y-e.size.height/2},{x:e.center.x+e.size.width/2,y:e.center.y+e.size.height/2}];
if (e.type === 'schematic_port') return [e.center];
return [];
});
const minX=Math.min(...points.map((p:any)=>p.x))-2, maxX=Math.max(...points.map((p:any)=>p.x))+3;
const minY=Math.min(...points.map((p:any)=>p.y))-2, maxY=Math.max(...points.map((p:any)=>p.y))+2;
const svg=readFileSync(`dist/schematic/${sheet.name}.svg`,'utf8');
const [a,b,c,d,e,f]=svg.match(/data-real-to-screen-transform="matrix\(([^)]+)\)"/)![1]!.split(',').map(Number) as number[];
const w=a!*(maxX-minX),h=-d!*(maxY-minY);
const cropped=svg.replace(/width="1600" height="1100"/,`width="1800" height="${Math.round(1800*h/w)}" viewBox="${a!*minX+e!} ${d!*maxY+f!} ${w} ${h}"`);
writeFileSync(`../schematic-auto-layout/${sheet.name}.svg`,cropped);
await sharp(Buffer.from(cropped)).flatten({background:'#f5f1ed'}).png().toFile(`../schematic-auto-layout/${sheet.name}.png`);
}
