use <Material.scad>;
use <../../WebWeaver/scad/Util.scad>;

$fs = 0.35;
$fa = 2 * $fs;

$str = 3;
$E   = 0.01;  // ε

// note: using constant functions here because they get imported via `use`

function pcb_t() = 1.6;

function pcb_w() = 75.5;
function pcb_h() = 65.0;

function pcb_edge_r() = 3;

function M3() = 3.2;

// x is all the same and y is +- 18.5 and 0 as there are three of them
function terminals_x() = 28.75;
function terminals_y() = 18.50;

// tpow = power terminal
function tpow_x()  = -30.25;
function tpow_y()  =  14.50;
// function tpow_sx() =  11.00;
// function tpow_sy() =  11.50;
// function tpow_sz() =   3.00;  // TODO: measure!!

tled_y = [18.5, 0, -18.5];
function tled_x() = 5.75 + 23;
function tled_y(i) = tled_y(i);

function sc_left_off() = 5.0;  // left side screw holes offset from edge

// co = cut-out
co_w =   6.1;
co_h =  18.0;
co_x = -34.7;
co_y =  -8.6;



module pcbScrewHoles(tz=0) {
  for (tx=[-32.75, 5.75], ty=[-27.5, 27.5]) 
    translate([tx, ty, tz])
      children();
}

module pcbOutline() {

  difference() {
    roundRect(pcb_w(), pcb_h(), pcb_edge_r(), center=true);
    
    pcbScrewHoles() circle(d=M3());
    
    translate([co_x, co_y, 0]) square([co_w + $E, co_h], center=true);
  }
}

module pcb3D(pcb_t=pcb_t()) {
  FR4()
    linear_extrude(pcb_t())
      pcbOutline();

  if ($preview) {

    pcbScrewHoles()
      Solder()
        difference() {
          translate([0, 0, -$E]) cylinder(d=M3()*2, h=pcb_t()+2*$E);
          translate([0, 0, -$E*2]) cylinder(d=M3()-$E,   h=pcb_t()+4*$E);
        }

//    Solder()
//      translate([tpow_x(), tpow_y(), pcb_t() - $E])
//        cube([tpow_sx(), tpow_sy(), tpow_sz()], center=true);
//    translate([tpow_x(), tpow_y(), pcb_t() - $E])
//      terminal2();


    translate([tpow_x(), tpow_y(), pcb_t()])
      terminal2();

    for (ty=tled_y)
      translate([tled_x(), ty, pcb_t()])
        rotate([0, 0, 180])
          terminal3();


  }
}


// MP = measure precisely!

t_h0 =  9.5;  // TODO: MP!
t_h1 = 14.0;  // TODO: MP!
t_l_sx = 5.5;  // TODO: MP!  // l = lever
t_oh = 1.5;  // oh = overhang

t2_sx = 11.0 + t_oh;
t2_sy = 11.5;
t2_l_sy = 8.5;  // TODO: MP!
module terminal2() {
  GreyPlastic() difference() {
    union() {
      translate([-t_oh/2, 0, 0])
        linear_extrude(t_h0)
          roundRect(t2_sx, t2_sy, 0.69, center=true);

      translate([-t_oh/2, 0, 0])
        linear_extrude(t_h1)
          roundRect(t_l_sx, t2_l_sy, 1.337, center=true);
    }

    _ty = t2_sy * 1/4;
    for (ty=[-_ty, _ty])
      translate([-t2_sx/2, ty, t_h0/2])
        rotate([0, -90, 0])
          cylinder(d=3, h=8, center=true);
  }
}

t3_sx = 11.0 + t_oh;
t3_sy = 16.0;
t3_l_sy = 13.5;  // TODO: MP!
module terminal3() {
  GreyPlastic() difference() {
    union() {
      translate([-t_oh/2, 0, 0])
        linear_extrude(t_h0)
          roundRect(t3_sx, t3_sy, 0.69, center=true);

      translate([-t_oh/2, 0, 0])
        linear_extrude(t_h1)
          roundRect(t_l_sx, t3_l_sy, 1.337, center=true);
    }

    _ty = t3_sy * 1/3;
    for (ty=[-_ty, 0, _ty])
      translate([-t3_sx/2, ty, t_h0/2])
        rotate([0, -90, 0])
          cylinder(d=3, h=8, center=true);
  }
}

pcb3D();