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