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