use<pitch.scad>;

module Box(
	in_l = 50,
	in_w = 45,
	in_h = 40,
	wall_th = 2,
	pot_d = 7.7,
	pot_x = 36,
	pot_y = 20,
	pot_mark_dist = 8,
	pot_mark_d = 3,
	sw_w = 6,
	sw_l = 8.2,
	sw_joint_dist = 15,
	sw_joint_d = 3,
	sw_joint_depth = 1,
	sw_screw_dist = 8,
	sw_screw_d = 3.6,
	sw_x = 12,
	sw_y = 25,
	ino_h = 26,
	ino_l = 47, // Length of Arduino itself
	ino_w = 18, // Width of Arduino itself
	ino_hold1_l = 2.25, // USB side
	ino_hold1_w = 4,
	ino_hold2_l = 2.25, // Other side
	ino_hold2_w = 2,
	ino_hold3_m = 1, // Inner side
	ino_hold3_h = 3,
	ino_hold3_l = 4,
	ino_hold3_th = 2,
	ino_hold3_supp = 5,
	ino_x = 0, // From USB side
	usb_y = 3, // From ino_h
	usb_w = 11,
	usb_h = 7,
	usb_y = 3, // Distance between bottom of Arduino PCB and center of USB port
	sensor_w = 10,
	sensor_h = 7,
	sensor_x = 18,
	sensor_y = 6,
	ir_d = 8,
	ir_x = 15,
	ir_y = 7,
	ir_hold_dist = 17,
	ir_hold_small_h = 2.2,
	ir_hold_small_d = 7,
	ir_hold_offset = -6,
	lid_screw_d = 4,
	lid_screw_dist_side = 16,
	lid_screw_dist_top = 5,
	arm_joint_d = 6,
	arm_joint_th = 5,
	arm_joint_margin = 4,
	arm_joint_dist = 8
) {
	difference() {
		cube([in_l+2*wall_th, in_w+2*wall_th, in_h+wall_th]);
		
		// Inside
		translate([wall_th, wall_th, wall_th]) cube([in_l, in_w, in_h+wall_th]);
		
		// Potentiometer
		translate([wall_th+pot_x, 0, wall_th+pot_y]) rotate([-90,0,0])
			cylinder(d=pot_d, h=wall_th, $fn=60);
		translate([wall_th+pot_x+pot_mark_dist, 0, wall_th+pot_y]) rotate([-90,0,0])
			cylinder(d=pot_mark_d, h=wall_th, $fn=60);
		translate([wall_th+pot_x+-pot_mark_dist, 0, wall_th+pot_y]) rotate([-90,0,0])
			cylinder(d=pot_mark_d, h=wall_th, $fn=60);
		
		// Switch
		translate([wall_th+sw_x, 0, wall_th+sw_y]) union() {
			translate([-sw_l/2, 0, -sw_w/2]) cube([sw_l, wall_th, sw_w]);
			translate([-sw_joint_dist/2, wall_th-sw_joint_depth, 0]) rotate([-90,0,0])
				cylinder(d=sw_joint_d, h=wall_th, $fn=60);
			translate([sw_joint_dist/2, wall_th-sw_joint_depth, 0]) rotate([-90,0,0])
				cylinder(d=sw_joint_d, h=wall_th, $fn=60);
			translate([0, 0, sw_screw_dist]) rotate([-90,0,0])
				cylinder(d=sw_screw_d, h=wall_th, $fn=60);
		};
		
		// USB
		translate([wall_th+in_l, wall_th+in_w-ino_w/2-usb_w/2, wall_th+ino_h+usb_y-usb_h/2])
			cube([wall_th, usb_w, usb_h]);
		
		// Sensor cable
		translate([wall_th+in_l, wall_th+sensor_x-sensor_w/2, wall_th+sensor_y-sensor_h/2])
			cube([wall_th, sensor_w, sensor_h]);
		
		// IR LED
		translate([0, wall_th+ir_x, wall_th+ir_y])
			rotate([0,90,0]) cylinder(d=ir_d, h=wall_th, $fn=60);
		translate([wall_th+ir_hold_dist, wall_th+ir_x+ir_hold_offset, 0])
			rotate([0,0,30]) pitch_inv_m3();
		
		// Lid screws
		translate([0, wall_th+lid_screw_dist_side, wall_th+in_h-lid_screw_dist_top])
			rotate([0,90,0]) cylinder(d=lid_screw_d, h=wall_th, $fn=60);
		translate([wall_th+in_l, wall_th+lid_screw_dist_side, wall_th+in_h-lid_screw_dist_top])
			rotate([0,90,0]) cylinder(d=lid_screw_d, h=wall_th, $fn=60);
	}
	
	// Arduino holder
	translate([wall_th, wall_th+in_w-ino_hold2_w, wall_th])
		cube([ino_x+ino_hold2_l, ino_hold2_w, ino_h]);
	translate([wall_th, wall_th+in_w-ino_w, wall_th])
		cube([ino_x+ino_hold2_l, ino_hold2_w, ino_h]);
	translate([wall_th+in_l-ino_hold1_l, wall_th+in_w-ino_hold1_w, wall_th])
		cube([ino_x+ino_hold1_l, ino_hold1_w, ino_h]);
	translate([wall_th+in_l-ino_hold1_l, wall_th+in_w-ino_w, wall_th])
		cube([ino_x+ino_hold1_l, ino_hold1_w, ino_h]);
	translate([wall_th, wall_th+in_w-ino_w-ino_hold3_m-ino_hold3_th, wall_th+ino_h])
		hull() {
			cube([ino_hold3_l, ino_hold3_th, ino_hold3_h]);
			translate([-wall_th, 0, -ino_hold3_supp])
				cube([wall_th, ino_hold3_th, ino_hold3_supp]);
		};
	translate([wall_th+in_l-ino_hold3_l, wall_th+in_w-ino_w-ino_hold3_m-ino_hold3_th, wall_th+ino_h])
		hull() {
			cube([ino_hold3_l, ino_hold3_th, ino_hold3_h]);
			translate([ino_hold3_l, 0, -ino_hold3_supp])
				cube([wall_th, ino_hold3_th, ino_hold3_supp]);
		};
	
	// IR LED holder
	translate([wall_th+ir_hold_dist, wall_th+ir_x+ir_hold_offset, wall_th]) rotate([0,0,-90])
		pitch_m3(s=ir_y-4-ir_hold_small_h);
	translate([wall_th+ir_hold_dist, wall_th+ir_x+ir_hold_offset, wall_th+ir_y-ir_hold_small_h])
		difference() {
			cylinder(d=ir_hold_small_d, h=ir_hold_small_h, $fn=60);
			cylinder(r=M3_RAY(), h=ir_hold_small_h, $fn=20);
		};
	
	// Arm joint
	translate([wall_th+in_l/2, 2*wall_th+in_w, 0]) difference() {
		union() {
			translate([-arm_joint_d/2-arm_joint_margin, 0, 0])
				cube([arm_joint_d+2*arm_joint_margin, arm_joint_dist, arm_joint_th]);
			translate([0, arm_joint_dist, 0])
				cylinder(d=arm_joint_d+2*arm_joint_margin, h=arm_joint_th, $fn=60);
		}
		translate([0, arm_joint_dist, 0])
			cylinder(d=arm_joint_d, h=arm_joint_th, $fn=60);
	};
	
	//color("#ff000066") translate([wall_th+sw_x, wall_th+0.4, wall_th+sw_y]) rotate([0,0,180]) SwitchAttach();
}

