include <NopSCADlib/core.scad>
include <RP2350_MIDI_Lighter_Definition.scad>

use <MCAD/boxes.scad>
include <NopSCADlib/vitamins/pcb.scad>


module PCB(Add_Audio_Jack = true)
{
	color([0.0, 1.0, 0.0])
	difference()
	{
		union()
		{
			translate([PCB_W/2, PCB_H/2+5, 0])	roundedBox([PCB_W, PCB_H-10, PCB_T], 2, true, $fn=100);
			translate([PCB_W/2, PCB_H/2-5, 0])	roundedBox([PCB_W, PCB_H-10, PCB_T], 1, true, $fn=100);
		}

		Screw_Holes_M3();
	}

	translate([0, 0, PCB_T/2]) translate(Power_Plug) 		Power_Plug();
	translate([0, 0, PCB_T/2]) translate(Button) 			Button();
	translate([0, 0, PCB_T/2]) translate(USB_C)				usb_C(false);
	translate([0, 0, PCB_T/2]) translate(MOLEX)				MOLEX_2x2P(false);
    if(Add_Audio_Jack) {
	    translate([0, 0, PCB_T/2]) translate(Audio_Jack)		Audio_Jack(false);
    }
	for (i = MIDI_Plugs) {
		translate(i)	MIDI_Plug_MABPM_5S(false);
	} 
	// translate([0, 0, PCB_T/2]) translate(Rotary_Encoder)	mirror([0, 0, 1]) potentiometer(KY_040_encoder, 1, 10);
}

module PCB_Cutouts_Top_Plate()
{
	translate([0, 0, PCB_T/2]) 		translate(USB_C)		usb_C(true);
	translate([0, 0, PCB_T/2-0.3]) 	translate(Power_Plug)	Power_Plug_Cutout();
	translate([0, 0, PCB_T/2]) 		translate(Audio_Jack)	Audio_Jack(true);
	translate(MOLEX)			
	translate([0, 10, PCB_T/2+Spacer_Top_Height+Cover_T_T/4-eps]) cube([5, 18, Cover_T_T/2], center=true);

	for (i = MIDI_Plugs) { 
		translate([0, 0, Cover_T_T/2])
		translate(i)
		scale([1, 1, 10]) MIDI_Plug_Cutout();
	}
}

module PCB_Cutouts_Top_Wall(Add_Audio_Jack = true)
{
	translate([0, 0, PCB_T/2]) 		translate(USB_C)		usb_C(true);
	translate([0, 0, PCB_T/2-0.3]) 	translate(Power_Plug)	Power_Plug_Cutout();
    if(Add_Audio_Jack) {
	    translate([0, 0, PCB_T/2]) 		translate(Audio_Jack)	Audio_Jack(true);
    }
	translate(MOLEX)			
	translate([0, 10, PCB_T/2+Spacer_Top_Height+Cover_T_T/4-eps]) cube([5, 18, Cover_T_T/2], center=true);

	for (i = MIDI_Plugs) {
		translate(i)	MIDI_Plug_MABPM_5S(true);
	} 
}

module PCB_Cutouts_From_Bottom()
{
	translate([0, 0, PCB_T/2]) translate(USB_C)			usb_C(true);
	translate([0, 0, PCB_T/2]) translate(Power_Plug)	Power_Plug(true);
	translate([0, 0, PCB_T/2]) translate(Audio_Jack)	Audio_Jack(true);
	for (i = MIDI_Plugs) {
		translate(i)	MIDI_Plug_MABPM_5S(true);
	} 
}

module Screw_Holes_M3()
{
	for (i = Mounting_Holes) { 
		translate(i) Screw_Drill(Drill_M3);
	}
}

module Screw_Pits_M3(depth)
{
	translate([0, 0, -depth])
	for (i = Mounting_Holes) { 
		translate(i) cylinder(d=Screw_Pit_M3, h=100, center=false, $fn=100);
	}
}

module Screw_Rings_M3()
{
	for (i = Mounting_Holes) { 
		translate(i) Screw_Ring_M3(Ring_M3_H);
	}
}

module Spacer(height)
{
	difference()
	{
		cylinder(h=height, d=5, $fn=6);
		Screw_Drill(Drill_M3);
	}
}

module Spacer_Top()
{
	color([0.3, 0.6, 0.0])
	// for (i = Mounting_Holes) { 
	for (i = [0:len(Mounting_Holes)-1]) { 
		translate(Mounting_Holes[i]) Spacer(Spacer_Top_Heights[i]);
	}
}

module Spacer_Bot()
{
	color([0.3, 0.6, 0.0])
	translate([0, 0, -Spacer_Bot_Height])
	for (i = Mounting_Holes) { 
		translate(i) Spacer(Spacer_Bot_Height);
	}
}

module Button()
{
	translate([0, 0, 7.4/2])
	{
		translate([-3.5/2, 0, 0])	cube([3.5, 7.3, 7.4], center=true);
		
		color([1, 0.65,0 ])
		translate([+3.9/2, 0, 0])	cube([3.9, 3.5, 3.5], center=true);
	}
}

