THICKNESS_MAX=2.44;
THICKNESS=2.4;

ORIGINAL_THICKNESS=25.4/32;

NUBUS_HEIGHT_BELOW_OUTER_LIP=92; // NuBus p57, max is 97.25
NUBUS_HEIGHT_ABOVE_OUTER_LIP=6; // NuBus p57

NUBUS_BETWEEN_NOTCHES=87.25; // DCDMF3 p625 & p635, bottom of low to top of high?
NUBUS_HIGH_NOTCH_TO_TOP=10.35;// DCDMF3 p635, top of high to bottom of inner lip ?

NUBUS_BASE_WIDTH=18.57;
NUBUS_EXTRA_WIDTH=2.93;
NUBUS_FULL_WIDTH=NUBUS_BASE_WIDTH+NUBUS_EXTRA_WIDTH;
NUBUS_TOP_WIDTH=21.82;
NUBUS_OVERHANG_WIDTH=NUBUS_TOP_WIDTH-NUBUS_BASE_WIDTH;
NUBUS_EXTRA_FROM_TOP=20.5;
NUBUS_ANGLE_SECTION_LACK_OF_WIDTH=2.57;
NUBUS_ANGLE_SECTION_WIDTH_REDUCTION=NUBUS_EXTRA_WIDTH+NUBUS_ANGLE_SECTION_LACK_OF_WIDTH;

NUBUS_HEIGHT_NARROW_BOTTOM_TO_TOP=94.6;
NUBUS_FULL_HEIGHT=(NUBUS_BETWEEN_NOTCHES+NUBUS_HIGH_NOTCH_TO_TOP);
NUBUS_NARROW_BOTTOM_HEIGHT=NUBUS_FULL_HEIGHT-NUBUS_HEIGHT_NARROW_BOTTOM_TO_TOP;

NUBUS_EXTRA_NARROW_BOTTOM=1.5;

NUBUS_OVERHANG_MIN_HEIGHT=8.4;
NUBUS_OVERHANG_MAX_HEIGHT=12.18;
NUBUS_OVERHANG_RADIUS=3.2;
NUBUS_TOP_BIT_WIDTH=9.57-2.57;

FRONT_NOTCH_FROM_TOP=85.9;
FRONT_NOTCH_STRAIGHT_WIDTH=2;
FRONT_NOTCH_STRAIGHT_HEIGHT=6;
FRONT_NOTCH_ANGLE_HEIGHT=(NUBUS_BETWEEN_NOTCHES+NUBUS_HIGH_NOTCH_TO_TOP)-(FRONT_NOTCH_FROM_TOP+FRONT_NOTCH_STRAIGHT_HEIGHT);
FRONT_NOTCH_ANGLE_WIDTH=FRONT_NOTCH_ANGLE_HEIGHT*sin(23);