// TODO ajouter supports Arduino
module Lid(
	in_l = 50,
	in_w = 45,
	in_h = 40,
	wall_th = 2,
	ino_h = 26,
	ino_l = 47, // Length of Arduino itself
	ino_w = 18, // Width of Arduino itself
	ino_hold_h = 13,
	ino_hold1_x = 9,
	ino_hold1_l = 2,
	ino_hold1_y = 29.5,
	ino_hold1_w = 11.5,
	ino_hold2_x = 37.5,
	ino_hold2_l = 2,
	ino_hold2_y = 37,
	ino_hold2_w = 4,
	border_h = 1.6,
	border_margin = 0.8,
	led1_x = 17,
	led1_y = 38,
	led1_d = 3,
	led2_x = 37,
	led2_y = 33,
	led2_d = 3,
	led3_x = 39,
	led3_y = 33,
	led3_d = 3,
	reset_x = 47,
	reset_y = 35,
	reset_d = 4.5,
	lid_screw_d = 4,
	lid_screw_dist_side = 16,
	lid_screw_dist_top = 5.5,
	lid_screw_margin = 2.5,
	lid_screw_th = 1.5
) {
	difference() {
		union() {
			cube([in_l+2*wall_th, in_w+2*wall_th, wall_th]);
			translate([wall_th+border_margin, wall_th+border_margin, wall_th]) difference() {
				cube([in_l-2*border_margin, in_w-2*border_margin, border_h]);
				translate([wall_th, wall_th, 0])
					cube([in_l-2*border_margin-2*wall_th, in_w-2*border_margin-2*wall_th, border_h]);
			}
		}
		translate([led1_x, led1_y, 0])
			cylinder(d=led1_d, h=wall_th+border_h, $fn=40);
		translate([led2_x, led2_y, 0])
			cylinder(d=led2_d, h=wall_th+border_h, $fn=40);
		translate([led3_x, led3_y, 0])
			cylinder(d=led3_d, h=wall_th+border_h, $fn=40);
		translate([reset_x, reset_y, 0])
			cylinder(d=reset_d, h=wall_th+border_h, $fn=40);
	}
	
	translate([wall_th+border_margin, wall_th+lid_screw_dist_side, wall_th]) difference() {
		union() {
			translate([0, -lid_screw_d/2-lid_screw_margin, 0])
				cube([lid_screw_th, lid_screw_d+2*lid_screw_margin, lid_screw_dist_top]);
			translate([0, 0, lid_screw_dist_top])
				rotate([0,90,0])
				cylinder(d=lid_screw_d+2*lid_screw_margin, h=lid_screw_th, $fn=60);
			translate([lid_screw_th,0,lid_screw_dist_top]) rotate([90,0,0]) rotate([0,90,0])
				pitch_m3();
		}
		translate([0, 0, lid_screw_dist_top])
			rotate([0,90,0]) cylinder(d=lid_screw_d, h=wall_th, $fn=60);
	}
	translate([in_l-border_margin+wall_th-lid_screw_th, wall_th+lid_screw_dist_side, wall_th]) difference() {
		union() {
			translate([0, -lid_screw_d/2-lid_screw_margin, 0])
				cube([lid_screw_th, lid_screw_d+2*lid_screw_margin, lid_screw_dist_top]);
			translate([0, 0, lid_screw_dist_top])
				rotate([0,90,0])
				cylinder(d=lid_screw_d+2*lid_screw_margin, h=lid_screw_th, $fn=60);
			translate([0,0,lid_screw_dist_top]) rotate([90,0,0]) rotate([0,-90,0])
				pitch_m3();
		}
		translate([0, 0, lid_screw_dist_top])
			rotate([0,90,0]) cylinder(d=lid_screw_d, h=wall_th, $fn=60);
	}
	
	translate([wall_th+ino_hold1_x, wall_th+ino_hold1_y, wall_th])
		cube([ino_hold1_l, ino_hold1_w, ino_hold_h]);
	translate([wall_th+ino_hold2_x, wall_th+ino_hold2_y, wall_th])
		cube([ino_hold2_l, ino_hold2_w, ino_hold_h]);
}

