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