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