chiark / gitweb /
fairphone-case: wip hinge
[reprap-play.git] / fairphone-case.scad
1 // -*- C -*-
2
3 include <utils.scad>
4
5 phone = [ 75.0, 145.0 ];
6
7 bumper = [ 0.250, -0.025 ];
8 // ^ One side.  Overall size is increased by twice this.
9 // If no bumpers, is the gap around the phone.
10
11 phone_cnr_rad = 6.0;
12
13 button_cutout_depth = 9;
14
15 phone_edge_thick = 9.0;
16 phone_total_thick = 12.0;
17 phone_backside_slope_inner = 1.5; // larger means shallower
18 phone_backside_slope_outer = 1.0; // larger means shallower
19
20 camera_pos_tl = [  6.450, 12.750 ]; // measured from tl corner
21 camera_pos_br = [ 22.300, 37.600 ]; // tl/br as seen from back
22
23 jack_pos = [ 13.92, 7.96 ];
24 jack_dia = 9.1 + .5; // some jack I had lying around
25
26 noisecancelmic_pos = [ 19.54, 7.37 ];   // from rhs
27 noisecancelmic_dia = 1.75;
28
29 fingerpushhole_dias = [ 14, 17 ];
30
31 rearspeaker_pos_bl = [ 14.92, 18.72 ];
32 rearspeaker_size   = [  3.76,  7.36 ];
33
34 case_th_bottom = 2.5;
35 case_th_lid = 2.5;
36 case_th_side = 2;
37 case_th_lip = 1.2;
38
39 case_struts_count = 6;
40 case_struts_solid_below = 1.00;
41 case_struts_solid_above = 0.75;
42 case_struts_width = 0.10;
43
44 keeper_th_z = 0.75;
45 keeper_th_x = 0.75;
46 keeper_inner_width = 2.75;
47 keeper_inner_height = 2.75;
48 keeper_slant_slope = 2; // larger means steeper
49
50 keeper_gap_z_top = 0.25;
51 keeper_gap_z_bot = 0.75;
52 keeper_gap_x     = 0.25;
53 keeper_gap_x_holes = 0.75;
54
55 case_lip = 1.25;
56
57 lid_gap_x = 0.25;
58 lid_gap_z = 0.25;
59 lid_lip = 1.75;
60
61 foldover_gap = 0.50;
62 foldover_lever_gap = 0.50;
63
64 hingescrew_shaft_dia = 2.0 + 0.25; // M2 x 12mm machine screw
65 hingescrew_shaft_len = 12;
66 hingescrew_nut_thick = 1.93 + 0.20; // incl. washer
67 hingescrew_nut_dia = 4.72 + 0.50; // washer, actually
68 lever_cover_th = 0.75;
69 hingemount_th = 2.5;
70
71 $fa = 5;
72 $fs = 0.1;
73
74 button_l_fudge = 4.4;
75 buttonishleg_default_l_is_fudge = 10;
76
77 strut_min_at_end = 1.5;
78
79 hinge_x_gap = 0.125;
80 hinge_x_postscrew_gap = 0.75;
81
82 rearspeaker_gap    = [ 1.0, 1.0 ]; // each side
83
84 // ---------- calculated ----------
85
86 phone_width =  (phone + bumper*2)[0];
87 phone_height = (phone + bumper*2)[1];
88
89 inside_br = [phone_width, -phone_height];
90
91 //echo(camera_pos_tl + bumper,
92 //     camera_pos_br + bumper);
93
94 // ----- could be changed -----
95 lid_buttoncover_gap = lid_gap_x;
96 lid_buttoncover_overlap = case_th_lip + keeper_gap_z_top;
97
98 phone_backside_slope_thick = phone_total_thick - phone_edge_thick;
99
100 //lid_lip_overlap_width xxx bad name = ;
101 //lid_lip_inner_slope = [ 5, 5 ]; // xxx
102
103 epp0 = [0,0];
104 epp1 = [0, -phone_edge_thick];
105 epp2i = epp1 + phone_backside_slope_thick * [ phone_backside_slope_inner, -1 ];
106 epp2o = epp1 + phone_backside_slope_thick * [ phone_backside_slope_outer, -1 ];
107 epp3 = epp2i + [10, 0];
108 epp5 = epp0 + [0,1] * (keeper_th_z + keeper_gap_z_top + case_lip);
109 epp4 = epp5 + [-1,0] * case_th_side;
110
111 kppe = [0,0];
112 kppd = kppe + [1,0] * keeper_inner_width;
113 kppc = kppd + [0,1] * keeper_th_z;
114 kppb = [ kppe[0] - keeper_th_x, kppc[1] ];
115 kppf = kppe - [0,1] * keeper_inner_height;
116 kppa = [ kppb[0], kppf[1] ];
117
118 lpp10 = [ epp5[0] + lid_gap_x, kppc[1] + lid_gap_z ];
119 lpp11 = [ lpp10[0],            epp5[1] + lid_gap_z ];
120
121 lp_r12 = case_th_lid - (lpp11[1] - lpp10[1]);
122
123 lpp12 = [ epp4[0] + lp_r12,    lpp11[1] ];
124 lpp13 = [ lpp12[0],            lpp12[1] + lp_r12 ];
125
126 // button profile
127 bppM = epp4 + [0,5];
128 bppN = [ 0.5 * (epp0[0] + epp4[0]), bppM[1] ];
129 bppR = [ bppN[0] + lid_buttoncover_gap, -button_cutout_depth ];
130 bppS = [ epp1[0], bppR[1] ];
131 bppQ = [ bppM[0], bppR[1] - lid_buttoncover_overlap ];
132 bppP = bppQ + [0,1] * lid_buttoncover_gap;
133 bppO = [ bppN[0], bppP[1] ];
134 bppL = lpp10 + [5,0];
135 bppK = [ bppL[0], bppN[1] ];
136 bppJ = [ bppN[0], bppL[1] ];
137
138 // hinge plan
139 hp_rn = hingescrew_nut_dia/2;
140 hp_r2 = hp_rn + lever_cover_th;
141 hp_rs = hingescrew_shaft_dia/2;
142 hp_r1 = hp_rs + hingemount_th;
143
144 hppU = lpp13;
145 hppS = epp2o + [0,-1] * case_th_bottom;
146 hp_k = 0.5 * (hppU[1] - hppS[1] + foldover_gap);
147
148 hppM = [ epp4[0] - foldover_lever_gap - hp_r2,
149          0.5 * (hppU + hppS)[1] ];
150 hppT = hppM + 0.5 * [0,+1] * hp_k;
151 hppB = hppM + 0.5 * [0,-1] * hp_k;
152
153 // hinge elevation x coords
154
155 hingescrew_portion_len =
156   0.5* (hingescrew_shaft_len - hingescrew_nut_thick - hinge_x_gap);
157
158 hex20 = max(epp2o[0], phone_cnr_rad);
159 hex21 = hex20 + hingescrew_portion_len;
160 hex22 = hex21 - hinge_x_gap;
161 hex23 = hex22 + hingescrew_portion_len;
162 hex24 = hex23 + hinge_x_postscrew_gap;
163
164 // ---------- modules ----------
165
166 module KeeperProfile(slant=0){
167   use_e = kppe + [0,-1] * slant * keeper_inner_width / keeper_slant_slope;
168   polygon([use_e, kppd, kppc, kppb, kppa, kppf]);
169 }
170
171 module EdgeProfile(){
172   difference(){
173     hull(){
174       translate(epp3) square(case_th_bottom*2, center=true);
175       circleat(epp2o, r=case_th_bottom);
176       circleat(epp1, r=case_th_side);
177       rectfromto(epp0, epp4);
178     }
179     polygon([ epp5 + [0,10],
180               epp1,
181               epp2i,
182               epp3 + [10,0] ]);
183   }
184 }
185
186 module LidEdgeProfile(){
187   polygon([ lpp10,
188             lpp11,
189             lpp12,
190             lpp13,
191             lpp13 + [10, 0],
192             lpp10 + [10, 0]
193             ]);
194   intersection(){
195     circleat(lpp12, r=lp_r12);
196     rectfromto( lpp12 + [-10,   0],
197                 lpp12 + [+10, +10] );
198   }
199 }
200
201 module ButtonCoverProfile(){
202   intersection(){
203     polygon([ bppM, bppP, bppO, bppJ, bppL, bppK ]);
204     hull(){
205       EdgeProfile();
206       LidEdgeProfile();
207     }
208   }
209 }
210
211 module ButtonPlan(l, deep, cut){
212   epsilon =
213     (cut  ? 0 : lid_buttoncover_gap);
214
215   delta =
216     (deep ? lid_buttoncover_overlap : 0);
217
218   C = [0,0]; // by definition
219   T = [ 0, epp4[1] ];
220   G = T + [0,10];
221
222   B0 = C + [0,-1] * button_cutout_depth;
223   B1 = B0 + [0,1] * epsilon;
224
225   r0 = 0.5 * (T[1] - B0[1]);
226   A = [  -(l + button_l_fudge)/2 + r0, 0.5 * (T[1] + B0[1]) ];
227   H = A + [0,-1] * delta;
228
229   D = A + [-2,0] * r0;
230   F = D + [0,10];
231
232   E0 = 0.5 * (D + A);
233   E1 = E0 + [1,0] * epsilon;
234
235   I0 = [ E0[0], H[1] ];
236   I1 = [ E1[0], H[1] ];
237
238   hull(){
239     for (m=[0,1]) mirror([m,0])
240       circleat(H, r0 - epsilon);
241   }
242   for (m=[0,1]) mirror([m,0]) {
243     difference(){
244       polygon([ E1,
245                 I1,
246                 H,
247                 B1,
248                 G,
249                 F,
250                 D
251                 ]);
252       circleat(D, r0 + epsilon);
253     }
254   }
255 }
256
257 module Flip_rhs(yn=[0,1]) {
258   for ($rhsflip=yn) {
259     translate([phone_width/2, 0, 0])
260       mirror([$rhsflip,0,0])
261       translate([-phone_width/2, 0, 0])
262       children();
263   }
264 }
265
266 module Flip_bot(yn=[0,1]) {
267   for ($botflip=yn) {
268     translate([0, -phone_height/2, 0])
269       mirror([0, $botflip, 0])
270       translate([0, phone_height/2, 0])
271       children();
272   }
273 }  
274
275 module AroundEdges(fill_zstart, fill_th, fill_downwards=0){
276   // sides
277   Flip_rhs(){
278     translate([0, -phone_cnr_rad, 0])
279       rotate([90,0,0])
280       linear_extrude(height = phone_height - phone_cnr_rad*2)
281       children(0);
282   }
283   // corners
284   Flip_rhs() Flip_bot() {
285     translate([+1,-1] * phone_cnr_rad)
286       intersection(){
287         rotate_extrude()
288           intersection(){
289             mirror([1,0,0])
290               translate([-1,0] * phone_cnr_rad)
291               children(0);
292             rectfromto([0,-20],[10,20]);
293           }
294         translate([-10, 0, -20] + 0.01 * [+1,-1, 0] )
295           cube([10,10,40]);
296       }
297   }
298   // top and bottom
299   Flip_bot(){
300     translate([ phone_width - phone_cnr_rad, 0,0 ])
301       rotate([90,0,-90])
302       linear_extrude(height = phone_width - phone_cnr_rad*2)
303       children(0);
304   }
305   // fill
306   translate([0,0, fill_zstart])
307     mirror([0,0, fill_downwards])
308     linear_extrude(height = fill_th)
309     rectfromto([+1,-1] * phone_cnr_rad,
310                [phone_width, -phone_height] + [-1,+1] * phone_cnr_rad);
311 }
312
313 module CaseAperture(pos, dia, $fn) {
314   theta = 180/$fn;
315   translate([ pos[0] + bumper[0],
316               -epp2i[0],
317               -pos[1] ])
318     rotate([-90, theta, 0])
319     cylinder(r = dia/2 / cos(theta),
320              h = 60);
321 }
322
323 module SideButton(y, y_ref_sign, l){
324   // y_ref_sign:
325   //   +1  measured from top    of actual phone to top    of button
326   //   -1  measured from bottom of actual phone to bottom of button
327   //    0  y is centre of button in coordinate system
328   $button_l= l;
329   eff_y = y_ref_sign > 0 ?         -bumper [1] -y -l/2 :
330           y_ref_sign < 0 ? (-phone -bumper)[1] +y +l/2 :
331           y;
332   echo(eff_y);
333   translate([0, eff_y, 0])
334     children();
335 }
336
337 module LidButtonishLeg(y, y_ref_sign, l=buttonishleg_default_l_is_fudge) {
338   $button_leg_only = true;
339   SideButton(y, y_ref_sign, l) children();
340 }
341
342 module Buttons(){
343   Flip_rhs(1) SideButton(15.580, +1, 8.9) children(); // power
344   Flip_rhs(1) SideButton(48.700, -1, 8.920) children(); // camera
345   Flip_rhs(0) SideButton(30.800, +1, 21.96) children(); // volume
346   Flip_rhs(   ) LidButtonishLeg(20, -1) children();
347 //  Flip_rhs(0) LidButtonishLeg(20, +1, 20) children();
348 }
349
350 module Struts(x_start, z_min, th){
351   // if th is negative, starts at z_min and works towards -ve z
352   // and object should then be printed other way up
353   for (i= [1 : 1 : case_struts_count]) {
354     translate([0,
355                0,
356                z_min])
357       mirror([0,0, th<0 ? 1 : 0])
358       translate([0,
359                  -phone_height * i / (case_struts_count+1),
360                  case_struts_solid_below])
361       linear_extrude(height= abs(th)
362                      -(case_struts_solid_below+case_struts_solid_above))
363       rectfromto([               x_start, -0.5 * case_struts_width ],
364                  [ phone_width - x_start, +0.5 * case_struts_width ]);
365   }
366 }
367
368 module OrdinaryRearAperture(rhs,bot, pos){
369   Flip_rhs(rhs) Flip_bot(bot)
370     linextr(-20, 20)
371     mirror([0,1])
372     translate(pos + bumper)
373     children();
374 }
375
376 module OrdinaryRearApertures(){
377   // rear speaker
378   OrdinaryRearAperture(1,1, rearspeaker_pos_bl)
379     rectfromto(-rearspeaker_gap,
380                rearspeaker_size + rearspeaker_gap);
381
382   // finger hole to remove phone
383   OrdinaryRearAperture(1,0, [ fingerpushhole_dias[0]/2 + epp2i[0],
384                               phone[1]/2 ])
385     scale(fingerpushhole_dias)
386     circle(r= 0.5 );
387 }
388
389 module RearCameraAperture(){
390   Flip_rhs(1)
391     mirror([0, 0, 1])
392     linear_extrude(height = 20)
393     mirror([0, 1, 0])
394     translate(bumper)
395     rectfromto(camera_pos_tl, camera_pos_br);
396 }
397
398 module HingeLidProfile(){
399   hull(){
400     circleat(hppT, hp_r1);
401     circleat(lpp12, lp_r12);
402     polygon([lpp10,
403              lpp13 + [2,0],
404              lpp12,
405              hppT]);
406   }
407 }
408
409 module HingeBaseProfile(){
410   difference(){
411     hull(){
412       circleat(hppB, hp_r1);
413       circleat(epp2o, case_th_bottom);
414       circleat(hppB + [10,0], hp_r1);
415     }
416     polygon([epp5, epp1, epp2i, epp3, bppL]);
417   }
418 }
419
420 module HingeLeverOuterProfile(){
421   hull(){
422     circleat(hppT, hp_r2);
423     circleat(hppB, hp_r2);
424   }
425 }
426
427 module HingeLeverInnerProfile(){
428   for (c = [hppT, hppB]) {
429     hull()
430       for (x=[0,20])
431         translate([x,0])
432           circleat(c, hp_rn);
433         circleat(hppB, hp_rn);
434   }
435 }
436
437 module HingePortion(x0,x1){
438   Flip_rhs() Flip_bot(1)
439     translate([x0,0,0])
440     mirror([1,0,0])
441     rotate([90,0,-90])
442     linear_extrude(height=x1-x0)
443     children(0);
444 }
445
446 module CaseBase(){
447   AroundEdges(epp3[1], case_th_bottom, 1)
448     EdgeProfile();
449 }
450
451 module Case(){ ////toplevel
452   difference(){
453     union(){
454       CaseBase();
455
456       // ledge (fixed keeper)
457       intersection(){
458         rotate([90, 0, 0])
459           linear_extrude(height = phone_height + phone_cnr_rad * 2)
460           KeeperProfile(1);
461
462         // outline of the whole case, to stop it protruding
463         translate([0,0, -25])
464           linear_extrude(height = 50)
465           hull()
466           // Flip_rhs() // actually, we only care about the LH
467           Flip_bot()
468           circleat([+1,-1] * phone_cnr_rad, phone_cnr_rad + case_th_side/2);
469       }
470
471       // hinge
472       HingePortion(hex20, hex21) HingeBaseProfile();
473     }
474
475     // slot for keeper
476     Flip_rhs(1)
477       translate([0, -phone_cnr_rad, 0])
478       rotate([90, 0, 0])
479       linear_extrude(height = phone_height + phone_cnr_rad * 2)
480       minkowski(){
481         KeeperProfile();
482         rectfromto([ -keeper_gap_x,    -keeper_gap_z_bot ],
483                    [ keeper_gap_x_holes,    +keeper_gap_z_top ]);
484       }
485
486     // front camera
487     RearCameraAperture();
488
489     // struts (invisible, because they're buried in the case)
490     Struts(epp2i[0], epp2i[1] - case_th_bottom, case_th_bottom);
491
492     Buttons(){
493       mirror([1,0,0])
494         rotate([90,0,90]) {
495           intersection(){
496             translate([0,0,-10])
497               linear_extrude(height= 20)
498               ButtonPlan($button_l, 0,1);
499             if ($button_leg_only)
500               rotate([-90,90,0])
501                 translate([phone_width/2, -400, kppe[1]])
502                 mirror([$rhsflip,0,0]) cube([400, 800, 50]);
503           }
504           translate([0,0, -bppR[0]])
505             linear_extrude(height= 20)
506             ButtonPlan($button_l, 1,1);
507         }
508     }
509
510     // apertures along top edge
511     CaseAperture(jack_pos, jack_dia, 8);
512     Flip_rhs(1)
513       CaseAperture(noisecancelmic_pos, noisecancelmic_dia, 20);
514
515     OrdinaryRearApertures();
516   }
517 }
518
519 module Lid(){ ////toplevel
520   difference(){
521     union(){
522       AroundEdges(lpp10[1], lpp13[1] - lpp10[1], 0)
523         LidEdgeProfile();
524
525       // button covers
526       Buttons(){
527         intersection(){
528           rotate([90,0,90])
529             translate([0,0,-10])
530             linear_extrude(height= 20)
531             ButtonPlan($button_l, 1,0);
532           rotate([90,0,0])
533              translate([0,0,-100])
534             linear_extrude(height= 200)
535             ButtonCoverProfile();
536         }
537       }
538     }
539     Struts(lpp10[0] + strut_min_at_end, lpp13[1], -case_th_lid);
540   }
541 }
542
543 module TestSelectLength(){
544   translate([-30, -200, -20])
545     cube([30 + 15, 250, 40]);
546 }
547
548 module TestLength(){ ////toplevel
549   intersection(){
550     Case();
551     TestSelectLength();
552   }
553 }
554
555 module TestLengthRight(){ ////toplevel
556   intersection(){
557     Case();
558     Flip_rhs(1)
559       TestSelectLength();
560   }
561 }
562
563 module TestSelectWidth(){
564   translate([-30, -(phone_height - 25), -20])
565     mirror([0, 1, 0])
566     cube([200, 50, 40]);
567 }
568
569 module TestWidth(){ ////toplevel
570   intersection(){
571     Case();
572     TestSelectWidth();
573   }
574 }
575
576 module TestLidWidthPrint(){ ////toplevel
577   rotate([0,180.0]) intersection(){
578     Lid();
579     TestSelectWidth();
580   }
581 }
582
583 module TestSelectRearAperture(){
584   minkowski(){
585     union() children();
586     translate([20, 0,0])
587       cube([42, 2, 1], center=true);
588   }
589 }
590
591 module TestSelectCamera(){
592   minkowski(){
593     TestSelectRearAperture()
594       RearCameraAperture();
595     cube([0.1, 50, 0.1]);
596   }
597 }
598
599 module TestSelectOrdinaryRearApertures(){
600   TestSelectRearAperture()
601     OrdinaryRearApertures();
602 }
603
604 module TestCamera(){ ////toplevel
605   intersection(){
606     Case();
607     TestSelectCamera();
608   }
609 }
610
611 module TestLidByCamera(){ ////toplevel
612   intersection(){
613     Lid();
614     TestSelectCamera();
615   }
616 }
617
618 module TestLidByCameraPrint(){ ////toplevel
619   rotate([180,0,0]) TestLidByCamera();
620 }
621
622 module DemoByCamera(){ ////toplevel
623   color("blue") TestLidByCamera();
624   color("red")  TestCamera();
625 }
626
627 module OneKeeper(){ ////toplevel
628   translate([0, -phone_cnr_rad, 0])
629     rotate([90, 0, 0])
630     linear_extrude(height = phone_height - phone_cnr_rad * 2)
631     KeeperProfile();
632 }
633
634 module OneKeeperPrint(){ ////toplevel
635   rotate([0,180,0])
636     OneKeeper();
637 }
638
639 module LidPrint(){ ////toplevel
640   rotate([0,180,0])
641     Lid();
642 }
643
644 module TestSelectFrame(){
645   include = [1,-1] * (epp2i[0] + 4);
646
647   difference(){
648     cube(1000, center=true);
649     translate([0,0, -100])
650       linear_extrude(height=200)
651       rectfromto(include,  inside_br - include);
652   }
653 }
654
655 module TestFrameCase(){ ////toplevel
656   intersection(){
657     Case();
658     union(){
659       TestSelectFrame();
660       TestSelectCamera();
661       TestSelectOrdinaryRearApertures();
662     }
663   }
664 }
665
666 module TestFrameLidPrint(){ ////toplevel
667   rotate([0,0,180]) intersection(){
668     Lid();
669     TestSelectFrame();
670   }
671 }
672
673 module Keeper(){ ////toplevel
674   Flip_rhs()
675     OneKeeper();
676 }
677
678 module ButtonPlanForDemo(z, deep, cut){
679   translate([0,0,z])
680     ButtonPlan(8, deep, cut);
681 }
682
683 module DemoProfiles(){ ////toplevel
684   LidEdgeProfile();
685   %EdgeProfile();
686   KeeperProfile();
687   translate([0,0,-1]) color("black") KeeperProfile(1);
688
689   translate([20,0]) {
690     LidEdgeProfile();
691     %EdgeProfile();
692
693     demopoint_QR = [ bppS[0], bppQ[1] - 0.1];
694   
695     color("blue") ButtonCoverProfile();
696     color("red") {
697       rectfromto(bppQ, demopoint_QR);
698       rectfromto(bppR, demopoint_QR);
699     }
700   }
701
702   translate([-20,0]) {
703     color("black") ButtonPlanForDemo(-2, 0,1);
704     color("red" )  ButtonPlanForDemo(-4, 1,1);
705     color("blue")  ButtonPlanForDemo(-6, 1,0);
706   }
707
708   translate([0, -30]) {
709     %LidEdgeProfile();
710     %EdgeProfile();
711     color("blue") HingeLidProfile();
712     color("red")  HingeBaseProfile();
713     color("black") translate([0,0,-2]) HingeLeverOuterProfile();
714     color("orange") translate([0,0,-1]) HingeLeverInnerProfile();
715   }
716 }
717
718 //EdgeProfile();
719 //KeeperProfile();
720 //CaseBase();
721 //%Case();
722 //Keeper();
723 //LidEdgeProfile();
724 //KeeperProfile();
725 //DemoProfiles();