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