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