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