points=[[0,0],
        [NUBUS_BASE_WIDTH-7.82-FRONT_NOTCH_STRAIGHT_WIDTH/2-FRONT_NOTCH_ANGLE_WIDTH,0],
        [NUBUS_BASE_WIDTH-7.82-FRONT_NOTCH_STRAIGHT_WIDTH/2,FRONT_NOTCH_ANGLE_HEIGHT],
        [NUBUS_BASE_WIDTH-7.82-FRONT_NOTCH_STRAIGHT_WIDTH/2,FRONT_NOTCH_ANGLE_HEIGHT+FRONT_NOTCH_STRAIGHT_HEIGHT],
        [NUBUS_BASE_WIDTH-7.82+FRONT_NOTCH_STRAIGHT_WIDTH/2,FRONT_NOTCH_ANGLE_HEIGHT+FRONT_NOTCH_STRAIGHT_HEIGHT],
        [NUBUS_BASE_WIDTH-7.82+FRONT_NOTCH_STRAIGHT_WIDTH/2,FRONT_NOTCH_ANGLE_HEIGHT],
        [NUBUS_BASE_WIDTH-7.82+FRONT_NOTCH_STRAIGHT_WIDTH/2+FRONT_NOTCH_ANGLE_WIDTH,0],
        [NUBUS_BASE_WIDTH-NUBUS_EXTRA_NARROW_BOTTOM,0],
        
        [NUBUS_BASE_WIDTH-NUBUS_EXTRA_NARROW_BOTTOM,NUBUS_NARROW_BOTTOM_HEIGHT],
        [NUBUS_FULL_WIDTH,NUBUS_NARROW_BOTTOM_HEIGHT],
        [NUBUS_FULL_WIDTH,NUBUS_FULL_HEIGHT-NUBUS_EXTRA_FROM_TOP],
        [NUBUS_FULL_WIDTH-NUBUS_ANGLE_SECTION_WIDTH_REDUCTION,(NUBUS_FULL_HEIGHT-NUBUS_EXTRA_FROM_TOP)+(NUBUS_ANGLE_SECTION_WIDTH_REDUCTION*2)],
        [NUBUS_FULL_WIDTH-NUBUS_ANGLE_SECTION_WIDTH_REDUCTION,NUBUS_FULL_HEIGHT+ORIGINAL_THICKNESS],
        [NUBUS_FULL_WIDTH-NUBUS_ANGLE_SECTION_WIDTH_REDUCTION-NUBUS_TOP_BIT_WIDTH,NUBUS_FULL_HEIGHT+ORIGINAL_THICKNESS],
        [NUBUS_FULL_WIDTH-NUBUS_ANGLE_SECTION_WIDTH_REDUCTION-NUBUS_TOP_BIT_WIDTH,NUBUS_FULL_HEIGHT+ORIGINAL_THICKNESS-6], //6: measurement
        [NUBUS_FULL_WIDTH-NUBUS_ANGLE_SECTION_WIDTH_REDUCTION-NUBUS_TOP_BIT_WIDTH-2,NUBUS_FULL_HEIGHT+ORIGINAL_THICKNESS-6], //2&6: measurement
        [NUBUS_FULL_WIDTH-NUBUS_ANGLE_SECTION_WIDTH_REDUCTION-NUBUS_TOP_BIT_WIDTH-2,NUBUS_FULL_HEIGHT+ORIGINAL_THICKNESS-NUBUS_HIGH_NOTCH_TO_TOP+5], // 5 is the diustance between top notch and top screw hole
        [-NUBUS_OVERHANG_WIDTH,NUBUS_FULL_HEIGHT+ORIGINAL_THICKNESS-NUBUS_HIGH_NOTCH_TO_TOP+5],
        [-NUBUS_OVERHANG_WIDTH,NUBUS_FULL_HEIGHT-NUBUS_OVERHANG_MAX_HEIGHT+NUBUS_OVERHANG_RADIUS],
        [0,NUBUS_FULL_HEIGHT-NUBUS_OVERHANG_MAX_HEIGHT]        
];

module backplate() {
linear_extrude(height = ORIGINAL_THICKNESS, center = true) { polygon(points); };
}

// PCB, don't print
PCB_THICKNESS=1.6;
PCB_OFFSET=(NUBUS_FULL_WIDTH-2.93)-PCB_THICKNESS/2;
module pcb() {
    color("green") translate([PCB_OFFSET,0,-50]) linear_extrude(height = 100, center = true) { polygon ([[-PCB_THICKNESS,-5.08],[0,-5.08],[0,91.52],[-PCB_THICKNESS,91.52]]); };
}

// NuBusFPGA
BOTTOM_HOLE_Y=8-1.08;
TOP_HOLE_Y=87.52;
FIRST_HOLE_X=-6.44;
SECOND_HOLE_X=-16.44;

module holders() {
difference() {
union() {
color("red") translate([0,0,-11]) linear_extrude(height = 22, center = true) { polygon([
    [PCB_OFFSET-PCB_THICKNESS,BOTTOM_HOLE_Y-4],
    [PCB_OFFSET-PCB_THICKNESS,BOTTOM_HOLE_Y+4],
    [PCB_OFFSET-PCB_THICKNESS-2,BOTTOM_HOLE_Y+4],
    [PCB_OFFSET-PCB_THICKNESS-2,BOTTOM_HOLE_Y-4],
    ]); };
color("red") translate([0,0,-11]) linear_extrude(height = 22, center = true) { polygon([
    [PCB_OFFSET-PCB_THICKNESS,TOP_HOLE_Y-4],
    [PCB_OFFSET-PCB_THICKNESS,TOP_HOLE_Y+4],
    [PCB_OFFSET-PCB_THICKNESS-2,TOP_HOLE_Y+4],
    [PCB_OFFSET-PCB_THICKNESS-2,TOP_HOLE_Y-4],
    ]); };
}
union() { 
color("blue") translate([0,BOTTOM_HOLE_Y,FIRST_HOLE_X]) rotate([0,90,0]) cylinder(r1=1.6,r2=1.6,h=20);
color("blue") translate([0,BOTTOM_HOLE_Y,SECOND_HOLE_X]) rotate([0,90,0]) cylinder(r1=1.6,r2=1.6,h=20);
color("blue") translate([0,TOP_HOLE_Y,FIRST_HOLE_X]) rotate([0,90,0]) cylinder(r1=1.6,r2=1.6,h=20);
color("blue") translate([0,TOP_HOLE_Y,SECOND_HOLE_X]) rotate([0,90,0]) cylinder(r1=1.6,r2=1.6,h=20);
}
}
}


