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