module Button_Cutout()
{
	Button_W = 8;
	Button_H = 8;

	Button_X = 30;
	Button_Z = 20;
	
	translate([+Button_X/2, 0, -Button_Z/2 + Button_H])
	cube([Button_X, Button_W, Button_Z], center=true);
}

module Button_Cutout_For_Bottom()
{
	Button_W = 8;
	Button_H = 8;

	Button_X = 1.5;
	Button_Z = 20;
	
	translate([+PCB_W - Button[0] + Wall_PCB_GAP + Button_X/2 - eps, 0, +Button_Z/2])
	cube([Button_X, Button_W, Button_Z], center=true);

	translate([+PCB_W - Button[0] + Wall_PCB_GAP + Button_X/2 - eps, 0, 4.75])
	cube([3*Button_X, 3.5, 3.5], center=true);
}

module Power_Plug(cutout = false)
{
	Scale_Cube = cutout == true ? [2, 1.1, 1] : [1, 1, 1];
	
	color([0.1, 0.1, 0.1])
	translate([10.625, -2.4, 0])
	difference()
	{
		translate([-3.5, 0, 0])
		union()
		{
			scale(Scale_Cube)
			translate([3.5/2, 0, 11/2])							cube([ 3.5, 9, 11.0], center=true);
			translate([-10.9/2, 0, 6.4/2])						cube([10.9, 9,  6.4], center=true);
			translate([-10.9/2, 0, 6.4])	rotate([0, 90, 0])	cylinder(h=10.9, d=9, center=true, $fn=60);
		}

		if(cutout == false)
		{
			translate([-9.2/2, 0, 6.4])
			rotate([0, 90, 0])
			cylinder(h=9.2+1, d=6.3, center=true, $fn=60);
		}
	}
}

module Power_Plug_Cutout()
{
	Plug_D = 14.5;
	Plug_W = 10;
	Plug_H = 11.5;
	
	Plug_X = 30;
	Plug_Z = 20;

	// #translate([0, -2.4, -Plug_Z/2 + Plug_H])
	// cube([Plug_X, Plug_W, Plug_Z], center=true);

	translate([0, 0, 0.5])
	translate([10.625, -2.4, 0])
	translate([-3.5, 0, 0])
	{
		translate([3.5/2, 0, 11/2-4/2])		scale([1.2, 1.2, 1.0])						cube([ 3.5, 9, 11.0+4], center=true);
		intersection()
		{
			translate([3.5/2, 0, 11/2-4/2])		scale([1.2, 1.2, 1.0])						cube([ 25, 9, 11.0+4], center=true);
			translate([-10.9/2, 0, 6.4])	scale([1.0, 1.5, 1.023])	rotate([0, 90, 0])	cylinder(h=10.9, d=9, center=true, $fn=60);
		}
	}
}

module Power_Plug_Cutout_For_Bottom()
{
	Plug_D = 14.5;
	Plug_W = 10;
	Plug_H = 11.5;
	
	Plug_X = 30;
	Plug_Z = 20;

	translate([+Plug_X/2-Plug_D+5.08, -0, -Plug_Z/2 + Plug_H + 5])
	cube([Plug_X, Plug_W, Plug_Z], center=true);

	translate([0, 0.5, -3])
	rotate([0, 90, 0])
	cylinder(d=11, h=Plug_X, center=true, $fn=60);
}

module MOLEX_2x2P(cutout = false)
{
	if(cutout == false)
	{
		color([0.76, 0.76, 0.76])
		{
			translate([-1.5, -1.5, -3.18]) rotate([0, 0, 45]) cylinder(d=1.02, h=8.7, center=false, $fn=4);
			translate([+1.5, -1.5, -3.18]) rotate([0, 0, 45]) cylinder(d=1.02, h=8.7, center=false, $fn=4);

			translate([-1.5, -1.5, 8.7-3.18-0.7*1.02/2]) rotate([-90, 45, 0]) cylinder(d=1.02, h=10, center=false, $fn=4);
			translate([+1.5, -1.5, 8.7-3.18-0.7*1.02/2]) rotate([-90, 45, 0]) cylinder(d=1.02, h=10, center=false, $fn=4);

			translate([-1.5, +1.5, -3.18]) rotate([0, 0, 45]) cylinder(d=1.02, h=3.18, center=false, $fn=4);
			translate([+1.5, +1.5, -3.18]) rotate([0, 0, 45]) cylinder(d=1.02, h=3.18, center=false, $fn=4);

			translate([-1.5, +1.5, 7.4/2 - 1.25-0.3]) rotate([-90, 45, 0]) cylinder(d=1.02, h=10-3, center=false, $fn=4);
			translate([+1.5, +1.5, 7.4/2 - 1.25-0.3]) rotate([-90, 45, 0]) cylinder(d=1.02, h=10-3, center=false, $fn=4);
		}
	}

