chiark / gitweb /
fairphone-case: ButtonCoverReinf improvments
[reprap-play.git] / fairphone-case.scad
1 // -*- C -*-
2
3 // Hard case for Fairphone 2
4 //
5 //  Copyright 2018 Ian Jackson.  There is NO WARRANTY.
6 //  See below for full licensing and disclaimer.
7 //
8 // Instructions
9 //
10 //  1. You will want to git clone this repository.
11 //
12 //  2. Decide about the notification LED aperture. See the variable
13 //     led_window_style, below.  The default here is "ad-hoc
14 //     multi-colour", which can produces a translucent (clear-ish)
15 //     window set into the lid, even on a single-nozzle printer.
16 //     See "Ad-hoc multi-colour", below.
17 //
18 //  3. use "make" to generate the necessary files:
19 //
20 //     make -j8 fairphone-case.auto.scads `for f in   \
21 //        HingePrint        \
22 //        LidWindowPrint    \
23 //        LidPrint          \
24 //        OneKeeperPrint    \
25 //        Case              \
26 //     ; do echo fairphone-case,$f.auto.stl; done`
27 //
28 //  4. Print them.  Case and OneKeeperPrint should probably be
29 //     the same colour.
30 //
31 //     For Lid and LidWindowPrint, if you are doing ad-hoc
32 //     multi-colour:
33 //        i.   Set up for clear filament
34 //        ii.  Print LidWindowPrint.  Wait for it to finish.
35 //             It won't take long.  As soon as it finishes, tell
36 //             your printer to warm up (so that in fact it does
37 //             not cool down).
38 //        iii. Leaving the output so far on the printbed, reload
39 //             your printer with the main lid colour.
40 //        iv.  Print LidPrint.  You can let this go unattended.
41 //
42 //  5. Assemble the hinge.  You will need 4x M2 12mm machine screws
43 //     and 8x M2 full nuts.
44 //
45 //     Make sure you get the hinge the right way round.  If you're not
46 //     sure, run
47 //         openscad fairphone-case,DemoHinge.auto.scad
48 //     to see an assembly diagram.
49 //
50 //     The nuts recess into the hinge.  You will want very fine
51 //     pliers.  As you screw each screw in, add the second nut when
52 //     the screw thread emerges from the first - this will be a
53 //     locknut.  Screw each screw to an appropriate tightness for the
54 //     hinge stiffness.  You want the lid-side hinge to be stiffer as
55 //     that makes closing the case work better.
56 //
57 //     When you have the stiffness right, tighten the locknuts onto
58 //     each first nut.
59 //
60 //  6. In use:
61 // 
62 //      - To put the phone in, drop its RH side into the RH side of
63 //        the case.  Then feed the keeper through the small hole.
64 //        Feed it right through.
65 //
66 //      - The optional prop can be used to prop the phone up (in
67 //        portrait orientation only right now).  See
68 //            openscad fairphone-case,DemoPropAngles.auto.scad
69 //
70 // Ad-hoc multi-colour
71 //     
72 //  This file is set up to let you make a translucent window using a
73 //  single-extruder printer, using a "two print run" technique.  This
74 //  works well with our Lulzbot TAZ 5 and Aleph Objects' version of
75 //  Cura.  If you are using a different printer, you may need to
76 //  adjust the parameters or try a different technique.  In particular,
77 //      initial_layer_thick
78 //         set so that the window is one layer thick
79 //      initial_layer_width
80 //         set so that the slicer draws a rectangle around the whole
81 //         object, rather than putting a "skirt" or anything inside
82 //
83 //  If you have a dual-extruder printer, you can set led_window_style
84 //  to 2 and do a single print of LidPrint and LidWindowPrint.
85 //
86 //  Alternatively you can set it to 1 (just a hole) or 0 (no hole).
87 //
88 //  Thanks to Clare Boothby for the ad-hoc multi-colour technique (and
89 //  the parameters for our Lulzbot TAZ 5 and Aleph Objects's Cura).
90 //
91 // Other phones
92 //
93 //  It might well be possible to adapt this file for other phones.
94 //  If you do, let me know how you get on.
95 //
96 //
97 // AUTHORSHIP, COPYRIGHT, LICENCE, AND LACK OF WARRANTY
98 //
99 //   Copyright (C)2018 Ian Jackson.
100 //
101 //    This program for generating a 3D model is free software: you can
102 //    redistribute it and/or modify it under the terms of the GNU
103 //    General Public License as published by the Free Software
104 //    Foundation, either version 3 of the License, or (at your option)
105 //    any later version.
106 //
107 //    This program is distributed in the hope that it will be useful,
108 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
109 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
110 //    GNU General Public License for more details.
111 //
112 //    You should have received a copy of the GNU General Public
113 //    License along with this program.  If not, see
114 //    <http://www.gnu.org/licenses/>.
115 //
116 //  In particular DO NOT BLAME ME IF THIS CASE DOES NOT ADEQUATELY
117 //  PROTECT YOUR PHONE !  It is your responsibility to decide whether
118 //  this case will meet your needs.
119
120 include <utils.scad>
121 include <funcs.scad>
122
123 phone = [ 75.0, 145.0 ];
124
125 prop_buildout_less = 3;
126
127 prop_angles = [ 15, 30, 45, 60 ];
128
129 bumper = [ 0.250, -0.025 ];
130 // ^ One side.  Overall size is increased by twice this.
131 // If no bumpers, is the gap around the phone.
132
133 enable_support = 1;
134
135 led_window_style = 3;
136 // 0: no window
137 // 1: simply an opening
138 // 2: opening with separate cover model, for printing in clear (two colour)
139 // 3: like 2 but one-layer window for ad-hoc multi-colour
140
141 initial_layer_thick = 0.400; // ^ needed for mode 3 only
142 initial_layer_width = 0.750; // ^ needed for mode 3 only
143 multicolour_gap = 0.15; // each side
144
145 phone_cnr_rad = 6.0;
146 phone_rim_depth = 0.80; // includes allowance for a screen protector
147
148 button_cutout_depth = 9;
149
150 phone_edge_thick = 9.0;
151 phone_total_thick = 12.0;
152 phone_backside_slope_inner = 1.5; // larger means shallower
153 phone_backside_slope_outer = 1.0; // larger means shallower
154
155 camera_pos_tl = [  6.450, 12.750 ]; // measured from tl corner
156 camera_pos_br = [ 22.300, 37.600 ]; // tl/br as seen from back
157
158 jack_pos = [ 13.83, 8.485 ];
159 jack_dia = 10.64 + .5; // some jack I had lying around
160
161 led_pos = [ 13.98, 10.00 ];
162 led_aperture = 9;
163 led_window_ledge = 0.75; // each side
164
165 noisecancelmic_pos = [ 19.54, 7.37 ];   // from rhs
166 noisecancelmic_dia = 4.00;
167
168 //fingerpushhole_dias = [ 15, 18 ];
169 fingerpushhole_dias = [];
170
171 lanyard_half_dia = 1.15;
172 lanyard_entry_rel_breadth = 2;
173 lanyard_channel_len = 15;
174 rearspeaker_pos_bl = [ 12.64, 18.72 ];
175 rearspeaker_size   = [  3.76,  7.36 ];
176
177 microusb_above = 3.27 - 0.25;
178 microusb_below = 0.0;
179 microusb_width = 16.12 + 1.25;
180
181 case_th_bottom = 2.5;
182 case_th_lid = 3.0;
183 case_th_side = 2.3;
184 case_th_lip = 1.2;
185
186 lid_screen_gap_extra = .66;
187
188 case_struts_count = 6;
189 case_struts_solid_below = 1.00;
190 case_struts_solid_above = 0.75;
191 case_struts_width = 0.10;
192
193 keeper_th_z = 0.75;
194 keeper_th_x = 0.75;
195 keeper_inner_width = 2.75;
196 keeper_inner_height = 2.75;
197 keeper_slant_slope = 2; // larger means steeper
198
199 keeper_gap_z_top = 0.25;
200 keeper_gap_z_bot = 0.75;
201 keeper_gap_x     = 0.25;
202 keeper_gap_x_holes = 0.75;
203
204 keeper_side = 0; // 0 = lhs; 1 = rhs
205
206 case_lip = 1.25;
207
208 lid_gap_x = 0.25;
209 lid_gap_z = 0.25;
210 lid_lip = 1.75;
211 lid_edgepart_width = 5.0;
212 lid_buttoncover_thick = 1.3;
213 lid_buttoncover_reinf = 1.0;
214
215 catch_slop = 0.50;
216
217 foldover_gap = 0.50;
218 foldover_lever_gap = 0.50;
219
220 // properties of the hinge fasteners
221 hingescrew_shaft_dia = 2.0 + 0.25; // M2 x 12mm machine screw
222 hingescrew_shaft_len = 12;
223 hingescrew_fasteners_extra_thick = 0.40;
224 // ^ amount of thread protruding if everything was completely nominal
225 //   and we are using two nuts
226 hingescrew_nut_access_dia = 4.72 + 0.50;
227 // ^ washer is 4.72 dia
228 //   also, want to get pliers or tiny spanner in to do up locknut
229 hingescrew_nut_across = 3.92 + 0.25; // incl. slop around recess slop
230 hingescrew_nut_thick = 1.93;
231 hingescrew_head_th = 1.38 + 0.75;
232 hingescrew_head_dia = 3.92;
233
234 hingescrew_nut_recess_portion = 2/3; // portion of nut in recess
235
236 lever_cover_th = 0.75;
237 hingemount_th = 2.5;
238 hingemount_wd = 4.8725;
239
240 $fa = 5;
241 $fs = 0.1;
242
243 button_l_fudge = 4.4;
244 buttonishleg_default_l_is_fudge = 10;
245
246 hinge_base_slope = 1.5; // bigger is steeper
247
248 strut_min_at_end = 1.5;
249
250 hinge_x_gap = 0.125;
251 hinge_x_postscrew_gap = 0.75;
252 hinge_x_arms_gap = 0.35;
253 hinge_r_arms_gap = 0.55;
254
255 rearspeaker_gap    = [ 2.0, 2.0 ]; // each side
256
257 catch_len = 7.5;
258 catch_width = 15;
259 catch_thickness = 1.3;
260 catch_side_gap = 0.75; // each side
261
262 catch_depth = 1.25;
263 catch_height = 0.35;
264 catch_finger_height = 1.5;
265 catch_finger_depth = 2.5;
266 catch_catch_slope = 0.25;
267
268 catch_topcurve_r = 5.0;
269
270 prop_recess_under = 0.50;
271 prop_recess_slop = 0.200; // each side
272 prop_end_dia = 0.5;
273 prop_main_th = 3;
274 prop_taper_len = 6;
275 prop_main_width = 4;
276 prop_side_gap = 0.75; // each side
277 prop_lidrecess_behind = 0.75;
278 prop_caserecess_behind = 0.75;
279 prop_caserecess_taper = 0.45; // one side only
280 prop_prop_gap = 0.5;
281 prop_prong_heel_slope = 0.5;
282
283 lid_fold_clearance_antislop = 0.5;
284
285 // ---------- calculated ----------
286
287 phone_width =  (phone + bumper*2)[0];
288 phone_height = (phone + bumper*2)[1];
289
290 inside_br = [phone_width, -phone_height];
291
292 prop_prong_h = prop_main_th;
293
294 //echo(camera_pos_tl + bumper,
295 //     camera_pos_br + bumper);
296
297 // ----- could be changed -----
298 lid_buttoncover_gap = lid_gap_x;
299 lid_buttoncover_overlap = case_th_lip + keeper_gap_z_top;
300
301 phone_backside_slope_thick = phone_total_thick - phone_edge_thick;
302
303 //prop_lidrecess_depth = case_th_lid - prop_recess_under;
304
305 //prop_nose_len = case_th_lid - prop_recess_under;
306 //prop_recess_slope = tan(prop_max_angle); // bigger means steeper
307 //prop_recess_width = prop_main_th / cos(prop_max_angle) + prop_backfwd_gap;
308
309
310 //lid_lip_overlap_width xxx bad name = ;
311 //lid_lip_inner_slope = [ 5, 5 ]; // xxx
312
313 epp0 = [0,0];
314 epp1 = [0, -phone_edge_thick];
315 epp2i = epp1 + phone_backside_slope_thick * [ phone_backside_slope_inner, -1 ];
316 epp2o = epp1 + phone_backside_slope_thick * [ phone_backside_slope_outer, -1 ];
317 epp3 = epp2i + [10, 0];
318 epp5 = epp0 + [0,1] * (keeper_th_z + keeper_gap_z_top + case_lip);
319 epp4 = epp5 + [-1,0] * case_th_side;
320
321 kppe = [0,0];
322 kppd = kppe + [1,0] * keeper_inner_width;
323 kppc = kppd + [0,1] * keeper_th_z;
324 kppb = [ kppe[0] - keeper_th_x, kppc[1] ];
325 kppf = kppe - [0,1] * keeper_inner_height;
326 kppa = [ kppb[0], kppf[1] ];
327
328 lpp10 = [ epp5[0] + lid_gap_x, kppc[1] + lid_gap_z ];
329 lpp11 = [ lpp10[0],            epp5[1] + lid_gap_z ];
330
331 lpp14 = lpp10 + [1,0] * max(keeper_inner_width, lid_edgepart_width);
332 // exact x posn not very important; must extend past end of keeper
333
334 lpp15 = [ lpp14[0],
335           epp0[1] - phone_rim_depth + 1/2.5 * case_th_lid
336           + lid_screen_gap_extra ];
337 // ^ beam theory says to maximise force before contact,
338 //   the gap below the `beam' (the lid) must be 1/3
339 //   the thickness (ie the lid thickness) if the beam
340 //   is solid, or 1/2 if it has a top and bottom only.
341 //   ours is mostly solid.
342
343 lp_r12 = max(case_th_lid - (lpp11[1] - lpp15[1]),
344              case_th_lip);
345
346 lpp12 = [ epp4[0] + lp_r12,    lpp11[1] ];
347 lpp13 = [ lpp12[0],            lpp12[1] + lp_r12 ];
348
349 case_bottom_z = epp2o[1] - case_th_bottom;
350
351 // button profile
352 bppM = epp4 + [0,5];
353 bppN = [ bppM[0] + lid_buttoncover_thick, bppM[1] ];
354 bppR = [ bppN[0] + lid_buttoncover_gap, -button_cutout_depth ];
355 bppS = [ epp1[0], bppR[1] ];
356 bppQ = [ bppM[0], bppR[1] - lid_buttoncover_overlap ];
357 bppP = bppQ + [0,1] * lid_buttoncover_gap;
358 bppO = [ bppN[0], bppP[1] ];
359 bppL = lpp10 + [5,0];
360 bppK = [ bppL[0], bppN[1] ];
361 bppJ = [ bppN[0], bppL[1] ];
362 bppU = [ bppJ[0], lpp12[1] ];
363 bppV = lpp11;
364 bppW = lpp10;
365
366 echo("BUTTON COVER TH", bppO[0] - bppP[0]);
367
368 // notification led aperture
369
370 nla_r0 = led_aperture/2;
371 nla_r1 = nla_r0 + led_window_ledge;
372 nla_r2 = nla_r1 + multicolour_gap;
373 nla_t =
374   led_window_style >= 3 ? initial_layer_thick :
375   led_window_style >= 2 ? led_window_ledge : 0;
376
377
378 // hinge plan
379 hp_rn = hingescrew_nut_access_dia/2;
380 hp_r2_min = hp_rn + lever_cover_th;
381 hp_rs = hingescrew_shaft_dia/2;
382 hp_r1_min = hp_rs + hingemount_th;
383
384 hp_r1 = max(hp_r1_min, hp_r2_min);
385 hp_r2 = hp_r1;
386
387 hppU = lpp13;
388 hppS = epp2o + [0,-1] * case_th_bottom;
389 hp_k = 0.5 * (hppU[1] - hppS[1] + foldover_gap);
390
391 hppM = [ epp4[0] - foldover_lever_gap - hp_r2,
392          0.5 * (hppU + hppS)[1] ];
393 hppT = [ hppM[0], hppU[1] - hp_r1 ];
394 hppB = hppT + [0,-1] * hp_k;
395
396 hppE_y = epp2o[1] - case_th_bottom + hp_r1;
397 hppE_x = hppB[0] + (hppB[1] - hppE_y) * hinge_base_slope;
398 hppE = [ hppE_x, hppE_y ];
399
400 // hinge elevation x coords
401
402 hex20 = max(epp2o[0],
403             phone_cnr_rad,
404             kppd[0] + hingescrew_head_th + keeper_gap_x_holes);
405 hex21 = hex20 + hingemount_wd;
406 hex22 = hex21 + hinge_x_gap;
407 hex27 = hex20 + hingescrew_shaft_len;
408 hex24 = hex27 + hinge_x_postscrew_gap;
409 hex23 = hex27 - (hingescrew_nut_thick*2
410                  + hingescrew_fasteners_extra_thick);
411 hex26 = hex23 + hingescrew_nut_thick * 2/3;
412
413 echo(hex20, hex21, hex22, hex23, hex24);
414 //  6, 10.8725, 10.9975, 13.74, 18.75
415 module chk(act,exp) {
416   if (abs(act-exp) > 1e-9) echo("WRONG", act, exp);
417   else echo("ok", act);
418 }
419 chk(hex20, 6);
420 chk(hex21, 10.8725);
421 chk(hex22, 10.9975);
422 chk(hex23, 13.74);
423 chk(hex24, 18.75);
424
425 lid_fold_clearance_skew =
426   (lpp10[1] - hppB[1]) /
427   (lpp10[0] - hppB[0]);
428
429 echo("SK",lid_fold_clearance_skew);
430
431 // catch
432
433 cppJ = [ epp4[0] + catch_thickness, lpp10[1] ];
434 cppA = cppJ + [lid_gap_x, -lid_gap_z];
435 cppB = [ epp0[0], cppA[1] ];
436 cppP = [ epp4[0], cppJ[1] ];
437
438 cppS = cppJ + [0,-1] * catch_len;
439 cppD = [ cppA[0], cppS[1] + catch_slop ];
440 cppC = [ cppB[0], cppD[1] ];
441 cppT = cppS + [1, -catch_catch_slope] * catch_depth;
442 cppU = cppT + [0,-1] * catch_height;
443 cppV = [ cppS[0], cppU[1] - catch_depth ];
444
445 cppR = 0.5*(cppP + cppJ);
446
447 cp_rQ = 0.5 * (cppJ[0] - cppP[0]);
448 cppQ = [ cppR[0],
449          cppV[1] - (catch_finger_height - cp_rQ) ];
450 cppF = [ cppV[0] + catch_finger_depth, cppC[1] ];
451
452 // lanyard
453
454 ly_r = lanyard_half_dia / 2;
455 ly_rc = ly_r * 2;
456
457 ly_theta = -atan2vector(epp2i - epp1);
458 ly_o = epp2i + 3 * ly_r * unitvector2d(epp1 - epp2i);
459
460 max_case_bottom_edge_thickness =
461   case_th_bottom
462   + sin(ly_theta) * (epp2i-epp2o)[0];
463
464 ly_q_z = -(ly_rc + ly_r);
465 ly_re = max_case_bottom_edge_thickness - (-ly_q_z);
466
467 ly_oec_y = lanyard_entry_rel_breadth * ly_r;
468
469 // prop recess in case
470
471 prop_x_pos = phone_width/2;
472
473 prop_recess_hw = 0.5 * prop_main_width + prop_side_gap;
474
475 prc_r1 = prop_end_dia/2;
476 prc_r3 = prc_r1 + prop_recess_slop;
477
478 prcp2 = [ epp4[0] + prop_buildout_less,
479           case_bottom_z ];
480
481 prop_caserecess_buildout_r = -1; // prcp2[0] - epp2o[0];
482
483 prcp1 = [ epp2o[0] + prc_r3 + prop_caserecess_behind,
484           epp2i[1] - prc_r3 - prop_recess_under];
485
486 // prop recess in lid
487
488 prl_r10 = prop_end_dia/2;
489 prl_r10o = prl_r10 + prop_recess_slop;
490
491 prlp10 = lpp10 + [1,1] * prl_r10o
492   + [1,0] * prop_lidrecess_behind
493   + [0,1] * prop_recess_under;
494
495 // prop
496
497 $prpp10 = [0,0];
498 $prpp11 = [0, prop_taper_len];
499
500 $prp_r10 = prl_r10;
501
502 // ---------- modules ----------
503
504 module AdhocMultiprintFrame(phase, z0, zs) {
505   // from z0 to z0 + zs*layer
506   extra = phase * (initial_layer_width + multicolour_gap) + 5;
507   xextra = extra + -epp4[0];
508   xrange = [ 0, phone_width ] + [-1,+1] * xextra;
509   yextra = extra + -epp4[0];
510   yrange = [ -phone_height + +hppB[0] - hp_r2, 0 ] + [-1,+1] * yextra;
511   p0 = [ xrange[0], yrange[0] ];
512   p1 = [ xrange[1], yrange[1] ];
513   echo(p0, p1);
514   translate([0,0, z0])
515     mirror([0,0, zs<0 ? 1 : 0])
516     linear_extrude(height= initial_layer_thick)
517     difference(){
518       rectfromto(p0 - [1,1] * initial_layer_width,
519                  p1 + [1,1] * initial_layer_width);
520       rectfromto(p0, p1);
521     }
522 }
523
524 module KeeperProfile(slant=0){
525   use_e = kppe + [0,-1] * slant * keeper_inner_width / keeper_slant_slope;
526   polygon([use_e, kppd, kppc, kppb, kppa, kppf]);
527 }
528
529 module EdgeProfile(){
530   difference(){
531     hull(){
532       translate(epp3) square(case_th_bottom*2, center=true);
533       circleat(epp2o, r=case_th_bottom);
534       circleat(epp1, r=case_th_side);
535       rectfromto(epp0, epp4);
536     }
537     polygon([ epp5 + [0,10],
538               epp1,
539               epp2i,
540               epp3 + [10,0] ]);
541   }
542 }
543
544 module LanyardLanyardProfile(entry=false){
545   hull(){
546     for (xs=[-1,+1] * (entry ? lanyard_entry_rel_breadth : 1))
547       translate(xs * 0.5 * lanyard_half_dia * [1,0])
548         circle(r= lanyard_half_dia/2);
549   }
550 }
551
552 module LanyardCurveChannelProfile(){
553   translate([0, -ly_r])
554     LanyardLanyardProfile();
555 }  
556
557 module LanyardEntryChannelProfile(){
558   LanyardLanyardProfile(true);
559 }  
560
561 module LanyardMainChannelProfile(){
562   LanyardCurveChannelProfile();
563   difference(){
564     square(center=true, ly_r * [6, 2]);
565     for (xs=[-1,+1])
566       translate(ly_r * [3 * xs, -1])
567         circle(r = ly_r);
568   }
569 }
570
571 module LanyardEntryOuterProfile(){
572   circleat([ly_re + ly_r, 0], ly_re);
573 }
574
575 module LanyardEntry(){
576   q_z = ly_q_z;
577   oec_y = ly_oec_y;
578
579   d_x = -ly_rc;
580
581   translate([d_x, 0, q_z]) {
582     intersection(){
583       rotate([90,0,0])
584         rotate_extrude(convexity=10)
585         rotate(90)
586         translate([0, -q_z])
587         LanyardCurveChannelProfile();
588       translate([0,-10,0])
589         cube([20,20,20]);
590     }
591   }
592
593   mirror([0,0,1])
594     translate([0,0,-1])
595     linear_extrude(height=20)
596     rotate(-90)
597     LanyardEntryChannelProfile();
598
599   translate([0, ly_r*2, 0])
600     rotate([90,0,0])
601     linear_extrude(height = ly_r*4){
602     difference(){
603       rectfromto([d_x, q_z], [ly_r, 0]);
604       circleat([d_x, q_z], ly_rc);
605     }
606   }
607
608   translate([0,0,q_z]){
609     for (my=[0,1])
610       mirror([0,my,0]){
611         translate([0, oec_y, 0]){
612           difference(){
613             translate(ly_re * [-1,0,-2])
614               cube(ly_re * [2,1,2]);
615             rotate_extrude(convexity=10)
616               LanyardEntryOuterProfile();
617           }
618         }
619       }
620     difference(){
621       translate([-ly_re, -(oec_y + 0.01), -2*ly_re])
622         cube([ly_re*2, 2*(oec_y + 0.01), 2*ly_re]);
623       for (mx=[0,1])
624         mirror([mx,0,0])
625           rotate([90,0,0])
626           translate([0,0,-10])
627           linear_extrude(height=20)
628           LanyardEntryOuterProfile();
629     }
630   }
631 }
632
633 module LanyardCutout(l){
634   rotate([0,-90,0])
635     linear_extrude(height=l)
636     rotate(-90)
637     LanyardMainChannelProfile();
638
639   for (ee=[0,1]){
640     translate(ee * l * [-1,0])
641       mirror([ee,0,0])
642       LanyardEntry();
643   }
644 }
645
646 module LidEdgeProfile(){
647   polygon([ lpp10,
648             lpp11,
649             lpp12,
650             lpp13,
651             lpp13 + [10, 0],
652             lpp15 + [10, 0],
653             lpp15,
654             lpp14,
655             ]);
656   intersection(){
657     circleat(lpp12, r=lp_r12);
658     rectfromto( lpp12 + [-10,   0],
659                 lpp12 + [+10, +10] );
660   }
661 }
662
663 module LidEdgeFoldClearanceProfile(){
664   translate([-lid_fold_clearance_antislop, 0])
665     polygon([ lpp10,
666               lpp11,
667               lpp11 + [-20,  0],
668               lpp11 + [-20, 20],
669               lpp11 + [+20, 20],
670               lpp10 + [+20,  0] ]);
671 }
672
673 module ButtonCoverProfile(){
674   intersection(){
675     polygon(concat([ bppM, bppP, bppO, bppJ ],
676                    (enable_support && !$button_suppress_over_keeper
677                     ? [ bppU, bppV, bppW ] : []),
678                    [ bppL, bppK ]));
679     hull(){
680       EdgeProfile();
681       LidEdgeProfile();
682     }
683   }
684 }
685
686 module ButtonPlan(l, deep, cut){
687   epsilon =
688     (cut  ? 0 : lid_buttoncover_gap);
689
690   delta =
691     (deep ? lid_buttoncover_overlap : 0);
692
693   C = [0,0]; // by definition
694   T = [ 0, epp4[1] ];
695   G = T + [0,10];
696
697   B0 = C + [0,-1] * button_cutout_depth;
698   B1 = B0 + [0,1] * epsilon;
699
700   r0 = 0.5 * (T[1] - B0[1]);
701   A = [  -(l + button_l_fudge)/2 + r0, 0.5 * (T[1] + B0[1]) ];
702   H = A + [0,-1] * delta;
703
704   D = A + [-2,0] * r0;
705   F = D + [0,10];
706
707   E0 = 0.5 * (D + A);
708   E1 = E0 + [1,0] * epsilon;
709
710   I0 = [ E0[0], H[1] ];
711   I1 = [ E1[0], H[1] ];
712
713   hull(){
714     for (m=[0,1]) mirror([m,0])
715       circleat(H, r0 - epsilon);
716   }
717   for (m=[0,1]) mirror([m,0]) {
718     difference(){
719       polygon([ E1,
720                 I1,
721                 H,
722                 B1,
723                 G,
724                 F,
725                 D
726                 ]);
727       circleat(D, r0 + epsilon);
728     }
729   }
730 }
731
732 module ButtonCoverReinf(){ ////toplevel
733   minkowski(){
734     rotate([90,0,0])
735       linear_extrude(height=0.01)
736       intersection(){
737         ButtonCoverProfile();
738         translate([bppJ[0] + 0.1, -50]) mirror([1,0])
739           square([100,100]);
740     }
741     mirror([0,0,1]) linear_extrude(height=0.01) intersection(){
742       circle(r= lid_buttoncover_reinf);
743       translate([-20,0]) square(40, center=true);
744     }
745   }
746 }
747
748 module CatchCatchProfile(){
749   hull(){
750     for (c=[ cppR, cppQ ])
751       circleat(c, cp_rQ);
752   }
753   hull(){
754     circleat(lpp12, lp_r12);
755     circleat(lpp12 + [5,0], lp_r12);
756     rectfromto(cppP, cppP + [5,0.1]);
757   }
758   polygon([cppJ, cppS, cppT, cppU, cppV, cppQ, cppR]);
759 }
760
761 module CatchCutProfile(){
762   polygon([ cppB,
763             cppA,
764             cppD,
765             cppF,
766             cppF + [0,-10],
767             cppF + [-10,-10],
768             lpp12 + [-10,0],
769             lpp12 + [10,0]
770             ]);
771 }
772
773 module Flip_rhs(yn=[0,1]) {
774   for ($rhsflip=yn) {
775     translate([phone_width/2, 0, 0])
776       mirror([$rhsflip,0,0])
777       translate([-phone_width/2, 0, 0])
778       children();
779   }
780 }
781
782 module Flip_bot(yn=[0,1]) {
783   for ($botflip=yn) {
784     translate([0, -phone_height/2, 0])
785       mirror([0, $botflip, 0])
786       translate([0, phone_height/2, 0])
787       children();
788   }
789 }  
790
791 module AroundEdges(fill_zstart, fill_th, fill_downwards=0){
792   // sides
793   Flip_rhs(){
794     translate([0, -phone_cnr_rad, 0])
795       rotate([90,0,0])
796       linear_extrude(height = phone_height - phone_cnr_rad*2)
797       children(0);
798   }
799   // corners
800   Flip_rhs() Flip_bot() {
801     translate([+1,-1] * phone_cnr_rad)
802       intersection(){
803         rotate_extrude()
804           intersection(){
805             mirror([1,0,0])
806               translate([-1,0] * phone_cnr_rad)
807               children(0);
808             rectfromto([0,-20],[10,20]);
809           }
810         translate([-10, 0, -20] + 0.01 * [+1,-1, 0] )
811           cube([10,10,40]);
812       }
813   }
814   // top and bottom
815   Flip_bot(){
816     translate([ phone_width - phone_cnr_rad, 0,0 ])
817       rotate([90,0,-90])
818       linear_extrude(height = phone_width - phone_cnr_rad*2)
819       children(0);
820   }
821   // fill
822   translate([0,0, fill_zstart])
823     mirror([0,0, fill_downwards])
824     linear_extrude(height = fill_th)
825     rectfromto([+1,-1] * phone_cnr_rad,
826                [phone_width, -phone_height] + [-1,+1] * phone_cnr_rad);
827 }
828
829 module CaseAperture(pos, dia, $fn) {
830   theta = 180/$fn;
831   translate([ pos[0] + bumper[0],
832               -epp2i[0],
833               -pos[1] ])
834     rotate([-90, theta, 0])
835     cylinder(r = dia/2 / cos(theta),
836              h = 60);
837 }
838
839 module SideButton(y, y_ref_sign, l, suppress_over_keeper=0){
840   // y_ref_sign:
841   //   +1  measured from top    of actual phone to top    of button
842   //   -1  measured from bottom of actual phone to bottom of button
843   //    0  y is centre of button in coordinate system
844   $button_l= l;
845   $button_suppress_over_keeper= suppress_over_keeper;
846   eff_y = y_ref_sign > 0 ?         -bumper [1] -y -l/2 :
847           y_ref_sign < 0 ? (-phone -bumper)[1] +y +l/2 :
848           y;
849   //echo(eff_y);
850   translate([0, eff_y, 0])
851     children();
852 }
853
854 module LidButtonishLeg(y, y_ref_sign, l=buttonishleg_default_l_is_fudge) {
855   $button_leg_only = true;
856   SideButton(y, y_ref_sign, l) children();
857 }
858
859 module Buttons(){
860   Flip_rhs(1) SideButton(15.580, +1, 8.9     ) children(); // power
861   Flip_rhs(1) SideButton(48.700, -1, 8.920   ) children(); // camera
862   Flip_rhs(0) SideButton(30.800, +1, 21.96, 1) children(); // volume
863   Flip_rhs(   ) LidButtonishLeg(14, -1) children();
864 //  Flip_rhs(0) LidButtonishLeg(20, +1, 20) children();
865 }
866
867 module Struts(x_start, z_min, th){
868   // if th is negative, starts at z_min and works towards -ve z
869   // and object should then be printed other way up
870   for (i= [1 : 1 : case_struts_count]) {
871     translate([0,
872                0,
873                z_min])
874       mirror([0,0, th<0 ? 1 : 0])
875       translate([0,
876                  -phone_height * i / (case_struts_count+1),
877                  case_struts_solid_below])
878       linear_extrude(height= abs(th)
879                      -(case_struts_solid_below+case_struts_solid_above))
880       rectfromto([               x_start, -0.5 * case_struts_width ],
881                  [ phone_width - x_start, +0.5 * case_struts_width ]);
882   }
883 }
884
885 module OrdinaryRearAperture(rhs,bot, pos){
886   Flip_rhs(rhs) Flip_bot(bot)
887     linextr(-20, 20)
888     mirror([0,1])
889     translate(pos + bumper)
890     children();
891 }
892
893 module MicroUSB(){
894   Flip_bot(1){
895     rotate([90,0,0])
896       mirror([0,0,1])
897       linextr(-epp2i[0], 60)
898       translate([0.5 * phone_width, 0, 0])
899       rectfromto([-microusb_width/2, epp2i[1] + microusb_below],
900                  [+microusb_width/2, epp0[1] + -microusb_above]);
901   }
902 }
903
904 module OrdinaryRearApertures(){
905   // rear speaker
906   OrdinaryRearAperture(1,1, rearspeaker_pos_bl)
907     rectfromto(-rearspeaker_gap,
908                rearspeaker_size + rearspeaker_gap);
909
910   // finger hole to remove phone
911   if (len(fingerpushhole_dias))
912     OrdinaryRearAperture(1,0, [ fingerpushhole_dias[0]/2 + epp2i[0],
913                                 phone[1]/2 ])
914     scale(fingerpushhole_dias)
915     circle(r= 0.5 );
916 }
917
918 module RearCameraAperture(){
919   Flip_rhs(1)
920     mirror([0, 0, 1])
921     linear_extrude(height = 20)
922     mirror([0, 1, 0])
923     translate(bumper)
924     rectfromto(camera_pos_tl, camera_pos_br);
925 }
926
927 module HingeLidProfile(){
928   hull(){
929     circleat(hppT, hp_r1);
930     circleat(lpp12, lp_r12);
931     polygon([lpp10,
932              lpp13 + [2,0],
933              lpp12,
934              hppT]);
935   }
936 }
937
938 module HingeBaseProfile(){
939   difference(){
940     hull(){
941       circleat(hppB, hp_r1);
942       circleat(hppE, hp_r1);
943       circleat(epp2o, case_th_bottom);
944       circleat(hppB + [10,0], hp_r1);
945     }
946     polygon([epp5, epp1, epp2i, epp3, bppL]);
947   }
948 }
949
950 module HingeLeverOuterProfile(){
951   hull(){
952     circleat(hppT, hp_r2);
953     circleat(hppB, hp_r2);
954   }
955 }
956
957 module HingeLeverInnerProfile(){
958   for (s = [-1,+1]) {
959     c = s > 0 ? hppT : hppB;
960     translate(c)
961       mirror([0,0,s>0])
962       rotate(s<0 ? -40 : 0)
963       hull()
964       for (x=[-20,20])
965         for (y=[0, s * 10])
966           translate([x,y])
967             circle(hp_rn);
968   }
969 }
970
971 module HingeLeverNutProfile(){
972   for (c= [hppB, hppT]) {
973     translate(c)
974       circle($fn=6, r= 0.5 * hingescrew_nut_across / cos(30));
975   }
976 }
977
978 module Flip_hinge(doflip=1){
979   hinge_origin = [0, -(phone_height - hppB[0]), hppB[1]];
980   translate(hinge_origin)
981     rotate([doflip*180,0,0])
982     translate(-hinge_origin)
983     children();
984 }
985
986 module HingePortion(x0,x1){
987   Flip_rhs() Flip_bot(1)
988     translate([x0,0,0])
989     mirror([1,0,0])
990     rotate([90,0,-90])
991     linear_extrude(height=x1-x0)
992     children();
993 }
994
995 module CatchPortion(xwidth, ztop){
996   width = catch_width + xwidth;
997   w = width + catch_topcurve_r*2 + 1;
998   translate([phone_width/2, 0,0]){
999     difference(){
1000       rotate([90,0,-90])
1001         linextr(-w/2, w/2)
1002         children(0);
1003       translate([0, 50, 0])
1004         rotate([90,0,0])
1005         linear_extrude(height=100){
1006         for (m=[0,1]) mirror([m,0,0]) {
1007           hull(){
1008             translate([w/2, ztop - catch_topcurve_r])
1009               circle(catch_topcurve_r);
1010             translate([w/2, -50])
1011               square(catch_topcurve_r*2, center=true);
1012           }
1013         }
1014       }
1015     }
1016   }
1017 }
1018
1019 module CaseBase(){
1020   AroundEdges(epp3[1], case_th_bottom, 1)
1021     EdgeProfile();
1022 }
1023
1024 function prop_x(gamma) = hp_k / (2 * sin(gamma/2)) - hppT[0];
1025
1026 module PropProfileAssignments(gamma){
1027   // https://en.wikipedia.org/wiki/Solution_of_triangles#Two_sides_and_the_included_angle_given_(SAS)
1028   x = prop_x(gamma);
1029   p = phone_height + prlp10[0] - hppB[0];
1030   b = p + x;
1031
1032   q = phone_height - hppT[0] - prcp1[0]; // $prpp7[0] is 0 by definition
1033   a = q + x;
1034   c = sqrt(a*a + b*b - 2*a*b*cos(gamma));
1035   $prp_alpha = acos( (b*b + c*c - a*a) / (2*b*c) );
1036
1037   $prp_theta = 90 - $prp_alpha;
1038   beta = 180 - $prp_alpha - gamma;
1039   psi = 90 - beta;
1040
1041   //echo("abc", a,b,c);
1042
1043   v1 = [ [ cos(psi), -sin(psi) ],    // x
1044          [ sin(psi),  cos(psi) ] ];  // y
1045
1046   $prpp7 = [0, c + (lpp13[1] - $prpp10[1] - hp_k) ];
1047
1048   $prp_r1 = prc_r1;
1049   $prp_r11 = prop_main_th/2;
1050
1051   $prpp1 = $prpp7 + [1,0] *
1052     // this is approximate, but will do
1053     (prop_main_th/2 + prop_prop_gap + prcp1[0] - cppA[0]);
1054   $prpp3 = $prpp1 +
1055     v1[0] * -$prp_r1 +
1056     v1[1] * ((prcp2[1] - prcp1[1]) - prop_prop_gap);
1057   $prpp12 = $prpp3 + v1[0] *
1058     (prop_end_dia + prop_caserecess_taper * ($prpp1[1] - $prpp3[1]));
1059   $prp_r8 = prop_main_th;
1060   $prpp4 = [ prop_main_th/2, $prpp3[1] ];
1061   $prp_r5 = $prp_r8;
1062   $prpp5 = [ $prpp12[0] - $prp_r5,
1063             $prpp3[1] - prop_prong_h + $prp_r5 ];
1064   $prpp6 = $prpp4 + [0,-1] * (prop_prong_h +
1065          prop_prong_heel_slope * ($prpp5[0] - $prpp4[0]));
1066   $prpp8 = $prpp4 + [0,-1] * $prp_r8;
1067   $prpp9 = $prpp8 + [-1,0] * $prp_r8;
1068
1069   children();
1070 }
1071
1072 module PropProfile(gamma, cut=0, rot=0){
1073   PropProfileAssignments(gamma){
1074
1075     //#circleat($prpp3,1);
1076     //#circleat($prpp12,1);
1077
1078     if (!cut) {
1079       hull(){
1080         translate($prpp8)
1081           intersection(){
1082             circle($prp_r8);
1083             polygon([[-20,-0], [20,20], [0,0]]);
1084           }
1085         rectfromto($prpp6, $prpp9);
1086         translate($prpp5) intersection(){
1087           circle($prp_r5);
1088           polygon([[-10,-10], [0,0], [10,0]]);
1089         }
1090         rectfromto($prpp12 + [0,-0.1], $prpp3);
1091       }
1092       hull(){
1093         circleat($prpp1, $prp_r1);
1094         rectfromto($prpp12 + [0,-0.1], $prpp3);
1095       }
1096     }
1097     // main shaft
1098     rotate([0,0, rot*-$prp_theta]){
1099       hull(){
1100         extra = cut ? prop_recess_slop : 0;
1101         rectfromto($prpp6, $prpp9);
1102         circleat($prpp11, $prp_r11 + extra);
1103         circleat($prpp10, $prp_r10 + extra);
1104       }
1105     }
1106   }
1107 }
1108
1109 module PropAggregateProfile(){
1110   for (angle = prop_angles)
1111     PropProfile(angle, 0,0);
1112 }
1113
1114 module Prop(){ ////toplevel
1115   hw = prop_main_width/2;
1116   linextr(-hw, +hw)
1117     PropAggregateProfile();
1118 }
1119
1120 module Case(){ ////toplevel
1121   difference(){
1122     union(){
1123       CaseBase();
1124
1125       // ledge (fixed keeper)
1126       Flip_rhs(1-keeper_side) intersection(){
1127         rotate([90, 0, 0])
1128           linear_extrude(height = phone_height + phone_cnr_rad * 2)
1129           KeeperProfile(1);
1130
1131         // outline of the whole case, to stop it protruding
1132         translate([0,0, -25])
1133           linear_extrude(height = 50)
1134           hull()
1135           Flip_bot()
1136           circleat([+1,-1] * phone_cnr_rad, phone_cnr_rad + case_th_side/2);
1137       }
1138
1139       // hinge
1140       HingePortion(hex20, hex21) HingeBaseProfile();
1141
1142       // buildout for prop recess
1143       if (prop_caserecess_buildout_r > 0) Flip_rhs(1)
1144         linextr(case_bottom_z, epp2i[1])
1145         hull() {
1146           for (dxs = [-1,+1])
1147             circleat([ prop_x_pos + dxs * prop_caserecess_buildout_r,
1148                        -epp2o[0] ],
1149                      r = epp2o[0] - prcp2[0]);
1150         }
1151     }
1152
1153     // slot for keeper
1154     Flip_rhs(keeper_side)
1155       translate([0, -phone_cnr_rad, 0])
1156       rotate([90, 0, 0])
1157       linear_extrude(height = phone_height + phone_cnr_rad * 2)
1158       minkowski(){
1159         KeeperProfile();
1160         rectfromto([ -keeper_gap_x,    -keeper_gap_z_bot ],
1161                    [ keeper_gap_x_holes,    +keeper_gap_z_top ]);
1162       }
1163
1164     // front camera
1165     RearCameraAperture();
1166
1167     // struts (invisible, because they're buried in the case)
1168     Struts(epp2i[0], epp2i[1] - case_th_bottom, case_th_bottom);
1169
1170     Buttons(){
1171       mirror([1,0,0])
1172         rotate([90,0,90]) {
1173           if (!($button_leg_only && enable_support))
1174           intersection(){
1175             translate([0,0,-10])
1176               linear_extrude(height= 20)
1177               ButtonPlan($button_l, 0,1);
1178             if ($button_leg_only)
1179               rotate([-90,90,0])
1180                 translate([phone_width/2, -400, kppe[1]])
1181                 mirror([1-abs($rhsflip - keeper_side),0,0])
1182                 cube([400, 800, 50]);
1183             if (enable_support && !$button_suppress_over_keeper)
1184               rotate([-90,90,0])
1185               translate([-400, -400, kppd[1]])
1186                 mirror([0,0,1])
1187                 cube([800,800,100]);
1188           }
1189           translate([0,0, -bppR[0]])
1190             linear_extrude(height= 20)
1191             ButtonPlan($button_l, 1,1);
1192         }
1193       
1194     }
1195
1196     // apertures along top edge
1197     CaseAperture(jack_pos, jack_dia, 8);
1198     Flip_rhs(1)
1199       CaseAperture(noisecancelmic_pos, noisecancelmic_dia, 8);
1200
1201     OrdinaryRearApertures();
1202
1203     MicroUSB();
1204
1205     // gaps for the lid's hinge arms
1206     HingePortion(hex20 - hinge_x_arms_gap,
1207                  hex21 + hinge_x_arms_gap)
1208       minkowski(){
1209         HingeLidProfile();
1210         circle(r= hinge_r_arms_gap, $fn= 8);
1211       }
1212
1213     // screw holes in the hinge arms
1214     HingeScrews();
1215
1216     // catch striker
1217     CatchPortion(catch_side_gap*2, epp4[1])
1218       CatchCutProfile();
1219
1220     // lanyard
1221     Flip_bot(1)
1222       translate([ly_o[0], -(phone_cnr_rad + ly_re), ly_o[1]])
1223       rotate([0, ly_theta, 0])
1224       rotate([0,0,90])
1225       LanyardCutout(lanyard_channel_len);
1226
1227     // prop recess
1228     Flip_rhs(1)
1229       translate([prop_x_pos,0,0])
1230       mirror([0,1,0])
1231       rotate([90,0,90])
1232       linextr(-prop_recess_hw, +prop_recess_hw)
1233       hull(){
1234         for (d=[ [0,0], [0,-1], [+1,-1/prop_caserecess_taper] ])
1235           circleat(prcp1 + 20*d,
1236                    prc_r3);
1237       }
1238   }
1239 }
1240
1241 module LidAdhocMultiprintFrame(phase){
1242   if (led_window_style >= 3) {
1243     AdhocMultiprintFrame(phase, lpp13[1], -1);
1244   }
1245 }
1246
1247 module LidAroundEdges(){
1248   AroundEdges(lpp15[1], lpp13[1] - lpp15[1], 0)
1249     children();
1250 }
1251
1252 module Lid(){ ////toplevel
1253   skew_centre = [0, lpp11[0], lpp11[1]];
1254   difference(){
1255     union(){
1256       intersection(){
1257         LidAroundEdges()
1258           LidEdgeProfile();
1259
1260         translate(skew_centre)
1261           multmatrix([[ 1, 0, 0, 0 ],
1262                       [ 0, 1, -lid_fold_clearance_skew, 0 ],
1263                       [ 0, 0, 1, 0 ],
1264                       [ 0, 0, 0, 1 ]])
1265           translate(-skew_centre)
1266           LidAroundEdges()
1267           LidEdgeFoldClearanceProfile();
1268       }
1269
1270       // button covers
1271       Buttons(){
1272         intersection(){
1273           rotate([90,0,90])
1274             translate([0,0,-10])
1275             linear_extrude(height= 20)
1276             ButtonPlan($button_l, 1,0);
1277           rotate([90,0,0])
1278              translate([0,0,-100])
1279             linear_extrude(height= 200)
1280             ButtonCoverProfile();
1281         }
1282       }
1283
1284       // hinge arms
1285       HingePortion(hex20, hex21) {
1286         LidEdgeProfile();
1287         HingeLidProfile();
1288       }
1289
1290       // catch
1291       CatchPortion(0, lpp12[1])
1292         CatchCatchProfile();
1293     }
1294     Struts(lpp10[0] + strut_min_at_end, lpp13[1], -case_th_lid);
1295
1296     // screw holes in the hinge arms
1297     HingeScrews();
1298
1299     // prop recess
1300     translate([prop_x_pos, -prlp10[0], prlp10[1]])
1301       mirror([0,1,0])
1302       rotate([90,0,90])
1303       linextr(-prop_recess_hw, +prop_recess_hw)
1304       hull()
1305       for (pa = prop_angles)
1306         PropProfile(pa, 1,1);
1307
1308     // notification led aperture
1309     if (led_window_style)
1310       translate([led_pos[0], -led_pos[1], lpp13[1]]) {
1311         translate([0,0,-10])
1312           cylinder(r=nla_r0, h=20);
1313         if (led_window_style >= 2)
1314           translate([0,0, -nla_t])
1315             cylinder(r=nla_r2, height=20);
1316       }
1317
1318     }
1319
1320   LidAdhocMultiprintFrame(1);
1321 }
1322
1323 module HingeLever(){ ////toplevel
1324   difference() {
1325     // outer body, positive
1326     HingePortion(hex22, hex22 + phone_width/2)
1327       HingeLeverOuterProfile();
1328
1329     // space for the screws
1330     HingePortion(hex26, hex24)
1331       HingeLeverInnerProfile();
1332
1333     // recesses for the nuts
1334     HingePortion(hex23, hex26+1)
1335       HingeLeverNutProfile();
1336
1337     // bores for the screws
1338     HingeScrews();
1339
1340     // space for the charging cable
1341     MicroUSB();
1342     Flip_hinge() MicroUSB();
1343   }
1344 }
1345
1346 module LidWindow(){ ////toplevel
1347   translate([led_pos[0], -led_pos[1], lpp13[1]])
1348     mirror([0,0,1])
1349     cylinder(r= nla_r1, h=nla_t);
1350   LidAdhocMultiprintFrame(0);
1351 }
1352
1353 module LidWindowPrint(){ ////toplevel
1354   rotate([0,180,0])
1355     LidWindow();
1356 }
1357
1358 module DemoLidWindowSelect(){
1359   translate([led_pos[0], led_pos[1], -100]) {
1360     translate([0, -30, 0]) cube([400, 400, 200]);
1361   }
1362 }
1363
1364 module DemoLidWindow(){ ////toplevel
1365   %Lid();
1366   LidWindow();
1367   translate([0,40,0]){
1368     color("blue") intersection(){ Lid(); DemoLidWindowSelect(); }
1369     color("red") intersection(){ LidWindow(); DemoLidWindowSelect(); }
1370   }
1371 }
1372
1373 module HingeLeverPrint(){ ////toplevel
1374   rotate([-90,0,0])
1375     translate([-phone_width/2, phone_height, 0])
1376     HingeLever();
1377 }
1378
1379 module TestSelectLength(){
1380   translate([-30, -200, -20])
1381     cube([30 + 15, 250, 40]);
1382 }
1383
1384 module TestLength(){ ////toplevel
1385   intersection(){
1386     Case();
1387     TestSelectLength();
1388   }
1389 }
1390
1391 module TestLengthRight(){ ////toplevel
1392   intersection(){
1393     Case();
1394     Flip_rhs(1)
1395       TestSelectLength();
1396   }
1397 }
1398
1399 module TestSelectWidth(){
1400   translate([-30, -(phone_height - 25), -20])
1401     mirror([0, 1, 0])
1402     cube([200, 50, 40]);
1403 }
1404
1405 module TestWidth(){ ////toplevel
1406   intersection(){
1407     Case();
1408     TestSelectWidth();
1409   }
1410 }
1411
1412 module TestLidWidthPrint(){ ////toplevel
1413   rotate([0,180.0]) intersection(){
1414     Lid();
1415     TestSelectWidth();
1416   }
1417 }
1418
1419 module TestSelectRearAperture(){
1420   minkowski(){
1421     union() children();
1422     translate([20, 0,0])
1423       cube([42, 2, 1], center=true);
1424   }
1425 }
1426
1427 module TestSelectCamera(){
1428   minkowski(){
1429     TestSelectRearAperture()
1430       RearCameraAperture();
1431     cube([0.1, 50, 0.1]);
1432   }
1433 }
1434
1435 module TestSelectOrdinaryRearApertures(){
1436   TestSelectRearAperture()
1437     OrdinaryRearApertures();
1438 }
1439
1440 module TestCamera(){ ////toplevel
1441   intersection(){
1442     Case();
1443     TestSelectCamera();
1444   }
1445 }
1446
1447 module TestLidByCamera(){ ////toplevel
1448   intersection(){
1449     Lid();
1450     TestSelectCamera();
1451   }
1452 }
1453
1454 module TestLidByCameraPrint(){ ////toplevel
1455   rotate([180,0,0]) TestLidByCamera();
1456 }
1457
1458 module DemoByCamera(){ ////toplevel
1459   color("blue") TestLidByCamera();
1460   color("red")  TestCamera();
1461 }
1462
1463 module OneKeeper(){ ////toplevel
1464   translate([0, -phone_cnr_rad, 0])
1465     rotate([90, 0, 0])
1466     linear_extrude(height = phone_height - phone_cnr_rad * 2)
1467     KeeperProfile();
1468 }
1469
1470 module OneKeeperPrint(){ ////toplevel
1471   rotate([0,180,0])
1472     OneKeeper();
1473 }
1474
1475 module LidPrint(){ ////toplevel
1476   rotate([0,180,0])
1477     Lid();
1478 }
1479
1480 module TestSelectFrame(){
1481   include = [1,-1] * (epp2i[0] + 4);
1482
1483   difference(){
1484     cube(1000, center=true);
1485     translate([0,0, -100])
1486       linear_extrude(height=200)
1487       rectfromto(include,  inside_br - include);
1488   }
1489 }
1490
1491 module TestSelectLidFrame(){
1492   TestSelectFrame();
1493   translate([led_pos[0], -led_pos[1], -50])
1494     cylinder(r= nla_r2+3, h=100);
1495 }
1496
1497 module TestFrameCase(){ ////toplevel
1498   intersection(){
1499     Case();
1500     union(){
1501       TestSelectFrame();
1502       TestSelectCamera();
1503       TestSelectOrdinaryRearApertures();
1504     }
1505   }
1506 }
1507
1508 module TestSelectTopApertures(){
1509   translate([-100, -35, -100])
1510     cube([400, 100, 200]);
1511   LidAdhocMultiprintFrame(0);
1512   LidAdhocMultiprintFrame(1);
1513 }
1514
1515 module TestTopApertures(){ ////toplevel
1516   intersection(){
1517     Case();
1518     TestSelectFrame();
1519     TestSelectTopApertures();
1520   }
1521 }
1522
1523 module TestLidTopAperturesPrint(){ ////toplevel
1524   rotate([0,180,0]) intersection(){
1525     Lid();
1526     TestSelectLidFrame();
1527     TestSelectTopApertures();
1528   }
1529 }
1530
1531 module TestLidWindowTopAperturesPrint(){ ////toplevel
1532   rotate([0,180,0]) intersection(){
1533     LidWindow();
1534     TestSelectTopApertures();
1535   }
1536 }
1537
1538 module TestFrameLidPrint(){ ////toplevel
1539   rotate([0,180,0]) intersection(){
1540     Lid();
1541     TestSelectLidFrame();
1542   }
1543 }
1544
1545 module ButtonPlanForDemo(z, deep, cut){
1546   translate([0,0,z])
1547     ButtonPlan(8, deep, cut);
1548 }
1549
1550 module HingeScrews(){
1551   Flip_rhs() Flip_bot(1){
1552     for (c= [ hppT, hppB ])
1553       translate([ hex20,
1554                   -c[0],
1555                   c[1] ]){
1556         rotate([0,90,0])
1557           translate([0,0,-.2])
1558           cylinder( r= hingescrew_shaft_dia/2,
1559                     h = hingescrew_shaft_len+0.2 );
1560         rotate([0,-90,0])
1561           translate([0,0,+.1])
1562           cylinder( r= hingescrew_head_dia/2, h = hingescrew_head_th );
1563       }
1564   }
1565 }
1566
1567 module DemoPropAngleSelect(c){
1568   color(c) difference(){
1569     union(){ children(); }
1570     translate([ prop_x_pos, -400, -200 ])
1571       cube([ 400,800,400 ]);
1572   }
1573 }
1574
1575 module DemoPropAngle(ang){
1576   hL = [0, -(phone_height - hppT[0]), hppT[1] - hp_k*2];
1577   hC = [0, -(phone_height - hppB[0]), hppB[1]];
1578
1579   translate(hL)
1580     rotate([ang/2,0,0])
1581     translate(-hL)
1582     translate(hC)
1583     rotate([ang/2,0,0])
1584     translate(-hC) {
1585       DemoPropAngleSelect("red") Case();
1586
1587       color("orange")
1588         translate([prop_x_pos, -prcp1[0], prcp1[1]])
1589         PropProfileAssignments(ang) {
1590           echo($prpp1);
1591           rotate([-$prp_theta, 0, 0])
1592           translate([0, $prpp1[0], -$prpp1[1]])
1593           rotate([90,0,-90])
1594           Prop();
1595         }
1596     }
1597
1598   translate([0,0, -hp_k*2])
1599     DemoPropAngleSelect("blue")
1600     Lid();
1601 }
1602
1603 module DemoPropAngles(){ ////toplevel
1604   for (i=[0 : len(prop_angles)-1])
1605     translate(i * [0, -100, 100])
1606     DemoPropAngle(prop_angles[i]);
1607 }
1608
1609 module DemoHingeAngle(ang1,ang2){
1610   hL = [0, -(phone_height - hppT[0]), hppT[1]];
1611   hC = [0, -(phone_height - hppB[0]), hppB[1]];
1612
1613   translate(hL)
1614     rotate([ang2,0,0])
1615     translate(-hL)
1616     translate(hC)
1617     rotate([ang1,0,0])
1618     translate(-hC) {
1619       color("red") Lid();
1620     }
1621
1622   color("blue") intersection(){
1623     Case();
1624     union(){
1625       translate([bppJ[0], -400, -200])
1626         mirror([1,0,0])
1627         cube([400, 800, 400]);
1628       translate([10, -400, -200])
1629         cube([10, 800, 400]);
1630     }
1631   }
1632 }
1633
1634 module DemoHingeAngles(){ ////toplevel
1635   angles = [ 0, 4, 8, 12 ];
1636   echo("angles",angles);
1637   for (i=[0 : len(angles)-1]) {
1638     translate(i * [0, 0, 30]) {
1639       DemoHingeAngle(0,angles[i]);
1640       translate([0, 200, 0])
1641         DemoHingeAngle(angles[i],0);
1642     }
1643   }
1644 }
1645
1646 module DemoSelectAdhocLeftRight(right=0) {
1647   translate([phone_width/2, -400, -100]) // , -15, -100  to cross-section
1648     mirror([1-right, 0,0])
1649     cube([400, 800, 200]);
1650 }
1651
1652 module DemoLeft(){ ////toplevel
1653   color("red")  intersection(){ Case(); DemoSelectAdhocLeftRight(); }
1654   color("blue") intersection(){ Lid();  DemoSelectAdhocLeftRight(); }
1655 }
1656
1657 module DemoFrame(){ ////toplevel
1658   color("red") TestFrameCase();
1659   color("blue") intersection(){ Lid(); TestSelectLidFrame(); }
1660   color("black") HingeScrews();
1661   %HingeLever();
1662 }
1663
1664 module DemoLanyardCutout(){ ////toplevel
1665   LanyardCutout(25);
1666 }
1667
1668 module DemoHingedFrame(){ ///toplevel
1669   color("red") TestFrameCase();
1670   translate([0,0, -2*hp_k])
1671   color("blue") intersection(){ Lid(); TestSelectLidFrame(); }
1672
1673   Flip_hinge(){
1674     color("orange") HingeLever();
1675     color("black") HingeScrews();
1676   }
1677 }
1678
1679 module DemoHinge(){ ////toplevel
1680   translate([ -0.5*phone_width, phone_height, hp_k*3 ]) {
1681     DemoFrame();
1682     translate([0,0, -hp_k*3])
1683       DemoHingedFrame();
1684   }
1685 }
1686
1687 module DemoProfiles(){ ////toplevel
1688   LidEdgeProfile();
1689   %EdgeProfile();
1690   KeeperProfile();
1691   translate([0,0,-1]) color("black") KeeperProfile(1);
1692   translate(ly_o){
1693     rotate(-ly_theta){
1694       translate([0,0,+1]) color("purple") LanyardMainChannelProfile();
1695       translate([0,0,+2]) color("red") LanyardCurveChannelProfile();
1696       translate([0, ly_q_z]){
1697         translate([0,0,-1]) color("blue") LanyardEntryChannelProfile();
1698         translate([ly_oec_y,0,-2]) color("black") LanyardEntryOuterProfile();
1699       }
1700     }
1701   }
1702   translate([0,0,-5]) color("white") translate(epp2i)
1703     rotate(-ly_theta)
1704     rectfromto([-15, 0],
1705                [+15, -max_case_bottom_edge_thickness]);
1706
1707   translate([0,20]) {
1708     LanyardMainChannelProfile();
1709     translate([0,0,1]) color("purple") LanyardCurveChannelProfile();
1710     translate([0,0,-1]) color("red") LanyardEntryChannelProfile();
1711   }
1712
1713   translate([20,0]) {
1714     LidEdgeProfile();
1715     %EdgeProfile();
1716
1717     demopoint_QR = [ bppS[0], bppQ[1] - 0.1];
1718   
1719     color("blue") ButtonCoverProfile();
1720     color("red") {
1721       rectfromto(bppQ, demopoint_QR);
1722       rectfromto(bppR, demopoint_QR);
1723     }
1724   }
1725
1726   translate([-20,0]) {
1727     color("black") ButtonPlanForDemo(-2, 0,1);
1728     color("red" )  ButtonPlanForDemo(-4, 1,1);
1729     color("blue")  ButtonPlanForDemo(-6, 1,0);
1730   }
1731
1732   translate([0, -30]) {
1733     %LidEdgeProfile();
1734     %EdgeProfile();
1735     color("blue") HingeLidProfile();
1736     color("red")  HingeBaseProfile();
1737     color("black") translate([0,0,-2]) HingeLeverOuterProfile();
1738   }
1739
1740   for (f=[0,1]) {
1741     translate([-30, -60 + 30*f]) {
1742       translate([0,0,-4]) EdgeProfile();
1743       %translate([0,0,-10]) HingeBaseProfile();
1744       translate([0,-2] * f * hp_k) {
1745         translate([0,0,-4]) LidEdgeProfile();
1746         %translate([0,0,-10]) %HingeLidProfile();
1747       }
1748       translate(+hppB) rotate([0,0,180*f]) translate(-hppB) {
1749         translate([0,0,-2]) color("black") HingeLeverOuterProfile(); 
1750         translate([0,0,0]) color("red") difference(){
1751           HingeLeverOuterProfile();
1752           HingeLeverInnerProfile();
1753         }
1754         translate([0,0,3]) color("yellow") HingeLeverNutProfile();
1755       }
1756     }
1757   }
1758
1759   translate([20,-30]) {
1760     %EdgeProfile();
1761     %LidEdgeProfile();
1762     //translate([0,0,1]) CatchCutProfile();
1763     color("blue") CatchCatchProfile();
1764     color("red") difference(){ EdgeProfile(); CatchCutProfile(); }
1765   }
1766
1767   translate([40,-30]) {
1768     difference(){
1769       LidEdgeProfile();
1770       translate(prlp10)
1771         PropProfile(10, 1, 0);
1772     }
1773     translate(prlp10)
1774       PropProfile(15, 0);
1775   }
1776   translate([60,-30]) {
1777     PropAggregateProfile();
1778   }
1779 }
1780
1781 //EdgeProfile();
1782 //KeeperProfile();
1783 //CaseBase();
1784 //%Case();
1785 //Keeper();
1786 //LidEdgeProfile();
1787 //KeeperProfile();
1788 //DemoProfiles();
1789 //PropRecess();