// License: CC0
//
// To the extent possible under law, Clementine Computing LLC has waived
// all copyright and related or neighboring rights to this file.
//
// This work is published from: United States.
//

include <PopufareHousingCommon.scad>

module PopufareHousingSideRightPlate() {
  w = POPUFARE_HOUSING_DEPTH;
  h = POPUFARE_HOUSING_HEIGHT;
  
  br = POPUFARE_BRACKET_D/2.0;
  bx = POPUFARE_BRACKET_CX;
  de = POPUFARE_BRACKET_DE;

  des = POPUFARE_BRACKET_DE_SHORT;
  
  dh = POPUFARE_HOUSING_HEIGHT - POPUFARE_FRONT_HEIGHT;
  alen = dh*sqrt(2.0);
  w_right = w - dh;
  
  spkr_dw = POPUFARE_SPEAKER_HOLE_WIDTH;

  lip = _MATERIAL_THICKNESS + 0.125;

  difference() {
    union() {
      square([w,h], center=true);
      
      // bootom skirt to cover under plate
      //
      translate([0,-(h/2 + lip/2)]) square([w,lip], center=true);
      
      translate([w/2-dh,h/2 + lip/2])
      difference() {
        square([lip, lip], center=true);
        translate([lip,-lip])  rotate(45, [0,0,1]) square([3*lip, 3*lip]);
      }
      
      translate([w/2,h/2 - dh])
      difference() {
        square([lip, lip], center=true);
        translate([lip,-lip])  rotate(45, [0,0,1]) square([3*lip, 3*lip]);
      }
    }
    
    // chop off angle
    //
    translate([ w/2,-(h/2-POPUFARE_FRONT_HEIGHT)]) rotate(-45, [0,0,1])
      translate([-4*w,0]) square([4*w,4*w]);
    
    // bottom bracket
    //
    translate([-(w/2-de), -(h/2-bx) ]) circle(br);
    translate([ (w/2-de), -(h/2-bx) ]) circle(br);
    
    // front bracket
    //
    translate( [ (w/2-bx), -(h/2 - POPUFARE_FRONT_HEIGHT/2) ]) circle(br);
    
    // front angle bracket
    //
    translate([ w/2, -(h/2-POPUFARE_FRONT_HEIGHT)]) rotate(-45, [0,0,1])
      translate([-de/2, -bx]) circle(br);
    translate([ w/2, -(h/2-POPUFARE_FRONT_HEIGHT)]) rotate(-45, [0,0,1])
      translate([-(alen - de/2), -bx]) circle(br);
      
    // top bracket
    //
    translate([ -(w/2  - des), (h/2 - bx) ]) circle(br);
    translate([ -w/2 + w_right - des, (h/2 - bx) ]) circle(br);
    
    // no back bracket
    //

    // speaker access and mounting screw holes
    //
    spkr_cx = -30;
    spkr_cy = 0;
    spkr_r = 2*spkr_dw/6;
    offset_x = 4.5;
    _w = 2.5;
    translate([spkr_cx,spkr_cy]) rotate(45, [0,0,1]) rounded_rect(_w,2*spkr_r, _w/2);
    translate([spkr_cx,spkr_cy]) rotate(45, [0,0,1]) translate([offset_x,0]) rounded_rect(_w,2*sqrt(spkr_r*spkr_r - offset_x*offset_x), _w/2);
    translate([spkr_cx,spkr_cy]) rotate(45, [0,0,1]) translate([2*offset_x,0]) rounded_rect(_w,2*sqrt(spkr_r*spkr_r - (2*2*offset_x*offset_x)), _w/2);
    //translate([spkr_cx,spkr_cy]) rotate(45, [0,0,1]) translate([3*offset_x,0]) rounded_rect(_w,2*sqrt(spkr_r*spkr_r - (3*3*offset_x*offset_x)), _w/2);
    
    translate([spkr_cx,spkr_cy]) rotate(45, [0,0,1]) translate([-offset_x,0]) rounded_rect(_w,2*sqrt(spkr_r*spkr_r - offset_x*offset_x), _w/2);
    translate([spkr_cx,spkr_cy]) rotate(45, [0,0,1]) translate([-2*offset_x,0]) rounded_rect(_w,2*sqrt(spkr_r*spkr_r - (2*2*offset_x*offset_x)), _w/2);
    //translate([spkr_cx,spkr_cy]) rotate(45, [0,0,1]) translate([-3*offset_x,0]) rounded_rect(_w,2*sqrt(spkr_r*spkr_r - (3*3*offset_x*offset_x)), _w/2);
    
    translate([spkr_cx - spkr_dw/2,spkr_cy]) circle(POPUFARE_SPEAKER_HOLE_R);
    translate([spkr_cx + spkr_dw/2,spkr_cy]) circle(POPUFARE_SPEAKER_HOLE_R);
    
  }
}

//PopufareHousingSideRightPlate();
