// License: CC0
//

FN=20;

hole_width = 162.5;
hole_height = 107.5;

bottom_plate_width = 90;
bottom_plate_height = 70;

rpi_center_x = -22;
rpi_center_y = -8;
rpi_hole_x_length = 58;
rpi_hole_y_length = 48;

m2r=1;
m3r=1.75;

m6r=3;

screen_width = 122;
screen_height = 76;

module rounded_rect(w,h,r) {
  _w = w-2*r;
  _h = h-2*r;
  
  x2 = _w/2;
  y2 = _h/2;
  
  hull() {
    translate([-x2, y2]) circle(r, $fn=FN);
    translate([ x2, y2]) circle(r, $fn=FN);
    translate([ x2,-y2]) circle(r, $fn=FN);
    translate([-x2,-y2]) circle(r, $fn=FN);
  }
}

module top_plate() {
  _outr = 16;
  _r = 3;
  
  w2 = hole_width/2;
  h2 = hole_height/2;
  difference() {
    rounded_rect(hole_width+_outr, hole_height+_outr, _r);
    

    translate([-w2, h2]) circle(m3r, $fn=FN);
    translate([ w2, h2]) circle(m3r, $fn=FN);
    translate([ w2+0.5,-h2+2.5]) circle(m3r, $fn=FN);
    translate([-w2+2.5,-h2+1.5]) circle(m3r, $fn=FN);
    square([screen_width,screen_height], center=true);
    
    // notch
    translate([-w2 + 30, h2+_outr/2]) rounded_rect(10,10,_r);
  }
}

module back_plate() {
  cx = rpi_center_x/2;
  cy = rpi_center_y/2;
  //x = bottom_plate_width;
  //y = bottom_plate_height;

  _outr = 16;
  w2 = hole_width/2;
  h2 = hole_height/2;
  x = hole_width + _outr;
  y = hole_height + _outr;
  
  first_hole_dx = 24.0;
  first_hole_dy = 4.0;
  
  first_hole_x = -(screen_width/2 - first_hole_dx);
  first_hole_y =  (screen_height/2 - first_hole_dy);
  
  _r = 3;
  
  sx2 = hole_width/2;
  sy2 = hole_height/2;
  difference() {
    rounded_rect(x,y,_r);
    
    translate([55,0]) rounded_rect(50,90,2);
    translate([-60,0]) rounded_rect(30,90,2);
    //translate([-40,35]) rounded_rect(70,40,2);
    //translate([ 40,35]) rounded_rect(70,40,2);
    //translate([ 41,-25]) rounded_rect(68,60,2);

    translate([first_hole_x, first_hole_y])
      translate([0,0]) circle(m2r, $fn=FN);
    
    translate([first_hole_x, first_hole_y])
      translate([rpi_hole_x_length,0]) circle(m2r, $fn=FN);
    translate([first_hole_x, first_hole_y])
      translate([rpi_hole_x_length,-rpi_hole_y_length]) circle(m2r, $fn=FN);
    translate([first_hole_x, first_hole_y])
      translate([0,-rpi_hole_y_length]) circle(m2r, $fn=FN);

    
    translate([sx2,sy2]) circle(m3r, $fn=FN);
    translate([sx2+0.5,-sy2+2.5]) circle(m3r, $fn=FN);
    translate([-sx2+2.5,-sy2+1.5]) circle(m3r, $fn=FN);
    translate([-sx2,sy2]) circle(m3r, $fn=FN);
    
    // notch
    translate([-w2 + 30, h2+_outr/2]) rounded_rect(10,10,_r);
  }
}

//back_plate();
//translate([0,150]) top_plate();
top_plate();