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