# base name of schematic/layout
TARGET=usb_cable_tester
# path to qEDA
QEDA=../qeda/bin/qeda
# path to elements
ELEMENTS=library/ic_mcu_st_stm32f103xc.yaml
ELEMENTS+=library/connector_usb-a-3.0_fci_10117835.yaml
ELEMENTS+=library/connector_usb-b-3.0_amphenol_gsb4211.yaml
ELEMENTS+=library/connector_usb-mini-b_edac_690-005-299-043.yaml
ELEMENTS+=library/connector_usb-micro-b-3.0_gct_usb3110.yaml
ELEMENTS+=library/connector_usb-micro-b-2.0_ali_32910334970.yaml
ELEMENTS+=library/connector_usb-c_hro_type-c-31-m-04.yaml
ELEMENTS+=library/connector_lightning_iphone5.yaml
ELEMENTS+=library/vreg_ldo_torex_xc6206.yaml
ELEMENTS+=library/connector_battery_18650.yaml
ELEMENTS+=library/display_lcd_lcm1602.yaml
ELEMENTS+=library/display_lcd_i2cadapter.yaml
ELEMENTS+=library/display_oled_0.96in.yaml
ELEMENTS+=library/vreg_pmic_tpower_tp4056.yaml
ELEMENTS+=library/transistor_pmos_nxp_bss84.yaml
ELEMENTS+=library/mechanical_button_6mm.yaml
ELEMENTS+=library/connector_xh-2.54-2p.yaml

netlist: $(TARGET).tdx

schematic: $(TARGET).ar.sch $(TARGET).sch.pdf

board: $(TARGET).brd-top.png $(TARGET).brd-bottom.png $(TARGET).brd.ps $(TARGET).brd.zip

#export netlist from schematic
%.tdx: %.sch
	lepton-netlist -g tEDAx -o $@ $<

# export schematic as pdf
%.sch.pdf: %.sch
	# export schematic
	lepton-cli export --color --paper iso_a4 --layout landscape --color --output $@ $<
	# rotate as it should be
	pdftk $@ rotate 1-endleft output $@.tmp
	mv $@.tmp $@

# create archive schematic
%.ar.sch: %.sch
	cp $< $@
	lepton-embed --embed $@

# export board as photorealistic preview (top side)
%.brd-top.png: %.lht
	pcb-rnd -x png --dpi 1200 --photo-mode --outfile $@ $<

# export board as photorealistic preview (bottom side)
%.brd-bottom.png: %.lht
	pcb-rnd -x png --dpi 1200 --photo-mode --photo-flip-y --outfile $@ $<

# export gerber from board (JLC_PCB naming style)
$(TARGET).brd.zip: $(TARGET).lht
	pcb-rnd -x cam gerber:JLC_PCB --outfile $(TARGET).brd $<
	zip $@ $(TARGET).brd.asb $(TARGET).brd.ast $(TARGET).brd.gbl $(TARGET).brd.gbo $(TARGET).brd.gbp $(TARGET).brd.gbs $(TARGET).brd.gko $(TARGET).brd.gtl $(TARGET).brd.gto $(TARGET).brd.gtp $(TARGET).brd.gts $(TARGET).brd.xln

# export all layers of the board in as ps document
%.brd.ps: %.lht
	pcb-rnd -x ps --psfile $@ $<

# generate symbols and footprints from part definitions
geda/symbols/lib/ coraleda/subc/lib lib: library ${ELEMENTS}
	# reset
	$(QEDA) reset
	# configure
	$(QEDA) config pattern.preferManufacturer false
	$(QEDA) config pattern.densityLevel M
	$(QEDA) config pattern.smoothPadCorners true
	# add local files
	$(QEDA) add ic_mcu_st_stm32f103xc@LQFP144
	$(QEDA) add connector_usb-a-3.0_fci_10117835
	$(QEDA) add connector_usb-b-3.0_amphenol_gsb4211
	$(QEDA) add connector_usb-mini-b_edac_690-005-299-043
	$(QEDA) add connector_usb-micro-b-3.0_gct_usb3110
	$(QEDA) add connector_usb-micro-b-2.0_ali_32910334970
	$(QEDA) add connector_usb-c_hro_type-c-31-m-04
	$(QEDA) add connector_lightning_iphone5
	$(QEDA) add vreg_ldo_torex_xc6206
	$(QEDA) add connector_battery_18650
	$(QEDA) add display_lcd_lcm1602
	$(QEDA) add display_lcd_i2cadapter
	$(QEDA) add display_oled_0.96in
	$(QEDA) add vreg_pmic_tpower_tp4056
	$(QEDA) add transistor_pmos_nxp_bss84
	$(QEDA) add mechanical_button_6mm
	$(QEDA) add connector_XH-2.54-2P
	# from github library
	$(QEDA) add ecs/csm-7
	$(QEDA) add capacitor/c0603
	$(QEDA) add resistor/r0603
	$(QEDA) add diode/led0603
	# generate outputs
	$(QEDA) config output geda
	$(QEDA) generate .
	$(QEDA) config output coraleda
	$(QEDA) generate .

clean:
	rm $(TARGET).brd-top.png $(TARGET).brd-bottom.png $(TARGET).brd.ps $(TARGET).brd.asb $(TARGET).brd.ast $(TARGET).brd.gbl $(TARGET).brd.gbo $(TARGET).brd.gbp $(TARGET).brd.gbs $(TARGET).brd.gko $(TARGET).brd.gtl $(TARGET).brd.gto $(TARGET).brd.gtp $(TARGET).brd.gts $(TARGET).brd.xln

dist-clean: clean
	rm $(TARGET).tdx $(TARGET).sch.pdf $(TARGET).brd.zip
