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