module SwitchAttach(
	joint_d = 1.5,
	joint_dist = 15,
	joint_h = 2,
	screw_dist = 8,
	screw_d = 3.6,
	arm_joint_in_w = 1,
	arm_joint_out_w = 2,
	arm_joint_bottom_w = 2.6,
	arm_screw_in_w = 3,
	arm_screw_out_w = 5,
	arm_th = 2,
) {
	translate([-joint_dist/2, arm_th, 0]) rotate([-90,0,0])
		cylinder(d=joint_d, h=joint_h, $fn=60);
	translate([joint_dist/2, arm_th, 0]) rotate([-90,0,0])
		cylinder(d=joint_d, h=joint_h, $fn=60);
	difference() {
		translate([-joint_dist/2-arm_joint_out_w, 0, screw_dist-arm_screw_in_w])
			cube([joint_dist+2*arm_joint_out_w, arm_th, arm_screw_in_w+arm_screw_out_w]);
		translate([0, 0, screw_dist]) rotate([-90,0,0])
			cylinder(d=screw_d, h=arm_th, $fn=60);
	};
	translate([-joint_dist/2-arm_joint_out_w, 0, -arm_joint_bottom_w])
		cube([arm_joint_out_w+arm_joint_in_w, arm_th, arm_joint_bottom_w+screw_dist]);
	translate([joint_dist/2-arm_joint_in_w, 0, -arm_joint_bottom_w])
		cube([arm_joint_out_w+arm_joint_in_w, arm_th, arm_joint_bottom_w+screw_dist]);
}

module ArmJoint(
	d = 19,
	rod_d = 3.8,
	rod_dist = 8.5,
	rod_z = 0.3,
	th = 4.8,
	hole_d = 4,
) {
	difference() {
		cylinder(d=d, h=th, $fn=80);
		
		cylinder(d=hole_d, h=th+1, $fn=40);
		
		translate([-rod_dist/2, -d/2, rod_z+th])
			rotate([-90, 0, 0])
			cylinder(d=rod_d, h=d, $fn=40);
		translate([rod_dist/2, -d/2, rod_z+th])
			rotate([-90, 0, 0])
			cylinder(d=rod_d, h=d, $fn=40);
	}
}

module RodSupport(
	ear_l = 15,
	ear_th = 4,
	out_d = 32,
	rod_d = 25.5,
	rod_offset = 0.5,
	screw_d = 4,
	screw_dist = 9,
	w = 10,
) {
	difference() {
		union() {
			cylinder(d=out_d, h=w, $fn=80);
			hull() {
				translate([out_d/2+screw_dist, 0, w/2])
					rotate([-90, 0, 0])
					cylinder(d=w, h=ear_th, $fn=60);
				translate([-out_d/2-screw_dist, 0, w/2])
					rotate([-90, 0, 0])
					cylinder(d=w, h=ear_th, $fn=60);
			}
			//translate([-out_d/2-ear_l, 0, 0]) cube([out_d+2*ear_l, ear_th, w]);
		}
		
		// Ears
		translate([-out_d/2, -out_d, 0]) cube([out_d, out_d, w]);
		// Rod
		translate([0, -rod_offset, 0]) cylinder(d=rod_d, h=w, $fn=80);
		// Screws
		translate([out_d/2+screw_dist, 0, w/2])
			rotate([-90, 0, 0])
			cylinder(d=screw_d, h=ear_th, $fn=40);
		translate([-out_d/2-screw_dist, 0, w/2])
			rotate([-90, 0, 0])
			cylinder(d=screw_d, h=ear_th, $fn=40);
	}
}

//Box();
//rotate([90,0,0]) SwitchAttach();
//Lid();
ArmJoint();
//RodSupport();
