//uniBox2

$fn = 32;
include <./camferCube.scad>
include <./screwShapes1.scad>

pcbW = 25; // inside width, mm
pcbL = 90; // inside length, mm
pcb_margin = 0.5; // extra space on each edge of PCB, but accounted for in pillar and hole location, mm
bottomHeight = 50; // height of inside wall for side without screws, mm
screwSideHeight = 3; // mm
wall = 2; // wall thickness, mm
pcb_thickness = 2; // leaves gap between pillars for PCB, mm
pillar_x = 1; // distance in from edge of PCB, mm
pillar_y = 1; // distance in from edge of PCB, mm
screw_thread_diam = 1.5; // diameter of pillar that will accept screw threads
screw_hole_diam = 2.2;

// TODO also consider taking PCB a) dimensions and b) hole spacing and
// building the box around those values (first screw holes, then box).

// Define box dimensions
bottomDimsInside = [pcbW + screw_thread_diam + (1.5 * wall),
                    pcbL + (2 * pcb_margin), 
                    bottomHeight + wall];
bottomDimsOutside = bottomDimsInside + [2 * wall, 2 * wall, 0];                    
screwSideDimsInside = [pcbW + screw_thread_diam + (1.5 * wall), 
                       pcbL + (2 * pcb_margin), 
                      screwSideHeight + wall];
screwSideDimsOutside = screwSideDimsInside + [2 * wall, 2 * wall, 0];                      

// Define pillar XY locations
pillarSW = [wall + pillar_x , 
            wall + pillar_y , 
            wall];
pillarSE = [bottomDimsInside[0] - pillar_x + wall, 
            wall + pillar_y, 
            wall];
pillarNW = [wall + pillar_x, 
            bottomDimsInside[1] - pillar_y + wall, 
            wall];
pillarNE = [bottomDimsInside[0] - pillar_x + wall, 
            bottomDimsInside[1] - pillar_y + wall, 
            wall];  
// Echo hole spacing
echo("X screw center spacing: ", pillarSE[0] - pillarSW[0]);   
echo("Y screw center spacing: ", pillarNE[1] - pillarSE[1]);    

// Define ridge locations
ridgeWest = [wall/2, bottomDimsInside[1] / 2 + wall, bottomDimsInside[2]];
ridgeEast = [bottomDimsOutside[0] - wall / 2, bottomDimsInside[1] / 2 + wall , bottomDimsInside[2]];
ridgeWestScrew = [wall/2, bottomDimsInside[1] / 2 + wall, screwSideDimsInside[2]];
ridgeEastScrew = [bottomDimsOutside[0] - wall / 2, bottomDimsInside[1] / 2 + wall , screwSideDimsInside[2]];
                  
                  
module bottom(screwSide = false) {
    difference() {
        if (screwSide) {
            difference(){
                difference() {
                    camferHalfCube(screwSideDimsOutside, false);
                    translate([wall, wall, wall]) cube(screwSideDimsInside);
                }
                // Add text
                translate([bottomDimsOutside[0] / 3,
                           bottomDimsOutside[1] / 3.5, 
                           1]) rotate([180, 0, 270]) 
                           linear_extrude(2) 
                           text("Vulpes v01", valign = "center", halign = "center", size = 6, );
                translate([bottomDimsOutside[0] / 1.5 ,
                           bottomDimsOutside[1] / 1.5, 
                           1]) rotate([180, 0, 270]) 
                           linear_extrude(2) 
                           text("W1CDN", valign = "center", halign = "center", size = 6, );
                
            }
        } else {
            difference() {
                difference(){
                    camferHalfCube(bottomDimsOutside, false);
                    translate([wall, wall, wall]) cube(bottomDimsInside);
                }
                
            }
        }
        
        // side ridge concave
        translate(ridgeWest) sideRidge();
        translate(ridgeEast) sideRidge();
        
        // Screw insets
        if (screwSide) {
             translate(pillarSW) translate([0, 0, -wall - 0.02]) flatHeadM2(bottomHeight);
             translate(pillarSE) translate([0, 0, -wall - 0.02]) flatHeadM2(bottomHeight);
             translate(pillarNW) translate([0, 0, -wall - 0.02]) flatHeadM2(bottomHeight);
             translate(pillarNE) translate([0, 0, -wall - 0.02]) flatHeadM2(bottomHeight);
             
             // Holes for Vulpes board v01. 
             // Lay out holes on XY plane, then rotate into place and move to correct side.
             // Measure relative to bottom of PCB.
             translate([bottomDimsInside[0] - (wall * 1.5) - screw_thread_diam,
                    wall*1.5,
                    0 + wall/2]){
                     rotate([0, -180, 90]){
                        // Key jack
                        translate([-26, 3, 0]) 
                            cylinder(d = 8, h = wall + 0.04, center = true);
                         // Key block
                        translate([-14, 4, 0]) 
                            cube([10, 6, wall + 0.04], center = true);
                        // USB
                        translate([-59, 14, 0]) 
                            cube([12, 8, wall + 0.04], center = true);
                        }
                    }   
        } 
    }

    
    if (screwSide) { // if screw side
        // side ridge convex
        translate(ridgeWestScrew) sideRidge(wall);
        translate(ridgeEastScrew) sideRidge(wall);
        // Pillars
        translate(pillarSW) screwPillar(screwSide, screwSideDimsInside[2] - wall, screw_hole_diam, screw_thread_diam);
        translate(pillarSE) screwPillar(screwSide, screwSideDimsInside[2] - wall, screw_hole_diam, screw_thread_diam);
        translate(pillarNW) screwPillar(screwSide, screwSideDimsInside[2] - wall, screw_hole_diam, screw_thread_diam);
        translate(pillarNE) screwPillar(screwSide, screwSideDimsInside[2] - wall, screw_hole_diam, screw_thread_diam);
        
        // Grooves for PCB
        translate([pillarSE[0] - pcb_thickness - (wall * 1.5) - (screw_thread_diam / 2), wall, 0]) 
            cube([wall/2, wall*1.5, screwSideDimsInside[2]]);
        translate([pillarNE[0] - pcb_thickness - (wall * 1.5) - (screw_thread_diam / 2), bottomDimsInside[1] - wall/2, 0]) 
            cube([wall/2, wall*1.5, screwSideDimsInside[2]]);
    }
    else { // if not screw side
        translate(pillarSW) screwPillar(screwSide, bottomDimsInside[2] - wall, screw_hole_diam, screw_thread_diam);
        translate(pillarSE) screwPillar(screwSide, bottomDimsInside[2] - wall, screw_hole_diam, screw_thread_diam);
        translate(pillarNW) screwPillar(screwSide, bottomDimsInside[2] - wall, screw_hole_diam, screw_thread_diam);
        translate(pillarNE) screwPillar(screwSide, bottomDimsInside[2] - wall, screw_hole_diam, screw_thread_diam);
        
        // Grooves for PCB
        translate([pillarSE[0] - pcb_thickness - (wall * 1.5) - (screw_thread_diam / 2), wall, 0]) 
            cube([wall/2, wall*1.5, bottomDimsInside[2]]);
        translate([pillarNE[0] - pcb_thickness - (wall * 1.5) - (screw_thread_diam / 2), bottomDimsInside[1] - wall/2, 0]) 
            cube([wall/2, wall*1.5, bottomDimsInside[2]]);
    }
    
}

