#!/usr/bin/perl

# Copyright (C) 2005 John C. Luciani Jr.

# This program may be distributed or modified under the terms of
# version 0.2 of the No-Fee Software License published by
# John C. Luciani Jr.

# Creates the PCB elements for Molex 8624 header connectors

use strict;
use warnings;

use Pcb_8;

my $Pcb = Pcb_8 -> new(debug => 1); 

my @Fields = qw(circuits body_length pin_row_length);

my @Def; # definitions that are common to all components

while (<DATA>) {
    s/\#.*//; # Remove comments
    s/^\s*//; # Remove leading spaces
    s/\s*$//; # Revove trailing spaces
    next unless length; # Skip empty lines

    # Lines that contain an '=' are global definitions.

    push(@Def, $1, $2), next if /(\S+)\s*=\s*(\S.*)/;

    my @values = split /\s*\|\s*/;

    # hash for each footprint

    my %f = ( @Def,
	      map { $_ => shift(@values) } @Fields);

    $Pcb -> element_begin(description => 'TH',
			  output_file => 
                             "tmp/" . &package_name($f{circuits}, $f{pin_rows}),
			  dim   => 'mils',
			  pin_one_square => 1);

    my $pin_num = 1;
    my $pins_per_row = $f{circuits} / 2;

    # lower left corner is pin one

    my $x = -$f{pin_spacing} * ($pins_per_row - 1) / 2;
    my $y =  $f{row_spacing} / 2;

    # These header connectors consist of two rows of pins.  With pin
    # one in the lower left corner we will place pins from left to
    # right until half the pins are placed. At the halfway point we
    # will shift to the top row and place pins from right to left.

    while ($pin_num <= $f{circuits}) {
	$Pcb -> element_add_pin(x => $x, y => $y,
				thickness  => 66,
				drill_hole => 46,
				mask       => 10,
				clearance  => 10,
				pin_number => $pin_num);

	# If this is the last pin in the row then
	# update the y value otherwise update the x 
	# value. If we are past the halfway point move
	# left (-) instead of right (+).

	$y *= -1;
	$x += $f{pin_spacing} if $y > 0;
	$pin_num++;
    }

    $Pcb -> element_add_rectangle(width => $f{body_width},
				  length=> $f{body_length},
				  x => 0,
				  y => 0);


    $Pcb -> element_set_text_xy(x => -$f{body_length}/2,
				y => -$f{body_width}/2 - 20);


    $Pcb -> element_output();
}

sub package_name ($$) { 
    my ($circuits, $rows) = @_;
    sprintf("CON_HDR-254P-%iC-%iR-%iN__Molex_8624-Series", 
	    $circuits/$rows, 
	    $rows, 
	    $circuits);
}

__DATA__

body_width = 200
pin_spacing = 100
row_spacing = 100
pin_diameter = 35
pin_rows = 2

# circuits | body_length | pin_row_length

4  | 190 | 100 
6  | 290 | 200 
8  | 390 | 300 
10 | 490 | 400 
12 | 590 | 500 
14 | 690 | 600 
16 | 790 | 700 
18 | 890 | 800 
20 | 990 | 900 
22 | 1090 | 1000 
24 | 1190| 1100 
26 | 1290| 1200 
28 | 1390| 1300

30 | 1490 | 1400 
32 | 1590 | 1500 
34 | 1690 | 1600 
36 | 1790 | 1700 
38 | 1890 | 1800 
40 | 1990 | 1900
42 | 2090 | 2000
44 | 2190 | 2100 
46 | 2290 | 2200
48 | 2390 | 2300 
50 | 2490 | 2400 
52 | 2590 | 2500 
54 | 2690 | 2600

56 | 2790 | 2700 
58 | 2890 | 2800 
60 | 2990 | 2900 
62 | 3090 | 3000 
64 | 3190 | 3100 
66 | 3290 | 3200 
68 | 3390 | 3300 
70 | 3490 | 3400 
72 | 3590 | 3500 
74 | 3690 | 3600 
76 | 3790 | 3700 
78 | 3890 | 3800 
80 | 3990 | 3900


