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