module sideRidge(redux = 0) {
    ridgeSize = wall * 0.5;
    rotate([0, 45, 0]) cube([ridgeSize, pcbL - 7 * wall - redux, ridgeSize], true);
}

module sideRidge2(redux = 0) {
    ridgeSize = wall * 0.5;
    rotate([0, 45, 0]) cube([ridgeSize, pcbW - 7 * wall - redux, ridgeSize], true);
}

module flatHeadM2(length) {
    headMax = 3.75;
    headMin = 2.2;
    headHeight = 2.01;
    cylinder(d1 = headMax, d2 = headMin, h = headHeight + 0.01);
    translate([0, 0, headHeight]) cylinder(d = headMin, h = length - headHeight);
}

module bezelCube(dims, center = false, camfer = 1.0) {
    width  = dims[0];
    height = dims[1];
    length = dims[2];
    radius = 0.01;
    translate([width, height, length]) {
        rotate([-90, 0, 180]) {
            hull() {
                // lower plane
                translate([0, 0, 0]) sphere(r = radius);
                translate([ width, 0, 0]) sphere(r = radius);
                translate([0,  length, 0]) sphere(r = radius); 
                translate([ width,  length, 0]) sphere(r = radius); 
                // upper plane
                translate([- camfer, - camfer, height]) sphere(r = radius);
                translate([ width + camfer, - camfer, height]) sphere(r = radius);
                translate([- camfer,  length + camfer, height]) sphere(r = radius); 
                translate([ width + camfer,  length + camfer, height]) sphere(r = radius); 
            }
        }
    }
}

module screwPillar(screwSide = false, height, insideDiamHole, insideDiamThread) {
    //insideDiam = 2.6;
    outsideDiam = insideDiamHole + 1.5 * wall;
    difference() {
        union(){
            cylinder(d = outsideDiam, h = height) ;
            //cylinder(h = height * 0.15, r2 = (outsideDiam/2), r1 = outsideDiam*0.7);
            }
        translate([0, 0, -0.01]) {
            if (screwSide) {
                cylinder(d = insideDiamHole, h = height + 0.02);
            } else {
                cylinder(d = insideDiamThread, h = height + 0.02);
            }
        }
    }
    // Extra support at base
    
}

// Make the not-screw side
translate([10 - wall, 0 - wall, 0]) bottom();
// Make the screw side and scale it
translate([-pcbW - 10 - wall, 0 - wall, 0])  bottom(true); 






