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