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