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