	color([0.2, 0.2, 0.2])
	difference()
	{
		union()
		{
			if(cutout == false)
			{
				translate([0, +5.82, -3.18]) rotate([0, 0, 45]) cylinder(d=3.2, h=3.18, center=false, $fn = $preview ? 32 : 100);
			}
			translate([0, +7.57/2+0.828, 7.4/2])	 	cube([10.1 , 7.57, 7.4], center=true);
			translate([0, +7.57+0.828 + 1.28/2, 7.4/2]) cube([ 9.75, 1.28, 7.4], center=true);

			translate([0, +7.57+0.828 + 1.28, 7.4/2]) hull()
			{
				translate([0, 0, 0]) cube([ 9.75, 0.01, 7.4], center=true);
				translate([0, 1.3, 0]) cube([ 7.15, 0.01, 7.4], center=true);
			}

		}
		if(cutout == false)
		{
			translate([0, 10, 7.4/2])
			for (i = [-1:2:1])
			{
				for (j = [-1:2:1])
				{  
					translate([i*(2.5/2 + 0.3), 0, j*(2.5/2 + 0.3)]) cube([2.5, 7, 2.5], center=true);
				}
			}
		}
	}
}

module Audio_Jack(cutout = false)
{
	color([0.2, 0.2, 0.2])
	difference()
	{
		union()
		{
			translate([0, -11.5/2+4.25, 5.2/2])	cube([6.5, 11.5, 5.2], center=true);
			translate([0, -4.25-2.5, 5/2])
			rotate([90, 0, 0]) cylinder(d=5, h=2.5, center=false);
		}

		translate([0, -4.25+8, 5/2])
		rotate([90, 0, 0]) cylinder(d=3.6, h=20, center=false);
	}

	if(cutout == true)
	{
		translate([0, -4.25+8, 5/2])
		rotate([90, 0, 0]) cylinder(d=6, h=20, center=false);
	}
}

module MIDI_Plug_MABPM_5S(cutout = false)
{
	color([0.76, 0.76, 0.76])
	{
		translate([+0.0, +0.0, -4.2]) rotate([0, 0, 45]) cylinder(d=1.4, h=8.2, center=false, $fn=4);
		translate([+5.0, +2.5, -4.2]) rotate([0, 0, 45]) cylinder(d=1.4, h=8.2, center=false, $fn=4);
		translate([-5.0, +2.5, -4.2]) rotate([0, 0, 45]) cylinder(d=1.4, h=8.2, center=false, $fn=4);
		translate([+7.5, +0.0, -4.2]) rotate([0, 0, 45]) cylinder(d=1.4, h=8.2, center=false, $fn=4);
		translate([-7.5, +0.0, -4.2]) rotate([0, 0, 45]) cylinder(d=1.4, h=8.2, center=false, $fn=4);

		translate([+5.0, -10.0, -4.2]) rotate([0, 0, 45]) cylinder(d=1.4, h=4.2, center=false, $fn=4);
		translate([-5.0, -10.0, -4.2]) rotate([0, 0, 45]) cylinder(d=1.4, h=4.2, center=false, $fn=4);
	}

	difference()
	{
		union()
		{
			color([0.2, 0.2, 0.2])
			{
				translate([0, -5, 10.2/2]) cube([18, 10, 10.2], center=true);
				translate([0, -5, 10.2]) rotate([90, 0, 0]) cylinder(h=10, d=15.7, center=true, $fn = $preview ? 32 : 100);
				translate([+0.5, -0.5-10-0, 20.35/2]) cube([19.5, 1.0, 20.35], center=true);
				translate([-0.5, -0.5-10-1, 20.35/2]) cube([19.5, 1.0, 20.35], center=true);
			}
			color([0.76, 0.76, 0.76])
			difference()
			{
				translate([0, -0.1/2-10-2-eps, 20.35/2]) cube([18, 0.1, 20.35], center=true);
				translate([0, -10-2, 20.35/2]) rotate([90, 0, 0]) cylinder(h=5, d=14, center=true, $fn = $preview ? 32 : 100);
			}
		}

		union()
		{
			translate([0, -10-2, 20.35/2])
			rotate([90, 0, 0])
			difference()
			{
				cylinder(h= 5, d=13.60, center=true, $fn = $preview ? 32 : 100);
				cylinder(h=10, d=11.44, center=true, $fn = $preview ? 32 : 100);
			}

			translate([0, 0, 15.0]) cube([2.5, 100, 2], center=true);
			translate([0, 100/2-10+eps, 17.0]) cube([2.5, 100, 4], center=true);
		}
	}

	if(cutout == true)
	{
		translate([0, 0, 1])
		union()
		{
			translate([0, -10-2, 20.35/2])	rotate([90, 0, 0])	cylinder(h=50, d=18, center=true, $fn = $preview ? 32 : 100);
			translate([0, -10-2, 20.35/2+9])	cube([18, 50, 18], center=true);
		}
	}
}