PCB_T 			= 1.6;
PCB_W 			= 58;
PCB_H 			= 74;

eps 			= 0.001;

Cover_O 		= 2.5;	// O = Overhang
Cover_T_T 		= 2.0;	// T_T = Top Thickness
Cover_B_T 		= 2.0;	// B_T = Bottom Thickness
Wall_PCB_GAP 	= 0.25;


Drill_M3    	= 3.5;
Ring_M3_H		= 2;

Screw_Ring_M3_Outer_Diameter = 7.5;
Screw_Ring_M3_Inner_Diameter = 5.5;
Screw_Pit_M3 				 = 6.5;

Spacer_Top_Height = 10;
Spacer_Bot_Height = 5;

Wall_T_H 	= Spacer_Top_Height + PCB_T/2 + Cover_T_T;	// T_H = Wall Top Height
Wall_B_H 	= Spacer_Bot_Height + PCB_T/2 + Cover_B_T + 1;	// B_H = Wall Bottom Height

Mounting_Holes = [
	[ 4.0, 70.0, 0],
	[54.0, 70.0, 0],
	[42.0, 46.5, 0],
	[ 4.0, 21.0, 0],
	[48.0, 23.5, 0]
];

MIDI_Plugs = [
	[11.5, 13.335, 0],
	[46.5, 13.335, 0]
];

SSD1306 		= [15.875, 53.34 , 0];
GC9A01A 		= [20.320, 22.56 , 0];
Rotary_Encoder 	= [48.895, 36.830, 0];
Power_Plug 		= [49.530, 64.770, 0];
Button			= [59.055, 23.495, 0];
USB_C			= [53.975, 50.165, 0];
MOLEX			= [19.05 , 66.04 , 0];
Audio_Jack		= [29.00 , 8.255 , 0];


Screw_Area_Cutout_Widths = [
	9,
	12,
	9,
	9
];

Screw_Area_Cutout_Offsets = [
	+Screw_Area_Cutout_Widths[0]/2 - Mounting_Holes[0][0] - Cover_O,
	-Screw_Area_Cutout_Widths[1]/2 + PCB_W - Mounting_Holes[1][0] + Cover_O,
	+Screw_Area_Cutout_Widths[2]/2 - Mounting_Holes[2][0] - Cover_O,
	-Screw_Area_Cutout_Widths[3]/2 + PCB_W - Mounting_Holes[3][0] + Cover_O
];

module Screw_Drill(drill)
{
    translate([0,0,-50])    
    cylinder(h=100, d=drill, center=false, $fn=100);
}

module Screw_Ring_M3(height)
{
    translate([0, 0, height/2])
	difference()
    {
        cylinder(h=1*height, d=Screw_Ring_M3_Outer_Diameter, center=true, $fn=100);
		cylinder(h=2*height, d=Screw_Ring_M3_Inner_Diameter, center=true, $fn=100);
    }
}

module SSD1306_Cutout()
{
    // Screw positions relative to the Display
    // See http://www.lcdwiki.com/File:MC096-015.jpg
    SSD1306_Screws = [
        [+27.3/2-2, 2      ,0],
        [-27.3/2+2, 2      ,0],
        [+27.3/2-2, 27.8-2 ,0],
        [-27.3/2+2, 27.8-2 ,0]
    ];
    
    mirror([0,1,0])
    translate([0,-2.54/2,0])
    {
        for (i = [0:3])
        {
            translate(SSD1306_Screws[i]) Screw_Drill(2.5);
        }
        
        translate([0, 1.5 ,0])      cube([4*2.54 , 2.54  , 20], center=true);
        translate([0,14.27,0])      cube([27     , 20    , 20], center=true);
        translate([0,25.77-0.01,0]) cube([14     ,  3    , 20], center=true);
    }
}

module GC9A01A_Cutout(thickness)
{
    Screw_Holes = [
        [+15, +06.2, 0],
        [-15, +06.2, 0],
        [+15, +36.2, 0],
        [-15, +36.2, 0]
    ];
    
    cube([20, 2, thickness], center=true);

    hull()
    {
        translate([0, 21.1, 0])
        cylinder(d=36.0, h=thickness, center=true, $fn = $preview ? 32 : 100);

        translate([0, 42.0, 0])
        cube([13, 0.1, thickness], center=true);
    }

    for (i = Screw_Holes) {
        translate(i)
        cylinder(r=1.27, h=thickness, center=true, $fn = $preview ? 32 : 100);
    }
}

module GC9A01A_Cover_Cutout(thickness)
{
    Screw_Holes = [
        [+15, +06.2, 0],
        [-15, +06.2, 0],
        [+15, +36.2, 0],
        [-15, +36.2, 0]
    ];

    translate([0, 21.1, 0])
    cylinder(d=36.0, h=thickness, center=true, $fn = $preview ? 32 : 100);

    for (i = Screw_Holes) {
        translate(i)
        cylinder(r=1.27, h=thickness, center=true, $fn = $preview ? 32 : 100);
    }
}

module GC9A01A_Cover_Plate(thickness, scale_factor, cutout=false)
{
    Plate_X = 40*scale_factor;
    Plate_Y = 47*scale_factor;
	
	translate([0, 0, thickness/2])
    difference()
    {
        translate([0, 20, 0]) roundedBox([Plate_X, Plate_Y, thickness], 5, true, $fn = $preview ? 32 : 100);
		if(cutout == false) {
			
			GC9A01A_Cover_Cutout(10);

			translate([-Plate_X/2-10+Screw_Pit_M3, 20-Plate_Y/2-10, 0])
			translate([0.5, Screw_Pit_M3-1.2, 0]) roundedBox([20, 20, 20], Screw_Pit_M3/1.8, true, $fn = $preview ? 32 : 100);
		}

    }
}

module Screw_Area_Cutout(width, offset, thickness)
{
	translate([offset, 0, thickness/2 + 0.1])
	cube([width, 5, thickness + 0.1], center=true);
}

module Screw_Area_Stripe(offset, thickness, scale_y, scale_z)
{
	Width = PCB_W + Cover_O + eps;
	
	translate([PCB_W/2, offset, thickness/2 + 0.1])
	scale([1, scale_y, scale_z])
	cube([Width, 10, thickness + 0.1], center=true);
}