chiark / gitweb /
scaffold-clamp: wip straphook
[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 = 0.65;
214
215 foldover_gap = 0.50;
216 foldover_lever_gap = 0.50;
217
218 // properties of the hinge fasteners
219 hingescrew_shaft_dia = 2.0 + 0.25; // M2 x 12mm machine screw
220 hingescrew_shaft_len = 12;
221 hingescrew_fasteners_extra_thick = 0.40;
222 // ^ amount of thread protruding if everything was completely nominal
223 //   and we are using two nuts
224 hingescrew_nut_access_dia = 4.72 + 0.50;
225 // ^ washer is 4.72 dia
226 //   also, want to get pliers or tiny spanner in to do up locknut
227 hingescrew_nut_across = 3.92 + 0.25; // incl. slop around recess slop
228 hingescrew_nut_thick = 1.93;
229 hingescrew_head_th = 1.38 + 0.75;
230 hingescrew_head_dia = 3.92;
231
232 hingescrew_nut_recess_portion = 2/3; // portion of nut in recess
233
234 lever_cover_th = 0.75;
235 hingemount_th = 2.5;
236 hingemount_wd = 4.8725;
237
238 $fa = 5;
239 $fs = 0.1;
240
241 button_l_fudge = 4.4;
242 buttonishleg_default_l_is_fudge = 10;
243
244 hinge_base_slope = 1.5; // bigger is steeper
245
246 strut_min_at_end = 1.5;
247
248 hinge_x_gap = 0.125;
249 hinge_x_postscrew_gap = 0.75;
250 hinge_x_arms_gap = 0.35;
251 hinge_r_arms_gap = 0.55;
252
253 rearspeaker_gap    = [ 2.0, 2.0 ]; // each side
254
255 thumbrecess_depth = 1.3;
256 thumbrecess_width = 16.5;
257 thumbrecess_topcurve_r = 5.0;
258
259 prop_recess_under = 0.50;
260 prop_recess_slop = 0.200; // each side
261 prop_end_dia = 0.5;
262 prop_main_th = 3;
263 prop_taper_len = 6;
264 prop_main_width = 4;
265 prop_side_gap = 0.75; // each side
266 prop_lidrecess_behind = 0.75;
267 prop_caserecess_behind = 0.75;
268 prop_caserecess_taper = 0.45; // one side only
269 prop_prop_gap = 0.5;
270 prop_prong_heel_slope = 0.5;
271
272 lid_fold_clearance_antislop = 0.5;
273
274 $button_leg_only = false;
275 $suppress_forward_holes = false;
276
277 // ---------- calculated ----------
278
279 phone_width =  (phone + bumper*2)[0];
280 phone_height = (phone + bumper*2)[1];
281
282 inside_br = [phone_width, -phone_height];
283
284 prop_prong_h = prop_main_th;
285
286 //echo(camera_pos_tl + bumper,
287 //     camera_pos_br + bumper);
288
289 // ----- could be changed -----
290 lid_buttoncover_gap = lid_gap_x;
291 lid_buttoncover_overlap = case_th_lip + keeper_gap_z_top;
292
293 phone_backside_slope_thick = phone_total_thick - phone_edge_thick;
294
295 //prop_lidrecess_depth = case_th_lid - prop_recess_under;
296
297 //prop_nose_len = case_th_lid - prop_recess_under;
298 //prop_recess_slope = tan(prop_max_angle); // bigger means steeper
299 //prop_recess_width = prop_main_th / cos(prop_max_angle) + prop_backfwd_gap;
300
301
302 //lid_lip_overlap_width xxx bad name = ;
303 //lid_lip_inner_slope = [ 5, 5 ]; // xxx
304
305 epp0 = [0,0];
306 epp1 = [0, -phone_edge_thick];
307 epp2i = epp1 + phone_backside_slope_thick * [ phone_backside_slope_inner, -1 ];
308 epp2o = epp1 + phone_backside_slope_thick * [ phone_backside_slope_outer, -1 ];
309 epp3 = epp2i + [10, 0];
310 epp5 = epp0 + [0,1] * (keeper_th_z + keeper_gap_z_top + case_lip);
311 epp4 = epp5 + [-1,0] * case_th_side;
312
313 kppe = [0,0];
314 kppd = kppe + [1,0] * keeper_inner_width;
315 kppc = kppd + [0,1] * keeper_th_z;
316 kppb = [ kppe[0] - keeper_th_x, kppc[1] ];
317 kppf = kppe - [0,1] * keeper_inner_height;
318 kppa = [ kppb[0], kppf[1] ];
319
320 lpp10 = [ epp5[0] + lid_gap_x, kppc[1] + lid_gap_z ];
321 lpp11 = [ lpp10[0],            epp5[1] + lid_gap_z ];
322
323 lpp14 = lpp10 + [1,0] * max(keeper_inner_width, lid_edgepart_width);
324 // exact x posn not very important; must extend past end of keeper
325
326 lpp15 = [ lpp14[0],
327           epp0[1] - phone_rim_depth + 1/2.5 * case_th_lid
328           + lid_screen_gap_extra ];
329 // ^ beam theory says to maximise force before contact,
330 //   the gap below the `beam' (the lid) must be 1/3
331 //   the thickness (ie the lid thickness) if the beam
332 //   is solid, or 1/2 if it has a top and bottom only.
333 //   ours is mostly solid.
334
335 lp_r12 = max(case_th_lid - (lpp11[1] - lpp15[1]),
336              case_th_lip);
337
338 lpp12 = [ epp4[0] + lp_r12,    lpp11[1] ];
339 lpp13 = [ lpp12[0],            lpp12[1] + lp_r12 ];
340
341 case_bottom_z = epp2o[1] - case_th_bottom;
342
343 // button profile
344 bppM = epp4 + [0,5];
345 bppN = [ bppM[0] + lid_buttoncover_thick, bppM[1] ];
346 bppR = [ bppN[0] + lid_buttoncover_gap, -button_cutout_depth ];
347 bppS = [ epp1[0], bppR[1] ];
348 bppQ = [ bppM[0], bppR[1] - lid_buttoncover_overlap ];
349 bppP = bppQ + [0,1] * lid_buttoncover_gap;
350 bppO = [ bppN[0], bppP[1] ];
351 bppL = lpp10 + [5,0];
352 bppK = [ bppL[0], bppN[1] ];
353 bppJ = [ bppN[0], bppL[1] ];
354 bppU = [ bppJ[0], lpp12[1] ];
355 bppV = lpp11;
356 bppW = lpp10;
357
358 echo("BUTTON COVER TH", bppO[0] - bppP[0]);
359
360 // notification led aperture
361
362 nla_r0 = led_aperture/2;
363 nla_r1 = nla_r0 + led_window_ledge;
364 nla_r2 = nla_r1 + multicolour_gap;
365 nla_t =
366   led_window_style >= 3 ? initial_layer_thick :
367   led_window_style >= 2 ? led_window_ledge : 0;
368
369
370 // hinge plan
371 hp_rn = hingescrew_nut_access_dia/2;
372 hp_r2_min = hp_rn + lever_cover_th;
373 hp_rs = hingescrew_shaft_dia/2;
374 hp_r1_min = hp_rs + hingemount_th;
375
376 hp_r1 = max(hp_r1_min, hp_r2_min);
377 hp_r2 = hp_r1;
378
379 hppU = lpp13;
380 hppS = epp2o + [0,-1] * case_th_bottom;
381 hp_k = 0.5 * (hppU[1] - hppS[1] + foldover_gap);
382
383 hppM = [ epp4[0] - foldover_lever_gap - hp_r2,
384          0.5 * (hppU + hppS)[1] ];
385 hppT = [ hppM[0], hppU[1] - hp_r1 ];
386 hppB = hppT + [0,-1] * hp_k;
387
388 hppE_y = epp2o[1] - case_th_bottom + hp_r1;
389 hppE_x = hppB[0] + (hppB[1] - hppE_y) * hinge_base_slope;
390 hppE = [ hppE_x, hppE_y ];
391
392 // hinge elevation x coords
393
394 hex20 = max(epp2o[0],
395             phone_cnr_rad,
396             kppd[0] + hingescrew_head_th + keeper_gap_x_holes);
397 hex21 = hex20 + hingemount_wd;
398 hex22 = hex21 + hinge_x_gap;
399 hex27 = hex20 + hingescrew_shaft_len;
400 hex24 = hex27 + hinge_x_postscrew_gap;
401 hex23 = hex27 - (hingescrew_nut_thick*2
402                  + hingescrew_fasteners_extra_thick);
403 hex26 = hex23 + hingescrew_nut_thick * 2/3;
404
405 echo(hex20, hex21, hex22, hex23, hex24);
406 //  6, 10.8725, 10.9975, 13.74, 18.75
407 module chk(act,exp) {
408   if (abs(act-exp) > 1e-9) echo("WRONG", act, exp);
409   else echo("ok", act);
410 }
411 chk(hex20, 6);
412 chk(hex21, 10.8725);
413 chk(hex22, 10.9975);
414 chk(hex23, 13.74);
415 chk(hex24, 18.75);
416
417 lid_fold_clearance_skew =
418   (lpp10[1] - hppB[1]) /
419   (lpp10[0] - hppB[0]);
420
421 echo("SK",lid_fold_clearance_skew);
422
423 // thumb recess (used to be "catch" hence cpp*
424
425 cppA = epp4 + [thumbrecess_depth, 0];
426 cppB = [ cppA[0], epp1[1] ];
427
428 // lanyard
429
430 ly_r = lanyard_half_dia / 2;
431 ly_rc = ly_r * 2;
432
433 ly_theta = -atan2vector(epp2i - epp1);
434 ly_o = epp2i + 3 * ly_r * unitvector2d(epp1 - epp2i);
435
436 max_case_bottom_edge_thickness =
437   case_th_bottom
438   + sin(ly_theta) * (epp2i-epp2o)[0];
439
440 ly_q_z = -(ly_rc + ly_r);
441 ly_re = max_case_bottom_edge_thickness - (-ly_q_z);
442
443 ly_oec_y = lanyard_entry_rel_breadth * ly_r;
444
445 // prop recess in case
446
447 prop_x_pos = phone_width/2;
448
449 prop_recess_hw = 0.5 * prop_main_width + prop_side_gap;
450
451 prc_r1 = prop_end_dia/2;
452 prc_r3 = prc_r1 + prop_recess_slop;
453
454 prcp2 = [ epp4[0] + prop_buildout_less,
455           case_bottom_z ];
456
457 prop_caserecess_buildout_r = -1; // prcp2[0] - epp2o[0];
458
459 prcp1 = [ epp2o[0] + prc_r3 + prop_caserecess_behind,
460           epp2i[1] - prc_r3 - prop_recess_under];
461
462 // prop recess in lid
463
464 prl_r10 = prop_end_dia/2;
465 prl_r10o = prl_r10 + prop_recess_slop;
466
467 prlp10 = lpp10 + [1,1] * prl_r10o
468   + [1,0] * prop_lidrecess_behind
469   + [0,1] * prop_recess_under;
470
471 // prop
472
473 $prpp10 = [0,0];
474 $prpp11 = [0, prop_taper_len];
475
476 $prp_r10 = prl_r10;
477
478 // ---------- modules ----------
479
480 module AdhocMultiprintFrame(phase, z0, zs) {
481   // from z0 to z0 + zs*layer
482   extra = phase * (initial_layer_width + multicolour_gap) + 5;
483   xextra = extra + -epp4[0];
484   xrange = [ 0, phone_width ] + [-1,+1] * xextra;
485   yextra = extra + -epp4[0];
486   yrange = [ -phone_height + +hppB[0] - hp_r2, 0 ] + [-1,+1] * yextra;
487   p0 = [ xrange[0], yrange[0] ];
488   p1 = [ xrange[1], yrange[1] ];
489   echo(p0, p1);
490   translate([0,0, z0])
491     mirror([0,0, zs<0 ? 1 : 0])
492     linear_extrude(height= initial_layer_thick)
493     difference(){
494       rectfromto(p0 - [1,1] * initial_layer_width,
495                  p1 + [1,1] * initial_layer_width);
496       rectfromto(p0, p1);
497     }
498 }
499
500 module KeeperProfile(slant=0){
501   use_e = kppe + [0,-1] * slant * keeper_inner_width / keeper_slant_slope;
502   polygon([use_e, kppd, kppc, kppb, kppa, kppf]);
503 }
504
505 module EdgeProfile(){
506   difference(){
507     hull(){
508       translate(epp3) square(case_th_bottom*2, center=true);
509       circleat(epp2o, r=case_th_bottom);
510       circleat(epp1, r=case_th_side);
511       rectfromto(epp0, epp4);
512     }
513     polygon([ epp5 + [0,10],
514               epp1,
515               epp2i,
516               epp3 + [10,0] ]);
517   }
518 }
519
520 module LanyardLanyardProfile(entry=false){
521   hull(){
522     for (xs=[-1,+1] * (entry ? lanyard_entry_rel_breadth : 1))
523       translate(xs * 0.5 * lanyard_half_dia * [1,0])
524         circle(r= lanyard_half_dia/2);
525   }
526 }
527
528 module LanyardCurveChannelProfile(){
529   translate([0, -ly_r])
530     LanyardLanyardProfile();
531 }  
532
533 module LanyardEntryChannelProfile(){
534   LanyardLanyardProfile(true);
535 }  
536
537 module LanyardMainChannelProfile(){
538   LanyardCurveChannelProfile();
539   difference(){
540     square(center=true, ly_r * [6, 2]);
541     for (xs=[-1,+1])
542       translate(ly_r * [3 * xs, -1])
543         circle(r = ly_r);
544   }
545 }
546
547 module LanyardEntryOuterProfile(){
548   circleat([ly_re + ly_r, 0], ly_re);
549 }
550
551 module LanyardEntry(){
552   q_z = ly_q_z;
553   oec_y = ly_oec_y;
554
555   d_x = -ly_rc;
556
557   translate([d_x, 0, q_z]) {
558     intersection(){
559       rotate([90,0,0])
560         rotate_extrude(convexity=10)
561         rotate(90)
562         translate([0, -q_z])
563         LanyardCurveChannelProfile();
564       translate([0,-10,0])
565         cube([20,20,20]);
566     }
567   }
568
569   mirror([0,0,1])
570     translate([0,0,-1])
571     linear_extrude(height=20)
572     rotate(-90)
573     LanyardEntryChannelProfile();
574
575   translate([0, ly_r*2, 0])
576     rotate([90,0,0])
577     linear_extrude(height = ly_r*4){
578     difference(){
579       rectfromto([d_x, q_z], [ly_r, 0]);
580       circleat([d_x, q_z], ly_rc);
581     }
582   }
583
584   translate([0,0,q_z]){
585     for (my=[0,1])
586       mirror([0,my,0]){
587         translate([0, oec_y, 0]){
588           difference(){
589             translate(ly_re * [-1,0,-2])
590               cube(ly_re * [2,1,2]);
591             rotate_extrude(convexity=10)
592               LanyardEntryOuterProfile();
593           }
594         }
595       }
596     difference(){
597       translate([-ly_re, -(oec_y + 0.01), -2*ly_re])
598         cube([ly_re*2, 2*(oec_y + 0.01), 2*ly_re]);
599       for (mx=[0,1])
600         mirror([mx,0,0])
601           rotate([90,0,0])
602           translate([0,0,-10])
603           linear_extrude(height=20)
604           LanyardEntryOuterProfile();
605     }
606   }
607 }
608
609 module LanyardCutout(l){
610   rotate([0,-90,0])
611     linear_extrude(height=l)
612     rotate(-90)
613     LanyardMainChannelProfile();
614
615   for (ee=[0,1]){
616     translate(ee * l * [-1,0])
617       mirror([ee,0,0])
618       LanyardEntry();
619   }
620 }
621
622 module LidEdgeProfile(){
623   polygon([ lpp10,
624             lpp11,
625             lpp12,
626             lpp13,
627             lpp13 + [10, 0],
628             lpp15 + [10, 0],
629             lpp15,
630             lpp14,
631             ]);
632   intersection(){
633     circleat(lpp12, r=lp_r12);
634     rectfromto( lpp12 + [-10,   0],
635                 lpp12 + [+10, +10] );
636   }
637 }
638
639 module LidEdgeFoldClearanceProfile(){
640   translate([-lid_fold_clearance_antislop, 0])
641     polygon([ lpp10,
642               lpp11,
643               lpp11 + [-20,  0],
644               lpp11 + [-20, 20],
645               lpp11 + [+20, 20],
646               lpp10 + [+20,  0] ]);
647 }
648
649 module ButtonCoverProfile(){
650   intersection(){
651     polygon(concat([ bppM, bppP, bppO, bppJ ],
652                    (enable_support && !$button_suppress_over_keeper
653                     ? [ bppU, bppV, bppW ] : []),
654                    [ bppL, bppK ]));
655     hull(){
656       EdgeProfile();
657       LidEdgeProfile();
658     }
659   }
660 }
661
662 module ButtonPlan(l, deep, cut){
663   epsilon =
664     (cut  ? 0 : lid_buttoncover_gap);
665
666   delta =
667     (deep ? lid_buttoncover_overlap : 0);
668
669   C = [0,0]; // by definition
670   T = [ 0, epp4[1] ];
671   G = T + [0,10];
672
673   B0 = C + [0,-1] * button_cutout_depth;
674   B1 = B0 + [0,1] * epsilon;
675
676   r0 = 0.5 * (T[1] - B0[1]);
677   A = [  -(l + button_l_fudge)/2 + r0, 0.5 * (T[1] + B0[1]) ];
678   H = A + [0,-1] * delta;
679
680   D = A + [-2,0] * r0;
681   F = D + [0,10];
682
683   E0 = 0.5 * (D + A);
684   E1 = E0 + [1,0] * epsilon;
685
686   I0 = [ E0[0], H[1] ];
687   I1 = [ E1[0], H[1] ];
688
689   hull(){
690     for (m=[0,1]) mirror([m,0])
691       circleat(H, r0 - epsilon);
692   }
693   for (m=[0,1]) mirror([m,0]) {
694     difference(){
695       polygon([ E1,
696                 I1,
697                 H,
698                 B1,
699                 G,
700                 F,
701                 D
702                 ]);
703       circleat(D, r0 + epsilon);
704     }
705   }
706 }
707
708 module ButtonCoverReinf(){ ////toplevel
709   minkowski(){
710     rotate([90,0,0])
711       linear_extrude(height=0.01)
712       intersection(){
713         ButtonCoverProfile();
714         translate([bppJ[0] + 0.1, -50]) mirror([1,0])
715           square([100,100]);
716     }
717     mirror([0,0,1]) linear_extrude(height=0.01) intersection(){
718       circle(r= lid_buttoncover_reinf);
719       translate([-20,0]) square(40, center=true);
720     }
721   }
722 }
723
724 module ThumbRecessCutProfile(){
725   difference(){
726     polygon([ cppA + [-10,0],
727               cppB + [-10,0],
728               cppB,
729               cppA ]);
730     circleat(epp1, r=case_th_side);
731   }
732 }
733
734 module Flip_rhs(yn=[0,1]) {
735   for ($rhsflip=yn) {
736     translate([phone_width/2, 0, 0])
737       mirror([$rhsflip,0,0])
738       translate([-phone_width/2, 0, 0])
739       children();
740   }
741 }
742
743 module Flip_bot(yn=[0,1]) {
744   for ($botflip=yn) {
745     translate([0, -phone_height/2, 0])
746       mirror([0, $botflip, 0])
747       translate([0, phone_height/2, 0])
748       children();
749   }
750 }  
751
752 module AroundEdges(fill_zstart, fill_th, fill_downwards=0){
753   // sides
754   Flip_rhs(){
755     translate([0, -phone_cnr_rad, 0])
756       rotate([90,0,0])
757       linear_extrude(height = phone_height - phone_cnr_rad*2)
758       children(0);
759   }
760   // corners
761   Flip_rhs() Flip_bot() {
762     translate([+1,-1] * phone_cnr_rad)
763       intersection(){
764         rotate_extrude()
765           intersection(){
766             mirror([1,0,0])
767               translate([-1,0] * phone_cnr_rad)
768               children(0);
769             rectfromto([0,-20],[10,20]);
770           }
771         translate([-10, 0, -20] + 0.01 * [+1,-1, 0] )
772           cube([10,10,40]);
773       }
774   }
775   // top and bottom
776   Flip_bot(){
777     translate([ phone_width - phone_cnr_rad, 0,0 ])
778       rotate([90,0,-90])
779       linear_extrude(height = phone_width - phone_cnr_rad*2)
780       children(0);
781   }
782   // fill
783   translate([0,0, fill_zstart])
784     mirror([0,0, fill_downwards])
785     linear_extrude(height = fill_th)
786     rectfromto([+1,-1] * phone_cnr_rad,
787                [phone_width, -phone_height] + [-1,+1] * phone_cnr_rad);
788 }
789
790 module CaseAperture(pos, dia, $fn) {
791   theta = 180/$fn;
792   translate([ pos[0] + bumper[0],
793               -epp2i[0],
794               -pos[1] ])
795     rotate([-90, theta, 0])
796     cylinder(r = dia/2 / cos(theta),
797              h = 60);
798 }
799
800 module SideButton(y, y_ref_sign, l, suppress_over_keeper=0){
801   // y_ref_sign:
802   //   +1  measured from top    of actual phone to top    of button
803   //   -1  measured from bottom of actual phone to bottom of button
804   //    0  y is centre of button in coordinate system
805   $button_l= l;
806   $button_suppress_over_keeper= suppress_over_keeper;
807   eff_y = y_ref_sign > 0 ?         -bumper [1] -y -l/2 :
808           y_ref_sign < 0 ? (-phone -bumper)[1] +y +l/2 :
809           y;
810   //echo(eff_y);
811   translate([0, eff_y, 0])
812     children();
813 }
814
815 module LidButtonishLeg(y, y_ref_sign, l=buttonishleg_default_l_is_fudge) {
816   $button_leg_only = true;
817   SideButton(y, y_ref_sign, l) children();
818 }
819
820 module Buttons(){
821   Flip_rhs(1) SideButton(15.580, +1, 8.9     ) children(); // power
822   Flip_rhs(1) SideButton(48.700, -1, 8.920   ) children(); // camera
823   Flip_rhs(0) SideButton(30.800, +1, 21.96, 1) children(); // volume
824   Flip_rhs(   ) LidButtonishLeg(14, -1) children();
825 //  Flip_rhs(0) LidButtonishLeg(20, +1, 20) children();
826 }
827
828 module Struts(x_start, z_min, th){
829   // if th is negative, starts at z_min and works towards -ve z
830   // and object should then be printed other way up
831   for (i= [1 : 1 : case_struts_count]) {
832     translate([0,
833                0,
834                z_min])
835       mirror([0,0, th<0 ? 1 : 0])
836       translate([0,
837                  -phone_height * i / (case_struts_count+1),
838                  case_struts_solid_below])
839       linear_extrude(height= abs(th)
840                      -(case_struts_solid_below+case_struts_solid_above))
841       rectfromto([               x_start, -0.5 * case_struts_width ],
842                  [ phone_width - x_start, +0.5 * case_struts_width ]);
843   }
844 }
845
846 module OrdinaryRearAperture(rhs,bot, pos){
847   Flip_rhs(rhs) Flip_bot(bot)
848     linextr(-20, 20)
849     mirror([0,1])
850     translate(pos + bumper)
851     children();
852 }
853
854 module MicroUSB(){
855   Flip_bot(1){
856     rotate([90,0,0])
857       mirror([0,0,1])
858       linextr(-epp2i[0], 60)
859       translate([0.5 * phone_width, 0, 0])
860       rectfromto([-microusb_width/2, epp2i[1] + microusb_below],
861                  [+microusb_width/2, epp0[1] + -microusb_above]);
862   }
863 }
864
865 module OrdinaryRearApertures(){
866   // rear speaker
867   OrdinaryRearAperture(1,1, rearspeaker_pos_bl)
868     rectfromto(-rearspeaker_gap,
869                rearspeaker_size + rearspeaker_gap);
870
871   // finger hole to remove phone
872   if (len(fingerpushhole_dias))
873     OrdinaryRearAperture(1,0, [ fingerpushhole_dias[0]/2 + epp2i[0],
874                                 phone[1]/2 ])
875     scale(fingerpushhole_dias)
876     circle(r= 0.5 );
877 }
878
879 module RearCameraAperture(){
880   Flip_rhs(1)
881     mirror([0, 0, 1])
882     linear_extrude(height = 20)
883     mirror([0, 1, 0])
884     translate(bumper)
885     rectfromto(camera_pos_tl, camera_pos_br);
886 }
887
888 module HingeLidProfile(){
889   hull(){
890     circleat(hppT, hp_r1);
891     circleat(lpp12, lp_r12);
892     polygon([lpp10,
893              lpp13 + [2,0],
894              lpp12,
895              hppT]);
896   }
897 }
898
899 module HingeBaseProfile(){
900   difference(){
901     hull(){
902       circleat(hppB, hp_r1);
903       circleat(hppE, hp_r1);
904       circleat(epp2o, case_th_bottom);
905       circleat(hppB + [10,0], hp_r1);
906     }
907     polygon([epp5, epp1, epp2i, epp3, bppL]);
908   }
909 }
910
911 module HingeLeverOuterProfile(){
912   hull(){
913     circleat(hppT, hp_r2);
914     circleat(hppB, hp_r2);
915   }
916 }
917
918 module HingeLeverInnerProfile(){
919   for (s = [-1,+1]) {
920     c = s > 0 ? hppT : hppB;
921     translate(c)
922       mirror([0,0,s>0])
923       rotate(s<0 ? -40 : 0)
924       hull()
925       for (x=[-20,20])
926         for (y=[0, s * 10])
927           translate([x,y])
928             circle(hp_rn);
929   }
930 }
931
932 module HingeLeverNutProfile(){
933   for (c= [hppB, hppT]) {
934     translate(c)
935       circle($fn=6, r= 0.5 * hingescrew_nut_across / cos(30));
936   }
937 }
938
939 module Flip_hinge(doflip=1){
940   hinge_origin = [0, -(phone_height - hppB[0]), hppB[1]];
941   translate(hinge_origin)
942     rotate([doflip*180,0,0])
943     translate(-hinge_origin)
944     children();
945 }
946
947 module HingePortion(x0,x1){
948   Flip_rhs() Flip_bot(1)
949     translate([x0,0,0])
950     mirror([1,0,0])
951     rotate([90,0,-90])
952     linear_extrude(height=x1-x0)
953     children();
954 }
955
956 module ThumbRecessApply(ztop){
957   width = thumbrecess_width;
958   w = width + thumbrecess_topcurve_r*2 + 1;
959   translate([phone_width/2, 0,0]){
960     difference(){
961       rotate([90,0,-90])
962         linextr(-w/2, w/2)
963         children(0);
964       translate([0, 50, 0])
965         rotate([90,0,0])
966         linear_extrude(height=100){
967         for (m=[0,1]) mirror([m,0,0]) {
968           hull(){
969             translate([w/2, ztop - thumbrecess_topcurve_r])
970               circle(thumbrecess_topcurve_r);
971             translate([w/2, -50])
972               square(thumbrecess_topcurve_r*2, center=true);
973           }
974         }
975       }
976     }
977   }
978 }
979
980 module CaseBase(){
981   AroundEdges(epp3[1], case_th_bottom, 1)
982     EdgeProfile();
983 }
984
985 function prop_x(gamma) = hp_k / (2 * sin(gamma/2)) - hppT[0];
986
987 module PropProfileAssignments(gamma){
988   // https://en.wikipedia.org/wiki/Solution_of_triangles#Two_sides_and_the_included_angle_given_(SAS)
989   x = prop_x(gamma);
990   p = phone_height + prlp10[0] - hppB[0];
991   b = p + x;
992
993   q = phone_height - hppT[0] - prcp1[0]; // $prpp7[0] is 0 by definition
994   a = q + x;
995   c = sqrt(a*a + b*b - 2*a*b*cos(gamma));
996   $prp_alpha = acos( (b*b + c*c - a*a) / (2*b*c) );
997
998   $prp_theta = 90 - $prp_alpha;
999   beta = 180 - $prp_alpha - gamma;
1000   psi = 90 - beta;
1001
1002   //echo("abc", a,b,c);
1003
1004   v1 = [ [ cos(psi), -sin(psi) ],    // x
1005          [ sin(psi),  cos(psi) ] ];  // y
1006
1007   $prpp7 = [0, c + (lpp13[1] - $prpp10[1] - hp_k) ];
1008
1009   $prp_r1 = prc_r1;
1010   $prp_r11 = prop_main_th/2;
1011
1012   $prpp1 = $prpp7 + [1,0] *
1013     // this is approximate, but will do
1014     (prop_main_th/2 + prop_prop_gap + prcp1[0] - cppA[0]);
1015   $prpp3 = $prpp1 +
1016     v1[0] * -$prp_r1 +
1017     v1[1] * ((prcp2[1] - prcp1[1]) - prop_prop_gap);
1018   $prpp12 = $prpp3 + v1[0] *
1019     (prop_end_dia + prop_caserecess_taper * ($prpp1[1] - $prpp3[1]));
1020   $prp_r8 = prop_main_th;
1021   $prpp4 = [ prop_main_th/2, $prpp3[1] ];
1022   $prp_r5 = $prp_r8;
1023   $prpp5 = [ $prpp12[0] - $prp_r5,
1024             $prpp3[1] - prop_prong_h + $prp_r5 ];
1025   $prpp6 = $prpp4 + [0,-1] * (prop_prong_h +
1026          prop_prong_heel_slope * ($prpp5[0] - $prpp4[0]));
1027   $prpp8 = $prpp4 + [0,-1] * $prp_r8;
1028   $prpp9 = $prpp8 + [-1,0] * $prp_r8;
1029
1030   children();
1031 }
1032
1033 module PropProfile(gamma, cut=0, rot=0){
1034   PropProfileAssignments(gamma){
1035
1036     //#circleat($prpp3,1);
1037     //#circleat($prpp12,1);
1038
1039     if (!cut) {
1040       hull(){
1041         translate($prpp8)
1042           intersection(){
1043             circle($prp_r8);
1044             polygon([[-20,-0], [20,20], [0,0]]);
1045           }
1046         rectfromto($prpp6, $prpp9);
1047         translate($prpp5) intersection(){
1048           circle($prp_r5);
1049           polygon([[-10,-10], [0,0], [10,0]]);
1050         }
1051         rectfromto($prpp12 + [0,-0.1], $prpp3);
1052       }
1053       hull(){
1054         circleat($prpp1, $prp_r1);
1055         rectfromto($prpp12 + [0,-0.1], $prpp3);
1056       }
1057     }
1058     // main shaft
1059     rotate([0,0, rot*-$prp_theta]){
1060       hull(){
1061         extra = cut ? prop_recess_slop : 0;
1062         rectfromto($prpp6, $prpp9);
1063         circleat($prpp11, $prp_r11 + extra);
1064         circleat($prpp10, $prp_r10 + extra);
1065       }
1066     }
1067   }
1068 }
1069
1070 module PropAggregateProfile(){
1071   for (angle = prop_angles)
1072     PropProfile(angle, 0,0);
1073 }
1074
1075 module Prop(){ ////toplevel
1076   hw = prop_main_width/2;
1077   linextr(-hw, +hw)
1078     PropAggregateProfile();
1079 }
1080
1081 module Case(){ ////toplevel
1082   difference(){
1083     union(){
1084       CaseBase();
1085
1086       // ledge (fixed keeper)
1087       Flip_rhs(1-keeper_side) intersection(){
1088         rotate([90, 0, 0])
1089           linear_extrude(height = phone_height + phone_cnr_rad * 2)
1090           KeeperProfile(1);
1091
1092         // outline of the whole case, to stop it protruding
1093         translate([0,0, -25])
1094           linear_extrude(height = 50)
1095           hull()
1096           Flip_bot()
1097           circleat([+1,-1] * phone_cnr_rad, phone_cnr_rad + case_th_side/2);
1098       }
1099
1100       // hinge
1101       HingePortion(hex20, hex21) HingeBaseProfile();
1102
1103       // buildout for prop recess
1104       if (prop_caserecess_buildout_r > 0) Flip_rhs(1)
1105         linextr(case_bottom_z, epp2i[1])
1106         hull() {
1107           for (dxs = [-1,+1])
1108             circleat([ prop_x_pos + dxs * prop_caserecess_buildout_r,
1109                        -epp2o[0] ],
1110                      r = epp2o[0] - prcp2[0]);
1111         }
1112     }
1113
1114     // slot for keeper
1115     Flip_rhs(keeper_side)
1116       translate([0, -phone_cnr_rad, 0])
1117       rotate([90, 0, 0])
1118       linear_extrude(height = phone_height + phone_cnr_rad * 2)
1119       minkowski(){
1120         KeeperProfile();
1121         rectfromto([ -keeper_gap_x,    -keeper_gap_z_bot ],
1122                    [ keeper_gap_x_holes,    +keeper_gap_z_top ]);
1123       }
1124
1125     // front camera
1126     RearCameraAperture();
1127
1128     // struts (invisible, because they're buried in the case)
1129     Struts(epp2i[0], epp2i[1] - case_th_bottom, case_th_bottom);
1130
1131     Buttons(){
1132       mirror([1,0,0])
1133         rotate([90,0,90]) {
1134           if (!($button_leg_only && enable_support))
1135           intersection(){
1136             translate([0,0,-10])
1137               linear_extrude(height= 20)
1138               ButtonPlan($button_l, 0,1);
1139             if ($button_leg_only)
1140               rotate([-90,90,0])
1141                 translate([phone_width/2, -400, kppe[1]])
1142                 mirror([1-abs($rhsflip - keeper_side),0,0])
1143                 cube([400, 800, 50]);
1144             if (enable_support && !$button_suppress_over_keeper)
1145               rotate([-90,90,0])
1146               translate([-400, -400, kppd[1]])
1147                 mirror([0,0,1])
1148                 cube([800,800,100]);
1149           }
1150           translate([0,0, -bppR[0]])
1151             linear_extrude(height= 20)
1152             ButtonPlan($button_l, 1,1);
1153         }
1154       
1155     }
1156
1157     // apertures along top edge
1158     if (!$suppress_forward_holes) {
1159       CaseAperture(jack_pos, jack_dia, 8);
1160       Flip_rhs(1)
1161         CaseAperture(noisecancelmic_pos, noisecancelmic_dia, 8);
1162     }
1163
1164     OrdinaryRearApertures();
1165
1166     MicroUSB();
1167
1168     // gaps for the lid's hinge arms
1169     HingePortion(hex20 - hinge_x_arms_gap,
1170                  hex21 + hinge_x_arms_gap)
1171       minkowski(){
1172         HingeLidProfile();
1173         circle(r= hinge_r_arms_gap, $fn= 8);
1174       }
1175
1176     // screw holes in the hinge arms
1177     HingeScrews();
1178
1179     // thumb recess
1180     ThumbRecessApply(epp4[1])
1181       ThumbRecessCutProfile();
1182
1183     // lanyard
1184     Flip_bot(1)
1185       translate([ly_o[0], -(phone_cnr_rad + ly_re), ly_o[1]])
1186       rotate([0, ly_theta, 0])
1187       rotate([0,0,90])
1188       LanyardCutout(lanyard_channel_len);
1189
1190     // prop recess
1191     Flip_rhs(1)
1192       translate([prop_x_pos,0,0])
1193       mirror([0,1,0])
1194       rotate([90,0,90])
1195       linextr(-prop_recess_hw, +prop_recess_hw)
1196       hull(){
1197         for (d=[ [0,0], [0,-1], [+1,-1/prop_caserecess_taper] ])
1198           circleat(prcp1 + 20*d,
1199                    prc_r3);
1200       }
1201   }
1202 }
1203
1204 module LidAdhocMultiprintFrame(phase){
1205   if (led_window_style >= 3) {
1206     AdhocMultiprintFrame(phase, lpp13[1], -1);
1207   }
1208 }
1209
1210 module LidAroundEdges(){
1211   AroundEdges(lpp15[1], lpp13[1] - lpp15[1], 0)
1212     children();
1213 }
1214
1215 module Lid(){ ////toplevel
1216   skew_centre = [0, lpp11[0], lpp11[1]];
1217   difference(){
1218     union(){
1219       intersection(){
1220         LidAroundEdges()
1221           LidEdgeProfile();
1222
1223         translate(skew_centre)
1224           multmatrix([[ 1, 0, 0, 0 ],
1225                       [ 0, 1, -lid_fold_clearance_skew, 0 ],
1226                       [ 0, 0, 1, 0 ],
1227                       [ 0, 0, 0, 1 ]])
1228           translate(-skew_centre)
1229           LidAroundEdges()
1230           LidEdgeFoldClearanceProfile();
1231       }
1232
1233       // button covers
1234       Buttons(){
1235         intersection(){
1236           rotate([90,0,90])
1237             translate([0,0,-10])
1238             linear_extrude(height= 20)
1239             ButtonPlan($button_l, 1,0);
1240           union(){
1241             rotate([90,0,0])
1242               translate([0,0,-100])
1243               linear_extrude(height= 200)
1244               ButtonCoverProfile();
1245             hull()
1246               for (y= [-1,+1] * (($button_l + button_l_fudge)/2
1247                                  - lid_buttoncover_reinf))
1248                 translate([0,y,0])
1249                   ButtonCoverReinf();
1250           }
1251         }
1252       }
1253
1254       // hinge arms
1255       HingePortion(hex20, hex21) {
1256         LidEdgeProfile();
1257         HingeLidProfile();
1258       }
1259     }
1260     Struts(lpp10[0] + strut_min_at_end, lpp13[1], -case_th_lid);
1261
1262     // screw holes in the hinge arms
1263     HingeScrews();
1264
1265     // prop recess
1266     translate([prop_x_pos, -prlp10[0], prlp10[1]])
1267       mirror([0,1,0])
1268       rotate([90,0,90])
1269       linextr(-prop_recess_hw, +prop_recess_hw)
1270       hull()
1271       for (pa = prop_angles)
1272         PropProfile(pa, 1,1);
1273
1274     // notification led aperture
1275     if (led_window_style)
1276       translate([led_pos[0], -led_pos[1], lpp13[1]]) {
1277         translate([0,0,-10])
1278           cylinder(r=nla_r0, h=20);
1279         if (led_window_style >= 2)
1280           translate([0,0, -nla_t])
1281             cylinder(r=nla_r2, height=20);
1282       }
1283
1284     }
1285
1286   LidAdhocMultiprintFrame(1);
1287 }
1288
1289 module HingeLever(){ ////toplevel
1290   difference() {
1291     // outer body, positive
1292     HingePortion(hex22, hex22 + phone_width/2)
1293       HingeLeverOuterProfile();
1294
1295     // space for the screws
1296     HingePortion(hex26, hex24)
1297       HingeLeverInnerProfile();
1298
1299     // recesses for the nuts
1300     HingePortion(hex23, hex26+1)
1301       HingeLeverNutProfile();
1302
1303     // bores for the screws
1304     HingeScrews();
1305
1306     // space for the charging cable
1307     MicroUSB();
1308     Flip_hinge() MicroUSB();
1309   }
1310 }
1311
1312 module LidWindow(){ ////toplevel
1313   translate([led_pos[0], -led_pos[1], lpp13[1]])
1314     mirror([0,0,1])
1315     cylinder(r= nla_r1, h=nla_t);
1316   LidAdhocMultiprintFrame(0);
1317 }
1318
1319 module LidWindowPrint(){ ////toplevel
1320   rotate([0,180,0])
1321     LidWindow();
1322 }
1323
1324 module DemoLidWindowSelect(){
1325   translate([led_pos[0], led_pos[1], -100]) {
1326     translate([0, -30, 0]) cube([400, 400, 200]);
1327   }
1328 }
1329
1330 module DemoLidWindow(){ ////toplevel
1331   %Lid();
1332   LidWindow();
1333   translate([0,40,0]){
1334     color("blue") intersection(){ Lid(); DemoLidWindowSelect(); }
1335     color("red") intersection(){ LidWindow(); DemoLidWindowSelect(); }
1336   }
1337 }
1338
1339 module HingeLeverPrint(){ ////toplevel
1340   rotate([-90,0,0])
1341     translate([-phone_width/2, phone_height, 0])
1342     HingeLever();
1343 }
1344
1345 module TestSelectLength(){
1346   translate([-30, -200, -20])
1347     cube([30 + 15, 250, 40]);
1348 }
1349
1350 module TestLength(){ ////toplevel
1351   intersection(){
1352     Case();
1353     TestSelectLength();
1354   }
1355 }
1356
1357 module TestLengthRight(){ ////toplevel
1358   intersection(){
1359     Case();
1360     Flip_rhs(1)
1361       TestSelectLength();
1362   }
1363 }
1364
1365 module TestSelectWidth(){
1366   translate([-30, -(phone_height - 25), -20])
1367     mirror([0, 1, 0])
1368     cube([200, 50, 40]);
1369 }
1370
1371 module TestWidth(){ ////toplevel
1372   intersection(){
1373     Case();
1374     TestSelectWidth();
1375   }
1376 }
1377
1378 module TestLidWidthPrint(){ ////toplevel
1379   rotate([0,180.0]) intersection(){
1380     Lid();
1381     TestSelectWidth();
1382   }
1383 }
1384
1385 module TestSelectRearAperture(){
1386   minkowski(){
1387     union() children();
1388     translate([20, 0,0])
1389       cube([42, 2, 1], center=true);
1390   }
1391 }
1392
1393 module TestSelectCamera(){
1394   minkowski(){
1395     TestSelectRearAperture()
1396       RearCameraAperture();
1397     cube([0.1, 50, 0.1]);
1398   }
1399 }
1400
1401 module TestSelectOrdinaryRearApertures(){
1402   TestSelectRearAperture()
1403     OrdinaryRearApertures();
1404 }
1405
1406 module TestCamera(){ ////toplevel
1407   intersection(){
1408     Case();
1409     TestSelectCamera();
1410   }
1411 }
1412
1413 module TestLidByCamera(){ ////toplevel
1414   intersection(){
1415     Lid();
1416     TestSelectCamera();
1417   }
1418 }
1419
1420 module TestLidByCameraPrint(){ ////toplevel
1421   rotate([180,0,0]) TestLidByCamera();
1422 }
1423
1424 module DemoByCamera(){ ////toplevel
1425   color("blue") TestLidByCamera();
1426   color("red")  TestCamera();
1427 }
1428
1429 module OneKeeper(){ ////toplevel
1430   translate([0, -phone_cnr_rad, 0])
1431     rotate([90, 0, 0])
1432     linear_extrude(height = phone_height - phone_cnr_rad * 2)
1433     KeeperProfile();
1434 }
1435
1436 module OneKeeperPrint(){ ////toplevel
1437   rotate([0,180,0])
1438     OneKeeper();
1439 }
1440
1441 module LidPrint(){ ////toplevel
1442   rotate([0,180,0])
1443     Lid();
1444 }
1445
1446 module TestSelectFrame(){
1447   include = [1,-1] * (epp2i[0] + 4);
1448
1449   difference(){
1450     cube(1000, center=true);
1451     translate([0,0, -100])
1452       linear_extrude(height=200)
1453       rectfromto(include,  inside_br - include);
1454   }
1455 }
1456
1457 module TestSelectLidFrame(){
1458   TestSelectFrame();
1459   translate([led_pos[0], -led_pos[1], -50])
1460     cylinder(r= nla_r2+3, h=100);
1461 }
1462
1463 module TestFrameCase(){ ////toplevel
1464   intersection(){
1465     Case();
1466     union(){
1467       TestSelectFrame();
1468       TestSelectCamera();
1469       TestSelectOrdinaryRearApertures();
1470     }
1471   }
1472 }
1473
1474 module TestSelectTopApertures(){
1475   translate([-100, -35, -100])
1476     cube([400, 100, 200]);
1477   LidAdhocMultiprintFrame(0);
1478   LidAdhocMultiprintFrame(1);
1479 }
1480
1481 module TestTopApertures(){ ////toplevel
1482   intersection(){
1483     Case();
1484     TestSelectFrame();
1485     TestSelectTopApertures();
1486   }
1487 }
1488
1489 module TestLidTopAperturesPrint(){ ////toplevel
1490   rotate([0,180,0]) intersection(){
1491     Lid();
1492     TestSelectLidFrame();
1493     TestSelectTopApertures();
1494   }
1495 }
1496
1497 module TestLidWindowTopAperturesPrint(){ ////toplevel
1498   rotate([0,180,0]) intersection(){
1499     LidWindow();
1500     TestSelectTopApertures();
1501   }
1502 }
1503
1504 module TestFrameLidPrint(){ ////toplevel
1505   rotate([0,180,0]) intersection(){
1506     Lid();
1507     TestSelectLidFrame();
1508   }
1509 }
1510
1511 module ButtonPlanForDemo(z, deep, cut){
1512   translate([0,0,z])
1513     ButtonPlan(8, deep, cut);
1514 }
1515
1516 module HingeScrews(){
1517   Flip_rhs() Flip_bot(1){
1518     for (c= [ hppT, hppB ])
1519       translate([ hex20,
1520                   -c[0],
1521                   c[1] ]){
1522         rotate([0,90,0])
1523           translate([0,0,-.2])
1524           cylinder( r= hingescrew_shaft_dia/2,
1525                     h = hingescrew_shaft_len+0.2 );
1526         rotate([0,-90,0])
1527           translate([0,0,+.1])
1528           cylinder( r= hingescrew_head_dia/2, h = hingescrew_head_th );
1529       }
1530   }
1531 }
1532
1533 module DemoPropAngleSelect(c){
1534   color(c) difference(){
1535     union(){ children(); }
1536     translate([ prop_x_pos, -400, -200 ])
1537       cube([ 400,800,400 ]);
1538   }
1539 }
1540
1541 module DemoPropAngle(ang){
1542   hL = [0, -(phone_height - hppT[0]), hppT[1] - hp_k*2];
1543   hC = [0, -(phone_height - hppB[0]), hppB[1]];
1544
1545   translate(hL)
1546     rotate([ang/2,0,0])
1547     translate(-hL)
1548     translate(hC)
1549     rotate([ang/2,0,0])
1550     translate(-hC) {
1551       DemoPropAngleSelect("red") Case();
1552
1553       color("orange")
1554         translate([prop_x_pos, -prcp1[0], prcp1[1]])
1555         PropProfileAssignments(ang) {
1556           echo($prpp1);
1557           rotate([-$prp_theta, 0, 0])
1558           translate([0, $prpp1[0], -$prpp1[1]])
1559           rotate([90,0,-90])
1560           Prop();
1561         }
1562     }
1563
1564   translate([0,0, -hp_k*2])
1565     DemoPropAngleSelect("blue")
1566     Lid();
1567 }
1568
1569 module DemoPropAngles(){ ////toplevel
1570   for (i=[0 : len(prop_angles)-1])
1571     translate(i * [0, -100, 100])
1572     DemoPropAngle(prop_angles[i]);
1573 }
1574
1575 module DemoHingeAngle(ang1,ang2){
1576   hL = [0, -(phone_height - hppT[0]), hppT[1]];
1577   hC = [0, -(phone_height - hppB[0]), hppB[1]];
1578
1579   translate(hL)
1580     rotate([ang2,0,0])
1581     translate(-hL)
1582     translate(hC)
1583     rotate([ang1,0,0])
1584     translate(-hC) {
1585       color("red") Lid();
1586     }
1587
1588   color("blue") intersection(){
1589     Case();
1590     union(){
1591       translate([bppJ[0], -400, -200])
1592         mirror([1,0,0])
1593         cube([400, 800, 400]);
1594       translate([10, -400, -200])
1595         cube([10, 800, 400]);
1596     }
1597   }
1598 }
1599
1600 module DemoHingeAngles(){ ////toplevel
1601   angles = [ 0, 4, 8, 12 ];
1602   echo("angles",angles);
1603   for (i=[0 : len(angles)-1]) {
1604     translate(i * [0, 0, 30]) {
1605       DemoHingeAngle(0,angles[i]);
1606       translate([0, 200, 0])
1607         DemoHingeAngle(angles[i],0);
1608     }
1609   }
1610 }
1611
1612 module DemoSelectAdhocLeftRight(right=0) {
1613   translate([phone_width/2, -400, -100]) // , -15, -100  to cross-section
1614     mirror([1-right, 0,0])
1615     cube([400, 800, 200]);
1616 }
1617
1618 module DemoLeft(){ ////toplevel
1619   color("red")  intersection(){ Case(); DemoSelectAdhocLeftRight(); }
1620   color("blue") intersection(){ Lid();  DemoSelectAdhocLeftRight(); }
1621 }
1622
1623 module DemoFrame(){ ////toplevel
1624   color("red") TestFrameCase();
1625   color("blue") intersection(){ Lid(); TestSelectLidFrame(); }
1626   color("black") HingeScrews();
1627   %HingeLever();
1628 }
1629
1630 module DemoLanyardCutout(){ ////toplevel
1631   LanyardCutout(25);
1632 }
1633
1634 module DemoHingedFrame(){ ///toplevel
1635   color("red") TestFrameCase();
1636   translate([0,0, -2*hp_k])
1637   color("blue") intersection(){ Lid(); TestSelectLidFrame(); }
1638
1639   Flip_hinge(){
1640     color("orange") HingeLever();
1641     color("black") HingeScrews();
1642   }
1643 }
1644
1645 module DemoHinge(){ ////toplevel
1646   translate([ -0.5*phone_width, phone_height, hp_k*3 ]) {
1647     DemoFrame();
1648     translate([0,0, -hp_k*3])
1649       DemoHingedFrame();
1650   }
1651 }
1652
1653 module DemoProfiles(){ ////toplevel
1654   LidEdgeProfile();
1655   %EdgeProfile();
1656   KeeperProfile();
1657   translate([0,0,-1]) color("black") KeeperProfile(1);
1658   translate(ly_o){
1659     rotate(-ly_theta){
1660       translate([0,0,+1]) color("purple") LanyardMainChannelProfile();
1661       translate([0,0,+2]) color("red") LanyardCurveChannelProfile();
1662       translate([0, ly_q_z]){
1663         translate([0,0,-1]) color("blue") LanyardEntryChannelProfile();
1664         translate([ly_oec_y,0,-2]) color("black") LanyardEntryOuterProfile();
1665       }
1666     }
1667   }
1668   translate([0,0,-5]) color("white") translate(epp2i)
1669     rotate(-ly_theta)
1670     rectfromto([-15, 0],
1671                [+15, -max_case_bottom_edge_thickness]);
1672
1673   translate([0,20]) {
1674     LanyardMainChannelProfile();
1675     translate([0,0,1]) color("purple") LanyardCurveChannelProfile();
1676     translate([0,0,-1]) color("red") LanyardEntryChannelProfile();
1677   }
1678
1679   translate([20,0]) {
1680     LidEdgeProfile();
1681     %EdgeProfile();
1682
1683     demopoint_QR = [ bppS[0], bppQ[1] - 0.1];
1684   
1685     color("blue") ButtonCoverProfile();
1686     color("red") {
1687       rectfromto(bppQ, demopoint_QR);
1688       rectfromto(bppR, demopoint_QR);
1689     }
1690   }
1691
1692   translate([-20,0]) {
1693     color("black") ButtonPlanForDemo(-2, 0,1);
1694     color("red" )  ButtonPlanForDemo(-4, 1,1);
1695     color("blue")  ButtonPlanForDemo(-6, 1,0);
1696   }
1697
1698   translate([0, -30]) {
1699     %LidEdgeProfile();
1700     %EdgeProfile();
1701     color("blue") HingeLidProfile();
1702     color("red")  HingeBaseProfile();
1703     color("black") translate([0,0,-2]) HingeLeverOuterProfile();
1704   }
1705
1706   for (f=[0,1]) {
1707     translate([-30, -60 + 30*f]) {
1708       translate([0,0,-4]) EdgeProfile();
1709       %translate([0,0,-10]) HingeBaseProfile();
1710       translate([0,-2] * f * hp_k) {
1711         translate([0,0,-4]) LidEdgeProfile();
1712         %translate([0,0,-10]) %HingeLidProfile();
1713       }
1714       translate(+hppB) rotate([0,0,180*f]) translate(-hppB) {
1715         translate([0,0,-2]) color("black") HingeLeverOuterProfile(); 
1716         translate([0,0,0]) color("red") difference(){
1717           HingeLeverOuterProfile();
1718           HingeLeverInnerProfile();
1719         }
1720         translate([0,0,3]) color("yellow") HingeLeverNutProfile();
1721       }
1722     }
1723   }
1724
1725   translate([20,-30]) {
1726     %EdgeProfile();
1727     %LidEdgeProfile();
1728     //translate([0,0,1]) ThumbRecessCutProfile();
1729     translate([0,0,+1]) color("red")
1730       difference(){ EdgeProfile(); ThumbRecessCutProfile(); }
1731   }
1732
1733   translate([40,-30]) {
1734     difference(){
1735       LidEdgeProfile();
1736       translate(prlp10)
1737         PropProfile(10, 1, 0);
1738     }
1739     translate(prlp10)
1740       PropProfile(15, 0);
1741   }
1742   translate([60,-30]) {
1743     PropAggregateProfile();
1744   }
1745 }
1746
1747 //EdgeProfile();
1748 //KeeperProfile();
1749 //CaseBase();
1750 //%Case();
1751 //Keeper();
1752 //LidEdgeProfile();
1753 //KeeperProfile();
1754 //DemoProfiles();
1755 //PropRecess();