chiark / gitweb /
fairphone-case: Demos of hinges much improved
[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 = [ 13.83, 8.485 ];
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 = 4;
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.45; // 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 bppU = [ bppJ[0], lpp12[1] ];
193 bppV = lpp11;
194 bppW = lpp10;
195
196 // hinge plan
197 hp_rn = hingescrew_nut_dia/2;
198 hp_r2_min = hp_rn + lever_cover_th;
199 hp_rs = hingescrew_shaft_dia/2;
200 hp_r1_min = hp_rs + hingemount_th;
201
202 hp_r1 = max(hp_r1_min, hp_r2_min);
203 hp_r2 = hp_r1;
204
205 hppU = lpp13;
206 hppS = epp2o + [0,-1] * case_th_bottom;
207 hp_k = 0.5 * (hppU[1] - hppS[1] + foldover_gap);
208
209 hppM = [ epp4[0] - foldover_lever_gap - hp_r2,
210          0.5 * (hppU + hppS)[1] ];
211 hppT = [ hppM[0], hppU[1] - hp_r1 ];
212 hppB = hppT + [0,-1] * hp_k;
213
214 hppE_y = epp2o[1] - case_th_bottom + hp_r1;
215 hppE_x = hppB[0] + (hppB[1] - hppE_y) * hinge_base_slope;
216 hppE = [ hppE_x, hppE_y ];
217
218 // hinge elevation x coords
219
220 hingescrew_portion_len =
221   0.5* (hingescrew_shaft_len - hingescrew_nut_thick - hinge_x_gap);
222
223 hex20 = max(epp2o[0],
224             phone_cnr_rad,
225             kppd[0] + hingescrew_head_th + keeper_gap_x_holes);
226 hex21 = hex20 + hingescrew_portion_len;
227 hex22 = hex21 + hinge_x_gap;
228 hex23 = hex22 + hingescrew_portion_len
229   - hingescrew_nut_thick; // bodge, need to divvy this up more sensibly
230 hex24 = hex20 + hingescrew_shaft_len + hinge_x_postscrew_gap;
231 //echo(hex20, hex21, hex22, hex23, hex24);
232 //echo(hingescrew_portion_len);
233
234 // catch
235
236 cppJ = [ epp4[0] + catch_thickness, lpp10[1] ];
237 cppA = cppJ + [lid_gap_x, -lid_gap_z];
238 cppB = [ epp0[0], cppA[1] ];
239 cppP = [ epp4[0], cppJ[1] ];
240
241 cppS = cppJ + [0,-1] * catch_len;
242 cppD = [ cppA[0], cppS[1] + catch_slop ];
243 cppC = [ cppB[0], cppD[1] ];
244 cppT = cppS + [1,0] * catch_depth;
245 cppU = cppT + [0,-1] * catch_height;
246 cppV = [ cppS[0], cppU[1] - catch_depth ];
247
248 cppR = 0.5*(cppP + cppJ);
249
250 cp_rQ = 0.5 * (cppJ[0] - cppP[0]);
251 cppQ = [ cppR[0],
252          cppV[1] - (catch_finger_height - cp_rQ) ];
253 cppF = [ cppV[0] + catch_finger_depth, cppC[1] ];
254
255 // prop recess in case
256
257 prop_x_pos = phone_width/2;
258
259 prop_recess_hw = 0.5 * prop_main_width + prop_side_gap;
260
261 prc_r1 = prop_end_dia/2;
262 prc_r3 = prc_r1 + prop_recess_slop;
263
264 prcp2 = [ epp4[0] + prop_buildout_less,
265           case_bottom_z ];
266
267 prop_caserecess_buildout_r = -1; // prcp2[0] - epp2o[0];
268
269 prcp1 = [ epp2o[0] + prc_r3 + prop_caserecess_behind,
270           epp2i[1] - prc_r3 - prop_recess_under];
271
272 // prop recess in lid
273
274 prl_r10 = prop_end_dia/2;
275 prl_r10o = prl_r10 + prop_recess_slop;
276
277 prlp10 = lpp10 + [1,1] * prl_r10o
278   + [1,0] * prop_lidrecess_behind
279   + [0,1] * prop_recess_under;
280
281 // prop
282
283 $prpp10 = [0,0];
284 $prpp11 = [0, prop_taper_len];
285
286 $prp_r10 = prl_r10;
287
288 // ---------- modules ----------
289
290 module KeeperProfile(slant=0){
291   use_e = kppe + [0,-1] * slant * keeper_inner_width / keeper_slant_slope;
292   polygon([use_e, kppd, kppc, kppb, kppa, kppf]);
293 }
294
295 module EdgeProfile(){
296   difference(){
297     hull(){
298       translate(epp3) square(case_th_bottom*2, center=true);
299       circleat(epp2o, r=case_th_bottom);
300       circleat(epp1, r=case_th_side);
301       rectfromto(epp0, epp4);
302     }
303     polygon([ epp5 + [0,10],
304               epp1,
305               epp2i,
306               epp3 + [10,0] ]);
307   }
308 }
309
310 module LidEdgeProfile(){
311   polygon([ lpp10,
312             lpp11,
313             lpp12,
314             lpp13,
315             lpp13 + [10, 0],
316             lpp10 + [10, 0]
317             ]);
318   intersection(){
319     circleat(lpp12, r=lp_r12);
320     rectfromto( lpp12 + [-10,   0],
321                 lpp12 + [+10, +10] );
322   }
323 }
324
325 module ButtonCoverProfile(){
326   intersection(){
327     polygon(concat([ bppM, bppP, bppO, bppJ ],
328                    (enable_support ? [ bppU, bppV, bppW ] : []),
329                    [ bppL, bppK ]));
330     hull(){
331       EdgeProfile();
332       LidEdgeProfile();
333     }
334   }
335 }
336
337 module ButtonPlan(l, deep, cut){
338   epsilon =
339     (cut  ? 0 : lid_buttoncover_gap);
340
341   delta =
342     (deep ? lid_buttoncover_overlap : 0);
343
344   C = [0,0]; // by definition
345   T = [ 0, epp4[1] ];
346   G = T + [0,10];
347
348   B0 = C + [0,-1] * button_cutout_depth;
349   B1 = B0 + [0,1] * epsilon;
350
351   r0 = 0.5 * (T[1] - B0[1]);
352   A = [  -(l + button_l_fudge)/2 + r0, 0.5 * (T[1] + B0[1]) ];
353   H = A + [0,-1] * delta;
354
355   D = A + [-2,0] * r0;
356   F = D + [0,10];
357
358   E0 = 0.5 * (D + A);
359   E1 = E0 + [1,0] * epsilon;
360
361   I0 = [ E0[0], H[1] ];
362   I1 = [ E1[0], H[1] ];
363
364   hull(){
365     for (m=[0,1]) mirror([m,0])
366       circleat(H, r0 - epsilon);
367   }
368   for (m=[0,1]) mirror([m,0]) {
369     difference(){
370       polygon([ E1,
371                 I1,
372                 H,
373                 B1,
374                 G,
375                 F,
376                 D
377                 ]);
378       circleat(D, r0 + epsilon);
379     }
380   }
381 }
382
383 module CatchCatchProfile(){
384   hull(){
385     for (c=[ cppR, cppQ ])
386       circleat(c, cp_rQ);
387   }
388   hull(){
389     circleat(lpp12, lp_r12);
390     circleat(lpp12 + [5,0], lp_r12);
391     rectfromto(cppP, cppP + [5,0.1]);
392   }
393   polygon([cppJ, cppS, cppT, cppU, cppV, cppQ, cppR]);
394 }
395
396 module CatchCutProfile(){
397   polygon([ cppB,
398             cppA,
399             cppD,
400             cppF,
401             cppF + [0,-10],
402             cppF + [-10,-10],
403             lpp12 + [-10,0],
404             lpp12 + [10,0]
405             ]);
406 }
407
408 module Flip_rhs(yn=[0,1]) {
409   for ($rhsflip=yn) {
410     translate([phone_width/2, 0, 0])
411       mirror([$rhsflip,0,0])
412       translate([-phone_width/2, 0, 0])
413       children();
414   }
415 }
416
417 module Flip_bot(yn=[0,1]) {
418   for ($botflip=yn) {
419     translate([0, -phone_height/2, 0])
420       mirror([0, $botflip, 0])
421       translate([0, phone_height/2, 0])
422       children();
423   }
424 }  
425
426 module AroundEdges(fill_zstart, fill_th, fill_downwards=0){
427   // sides
428   Flip_rhs(){
429     translate([0, -phone_cnr_rad, 0])
430       rotate([90,0,0])
431       linear_extrude(height = phone_height - phone_cnr_rad*2)
432       children(0);
433   }
434   // corners
435   Flip_rhs() Flip_bot() {
436     translate([+1,-1] * phone_cnr_rad)
437       intersection(){
438         rotate_extrude()
439           intersection(){
440             mirror([1,0,0])
441               translate([-1,0] * phone_cnr_rad)
442               children(0);
443             rectfromto([0,-20],[10,20]);
444           }
445         translate([-10, 0, -20] + 0.01 * [+1,-1, 0] )
446           cube([10,10,40]);
447       }
448   }
449   // top and bottom
450   Flip_bot(){
451     translate([ phone_width - phone_cnr_rad, 0,0 ])
452       rotate([90,0,-90])
453       linear_extrude(height = phone_width - phone_cnr_rad*2)
454       children(0);
455   }
456   // fill
457   translate([0,0, fill_zstart])
458     mirror([0,0, fill_downwards])
459     linear_extrude(height = fill_th)
460     rectfromto([+1,-1] * phone_cnr_rad,
461                [phone_width, -phone_height] + [-1,+1] * phone_cnr_rad);
462 }
463
464 module CaseAperture(pos, dia, $fn) {
465   theta = 180/$fn;
466   translate([ pos[0] + bumper[0],
467               -epp2i[0],
468               -pos[1] ])
469     rotate([-90, theta, 0])
470     cylinder(r = dia/2 / cos(theta),
471              h = 60);
472 }
473
474 module SideButton(y, y_ref_sign, l){
475   // y_ref_sign:
476   //   +1  measured from top    of actual phone to top    of button
477   //   -1  measured from bottom of actual phone to bottom of button
478   //    0  y is centre of button in coordinate system
479   $button_l= l;
480   eff_y = y_ref_sign > 0 ?         -bumper [1] -y -l/2 :
481           y_ref_sign < 0 ? (-phone -bumper)[1] +y +l/2 :
482           y;
483   //echo(eff_y);
484   translate([0, eff_y, 0])
485     children();
486 }
487
488 module LidButtonishLeg(y, y_ref_sign, l=buttonishleg_default_l_is_fudge) {
489   $button_leg_only = true;
490   SideButton(y, y_ref_sign, l) children();
491 }
492
493 module Buttons(){
494   Flip_rhs(1) SideButton(15.580, +1, 8.9) children(); // power
495   Flip_rhs(1) SideButton(48.700, -1, 8.920) children(); // camera
496   Flip_rhs(0) SideButton(30.800, +1, 21.96) children(); // volume
497   Flip_rhs(   ) LidButtonishLeg(14, -1) children();
498 //  Flip_rhs(0) LidButtonishLeg(20, +1, 20) children();
499 }
500
501 module Struts(x_start, z_min, th){
502   // if th is negative, starts at z_min and works towards -ve z
503   // and object should then be printed other way up
504   for (i= [1 : 1 : case_struts_count]) {
505     translate([0,
506                0,
507                z_min])
508       mirror([0,0, th<0 ? 1 : 0])
509       translate([0,
510                  -phone_height * i / (case_struts_count+1),
511                  case_struts_solid_below])
512       linear_extrude(height= abs(th)
513                      -(case_struts_solid_below+case_struts_solid_above))
514       rectfromto([               x_start, -0.5 * case_struts_width ],
515                  [ phone_width - x_start, +0.5 * case_struts_width ]);
516   }
517 }
518
519 module OrdinaryRearAperture(rhs,bot, pos){
520   Flip_rhs(rhs) Flip_bot(bot)
521     linextr(-20, 20)
522     mirror([0,1])
523     translate(pos + bumper)
524     children();
525 }
526
527 module MicroUSB(){
528   Flip_bot(1){
529     rotate([90,0,0])
530       mirror([0,0,1])
531       linextr(-epp2i[0], 60)
532       translate([0.5 * phone_width, 0, 0])
533       rectfromto([-microusb_width/2, epp2i[1] + microusb_below],
534                  [+microusb_width/2, epp0[1] + -microusb_above]);
535   }
536 }
537
538 module OrdinaryRearApertures(){
539   // rear speaker
540   OrdinaryRearAperture(1,1, rearspeaker_pos_bl)
541     rectfromto(-rearspeaker_gap,
542                rearspeaker_size + rearspeaker_gap);
543
544   // finger hole to remove phone
545   if (len(fingerpushhole_dias))
546     OrdinaryRearAperture(1,0, [ fingerpushhole_dias[0]/2 + epp2i[0],
547                                 phone[1]/2 ])
548     scale(fingerpushhole_dias)
549     circle(r= 0.5 );
550 }
551
552 module RearCameraAperture(){
553   Flip_rhs(1)
554     mirror([0, 0, 1])
555     linear_extrude(height = 20)
556     mirror([0, 1, 0])
557     translate(bumper)
558     rectfromto(camera_pos_tl, camera_pos_br);
559 }
560
561 module HingeLidProfile(){
562   hull(){
563     circleat(hppT, hp_r1);
564     circleat(lpp12, lp_r12);
565     polygon([lpp10,
566              lpp13 + [2,0],
567              lpp12,
568              hppT]);
569   }
570 }
571
572 module HingeBaseProfile(){
573   difference(){
574     hull(){
575       circleat(hppB, hp_r1);
576       circleat(hppE, hp_r1);
577       circleat(epp2o, case_th_bottom);
578       circleat(hppB + [10,0], hp_r1);
579     }
580     polygon([epp5, epp1, epp2i, epp3, bppL]);
581   }
582 }
583
584 module HingeLeverOuterProfile(){
585   hull(){
586     circleat(hppT, hp_r2);
587     circleat(hppB, hp_r2);
588   }
589 }
590
591 module HingeLeverInnerProfile(){
592   for (s = [-1,+1]) {
593     c = s > 0 ? hppT : hppB;
594     translate(c)
595       mirror([0,0,s>0])
596       rotate(-s * 45)
597       hull()
598       for (x=[-20,20])
599         for (y=[0, s * 10])
600           translate([x,y])
601             circle(hp_rn);
602   }
603 }
604
605 module HingeLeverNutProfile(){
606   for (c= [hppB, hppT]) {
607     translate(c)
608       circle($fn=6, r= 0.5 * hingescrew_nut_across / cos(30));
609   }
610 }
611
612 module Flip_hinge(doflip=1){
613   hinge_origin = [0, -(phone_height - hppB[0]), hppB[1]];
614   translate(hinge_origin)
615     rotate([doflip*180,0,0])
616     translate(-hinge_origin)
617     children();
618 }
619
620 module HingePortion(x0,x1){
621   Flip_rhs() Flip_bot(1)
622     translate([x0,0,0])
623     mirror([1,0,0])
624     rotate([90,0,-90])
625     linear_extrude(height=x1-x0)
626     children(0);
627 }
628
629 module CatchPortion(width){
630   translate([phone_width/2, 0,0])
631     rotate([90,0,-90])
632     linextr(-width/2, width/2)
633     children(0);
634 }
635
636 module CaseBase(){
637   AroundEdges(epp3[1], case_th_bottom, 1)
638     EdgeProfile();
639 }
640
641 function prop_x(gamma) = hp_k / (2 * sin(gamma/2)) - hppT[0];
642
643 module PropProfileAssignments(gamma){
644   // https://en.wikipedia.org/wiki/Solution_of_triangles#Two_sides_and_the_included_angle_given_(SAS)
645   x = prop_x(gamma);
646   p = phone_height + prlp10[0] - hppB[0];
647   b = p + x;
648
649   q = phone_height - hppT[0] - prcp1[0]; // $prpp7[0] is 0 by definition
650   a = q + x;
651   c = sqrt(a*a + b*b - 2*a*b*cos(gamma));
652   $prp_alpha = acos( (b*b + c*c - a*a) / (2*b*c) );
653
654   $prp_theta = 90 - $prp_alpha;
655   beta = 180 - $prp_alpha - gamma;
656   psi = 90 - beta;
657
658   //echo("abc", a,b,c);
659
660   v1 = [ [ cos(psi), -sin(psi) ],    // x
661          [ sin(psi),  cos(psi) ] ];  // y
662
663   $prpp7 = [0, c + (lpp13[1] - $prpp10[1] - hp_k) ];
664
665   $prp_r1 = prc_r1;
666   $prp_r11 = prop_main_th/2;
667
668   $prpp1 = $prpp7 + [1,0] *
669     // this is approximate, but will do
670     (prop_main_th/2 + prop_prop_gap + prcp1[0] - cppA[0]);
671   $prpp3 = $prpp1 +
672     v1[0] * -$prp_r1 +
673     v1[1] * ((prcp2[1] - prcp1[1]) - prop_prop_gap);
674   $prpp12 = $prpp3 + v1[0] *
675     (prop_end_dia + prop_caserecess_taper * ($prpp1[1] - $prpp3[1]));
676   $prp_r8 = prop_main_th;
677   $prpp4 = [ prop_main_th/2, $prpp3[1] ];
678   $prp_r5 = $prp_r8;
679   $prpp5 = [ $prpp12[0] - $prp_r5,
680             $prpp3[1] - prop_prong_h + $prp_r5 ];
681   $prpp6 = $prpp4 + [0,-1] * (prop_prong_h +
682          prop_prong_heel_slope * ($prpp5[0] - $prpp4[0]));
683   $prpp8 = $prpp4 + [0,-1] * $prp_r8;
684   $prpp9 = $prpp8 + [-1,0] * $prp_r8;
685
686   children();
687 }
688
689 module PropProfile(gamma, cut=0, rot=0){ ////toplevel
690   PropProfileAssignments(gamma){
691
692     //#circleat($prpp3,1);
693     //#circleat($prpp12,1);
694
695     if (!cut) {
696       hull(){
697         translate($prpp8)
698           intersection(){
699             circle($prp_r8);
700             polygon([[-20,-0], [20,20], [0,0]]);
701           }
702         rectfromto($prpp6, $prpp9);
703         translate($prpp5) intersection(){
704           circle($prp_r5);
705           polygon([[-10,-10], [0,0], [10,0]]);
706         }
707         rectfromto($prpp12 + [0,-0.1], $prpp3);
708       }
709       hull(){
710         circleat($prpp1, $prp_r1);
711         rectfromto($prpp12 + [0,-0.1], $prpp3);
712       }
713     }
714     // main shaft
715     rotate([0,0, rot*-$prp_theta]){
716       hull(){
717         extra = cut ? prop_recess_slop : 0;
718         rectfromto($prpp6, $prpp9);
719         circleat($prpp11, $prp_r11 + extra);
720         circleat($prpp10, $prp_r10 + extra);
721       }
722     }
723   }
724 }
725
726 module PropAggregateProfile(){
727   for (angle = prop_angles)
728     PropProfile(angle, 0,0);
729 }
730
731 module Prop(){ ////toplevel
732   hw = prop_main_width/2;
733   linextr(-hw, +hw)
734     PropAggregateProfile();
735 }
736
737 module Case(){ ////toplevel
738   difference(){
739     union(){
740       CaseBase();
741
742       // ledge (fixed keeper)
743       Flip_rhs(1-keeper_side) intersection(){
744         rotate([90, 0, 0])
745           linear_extrude(height = phone_height + phone_cnr_rad * 2)
746           KeeperProfile(1);
747
748         // outline of the whole case, to stop it protruding
749         translate([0,0, -25])
750           linear_extrude(height = 50)
751           hull()
752           Flip_bot()
753           circleat([+1,-1] * phone_cnr_rad, phone_cnr_rad + case_th_side/2);
754       }
755
756       // hinge
757       HingePortion(hex20, hex21) HingeBaseProfile();
758
759       // buildout for prop recess
760       if (prop_caserecess_buildout_r > 0) Flip_rhs(1)
761         linextr(case_bottom_z, epp2i[1])
762         hull() {
763           for (dxs = [-1,+1])
764             circleat([ prop_x_pos + dxs * prop_caserecess_buildout_r,
765                        -epp2o[0] ],
766                      r = epp2o[0] - prcp2[0]);
767         }
768     }
769
770     // slot for keeper
771     Flip_rhs(keeper_side)
772       translate([0, -phone_cnr_rad, 0])
773       rotate([90, 0, 0])
774       linear_extrude(height = phone_height + phone_cnr_rad * 2)
775       minkowski(){
776         KeeperProfile();
777         rectfromto([ -keeper_gap_x,    -keeper_gap_z_bot ],
778                    [ keeper_gap_x_holes,    +keeper_gap_z_top ]);
779       }
780
781     // front camera
782     RearCameraAperture();
783
784     // struts (invisible, because they're buried in the case)
785     Struts(epp2i[0], epp2i[1] - case_th_bottom, case_th_bottom);
786
787     Buttons(){
788       mirror([1,0,0])
789         rotate([90,0,90]) {
790           if (!($button_leg_only && enable_support))
791           intersection(){
792             translate([0,0,-10])
793               linear_extrude(height= 20)
794               ButtonPlan($button_l, 0,1);
795             if ($button_leg_only)
796               rotate([-90,90,0])
797                 translate([phone_width/2, -400, kppe[1]])
798                 mirror([1-abs($rhsflip - keeper_side),0,0])
799                 cube([400, 800, 50]);
800             if (enable_support)
801               rotate([-90,90,0])
802               translate([-400, -400, kppd[1]])
803                 mirror([0,0,1])
804                 cube([800,800,100]);
805           }
806           translate([0,0, -bppR[0]])
807             linear_extrude(height= 20)
808             ButtonPlan($button_l, 1,1);
809         }
810       
811     }
812
813     // apertures along top edge
814     CaseAperture(jack_pos, jack_dia, 8);
815     Flip_rhs(1)
816       CaseAperture(noisecancelmic_pos, noisecancelmic_dia, 8);
817
818     OrdinaryRearApertures();
819
820     MicroUSB();
821
822     // gaps for the lid's hinge arms
823     HingePortion(hex20 - hinge_x_arms_gap,
824                  hex21 + hinge_x_arms_gap)
825       minkowski(){
826         HingeLidProfile();
827         circle(r= hinge_r_arms_gap, $fn= 8);
828       }
829
830     // screw holes in the hinge arms
831     HingeScrews();
832
833     // catch striker
834     CatchPortion(catch_width + catch_side_gap*2)
835       CatchCutProfile();
836
837     // prop recess
838     Flip_rhs(1)
839       translate([prop_x_pos,0,0])
840       mirror([0,1,0])
841       rotate([90,0,90])
842       linextr(-prop_recess_hw, +prop_recess_hw)
843       hull(){
844         for (d=[ [0,0], [0,-1], [+1,-1/prop_caserecess_taper] ])
845           circleat(prcp1 + 20*d,
846                    prc_r3);
847       }
848   }
849 }
850
851 module Lid(){ ////toplevel
852   difference(){
853     union(){
854       AroundEdges(lpp10[1], lpp13[1] - lpp10[1], 0)
855         LidEdgeProfile();
856
857       // button covers
858       Buttons(){
859         intersection(){
860           rotate([90,0,90])
861             translate([0,0,-10])
862             linear_extrude(height= 20)
863             ButtonPlan($button_l, 1,0);
864           rotate([90,0,0])
865              translate([0,0,-100])
866             linear_extrude(height= 200)
867             ButtonCoverProfile();
868         }
869       }
870
871       // hinge arms
872       HingePortion(hex20, hex21) HingeLidProfile();
873
874       // catch
875       CatchPortion(catch_width)
876         CatchCatchProfile();
877     }
878     Struts(lpp10[0] + strut_min_at_end, lpp13[1], -case_th_lid);
879
880     // screw holes in the hinge arms
881     HingeScrews();
882
883     // prop recess
884     translate([prop_x_pos, -prlp10[0], prlp10[1]])
885       mirror([0,1,0])
886       rotate([90,0,90])
887       linextr(-prop_recess_hw, +prop_recess_hw)
888       hull()
889       for (pa = prop_angles)
890         PropProfile(pa, 1,1);
891   }
892 }
893
894 module HingeLever(){ ////toplevel
895   difference() {
896     // outer body, positive
897     HingePortion(hex22, hex22 + phone_width/2)
898       HingeLeverOuterProfile();
899
900     // space for the screws
901     HingePortion(hex23, hex24)
902       HingeLeverInnerProfile();
903
904     // bores for the screws
905     HingeScrews();
906
907     // space for the charging cable
908     MicroUSB();
909     Flip_hinge() MicroUSB();
910   }
911 }
912
913 module HingeLeverPrint(){ ////toplevel
914   rotate([-90,0,0])
915     HingeLever();
916 }
917
918 module TestSelectLength(){
919   translate([-30, -200, -20])
920     cube([30 + 15, 250, 40]);
921 }
922
923 module TestLength(){ ////toplevel
924   intersection(){
925     Case();
926     TestSelectLength();
927   }
928 }
929
930 module TestLengthRight(){ ////toplevel
931   intersection(){
932     Case();
933     Flip_rhs(1)
934       TestSelectLength();
935   }
936 }
937
938 module TestSelectWidth(){
939   translate([-30, -(phone_height - 25), -20])
940     mirror([0, 1, 0])
941     cube([200, 50, 40]);
942 }
943
944 module TestWidth(){ ////toplevel
945   intersection(){
946     Case();
947     TestSelectWidth();
948   }
949 }
950
951 module TestLidWidthPrint(){ ////toplevel
952   rotate([0,180.0]) intersection(){
953     Lid();
954     TestSelectWidth();
955   }
956 }
957
958 module TestSelectRearAperture(){
959   minkowski(){
960     union() children();
961     translate([20, 0,0])
962       cube([42, 2, 1], center=true);
963   }
964 }
965
966 module TestSelectCamera(){
967   minkowski(){
968     TestSelectRearAperture()
969       RearCameraAperture();
970     cube([0.1, 50, 0.1]);
971   }
972 }
973
974 module TestSelectOrdinaryRearApertures(){
975   TestSelectRearAperture()
976     OrdinaryRearApertures();
977 }
978
979 module TestCamera(){ ////toplevel
980   intersection(){
981     Case();
982     TestSelectCamera();
983   }
984 }
985
986 module TestLidByCamera(){ ////toplevel
987   intersection(){
988     Lid();
989     TestSelectCamera();
990   }
991 }
992
993 module TestLidByCameraPrint(){ ////toplevel
994   rotate([180,0,0]) TestLidByCamera();
995 }
996
997 module DemoByCamera(){ ////toplevel
998   color("blue") TestLidByCamera();
999   color("red")  TestCamera();
1000 }
1001
1002 module OneKeeper(){ ////toplevel
1003   translate([0, -phone_cnr_rad, 0])
1004     rotate([90, 0, 0])
1005     linear_extrude(height = phone_height - phone_cnr_rad * 2)
1006     KeeperProfile();
1007 }
1008
1009 module OneKeeperPrint(){ ////toplevel
1010   rotate([0,180,0])
1011     OneKeeper();
1012 }
1013
1014 module LidPrint(){ ////toplevel
1015   rotate([0,180,0])
1016     Lid();
1017 }
1018
1019 module TestSelectFrame(){
1020   include = [1,-1] * (epp2i[0] + 4);
1021
1022   difference(){
1023     cube(1000, center=true);
1024     translate([0,0, -100])
1025       linear_extrude(height=200)
1026       rectfromto(include,  inside_br - include);
1027   }
1028 }
1029
1030 module TestSelectLidFrame(){
1031   TestSelectFrame();
1032 }
1033
1034 module TestFrameCase(){ ////toplevel
1035   intersection(){
1036     Case();
1037     union(){
1038       TestSelectFrame();
1039       TestSelectCamera();
1040       TestSelectOrdinaryRearApertures();
1041     }
1042   }
1043 }
1044
1045 module TestTopApertures(){ ////toplevel
1046   intersection(){
1047     Case();
1048     TestSelectFrame();
1049     translate([-100, -35, -100])
1050       cube([400, 100, 200]);
1051   }
1052 }
1053
1054 module TestFrameLidPrint(){ ////toplevel
1055   rotate([0,180,0]) intersection(){
1056     Lid();
1057     TestSelectLidFrame();
1058   }
1059 }
1060
1061 module ButtonPlanForDemo(z, deep, cut){
1062   translate([0,0,z])
1063     ButtonPlan(8, deep, cut);
1064 }
1065
1066 module HingeScrews(){
1067   Flip_rhs() Flip_bot(1){
1068     for (c= [ hppT, hppB ])
1069       translate([ hex20,
1070                   -c[0],
1071                   c[1] ]){
1072         rotate([0,90,0])
1073           translate([0,0,-.2])
1074           cylinder( r= hingescrew_shaft_dia/2,
1075                     h = hingescrew_shaft_len+0.2 );
1076         rotate([0,-90,0])
1077           translate([0,0,+.1])
1078           cylinder( r= hingescrew_head_dia/2, h = hingescrew_head_th );
1079       }
1080   }
1081 }
1082
1083 module DemoPropAngleSelect(c){
1084   color(c) difference(){
1085     union(){ children(); }
1086     translate([ prop_x_pos, -400, -200 ])
1087       cube([ 400,800,400 ]);
1088   }
1089 }
1090
1091 module DemoPropAngle(ang){
1092   hL = [0, -(phone_height - hppT[0]), hppT[1] - hp_k*2];
1093   hC = [0, -(phone_height - hppB[0]), hppB[1]];
1094
1095   translate(hL)
1096     rotate([ang/2,0,0])
1097     translate(-hL)
1098     translate(hC)
1099     rotate([ang/2,0,0])
1100     translate(-hC) {
1101       DemoPropAngleSelect("red") Case();
1102
1103       color("orange")
1104         translate([prop_x_pos, -prcp1[0], prcp1[1]])
1105         PropProfileAssignments(ang) {
1106           echo($prpp1);
1107           rotate([-$prp_theta, 0, 0])
1108           translate([0, $prpp1[0], -$prpp1[1]])
1109           rotate([90,0,-90])
1110           Prop();
1111         }
1112     }
1113
1114   translate([0,0, -hp_k*2])
1115     DemoPropAngleSelect("blue")
1116     Lid();
1117 }
1118
1119 module DemoPropAngles(){ ////toplevel
1120   for (i=[0 : len(prop_angles)-1])
1121     translate(i * [0, -100, 100])
1122     DemoPropAngle(prop_angles[i]);
1123 }
1124
1125 module DemoFrame(){ ////toplevel
1126   color("red") TestFrameCase();
1127   color("blue") intersection(){ Lid(); TestSelectLidFrame(); }
1128   color("black") HingeScrews();
1129   %HingeLever();
1130 }
1131
1132 module DemoHingedFrame(){ ///toplevel
1133   color("red") TestFrameCase();
1134   translate([0,0, -2*hp_k])
1135   color("blue") intersection(){ Lid(); TestSelectLidFrame(); }
1136
1137   Flip_hinge(){
1138     color("orange") HingeLever();
1139     color("black") HingeScrews();
1140   }
1141 }
1142
1143 module DemoHinge(){ ////toplevel
1144   DemoFrame();
1145   translate([0,0, -hp_k*3])
1146     DemoHingedFrame();
1147 }
1148
1149 module DemoProfiles(){ ////toplevel
1150   LidEdgeProfile();
1151   %EdgeProfile();
1152   KeeperProfile();
1153   translate([0,0,-1]) color("black") KeeperProfile(1);
1154
1155   translate([20,0]) {
1156     LidEdgeProfile();
1157     %EdgeProfile();
1158
1159     demopoint_QR = [ bppS[0], bppQ[1] - 0.1];
1160   
1161     color("blue") ButtonCoverProfile();
1162     color("red") {
1163       rectfromto(bppQ, demopoint_QR);
1164       rectfromto(bppR, demopoint_QR);
1165     }
1166   }
1167
1168   translate([-20,0]) {
1169     color("black") ButtonPlanForDemo(-2, 0,1);
1170     color("red" )  ButtonPlanForDemo(-4, 1,1);
1171     color("blue")  ButtonPlanForDemo(-6, 1,0);
1172   }
1173
1174   translate([0, -30]) {
1175     %LidEdgeProfile();
1176     %EdgeProfile();
1177     color("blue") HingeLidProfile();
1178     color("red")  HingeBaseProfile();
1179     color("black") translate([0,0,-2]) HingeLeverOuterProfile();
1180   }
1181
1182   for (f=[0,1]) {
1183     translate([-30, -60 + 30*f]) {
1184       translate([0,0,-4]) EdgeProfile();
1185       %translate([0,0,-10]) HingeBaseProfile();
1186       translate([0,-2] * f * hp_k) {
1187         translate([0,0,-4]) LidEdgeProfile();
1188         %translate([0,0,-10]) %HingeLidProfile();
1189       }
1190       translate(+hppB) rotate([0,0,180*f]) translate(-hppB) {
1191         translate([0,0,-2]) color("black") HingeLeverOuterProfile(); 
1192         translate([0,0,0]) color("red") difference(){
1193           HingeLeverOuterProfile();
1194           HingeLeverInnerProfile();
1195         }
1196         translate([0,0,3]) color("yellow") HingeLeverNutProfile();
1197       }
1198     }
1199   }
1200
1201   translate([20,-30]) {
1202     %EdgeProfile();
1203     %LidEdgeProfile();
1204     //translate([0,0,1]) CatchCutProfile();
1205     color("blue") CatchCatchProfile();
1206     color("red") difference(){ EdgeProfile(); CatchCutProfile(); }
1207   }
1208
1209   translate([40,-30]) {
1210     difference(){
1211       LidEdgeProfile();
1212       translate(prlp10)
1213         PropProfile(10, 1, 0);
1214     }
1215     translate(prlp10)
1216       PropProfile(15, 0);
1217   }
1218   translate([60,-30]) {
1219     PropAggregateProfile();
1220   }
1221 }
1222
1223 //EdgeProfile();
1224 //KeeperProfile();
1225 //CaseBase();
1226 //%Case();
1227 //Keeper();
1228 //LidEdgeProfile();
1229 //KeeperProfile();
1230 //DemoProfiles();
1231 //PropRecess();