//cube ([16.5+4, 12, 9.5+4 /* 14.8 max */], center = true);
USB_HOLDER_HEIGHT=5;
USB_HOLDER_WIDTH=10;
USB_HOLDER_DEPTH=5;
module usb_pos() {
translate([-1-USB_HOLDER_HEIGHT/2+PCB_OFFSET-PCB_THICKNESS,100-(74.67+(83.47-74.67)/2),-USB_HOLDER_DEPTH/2]) cube([2+USB_HOLDER_HEIGHT,2+USB_HOLDER_WIDTH,USB_HOLDER_DEPTH], center = true);
}
module usb_neg() {
translate([-0.499-USB_HOLDER_HEIGHT/2+PCB_OFFSET-PCB_THICKNESS,100-(74.67+(83.47-74.67)/2),-USB_HOLDER_DEPTH/2]) cube([1.001+USB_HOLDER_HEIGHT,USB_HOLDER_WIDTH,USB_HOLDER_DEPTH*4], center = true);
}

VGA_HOLDER_HEIGHT=14.8;
VGA_HOLDER_WIDTH=31.8;
VGA_HOLDER_DEPTH=2;
module vga_pos() {
    translate([-VGA_HOLDER_HEIGHT/2+PCB_OFFSET-PCB_THICKNESS,100-(18.21+(49.05-18.21)/2),-VGA_HOLDER_DEPTH/2+VGA_HOLDER_DEPTH]) cube ([VGA_HOLDER_HEIGHT, 6+VGA_HOLDER_WIDTH, VGA_HOLDER_DEPTH], center = true);
}
module vga_neg() {
    translate([-VGA_HOLDER_HEIGHT/2+PCB_OFFSET-PCB_THICKNESS,100-(18.21+(49.05-18.21)/2),-VGA_HOLDER_DEPTH/2]) cube ([10, 18, 10+0.001], center = true);
    
    
    translate([-VGA_HOLDER_HEIGHT/2+PCB_OFFSET-PCB_THICKNESS,100-(18.21+(49.05-18.21)/2)-12.5,-VGA_HOLDER_DEPTH/2]) color("black") rotate([0,0,90]) cylinder (h = 10, r1 = 1.6, r2 = 1.6, center = true);
    translate([-VGA_HOLDER_HEIGHT/2+PCB_OFFSET-PCB_THICKNESS,100-(18.21+(49.05-18.21)/2)+12.5,-VGA_HOLDER_DEPTH/2]) color("black") rotate([0,0,90]) cylinder (h = 10, r1 = 1.6, r2 = 1.6, center = true);
}

HDMI_HOLDER_HEIGHT=8;
HDMI_HOLDER_WIDTH=18;
HDMI_HOLDER_DEPTH=2;
module hdmi_pos() {
    translate([-1-HDMI_HOLDER_HEIGHT/2+PCB_OFFSET-PCB_THICKNESS,100-(53.3+(71.3-53.3)/2),-HDMI_HOLDER_DEPTH/2]) cube ([2+HDMI_HOLDER_HEIGHT, 2+HDMI_HOLDER_WIDTH, HDMI_HOLDER_DEPTH], center = true);
}
module hdmi_neg() {
    translate([-0.499-HDMI_HOLDER_HEIGHT/2+PCB_OFFSET-PCB_THICKNESS,100-(53.3+(71.3-53.3)/2),-HDMI_HOLDER_DEPTH/2]) cube ([1.001+HDMI_HOLDER_HEIGHT, HDMI_HOLDER_WIDTH, 5+0.001], center = true);
}

//pcb();

module complete() {
    difference() {
        union() {
            backplate();
            holders();
            usb_pos();
            vga_pos();
            hdmi_pos();
        }
        union() {
            usb_neg();
            vga_neg();
            hdmi_neg();
        }
    }
}

complete();
