include <rounded_square.scad>
include <MCAD/nuts_and_bolts.scad>

l = 384;
w = 135.5;
h = 7;

wal = 1.5;
flo = 1.5; // 1mm

c = 2; // corners diameter

h_po = 4.5; // hole for screws
h_di = 5;
h_de = 6.5;

u_height = 6;
u_s_width = 13.2;
u_s_depth = 12;
u_s_pins = 10;
u_s_depression = 0.3;
u_p_width = 13;
u_wall = 2;
u_pl_w = 1; // small platic stuff on the usb socket
u_pl_d = 3; // small platic stuff on the usb socket

nbHolesW = 2;
nbHolesL = 6;

part="right"; // all / left / right

$fn=30;

module hole(px, py, de, di, p) {
    translate([px, py, flo])
    difference() {
        cylinder(h = h, r = de/2 + 1);
        cylinder(h = h+0.1, r = di/2);
    }
}

module hole_sub(px, py, de, di, p) {
    translate([px, py, -0.05])
    union() {
        // Max plastic height = 8mm - 1.5mm (metal) - 2mm (nut) => 4.5mm
        tolerance = 0.2;
        radius = METRIC_NUT_AC_WIDTHS[3] / 2 + tolerance;
        cylinder(r = radius, h = (h + flo) - 4.5 , $fn = 6, center=[0,0]);
        nutHole(size = 3, tolerance = 0.1);
        cylinder(h = h + flo + 0.1, r = di/2);
    }
}

module ellipsoid(diameter, width, height) {
    translate([0, 0, -height/4])
    scale([1, width/diameter, 1]) linear_extrude(height = height) circle(d = diameter); 
}

module usb_support(x, y, z) {
    translate([x, y, z]) difference() {
        translate([- u_wall + (u_p_width - u_s_width)/2, - u_s_depth - u_wall, 0])
            cube(size = [u_s_width + u_wall * 2, u_s_depth + u_wall, h]);
        translate([(u_p_width - u_s_width)/2, -u_s_depth, h-u_height - u_s_depression])
            cube(size = [u_s_width, u_s_depth, u_height + u_s_depression + 0.05]);
        translate([(u_p_width - u_s_width)/2-u_pl_w, -u_s_depth, h-u_height - u_s_depression])
            cube(size = [u_s_width + 2*u_pl_w, u_pl_d, u_height + u_s_depression + 0.05]);
        translate([u_p_width/2 - u_s_pins/2, -(u_s_depth + u_wall)-0.05, h-u_height/2])
            cube(size = [u_s_pins, u_s_depth + u_wall + 0.05, u_height/2 + 0.05]);
    }
}

module usb_sub(x, y, z) {
    translate([x, y, z])
        translate([0, 0.05, 0]) cube(size = [u_p_width, 12, u_height + 0.05]);  
}

module floor_sub() {    
    // Floor holes
    for (j = [0: nbHolesW - 1]) for (i = [0:nbHolesL - 1]) {
        orientation = ((i + j) % 2) * 2 - 1;
        translate([  l / (2 * nbHolesL) + i * (l / 6),  w / (2 * nbHolesW) + j * (w / 2), 0])
            rotate([0, 0,  orientation * 40]) ellipsoid(35, 55, flo * 2);
    }
}

module case() {
    difference() {
        linear_extrude(height = h + flo)
            rounded_square([l, w], corners = [5, 5, 5, 5]);
        translate([wal, wal, flo])
            cube(size = [l-2*wal, w-2*wal, h+0.1]);
    }
}

module keyboard() {
    
    difference() {
        union() {
            case();
            usb_support(50, w - wal, flo);
            
            hole(       h_po,     h_po, h_de, h_di, h_po);
            hole( l/2       ,     h_po, h_de, h_di, h_po);
            hole(   l - h_po,     h_po, h_de, h_di, h_po);
            hole(       h_po, w - h_po, h_de, h_di, h_po);
            hole( l/2       , w - h_po, h_de, h_di, h_po);
            hole(   l - h_po, w - h_po, h_de, h_di, h_po);
        }
        
        usb_sub(50, w - 12, flo + h - u_height);
        
        floor_sub();
    
        hole_sub(       h_po,     h_po, h_de, h_di, h_po);
        hole_sub( l/2       ,     h_po, h_de, h_di, h_po);
        hole_sub(   l - h_po,     h_po, h_de, h_di, h_po);
        hole_sub(       h_po, w - h_po, h_de, h_di, h_po);
        hole_sub( l/2       , w - h_po, h_de, h_di, h_po);
        hole_sub(   l - h_po, w - h_po, h_de, h_di, h_po);
        
    }
}

if (part == "all") {
    keyboard();
}
if (part == "right" || part == "both") difference() {
    keyboard();
    translate([0, 0, 0]) cube(size = [l/2 - h_po, w, h+flo]);
    translate([0, 0, 0]) cube(size = [l/2 + h_po, w, h+flo - 3]);
}
if (part == "left" || part == "both") difference() {
    keyboard();
    translate([l/2 + h_po, 0,         0]) cube(size = [l/2 - h_po, w, h+flo]);
    translate([l/2 - h_po, 0, h+flo - 3]) cube(size = [l/2 + h_po, w, h+flo - 3]);
}