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