chiark / gitweb /
3f47c9f173a1f2aab61f9fb6ebf4bc9bf121dd7e
[reprap-play.git] / fairphone-case.scad
1 // -*- C -*-
2
3 include <utils.scad>
4
5 phone = [ 75.0, 145.0 ];
6
7 prop_buildout_less = 3;
8
9 prop_angles = [ 15, 30, 45, 60 ];
10
11 bumper = [ 0.250, -0.025 ];
12 // ^ One side.  Overall size is increased by twice this.
13 // If no bumpers, is the gap around the phone.
14
15 enable_support = 1;
16
17 phone_cnr_rad = 6.0;
18
19 button_cutout_depth = 9;
20
21 phone_edge_thick = 9.0;
22 phone_total_thick = 12.0;
23 phone_backside_slope_inner = 1.5; // larger means shallower
24 phone_backside_slope_outer = 1.0; // larger means shallower
25
26 camera_pos_tl = [  6.450, 12.750 ]; // measured from tl corner
27 camera_pos_br = [ 22.300, 37.600 ]; // tl/br as seen from back
28
29 jack_pos = [ 14.38, 7.96 ];
30 jack_dia = 10.64 + .5; // some jack I had lying around
31
32 noisecancelmic_pos = [ 19.54, 7.37 ];   // from rhs
33 noisecancelmic_dia = 4.00;
34
35 //fingerpushhole_dias = [ 15, 18 ];
36 fingerpushhole_dias = [];
37
38 rearspeaker_pos_bl = [ 12.64, 18.72 ];
39 rearspeaker_size   = [  3.76,  7.36 ];
40
41 microusb_above = 3.27 - 0.25;
42 microusb_below = 0.0;
43 microusb_width = 16.12 + 1.25;
44
45 case_th_bottom = 2.5;
46 case_th_lid = 2.5;
47 case_th_side = 2;
48 case_th_lip = 1.2;
49
50 case_struts_count = 6;
51 case_struts_solid_below = 1.00;
52 case_struts_solid_above = 0.75;
53 case_struts_width = 0.10;
54
55 keeper_th_z = 0.75;
56 keeper_th_x = 0.75;
57 keeper_inner_width = 2.75;
58 keeper_inner_height = 2.75;
59 keeper_slant_slope = 2; // larger means steeper
60
61 keeper_gap_z_top = 0.25;
62 keeper_gap_z_bot = 0.75;
63 keeper_gap_x     = 0.25;
64 keeper_gap_x_holes = 0.75;
65
66 keeper_side = 0; // 0 = lhs; 1 = rhs
67
68 case_lip = 1.25;
69
70 lid_gap_x = 0.25;
71 lid_gap_z = 0.25;
72 lid_lip = 1.75;
73
74 catch_slop = 0.50;
75
76 foldover_gap = 0.50;
77 foldover_lever_gap = 0.50;
78
79 hingescrew_shaft_dia = 2.0 + 0.25; // M2 x 12mm machine screw
80 hingescrew_shaft_len = 12;
81 hingescrew_nut_thick = 1.93 + 0.20; // incl. washer
82 hingescrew_nut_dia = 4.72 + 0.50; // washer, actually
83 hingescrew_head_th = 1.38 + 0.75;
84 hingescrew_head_dia = 3.92;
85 lever_cover_th = 0.75;
86 hingemount_th = 2.5;
87
88 $fa = 5;
89 $fs = 0.1;
90
91 button_l_fudge = 4.4;
92 buttonishleg_default_l_is_fudge = 10;
93
94 hinge_base_slope = 1.5; // bigger is steeper
95
96 strut_min_at_end = 1.5;
97
98 hinge_x_gap = 0.125;
99 hinge_x_postscrew_gap = 0.75;
100 hinge_x_arms_gap = 0.35;
101 hinge_r_arms_gap = 0.55;
102
103 rearspeaker_gap    = [ 2.0, 2.0 ]; // each side
104
105 catch_len = 7.5;
106 catch_width = 15;
107 catch_thickness = 1.0;
108 catch_side_gap = 0.75; // each side
109
110 catch_depth = 0.75;
111 catch_height = 0.35;
112 catch_finger_height = 1.5;
113 catch_finger_depth = 2.5;
114
115 prop_recess_under = 0.50;
116 prop_recess_slop = 0.200; // each side
117 prop_end_dia = 0.5;
118 prop_main_th = 3;
119 prop_taper_len = 6;
120 prop_main_width = 3;
121 prop_side_gap = 0.75; // each side
122 prop_caserecess_behind = 0.75;
123 prop_caserecess_taper = 0.35; // one side only
124 prop_prop_gap = 0.5;
125 prop_prong_heel_slope = 1.0;
126
127 // ---------- calculated ----------
128
129 phone_width =  (phone + bumper*2)[0];
130 phone_height = (phone + bumper*2)[1];
131
132 inside_br = [phone_width, -phone_height];
133
134 prop_prong_h = prop_main_th;
135
136 //echo(camera_pos_tl + bumper,
137 //     camera_pos_br + bumper);
138
139 // ----- could be changed -----
140 lid_buttoncover_gap = lid_gap_x;
141 lid_buttoncover_overlap = case_th_lip + keeper_gap_z_top;
142
143 phone_backside_slope_thick = phone_total_thick - phone_edge_thick;
144
145 //prop_lidrecess_depth = case_th_lid - prop_recess_under;
146
147 //prop_nose_len = case_th_lid - prop_recess_under;
148 //prop_recess_slope = tan(prop_max_angle); // bigger means steeper
149 //prop_recess_width = prop_main_th / cos(prop_max_angle) + prop_backfwd_gap;
150
151
152 //lid_lip_overlap_width xxx bad name = ;
153 //lid_lip_inner_slope = [ 5, 5 ]; // xxx
154
155 epp0 = [0,0];
156 epp1 = [0, -phone_edge_thick];
157 epp2i = epp1 + phone_backside_slope_thick * [ phone_backside_slope_inner, -1 ];
158 epp2o = epp1 + phone_backside_slope_thick * [ phone_backside_slope_outer, -1 ];
159 epp3 = epp2i + [10, 0];
160 epp5 = epp0 + [0,1] * (keeper_th_z + keeper_gap_z_top + case_lip);
161 epp4 = epp5 + [-1,0] * case_th_side;
162
163 kppe = [0,0];
164 kppd = kppe + [1,0] * keeper_inner_width;
165 kppc = kppd + [0,1] * keeper_th_z;
166 kppb = [ kppe[0] - keeper_th_x, kppc[1] ];
167 kppf = kppe - [0,1] * keeper_inner_height;
168 kppa = [ kppb[0], kppf[1] ];
169
170 lpp10 = [ epp5[0] + lid_gap_x, kppc[1] + lid_gap_z ];
171 lpp11 = [ lpp10[0],            epp5[1] + lid_gap_z ];
172
173 lp_r12 = case_th_lid - (lpp11[1] - lpp10[1]);
174
175 lpp12 = [ epp4[0] + lp_r12,    lpp11[1] ];
176 lpp13 = [ lpp12[0],            lpp12[1] + lp_r12 ];
177
178 case_bottom_z = epp2o[1] - case_th_bottom;
179
180 // button profile
181 bppM = epp4 + [0,5];
182 bppN = [ 0.5 * (epp0[0] + epp4[0]), bppM[1] ];
183 bppR = [ bppN[0] + lid_buttoncover_gap, -button_cutout_depth ];
184 bppS = [ epp1[0], bppR[1] ];
185 bppQ = [ bppM[0], bppR[1] - lid_buttoncover_overlap ];
186 bppP = bppQ + [0,1] * lid_buttoncover_gap;
187 bppO = [ bppN[0], bppP[1] ];
188 bppL = lpp10 + [5,0];
189 bppK = [ bppL[0], bppN[1] ];
190 bppJ = [ bppN[0], bppL[1] ];
191
192 // hinge plan
193 hp_rn = hingescrew_nut_dia/2;
194 hp_r2_min = hp_rn + lever_cover_th;
195 hp_rs = hingescrew_shaft_dia/2;
196 hp_r1_min = hp_rs + hingemount_th;
197
198 hp_r1 = max(hp_r1_min, hp_r2_min);
199 hp_r2 = hp_r1;
200
201 hppU = lpp13;
202 hppS = epp2o + [0,-1] * case_th_bottom;
203 hp_k = 0.5 * (hppU[1] - hppS[1] + foldover_gap);
204
205 hppM = [ epp4[0] - foldover_lever_gap - hp_r2,
206          0.5 * (hppU + hppS)[1] ];
207 hppT = [ hppM[0], hppU[1] - hp_r1 ];
208 hppB = hppT + [0,-1] * hp_k;
209
210 hppE_y = epp2o[1] - case_th_bottom + hp_r1;
211 hppE_x = hppB[0] + (hppB[1] - hppE_y) * hinge_base_slope;
212 hppE = [ hppE_x, hppE_y ];
213
214 // hinge elevation x coords
215
216 hingescrew_portion_len =
217   0.5* (hingescrew_shaft_len - hingescrew_nut_thick - hinge_x_gap);
218
219 hex20 = max(epp2o[0],
220             phone_cnr_rad,
221             kppd[0] + hingescrew_head_th + keeper_gap_x_holes);
222 hex21 = hex20 + hingescrew_portion_len;
223 hex22 = hex21 + hinge_x_gap;
224 hex23 = hex22 + hingescrew_portion_len
225   - hingescrew_nut_thick; // bodge, need to divvy this up more sensibly
226 hex24 = hex20 + hingescrew_shaft_len + hinge_x_postscrew_gap;
227 //echo(hex20, hex21, hex22, hex23, hex24);
228 //echo(hingescrew_portion_len);
229
230 // catch
231
232 cppJ = [ epp4[0] + catch_thickness, lpp10[1] ];
233 cppA = cppJ + [lid_gap_x, -lid_gap_z];
234 cppB = [ epp0[0], cppA[1] ];
235 cppP = [ epp4[0], cppJ[1] ];
236
237 cppS = cppJ + [0,-1] * catch_len;
238 cppD = [ cppA[0], cppS[1] + catch_slop ];
239 cppC = [ cppB[0], cppD[1] ];
240 cppT = cppS + [1,0] * catch_depth;
241 cppU = cppT + [0,-1] * catch_height;
242 cppV = [ cppS[0], cppU[1] - catch_depth ];
243
244 cppR = 0.5*(cppP + cppJ);
245
246 cp_rQ = 0.5 * (cppJ[0] - cppP[0]);
247 cppQ = [ cppR[0],
248          cppV[1] - (catch_finger_height - cp_rQ) ];
249 cppF = [ cppV[0] + catch_finger_depth, cppC[1] ];
250
251 // prop recess in case
252
253 prop_x_pos = phone_width/2;
254
255 prop_recess_hw = 0.5 * prop_main_width + prop_side_gap;
256
257 prc_r1 = prop_end_dia/2;
258 prc_r3 = prc_r1 + prop_recess_slop;
259
260 prcp2 = [ epp4[0] + prop_buildout_less,
261           case_bottom_z ];
262
263 prop_caserecess_buildout_r = -1; // prcp2[0] - epp2o[0];
264
265 prcp1 = [ epp2o[0],
266           epp2i[1] - prc_r3 ];
267
268 // prop recess in lid
269
270 prl_r10 = prop_end_dia/2;
271 prl_r10o = prl_r10 + prop_recess_slop;
272
273 prlp10 = lpp10 + [1,1] * prl_r10o;
274
275 // prop
276
277 prpp10 = [0,0];
278 prpp11 = [0, prop_taper_len];
279
280 prp_r10 = prl_r10;
281
282 // ---------- modules ----------
283
284 module KeeperProfile(slant=0){
285   use_e = kppe + [0,-1] * slant * keeper_inner_width / keeper_slant_slope;
286   polygon([use_e, kppd, kppc, kppb, kppa, kppf]);
287 }
288
289 module EdgeProfile(){
290   difference(){
291     hull(){
292       translate(epp3) square(case_th_bottom*2, center=true);
293       circleat(epp2o, r=case_th_bottom);
294       circleat(epp1, r=case_th_side);
295       rectfromto(epp0, epp4);
296     }
297     polygon([ epp5 + [0,10],
298               epp1,
299               epp2i,
300               epp3 + [10,0] ]);
301   }
302 }
303
304 module LidEdgeProfile(){
305   polygon([ lpp10,
306             lpp11,
307             lpp12,
308             lpp13,
309             lpp13 + [10, 0],
310             lpp10 + [10, 0]
311             ]);
312   intersection(){
313     circleat(lpp12, r=lp_r12);
314     rectfromto( lpp12 + [-10,   0],
315                 lpp12 + [+10, +10] );
316   }
317 }
318
319 module ButtonCoverProfile(){
320   intersection(){
321     polygon([ bppM, bppP, bppO, bppJ, bppL, bppK ]);
322     hull(){
323       EdgeProfile();
324       LidEdgeProfile();
325     }
326   }
327 }
328
329 module ButtonPlan(l, deep, cut){
330   epsilon =
331     (cut  ? 0 : lid_buttoncover_gap);
332
333   delta =
334     (deep ? lid_buttoncover_overlap : 0);
335
336   C = [0,0]; // by definition
337   T = [ 0, epp4[1] ];
338   G = T + [0,10];
339
340   B0 = C + [0,-1] * button_cutout_depth;
341   B1 = B0 + [0,1] * epsilon;
342
343   r0 = 0.5 * (T[1] - B0[1]);
344   A = [  -(l + button_l_fudge)/2 + r0, 0.5 * (T[1] + B0[1]) ];
345   H = A + [0,-1] * delta;
346
347   D = A + [-2,0] * r0;
348   F = D + [0,10];
349
350   E0 = 0.5 * (D + A);
351   E1 = E0 + [1,0] * epsilon;
352
353   I0 = [ E0[0], H[1] ];
354   I1 = [ E1[0], H[1] ];
355
356   hull(){
357     for (m=[0,1]) mirror([m,0])
358       circleat(H, r0 - epsilon);
359   }
360   for (m=[0,1]) mirror([m,0]) {
361     difference(){
362       polygon([ E1,
363                 I1,
364                 H,
365                 B1,
366                 G,
367                 F,
368                 D
369                 ]);
370       circleat(D, r0 + epsilon);
371     }
372   }
373 }
374
375 module CatchCatchProfile(){
376   hull(){
377     for (c=[ cppR, cppQ ])
378       circleat(c, cp_rQ);
379   }
380   hull(){
381     circleat(lpp12, lp_r12);
382     circleat(lpp12 + [5,0], lp_r12);
383     rectfromto(cppP, cppP + [5,0.1]);
384   }
385   polygon([cppJ, cppS, cppT, cppU, cppV, cppQ, cppR]);
386 }
387
388 module CatchCutProfile(){
389   polygon([ cppB,
390             cppA,
391             cppD,
392             cppF,
393             cppF + [0,-10],
394             cppF + [-10,-10],
395             lpp12 + [-10,0],
396             lpp12 + [10,0]
397             ]);
398 }
399
400 module Flip_rhs(yn=[0,1]) {
401   for ($rhsflip=yn) {
402     translate([phone_width/2, 0, 0])
403       mirror([$rhsflip,0,0])
404       translate([-phone_width/2, 0, 0])
405       children();
406   }
407 }
408
409 module Flip_bot(yn=[0,1]) {
410   for ($botflip=yn) {
411     translate([0, -phone_height/2, 0])
412       mirror([0, $botflip, 0])
413       translate([0, phone_height/2, 0])
414       children();
415   }
416 }  
417
418 module AroundEdges(fill_zstart, fill_th, fill_downwards=0){
419   // sides
420   Flip_rhs(){
421     translate([0, -phone_cnr_rad, 0])
422       rotate([90,0,0])
423       linear_extrude(height = phone_height - phone_cnr_rad*2)
424       children(0);
425   }
426   // corners
427   Flip_rhs() Flip_bot() {
428     translate([+1,-1] * phone_cnr_rad)
429       intersection(){
430         rotate_extrude()
431           intersection(){
432             mirror([1,0,0])
433               translate([-1,0] * phone_cnr_rad)
434               children(0);
435             rectfromto([0,-20],[10,20]);
436           }
437         translate([-10, 0, -20] + 0.01 * [+1,-1, 0] )
438           cube([10,10,40]);
439       }
440   }
441   // top and bottom
442   Flip_bot(){
443     translate([ phone_width - phone_cnr_rad, 0,0 ])
444       rotate([90,0,-90])
445       linear_extrude(height = phone_width - phone_cnr_rad*2)
446       children(0);
447   }
448   // fill
449   translate([0,0, fill_zstart])
450     mirror([0,0, fill_downwards])
451     linear_extrude(height = fill_th)
452     rectfromto([+1,-1] * phone_cnr_rad,
453                [phone_width, -phone_height] + [-1,+1] * phone_cnr_rad);
454 }
455
456 module CaseAperture(pos, dia, $fn) {
457   theta = 180/$fn;
458   translate([ pos[0] + bumper[0],
459               -epp2i[0],
460               -pos[1] ])
461     rotate([-90, theta, 0])
462     cylinder(r = dia/2 / cos(theta),
463              h = 60);
464 }
465
466 module SideButton(y, y_ref_sign, l){
467   // y_ref_sign:
468   //   +1  measured from top    of actual phone to top    of button
469   //   -1  measured from bottom of actual phone to bottom of button
470   //    0  y is centre of button in coordinate system
471   $button_l= l;
472   eff_y = y_ref_sign > 0 ?         -bumper [1] -y -l/2 :
473           y_ref_sign < 0 ? (-phone -bumper)[1] +y +l/2 :
474           y;
475   //echo(eff_y);
476   translate([0, eff_y, 0])
477     children();
478 }
479
480 module LidButtonishLeg(y, y_ref_sign, l=buttonishleg_default_l_is_fudge) {
481   $button_leg_only = true;
482   SideButton(y, y_ref_sign, l) children();
483 }
484
485 module Buttons(){
486   Flip_rhs(1) SideButton(15.580, +1, 8.9) children(); // power
487   Flip_rhs(1) SideButton(48.700, -1, 8.920) children(); // camera
488   Flip_rhs(0) SideButton(30.800, +1, 21.96) children(); // volume
489   Flip_rhs(   ) LidButtonishLeg(14, -1) children();
490 //  Flip_rhs(0) LidButtonishLeg(20, +1, 20) children();
491 }
492
493 module Struts(x_start, z_min, th){
494   // if th is negative, starts at z_min and works towards -ve z
495   // and object should then be printed other way up
496   for (i= [1 : 1 : case_struts_count]) {
497     translate([0,
498                0,
499                z_min])
500       mirror([0,0, th<0 ? 1 : 0])
501       translate([0,
502                  -phone_height * i / (case_struts_count+1),
503                  case_struts_solid_below])
504       linear_extrude(height= abs(th)
505                      -(case_struts_solid_below+case_struts_solid_above))
506       rectfromto([               x_start, -0.5 * case_struts_width ],
507                  [ phone_width - x_start, +0.5 * case_struts_width ]);
508   }
509 }
510
511 module OrdinaryRearAperture(rhs,bot, pos){
512   Flip_rhs(rhs) Flip_bot(bot)
513     linextr(-20, 20)
514     mirror([0,1])
515     translate(pos + bumper)
516     children();
517 }
518
519 module MicroUSB(){
520   Flip_bot(1){
521     rotate([90,0,0])
522       mirror([0,0,1])
523       linextr(-epp2i[0], 60)
524       translate([0.5 * phone_width, 0, 0])
525       rectfromto([-microusb_width/2, epp2i[1] + microusb_below],
526                  [+microusb_width/2, epp0[1] + -microusb_above]);
527   }
528 }
529
530 module OrdinaryRearApertures(){
531   // rear speaker
532   OrdinaryRearAperture(1,1, rearspeaker_pos_bl)
533     rectfromto(-rearspeaker_gap,
534                rearspeaker_size + rearspeaker_gap);
535
536   // finger hole to remove phone
537   if (len(fingerpushhole_dias))
538     OrdinaryRearAperture(1,0, [ fingerpushhole_dias[0]/2 + epp2i[0],
539                                 phone[1]/2 ])
540     scale(fingerpushhole_dias)
541     circle(r= 0.5 );
542 }
543
544 module RearCameraAperture(){
545   Flip_rhs(1)
546     mirror([0, 0, 1])
547     linear_extrude(height = 20)
548     mirror([0, 1, 0])
549     translate(bumper)
550     rectfromto(camera_pos_tl, camera_pos_br);
551 }
552
553 module HingeLidProfile(){
554   hull(){
555     circleat(hppT, hp_r1);
556     circleat(lpp12, lp_r12);
557     polygon([lpp10,
558              lpp13 + [2,0],
559              lpp12,
560              hppT]);
561   }
562 }
563
564 module HingeBaseProfile(){
565   difference(){
566     hull(){
567       circleat(hppB, hp_r1);
568       circleat(hppE, hp_r1);
569       circleat(epp2o, case_th_bottom);
570       circleat(hppB + [10,0], hp_r1);
571     }
572     polygon([epp5, epp1, epp2i, epp3, bppL]);
573   }
574 }
575
576 module HingeLeverOuterProfile(){
577   hull(){
578     circleat(hppT, hp_r2);
579     circleat(hppB, hp_r2);
580   }
581 }
582
583 module HingeLeverInnerProfile(){
584   for (c = [hppT, hppB]) {
585     hull()
586       for (x=[-20,20])
587         for (y=[0, c[1] - hppM[1]])
588           translate([x,y])
589             circleat(c, hp_rn);
590   }
591 }
592
593 module Flip_hinge(){
594   hinge_origin = [0, -(phone_height - hppB[0]), hppB[1]];
595   translate(hinge_origin)
596     rotate([180,0,0])
597     translate(-hinge_origin)
598     children();
599 }
600
601 module HingePortion(x0,x1){
602   Flip_rhs() Flip_bot(1)
603     translate([x0,0,0])
604     mirror([1,0,0])
605     rotate([90,0,-90])
606     linear_extrude(height=x1-x0)
607     children(0);
608 }
609
610 module CatchPortion(width){
611   translate([phone_width/2, 0,0])
612     rotate([90,0,-90])
613     linextr(-width/2, width/2)
614     children(0);
615 }
616
617 module CaseBase(){
618   AroundEdges(epp3[1], case_th_bottom, 1)
619     EdgeProfile();
620 }
621
622 module PropProfile(gamma, cut=0){ ////toplevel
623   // https://en.wikipedia.org/wiki/Solution_of_triangles#Two_sides_and_the_included_angle_given_(SAS)
624   k = hp_k;
625   p = phone_height - prlp10[0] - hppB[0];
626   x = k / (2 * sin(gamma/2)) - hppT[0];
627   b = p + x;
628
629   q = phone_height - hppT[0]; // prpp7[0] is 0 by definition
630   a = q + x;
631   c = sqrt(a*a + b*b - 2*a*b*cos(gamma));
632   alpha = acos( (b*b + c*c - a*a) / (2*b*c) );
633
634   theta = 90 - alpha;
635   beta = 180 - alpha - gamma;
636   psi = 90 - beta + atan(prop_caserecess_taper);
637   prpv1 = [ sin(psi), cos(psi) ];
638
639   prpp7 = [0, c + (lpp13[1] - prpp10[1]) ];
640
641   prp_r1 = prc_r1;
642   prp_r11 = prop_main_th/2;
643
644   prpp1 = prpp7 + [1,0] *
645     // this is approximate, but will do
646     (prop_main_th/2 + prop_prop_gap + prcp1[0] - prcp1[1]);
647   prpp3 = prpp1 + [ -prp_r1,
648                     (prcp2[1] - prcp1[1]) - prop_prop_gap ];
649   prpp4 = [ prop_main_th/2, prpp3[1] ];
650   prp_r5 = prop_end_dia/2;
651   prpp5 = [ prpp1[0], prpp3[1] - prop_prong_h + prp_r5 ];
652   prpp6 = prpp4 + [0,-1] * (prop_prong_h +
653          prop_prong_heel_slope * (prpp5[0] - prpp4[0]));
654   prp_r8 = prop_main_th/2;
655   prpp8 = prpp4 + [0,-1] * prp_r8;
656   prpp9 = prpp8 + [-1,0] * prp_r8;
657   prpp12 = prpp3 + [0,1] * prop_end_dia;
658
659   if (!cut) {
660     hull(){
661       translate(prpp8)
662         intersection(){
663           circle(prp_r8);
664           rectfromto([-20,-0], [20,20]);
665         }
666       rectfromto(prpp6, prpp9);
667       circleat(prpp5, prp_r5);
668       rectfromto(prpp12 + [0,-0.1], prpp3);
669     }
670     hull(){
671       circleat(prpp1, prp_r1);
672       rectfromto(prpp12 + [0,-0.1], prpp3);
673     }
674   }
675   rotate([0,0, cut ? -theta : 0]){
676     hull(){
677       extra = cut ? prop_recess_slop : 0;
678       rectfromto(prpp6, prpp9);
679       circleat(prpp11, prp_r11 + extra);
680       circleat(prpp10, prp_r10 + extra);
681     }
682   }
683 }
684
685 module Case(){ ////toplevel
686   difference(){
687     union(){
688       CaseBase();
689
690       // ledge (fixed keeper)
691       Flip_rhs(1-keeper_side) intersection(){
692         rotate([90, 0, 0])
693           linear_extrude(height = phone_height + phone_cnr_rad * 2)
694           KeeperProfile(1);
695
696         // outline of the whole case, to stop it protruding
697         translate([0,0, -25])
698           linear_extrude(height = 50)
699           hull()
700           Flip_bot()
701           circleat([+1,-1] * phone_cnr_rad, phone_cnr_rad + case_th_side/2);
702       }
703
704       // hinge
705       HingePortion(hex20, hex21) HingeBaseProfile();
706
707       // buildout for prop recess
708       if (prop_caserecess_buildout_r > 0) Flip_rhs(1)
709         linextr(case_bottom_z, epp2i[1])
710         hull() {
711           for (dxs = [-1,+1])
712             circleat([ prop_x_pos + dxs * prop_caserecess_buildout_r,
713                        -epp2o[0] ],
714                      r = epp2o[0] - prcp2[0]);
715         }
716     }
717
718     // slot for keeper
719     Flip_rhs(keeper_side)
720       translate([0, -phone_cnr_rad, 0])
721       rotate([90, 0, 0])
722       linear_extrude(height = phone_height + phone_cnr_rad * 2)
723       minkowski(){
724         KeeperProfile();
725         rectfromto([ -keeper_gap_x,    -keeper_gap_z_bot ],
726                    [ keeper_gap_x_holes,    +keeper_gap_z_top ]);
727       }
728
729     // front camera
730     RearCameraAperture();
731
732     // struts (invisible, because they're buried in the case)
733     Struts(epp2i[0], epp2i[1] - case_th_bottom, case_th_bottom);
734
735     Buttons(){
736       mirror([1,0,0])
737         rotate([90,0,90]) {
738           if (!($button_leg_only && enable_support))
739           intersection(){
740             translate([0,0,-10])
741               linear_extrude(height= 20)
742               ButtonPlan($button_l, 0,1);
743             if ($button_leg_only)
744               rotate([-90,90,0])
745                 translate([phone_width/2, -400, kppe[1]])
746                 mirror([1-abs($rhsflip - keeper_side),0,0])
747                 cube([400, 800, 50]);
748             if (enable_support)
749               rotate([-90,90,0])
750               translate([-400, -400, kppd[1]])
751                 mirror([0,0,1])
752                 cube([800,800,100]);
753           }
754           translate([0,0, -bppR[0]])
755             linear_extrude(height= 20)
756             ButtonPlan($button_l, 1,1);
757         }
758       
759     }
760
761     // apertures along top edge
762     CaseAperture(jack_pos, jack_dia, 8);
763     Flip_rhs(1)
764       CaseAperture(noisecancelmic_pos, noisecancelmic_dia, 8);
765
766     OrdinaryRearApertures();
767
768     MicroUSB();
769
770     // gaps for the lid's hinge arms
771     HingePortion(hex20 - hinge_x_arms_gap,
772                  hex21 + hinge_x_arms_gap)
773       minkowski(){
774         HingeLidProfile();
775         circle(r= hinge_r_arms_gap, $fn= 8);
776       }
777
778     // screw holes in the hinge arms
779     HingeScrews();
780
781     // catch striker
782     CatchPortion(catch_width + catch_side_gap*2)
783       CatchCutProfile();
784
785     // prop recess
786     Flip_rhs(1)
787       translate([prop_x_pos,0,0])
788       rotate([90,0,90])
789       linextr(-prop_recess_hw, +prop_recess_hw)
790       mirror([1,0])
791       hull(){
792         for (d=[ [0,0], [0,-1], [+1,-1/prop_caserecess_taper] ])
793           circleat(prcp1 + 20*d,
794                    prc_r3);
795       }
796   }
797 }
798
799 module Lid(){ ////toplevel
800   difference(){
801     union(){
802       AroundEdges(lpp10[1], lpp13[1] - lpp10[1], 0)
803         LidEdgeProfile();
804
805       // button covers
806       Buttons(){
807         intersection(){
808           rotate([90,0,90])
809             translate([0,0,-10])
810             linear_extrude(height= 20)
811             ButtonPlan($button_l, 1,0);
812           rotate([90,0,0])
813              translate([0,0,-100])
814             linear_extrude(height= 200)
815             ButtonCoverProfile();
816         }
817       }
818
819       // hinge arms
820       HingePortion(hex20, hex21) HingeLidProfile();
821
822       // catch
823       CatchPortion(catch_width)
824         CatchCatchProfile();
825     }
826     Struts(lpp10[0] + strut_min_at_end, lpp13[1], -case_th_lid);
827
828     // screw holes in the hinge arms
829     HingeScrews();
830   }
831 }
832
833 module HingeLever(){ ////toplevel
834   difference() {
835     // outer body, positive
836     HingePortion(hex22, hex22 + phone_width/2)
837       HingeLeverOuterProfile();
838
839     // space for the screws
840     HingePortion(hex23, hex24)
841       HingeLeverInnerProfile();
842
843     // bores for the screws
844     HingeScrews();
845
846     // space for the charging cable
847     MicroUSB();
848     Flip_hinge() MicroUSB();
849   }
850 }
851
852 module HingeLeverPrint(){ ////toplevel
853   rotate([90,0,0])
854     HingeLever();
855 }
856
857 module TestSelectLength(){
858   translate([-30, -200, -20])
859     cube([30 + 15, 250, 40]);
860 }
861
862 module TestLength(){ ////toplevel
863   intersection(){
864     Case();
865     TestSelectLength();
866   }
867 }
868
869 module TestLengthRight(){ ////toplevel
870   intersection(){
871     Case();
872     Flip_rhs(1)
873       TestSelectLength();
874   }
875 }
876
877 module TestSelectWidth(){
878   translate([-30, -(phone_height - 25), -20])
879     mirror([0, 1, 0])
880     cube([200, 50, 40]);
881 }
882
883 module TestWidth(){ ////toplevel
884   intersection(){
885     Case();
886     TestSelectWidth();
887   }
888 }
889
890 module TestLidWidthPrint(){ ////toplevel
891   rotate([0,180.0]) intersection(){
892     Lid();
893     TestSelectWidth();
894   }
895 }
896
897 module TestSelectRearAperture(){
898   minkowski(){
899     union() children();
900     translate([20, 0,0])
901       cube([42, 2, 1], center=true);
902   }
903 }
904
905 module TestSelectCamera(){
906   minkowski(){
907     TestSelectRearAperture()
908       RearCameraAperture();
909     cube([0.1, 50, 0.1]);
910   }
911 }
912
913 module TestSelectOrdinaryRearApertures(){
914   TestSelectRearAperture()
915     OrdinaryRearApertures();
916 }
917
918 module TestCamera(){ ////toplevel
919   intersection(){
920     Case();
921     TestSelectCamera();
922   }
923 }
924
925 module TestLidByCamera(){ ////toplevel
926   intersection(){
927     Lid();
928     TestSelectCamera();
929   }
930 }
931
932 module TestLidByCameraPrint(){ ////toplevel
933   rotate([180,0,0]) TestLidByCamera();
934 }
935
936 module DemoByCamera(){ ////toplevel
937   color("blue") TestLidByCamera();
938   color("red")  TestCamera();
939 }
940
941 module OneKeeper(){ ////toplevel
942   translate([0, -phone_cnr_rad, 0])
943     rotate([90, 0, 0])
944     linear_extrude(height = phone_height - phone_cnr_rad * 2)
945     KeeperProfile();
946 }
947
948 module OneKeeperPrint(){ ////toplevel
949   rotate([0,180,0])
950     OneKeeper();
951 }
952
953 module LidPrint(){ ////toplevel
954   rotate([0,180,0])
955     Lid();
956 }
957
958 module TestSelectPropRecesses(posns){
959   linextr(-100,100){
960     translate([phone_width/2, -phone_height]){
961       square([ 3, 500 ], center=true);
962       for (y=posns)
963         hull()
964           for (dy=[ -prp5[0], -prp2[0] ])
965             translate([0,y+dy])
966               square([prop_main_width+5, 5], center=true);
967     }
968   }
969 }
970
971 module TestSelectFrame(){
972   include = [1,-1] * (epp2i[0] + 4);
973
974   difference(){
975     cube(1000, center=true);
976     translate([0,0, -100])
977       linear_extrude(height=200)
978       rectfromto(include,  inside_br - include);
979   }
980 }
981
982 module TestSelectLidFrame(){
983   TestSelectFrame();
984   TestSelectPropRecesses(prop_lid_posns);
985 }
986
987 module TestFrameCase(){ ////toplevel
988   intersection(){
989     Case();
990     union(){
991       TestSelectFrame();
992       TestSelectCamera();
993       TestSelectOrdinaryRearApertures();
994     }
995   }
996 }
997
998 module TestFrameLidPrint(){ ////toplevel
999   rotate([0,180,0]) intersection(){
1000     Lid();
1001     TestSelectLidFrame();
1002   }
1003 }
1004
1005 module ButtonPlanForDemo(z, deep, cut){
1006   translate([0,0,z])
1007     ButtonPlan(8, deep, cut);
1008 }
1009
1010 module HingeScrews(){
1011   Flip_rhs() Flip_bot(1){
1012     for (c= [ hppT, hppB ])
1013       translate([ hex20,
1014                   -c[0],
1015                   c[1] ]){
1016         rotate([0,90,0])
1017           translate([0,0,-.2])
1018           cylinder( r= hingescrew_shaft_dia/2,
1019                     h = hingescrew_shaft_len+0.2 );
1020         rotate([0,-90,0])
1021           translate([0,0,+.1])
1022           cylinder( r= hingescrew_head_dia/2, h = hingescrew_head_th );
1023       }
1024   }
1025 }
1026
1027 module DemoFrame(){ ////toplevel
1028   color("red") TestFrameCase();
1029   color("blue") intersection(){ Lid(); TestSelectLidFrame(); }
1030   color("black") HingeScrews();
1031   %HingeLever();
1032 }
1033
1034 module DemoHingedFrame(){ ///toplevel
1035   color("red") TestFrameCase();
1036   translate([0,0, -2*hp_k])
1037   color("blue") intersection(){ Lid(); TestSelectLidFrame(); }
1038
1039   Flip_hinge(){
1040     color("orange") HingeLever();
1041     color("black") HingeScrews();
1042   }
1043 }
1044
1045 module DemoHinge(){ ////toplevel
1046   DemoFrame();
1047   translate([0,0, -hp_k*3])
1048     DemoHingedFrame();
1049 }
1050
1051 module DemoProfiles(){ ////toplevel
1052   LidEdgeProfile();
1053   %EdgeProfile();
1054   KeeperProfile();
1055   translate([0,0,-1]) color("black") KeeperProfile(1);
1056
1057   translate([20,0]) {
1058     LidEdgeProfile();
1059     %EdgeProfile();
1060
1061     demopoint_QR = [ bppS[0], bppQ[1] - 0.1];
1062   
1063     color("blue") ButtonCoverProfile();
1064     color("red") {
1065       rectfromto(bppQ, demopoint_QR);
1066       rectfromto(bppR, demopoint_QR);
1067     }
1068   }
1069
1070   translate([-20,0]) {
1071     color("black") ButtonPlanForDemo(-2, 0,1);
1072     color("red" )  ButtonPlanForDemo(-4, 1,1);
1073     color("blue")  ButtonPlanForDemo(-6, 1,0);
1074   }
1075
1076   translate([0, -30]) {
1077     %LidEdgeProfile();
1078     %EdgeProfile();
1079     color("blue") HingeLidProfile();
1080     color("red")  HingeBaseProfile();
1081     color("black") translate([0,0,-2]) HingeLeverOuterProfile();
1082     color("orange") translate([0,0,-1]) HingeLeverInnerProfile();
1083   }
1084
1085   translate([20,-30]) {
1086     %EdgeProfile();
1087     %LidEdgeProfile();
1088     //translate([0,0,1]) CatchCutProfile();
1089     color("blue") CatchCatchProfile();
1090     color("red") difference(){ EdgeProfile(); CatchCutProfile(); }
1091   }
1092
1093   translate([40,-30]) {
1094     %LidEdgeProfile();
1095     PropProfile(10, 0);
1096   }
1097 }
1098
1099 //EdgeProfile();
1100 //KeeperProfile();
1101 //CaseBase();
1102 //%Case();
1103 //Keeper();
1104 //LidEdgeProfile();
1105 //KeeperProfile();
1106 //DemoProfiles();
1107 //PropRecess();