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