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