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