chiark / gitweb /
fairphone-case: make lid thicker to protect screen better
[reprap-play.git] / fairphone-case.scad
1 // -*- C -*-
2
3 include <utils.scad>
4
5 phone = [ 75.0, 145.0 ];
6
7 prop_buildout_less = 3;
8
9 prop_angles = [ 15, 30, 45, 60 ];
10
11 bumper = [ 0.250, -0.025 ];
12 // ^ One side.  Overall size is increased by twice this.
13 // If no bumpers, is the gap around the phone.
14
15 enable_support = 1;
16
17 led_window_style = 3;
18 // 0: no window
19 // 1: simply an opening
20 // 2: opening with separate cover model, for printing in clear (two colour)
21 // 3: like 2 but one-layer window for ad-hoc multi-colour
22
23 initial_layer_thick = 0.400; // ^ needed for mode 3 only
24 initial_layer_width = 0.125; // ^ needed for mode 3 only
25 multicolour_gap = 0.15; // each side
26
27 phone_cnr_rad = 6.0;
28
29 button_cutout_depth = 9;
30
31 phone_edge_thick = 9.0;
32 phone_total_thick = 12.0;
33 phone_backside_slope_inner = 1.5; // larger means shallower
34 phone_backside_slope_outer = 1.0; // larger means shallower
35
36 camera_pos_tl = [  6.450, 12.750 ]; // measured from tl corner
37 camera_pos_br = [ 22.300, 37.600 ]; // tl/br as seen from back
38
39 jack_pos = [ 13.83, 8.485 ];
40 jack_dia = 10.64 + .5; // some jack I had lying around
41
42 led_pos = [ 12.50, 8.00 ];
43 led_aperture = 5;
44 led_window_ledge = 0.75; // each side
45
46 noisecancelmic_pos = [ 19.54, 7.37 ];   // from rhs
47 noisecancelmic_dia = 4.00;
48
49 //fingerpushhole_dias = [ 15, 18 ];
50 fingerpushhole_dias = [];
51
52 rearspeaker_pos_bl = [ 12.64, 18.72 ];
53 rearspeaker_size   = [  3.76,  7.36 ];
54
55 microusb_above = 3.27 - 0.25;
56 microusb_below = 0.0;
57 microusb_width = 16.12 + 1.25;
58
59 case_th_bottom = 2.5;
60 case_th_lid = 3.0;
61 case_th_side = 2;
62 case_th_lip = 1.2;
63
64 case_struts_count = 6;
65 case_struts_solid_below = 1.00;
66 case_struts_solid_above = 0.75;
67 case_struts_width = 0.10;
68
69 keeper_th_z = 0.75;
70 keeper_th_x = 0.75;
71 keeper_inner_width = 2.75;
72 keeper_inner_height = 2.75;
73 keeper_slant_slope = 2; // larger means steeper
74
75 keeper_gap_z_top = 0.25;
76 keeper_gap_z_bot = 0.75;
77 keeper_gap_x     = 0.25;
78 keeper_gap_x_holes = 0.75;
79
80 keeper_side = 0; // 0 = lhs; 1 = rhs
81
82 case_lip = 1.25;
83
84 lid_gap_x = 0.25;
85 lid_gap_z = 0.25;
86 lid_lip = 1.75;
87
88 catch_slop = 0.50;
89
90 foldover_gap = 0.50;
91 foldover_lever_gap = 0.50;
92
93 // properties of the hinge fasteners
94 hingescrew_shaft_dia = 2.0 + 0.25; // M2 x 12mm machine screw
95 hingescrew_shaft_len = 12;
96 hingescrew_fasteners_extra_thick = 0.40;
97 // ^ amount of thread protruding if everything was completely nominal
98 //   and we are using two nuts
99 hingescrew_nut_access_dia = 4.72 + 0.50;
100 // ^ washer is 4.72 dia
101 //   also, want to get pliers or tiny spanner in to do up locknut
102 hingescrew_nut_across = 3.92 + 0.25; // incl. slop around recess slop
103 hingescrew_nut_thick = 1.93;
104 hingescrew_head_th = 1.38 + 0.75;
105 hingescrew_head_dia = 3.92;
106
107 hingescrew_nut_recess_portion = 2/3; // portion of nut in recess
108
109 lever_cover_th = 0.75;
110 hingemount_th = 2.5;
111 hingemount_wd = 4.8725;
112
113 $fa = 5;
114 $fs = 0.1;
115
116 button_l_fudge = 4.4;
117 buttonishleg_default_l_is_fudge = 10;
118
119 hinge_base_slope = 1.5; // bigger is steeper
120
121 strut_min_at_end = 1.5;
122
123 hinge_x_gap = 0.125;
124 hinge_x_postscrew_gap = 0.75;
125 hinge_x_arms_gap = 0.35;
126 hinge_r_arms_gap = 0.55;
127
128 rearspeaker_gap    = [ 2.0, 2.0 ]; // each side
129
130 catch_len = 7.5;
131 catch_width = 15;
132 catch_thickness = 1.0;
133 catch_side_gap = 0.75; // each side
134
135 catch_depth = 0.75;
136 catch_height = 0.35;
137 catch_finger_height = 1.5;
138 catch_finger_depth = 2.5;
139
140 prop_recess_under = 0.50;
141 prop_recess_slop = 0.200; // each side
142 prop_end_dia = 0.5;
143 prop_main_th = 3;
144 prop_taper_len = 6;
145 prop_main_width = 4;
146 prop_side_gap = 0.75; // each side
147 prop_lidrecess_behind = 0.75;
148 prop_caserecess_behind = 0.75;
149 prop_caserecess_taper = 0.45; // one side only
150 prop_prop_gap = 0.5;
151 prop_prong_heel_slope = 0.5;
152
153 // ---------- calculated ----------
154
155 phone_width =  (phone + bumper*2)[0];
156 phone_height = (phone + bumper*2)[1];
157
158 inside_br = [phone_width, -phone_height];
159
160 prop_prong_h = prop_main_th;
161
162 //echo(camera_pos_tl + bumper,
163 //     camera_pos_br + bumper);
164
165 // ----- could be changed -----
166 lid_buttoncover_gap = lid_gap_x;
167 lid_buttoncover_overlap = case_th_lip + keeper_gap_z_top;
168
169 phone_backside_slope_thick = phone_total_thick - phone_edge_thick;
170
171 //prop_lidrecess_depth = case_th_lid - prop_recess_under;
172
173 //prop_nose_len = case_th_lid - prop_recess_under;
174 //prop_recess_slope = tan(prop_max_angle); // bigger means steeper
175 //prop_recess_width = prop_main_th / cos(prop_max_angle) + prop_backfwd_gap;
176
177
178 //lid_lip_overlap_width xxx bad name = ;
179 //lid_lip_inner_slope = [ 5, 5 ]; // xxx
180
181 epp0 = [0,0];
182 epp1 = [0, -phone_edge_thick];
183 epp2i = epp1 + phone_backside_slope_thick * [ phone_backside_slope_inner, -1 ];
184 epp2o = epp1 + phone_backside_slope_thick * [ phone_backside_slope_outer, -1 ];
185 epp3 = epp2i + [10, 0];
186 epp5 = epp0 + [0,1] * (keeper_th_z + keeper_gap_z_top + case_lip);
187 epp4 = epp5 + [-1,0] * case_th_side;
188
189 kppe = [0,0];
190 kppd = kppe + [1,0] * keeper_inner_width;
191 kppc = kppd + [0,1] * keeper_th_z;
192 kppb = [ kppe[0] - keeper_th_x, kppc[1] ];
193 kppf = kppe - [0,1] * keeper_inner_height;
194 kppa = [ kppb[0], kppf[1] ];
195
196 lpp10 = [ epp5[0] + lid_gap_x, kppc[1] + lid_gap_z ];
197 lpp11 = [ lpp10[0],            epp5[1] + lid_gap_z ];
198
199 lp_r12 = case_th_lid - (lpp11[1] - lpp10[1]);
200
201 lpp12 = [ epp4[0] + lp_r12,    lpp11[1] ];
202 lpp13 = [ lpp12[0],            lpp12[1] + lp_r12 ];
203
204 case_bottom_z = epp2o[1] - case_th_bottom;
205
206 // button profile
207 bppM = epp4 + [0,5];
208 bppN = [ 0.5 * (epp0[0] + epp4[0]), bppM[1] ];
209 bppR = [ bppN[0] + lid_buttoncover_gap, -button_cutout_depth ];
210 bppS = [ epp1[0], bppR[1] ];
211 bppQ = [ bppM[0], bppR[1] - lid_buttoncover_overlap ];
212 bppP = bppQ + [0,1] * lid_buttoncover_gap;
213 bppO = [ bppN[0], bppP[1] ];
214 bppL = lpp10 + [5,0];
215 bppK = [ bppL[0], bppN[1] ];
216 bppJ = [ bppN[0], bppL[1] ];
217 bppU = [ bppJ[0], lpp12[1] ];
218 bppV = lpp11;
219 bppW = lpp10;
220
221 // notification led aperture
222
223 nla_r0 = led_aperture/2;
224 nla_r1 = nla_r0 + led_window_ledge;
225 nla_r2 = nla_r1 + multicolour_gap;
226 nla_t =
227   led_window_style >= 3 ? initial_layer_thick :
228   led_window_style >= 2 ? led_window_ledge : 0;
229
230
231 // hinge plan
232 hp_rn = hingescrew_nut_access_dia/2;
233 hp_r2_min = hp_rn + lever_cover_th;
234 hp_rs = hingescrew_shaft_dia/2;
235 hp_r1_min = hp_rs + hingemount_th;
236
237 hp_r1 = max(hp_r1_min, hp_r2_min);
238 hp_r2 = hp_r1;
239
240 hppU = lpp13;
241 hppS = epp2o + [0,-1] * case_th_bottom;
242 hp_k = 0.5 * (hppU[1] - hppS[1] + foldover_gap);
243
244 hppM = [ epp4[0] - foldover_lever_gap - hp_r2,
245          0.5 * (hppU + hppS)[1] ];
246 hppT = [ hppM[0], hppU[1] - hp_r1 ];
247 hppB = hppT + [0,-1] * hp_k;
248
249 hppE_y = epp2o[1] - case_th_bottom + hp_r1;
250 hppE_x = hppB[0] + (hppB[1] - hppE_y) * hinge_base_slope;
251 hppE = [ hppE_x, hppE_y ];
252
253 // hinge elevation x coords
254
255 hex20 = max(epp2o[0],
256             phone_cnr_rad,
257             kppd[0] + hingescrew_head_th + keeper_gap_x_holes);
258 hex21 = hex20 + hingemount_wd;
259 hex22 = hex21 + hinge_x_gap;
260 hex27 = hex20 + hingescrew_shaft_len;
261 hex24 = hex27 + hinge_x_postscrew_gap;
262 hex23 = hex27 - (hingescrew_nut_thick*2
263                  + hingescrew_fasteners_extra_thick);
264 hex26 = hex23 + hingescrew_nut_thick * 2/3;
265
266 echo(hex20, hex21, hex22, hex23, hex24);
267 //  6, 10.8725, 10.9975, 13.74, 18.75
268 module chk(act,exp) {
269   if (abs(act-exp) > 1e-9) echo("WRONG", act, exp);
270   else echo("ok", act);
271 }
272 chk(hex20, 6);
273 chk(hex21, 10.8725);
274 chk(hex22, 10.9975);
275 chk(hex23, 13.74);
276 chk(hex24, 18.75);
277
278 // catch
279
280 cppJ = [ epp4[0] + catch_thickness, lpp10[1] ];
281 cppA = cppJ + [lid_gap_x, -lid_gap_z];
282 cppB = [ epp0[0], cppA[1] ];
283 cppP = [ epp4[0], cppJ[1] ];
284
285 cppS = cppJ + [0,-1] * catch_len;
286 cppD = [ cppA[0], cppS[1] + catch_slop ];
287 cppC = [ cppB[0], cppD[1] ];
288 cppT = cppS + [1,0] * catch_depth;
289 cppU = cppT + [0,-1] * catch_height;
290 cppV = [ cppS[0], cppU[1] - catch_depth ];
291
292 cppR = 0.5*(cppP + cppJ);
293
294 cp_rQ = 0.5 * (cppJ[0] - cppP[0]);
295 cppQ = [ cppR[0],
296          cppV[1] - (catch_finger_height - cp_rQ) ];
297 cppF = [ cppV[0] + catch_finger_depth, cppC[1] ];
298
299 // prop recess in case
300
301 prop_x_pos = phone_width/2;
302
303 prop_recess_hw = 0.5 * prop_main_width + prop_side_gap;
304
305 prc_r1 = prop_end_dia/2;
306 prc_r3 = prc_r1 + prop_recess_slop;
307
308 prcp2 = [ epp4[0] + prop_buildout_less,
309           case_bottom_z ];
310
311 prop_caserecess_buildout_r = -1; // prcp2[0] - epp2o[0];
312
313 prcp1 = [ epp2o[0] + prc_r3 + prop_caserecess_behind,
314           epp2i[1] - prc_r3 - prop_recess_under];
315
316 // prop recess in lid
317
318 prl_r10 = prop_end_dia/2;
319 prl_r10o = prl_r10 + prop_recess_slop;
320
321 prlp10 = lpp10 + [1,1] * prl_r10o
322   + [1,0] * prop_lidrecess_behind
323   + [0,1] * prop_recess_under;
324
325 // prop
326
327 $prpp10 = [0,0];
328 $prpp11 = [0, prop_taper_len];
329
330 $prp_r10 = prl_r10;
331
332 // ---------- modules ----------
333
334 module AdhocMultiprintFrame(phase, z0, zs) {
335   // from z0 to z0 + zs*layer
336   extra = phase * 0.65 + 5;
337   xextra = extra + -epp4[0];
338   xrange = [ 0, phone_width ] + [-1,+1] * xextra;
339   yextra = extra + -epp4[0];
340   yrange = [ -phone_height + -hppB[0] + hp_r2, 0 ] + [-1,+1] * yextra;
341   p0 = [ xrange[0], yrange[0] ];
342   p1 = [ xrange[1], yrange[1] ];
343   echo(p0, p1);
344   translate([0,0, z0])
345     mirror([0,0, zs<0 ? 1 : 0])
346     linear_extrude(height= initial_layer_thick)
347     difference(){
348       rectfromto(p0 - [1,1] * initial_layer_width,
349                  p1 + [1,1] * initial_layer_width);
350       rectfromto(p0, p1);
351     }
352 }
353
354 module KeeperProfile(slant=0){
355   use_e = kppe + [0,-1] * slant * keeper_inner_width / keeper_slant_slope;
356   polygon([use_e, kppd, kppc, kppb, kppa, kppf]);
357 }
358
359 module EdgeProfile(){
360   difference(){
361     hull(){
362       translate(epp3) square(case_th_bottom*2, center=true);
363       circleat(epp2o, r=case_th_bottom);
364       circleat(epp1, r=case_th_side);
365       rectfromto(epp0, epp4);
366     }
367     polygon([ epp5 + [0,10],
368               epp1,
369               epp2i,
370               epp3 + [10,0] ]);
371   }
372 }
373
374 module LidEdgeProfile(){
375   polygon([ lpp10,
376             lpp11,
377             lpp12,
378             lpp13,
379             lpp13 + [10, 0],
380             lpp10 + [10, 0]
381             ]);
382   intersection(){
383     circleat(lpp12, r=lp_r12);
384     rectfromto( lpp12 + [-10,   0],
385                 lpp12 + [+10, +10] );
386   }
387 }
388
389 module ButtonCoverProfile(){
390   intersection(){
391     polygon(concat([ bppM, bppP, bppO, bppJ ],
392                    (enable_support ? [ bppU, bppV, bppW ] : []),
393                    [ bppL, bppK ]));
394     hull(){
395       EdgeProfile();
396       LidEdgeProfile();
397     }
398   }
399 }
400
401 module ButtonPlan(l, deep, cut){
402   epsilon =
403     (cut  ? 0 : lid_buttoncover_gap);
404
405   delta =
406     (deep ? lid_buttoncover_overlap : 0);
407
408   C = [0,0]; // by definition
409   T = [ 0, epp4[1] ];
410   G = T + [0,10];
411
412   B0 = C + [0,-1] * button_cutout_depth;
413   B1 = B0 + [0,1] * epsilon;
414
415   r0 = 0.5 * (T[1] - B0[1]);
416   A = [  -(l + button_l_fudge)/2 + r0, 0.5 * (T[1] + B0[1]) ];
417   H = A + [0,-1] * delta;
418
419   D = A + [-2,0] * r0;
420   F = D + [0,10];
421
422   E0 = 0.5 * (D + A);
423   E1 = E0 + [1,0] * epsilon;
424
425   I0 = [ E0[0], H[1] ];
426   I1 = [ E1[0], H[1] ];
427
428   hull(){
429     for (m=[0,1]) mirror([m,0])
430       circleat(H, r0 - epsilon);
431   }
432   for (m=[0,1]) mirror([m,0]) {
433     difference(){
434       polygon([ E1,
435                 I1,
436                 H,
437                 B1,
438                 G,
439                 F,
440                 D
441                 ]);
442       circleat(D, r0 + epsilon);
443     }
444   }
445 }
446
447 module CatchCatchProfile(){
448   hull(){
449     for (c=[ cppR, cppQ ])
450       circleat(c, cp_rQ);
451   }
452   hull(){
453     circleat(lpp12, lp_r12);
454     circleat(lpp12 + [5,0], lp_r12);
455     rectfromto(cppP, cppP + [5,0.1]);
456   }
457   polygon([cppJ, cppS, cppT, cppU, cppV, cppQ, cppR]);
458 }
459
460 module CatchCutProfile(){
461   polygon([ cppB,
462             cppA,
463             cppD,
464             cppF,
465             cppF + [0,-10],
466             cppF + [-10,-10],
467             lpp12 + [-10,0],
468             lpp12 + [10,0]
469             ]);
470 }
471
472 module Flip_rhs(yn=[0,1]) {
473   for ($rhsflip=yn) {
474     translate([phone_width/2, 0, 0])
475       mirror([$rhsflip,0,0])
476       translate([-phone_width/2, 0, 0])
477       children();
478   }
479 }
480
481 module Flip_bot(yn=[0,1]) {
482   for ($botflip=yn) {
483     translate([0, -phone_height/2, 0])
484       mirror([0, $botflip, 0])
485       translate([0, phone_height/2, 0])
486       children();
487   }
488 }  
489
490 module AroundEdges(fill_zstart, fill_th, fill_downwards=0){
491   // sides
492   Flip_rhs(){
493     translate([0, -phone_cnr_rad, 0])
494       rotate([90,0,0])
495       linear_extrude(height = phone_height - phone_cnr_rad*2)
496       children(0);
497   }
498   // corners
499   Flip_rhs() Flip_bot() {
500     translate([+1,-1] * phone_cnr_rad)
501       intersection(){
502         rotate_extrude()
503           intersection(){
504             mirror([1,0,0])
505               translate([-1,0] * phone_cnr_rad)
506               children(0);
507             rectfromto([0,-20],[10,20]);
508           }
509         translate([-10, 0, -20] + 0.01 * [+1,-1, 0] )
510           cube([10,10,40]);
511       }
512   }
513   // top and bottom
514   Flip_bot(){
515     translate([ phone_width - phone_cnr_rad, 0,0 ])
516       rotate([90,0,-90])
517       linear_extrude(height = phone_width - phone_cnr_rad*2)
518       children(0);
519   }
520   // fill
521   translate([0,0, fill_zstart])
522     mirror([0,0, fill_downwards])
523     linear_extrude(height = fill_th)
524     rectfromto([+1,-1] * phone_cnr_rad,
525                [phone_width, -phone_height] + [-1,+1] * phone_cnr_rad);
526 }
527
528 module CaseAperture(pos, dia, $fn) {
529   theta = 180/$fn;
530   translate([ pos[0] + bumper[0],
531               -epp2i[0],
532               -pos[1] ])
533     rotate([-90, theta, 0])
534     cylinder(r = dia/2 / cos(theta),
535              h = 60);
536 }
537
538 module SideButton(y, y_ref_sign, l){
539   // y_ref_sign:
540   //   +1  measured from top    of actual phone to top    of button
541   //   -1  measured from bottom of actual phone to bottom of button
542   //    0  y is centre of button in coordinate system
543   $button_l= l;
544   eff_y = y_ref_sign > 0 ?         -bumper [1] -y -l/2 :
545           y_ref_sign < 0 ? (-phone -bumper)[1] +y +l/2 :
546           y;
547   //echo(eff_y);
548   translate([0, eff_y, 0])
549     children();
550 }
551
552 module LidButtonishLeg(y, y_ref_sign, l=buttonishleg_default_l_is_fudge) {
553   $button_leg_only = true;
554   SideButton(y, y_ref_sign, l) children();
555 }
556
557 module Buttons(){
558   Flip_rhs(1) SideButton(15.580, +1, 8.9) children(); // power
559   Flip_rhs(1) SideButton(48.700, -1, 8.920) children(); // camera
560   Flip_rhs(0) SideButton(30.800, +1, 21.96) children(); // volume
561   Flip_rhs(   ) LidButtonishLeg(14, -1) children();
562 //  Flip_rhs(0) LidButtonishLeg(20, +1, 20) children();
563 }
564
565 module Struts(x_start, z_min, th){
566   // if th is negative, starts at z_min and works towards -ve z
567   // and object should then be printed other way up
568   for (i= [1 : 1 : case_struts_count]) {
569     translate([0,
570                0,
571                z_min])
572       mirror([0,0, th<0 ? 1 : 0])
573       translate([0,
574                  -phone_height * i / (case_struts_count+1),
575                  case_struts_solid_below])
576       linear_extrude(height= abs(th)
577                      -(case_struts_solid_below+case_struts_solid_above))
578       rectfromto([               x_start, -0.5 * case_struts_width ],
579                  [ phone_width - x_start, +0.5 * case_struts_width ]);
580   }
581 }
582
583 module OrdinaryRearAperture(rhs,bot, pos){
584   Flip_rhs(rhs) Flip_bot(bot)
585     linextr(-20, 20)
586     mirror([0,1])
587     translate(pos + bumper)
588     children();
589 }
590
591 module MicroUSB(){
592   Flip_bot(1){
593     rotate([90,0,0])
594       mirror([0,0,1])
595       linextr(-epp2i[0], 60)
596       translate([0.5 * phone_width, 0, 0])
597       rectfromto([-microusb_width/2, epp2i[1] + microusb_below],
598                  [+microusb_width/2, epp0[1] + -microusb_above]);
599   }
600 }
601
602 module OrdinaryRearApertures(){
603   // rear speaker
604   OrdinaryRearAperture(1,1, rearspeaker_pos_bl)
605     rectfromto(-rearspeaker_gap,
606                rearspeaker_size + rearspeaker_gap);
607
608   // finger hole to remove phone
609   if (len(fingerpushhole_dias))
610     OrdinaryRearAperture(1,0, [ fingerpushhole_dias[0]/2 + epp2i[0],
611                                 phone[1]/2 ])
612     scale(fingerpushhole_dias)
613     circle(r= 0.5 );
614 }
615
616 module RearCameraAperture(){
617   Flip_rhs(1)
618     mirror([0, 0, 1])
619     linear_extrude(height = 20)
620     mirror([0, 1, 0])
621     translate(bumper)
622     rectfromto(camera_pos_tl, camera_pos_br);
623 }
624
625 module HingeLidProfile(){
626   hull(){
627     circleat(hppT, hp_r1);
628     circleat(lpp12, lp_r12);
629     polygon([lpp10,
630              lpp13 + [2,0],
631              lpp12,
632              hppT]);
633   }
634 }
635
636 module HingeBaseProfile(){
637   difference(){
638     hull(){
639       circleat(hppB, hp_r1);
640       circleat(hppE, hp_r1);
641       circleat(epp2o, case_th_bottom);
642       circleat(hppB + [10,0], hp_r1);
643     }
644     polygon([epp5, epp1, epp2i, epp3, bppL]);
645   }
646 }
647
648 module HingeLeverOuterProfile(){
649   hull(){
650     circleat(hppT, hp_r2);
651     circleat(hppB, hp_r2);
652   }
653 }
654
655 module HingeLeverInnerProfile(){
656   for (s = [-1,+1]) {
657     c = s > 0 ? hppT : hppB;
658     translate(c)
659       mirror([0,0,s>0])
660       rotate(s<0 ? -40 : 0)
661       hull()
662       for (x=[-20,20])
663         for (y=[0, s * 10])
664           translate([x,y])
665             circle(hp_rn);
666   }
667 }
668
669 module HingeLeverNutProfile(){
670   for (c= [hppB, hppT]) {
671     translate(c)
672       circle($fn=6, r= 0.5 * hingescrew_nut_across / cos(30));
673   }
674 }
675
676 module Flip_hinge(doflip=1){
677   hinge_origin = [0, -(phone_height - hppB[0]), hppB[1]];
678   translate(hinge_origin)
679     rotate([doflip*180,0,0])
680     translate(-hinge_origin)
681     children();
682 }
683
684 module HingePortion(x0,x1){
685   Flip_rhs() Flip_bot(1)
686     translate([x0,0,0])
687     mirror([1,0,0])
688     rotate([90,0,-90])
689     linear_extrude(height=x1-x0)
690     children(0);
691 }
692
693 module CatchPortion(width){
694   translate([phone_width/2, 0,0])
695     rotate([90,0,-90])
696     linextr(-width/2, width/2)
697     children(0);
698 }
699
700 module CaseBase(){
701   AroundEdges(epp3[1], case_th_bottom, 1)
702     EdgeProfile();
703 }
704
705 function prop_x(gamma) = hp_k / (2 * sin(gamma/2)) - hppT[0];
706
707 module PropProfileAssignments(gamma){
708   // https://en.wikipedia.org/wiki/Solution_of_triangles#Two_sides_and_the_included_angle_given_(SAS)
709   x = prop_x(gamma);
710   p = phone_height + prlp10[0] - hppB[0];
711   b = p + x;
712
713   q = phone_height - hppT[0] - prcp1[0]; // $prpp7[0] is 0 by definition
714   a = q + x;
715   c = sqrt(a*a + b*b - 2*a*b*cos(gamma));
716   $prp_alpha = acos( (b*b + c*c - a*a) / (2*b*c) );
717
718   $prp_theta = 90 - $prp_alpha;
719   beta = 180 - $prp_alpha - gamma;
720   psi = 90 - beta;
721
722   //echo("abc", a,b,c);
723
724   v1 = [ [ cos(psi), -sin(psi) ],    // x
725          [ sin(psi),  cos(psi) ] ];  // y
726
727   $prpp7 = [0, c + (lpp13[1] - $prpp10[1] - hp_k) ];
728
729   $prp_r1 = prc_r1;
730   $prp_r11 = prop_main_th/2;
731
732   $prpp1 = $prpp7 + [1,0] *
733     // this is approximate, but will do
734     (prop_main_th/2 + prop_prop_gap + prcp1[0] - cppA[0]);
735   $prpp3 = $prpp1 +
736     v1[0] * -$prp_r1 +
737     v1[1] * ((prcp2[1] - prcp1[1]) - prop_prop_gap);
738   $prpp12 = $prpp3 + v1[0] *
739     (prop_end_dia + prop_caserecess_taper * ($prpp1[1] - $prpp3[1]));
740   $prp_r8 = prop_main_th;
741   $prpp4 = [ prop_main_th/2, $prpp3[1] ];
742   $prp_r5 = $prp_r8;
743   $prpp5 = [ $prpp12[0] - $prp_r5,
744             $prpp3[1] - prop_prong_h + $prp_r5 ];
745   $prpp6 = $prpp4 + [0,-1] * (prop_prong_h +
746          prop_prong_heel_slope * ($prpp5[0] - $prpp4[0]));
747   $prpp8 = $prpp4 + [0,-1] * $prp_r8;
748   $prpp9 = $prpp8 + [-1,0] * $prp_r8;
749
750   children();
751 }
752
753 module PropProfile(gamma, cut=0, rot=0){ ////toplevel
754   PropProfileAssignments(gamma){
755
756     //#circleat($prpp3,1);
757     //#circleat($prpp12,1);
758
759     if (!cut) {
760       hull(){
761         translate($prpp8)
762           intersection(){
763             circle($prp_r8);
764             polygon([[-20,-0], [20,20], [0,0]]);
765           }
766         rectfromto($prpp6, $prpp9);
767         translate($prpp5) intersection(){
768           circle($prp_r5);
769           polygon([[-10,-10], [0,0], [10,0]]);
770         }
771         rectfromto($prpp12 + [0,-0.1], $prpp3);
772       }
773       hull(){
774         circleat($prpp1, $prp_r1);
775         rectfromto($prpp12 + [0,-0.1], $prpp3);
776       }
777     }
778     // main shaft
779     rotate([0,0, rot*-$prp_theta]){
780       hull(){
781         extra = cut ? prop_recess_slop : 0;
782         rectfromto($prpp6, $prpp9);
783         circleat($prpp11, $prp_r11 + extra);
784         circleat($prpp10, $prp_r10 + extra);
785       }
786     }
787   }
788 }
789
790 module PropAggregateProfile(){
791   for (angle = prop_angles)
792     PropProfile(angle, 0,0);
793 }
794
795 module Prop(){ ////toplevel
796   hw = prop_main_width/2;
797   linextr(-hw, +hw)
798     PropAggregateProfile();
799 }
800
801 module Case(){ ////toplevel
802   difference(){
803     union(){
804       CaseBase();
805
806       // ledge (fixed keeper)
807       Flip_rhs(1-keeper_side) intersection(){
808         rotate([90, 0, 0])
809           linear_extrude(height = phone_height + phone_cnr_rad * 2)
810           KeeperProfile(1);
811
812         // outline of the whole case, to stop it protruding
813         translate([0,0, -25])
814           linear_extrude(height = 50)
815           hull()
816           Flip_bot()
817           circleat([+1,-1] * phone_cnr_rad, phone_cnr_rad + case_th_side/2);
818       }
819
820       // hinge
821       HingePortion(hex20, hex21) HingeBaseProfile();
822
823       // buildout for prop recess
824       if (prop_caserecess_buildout_r > 0) Flip_rhs(1)
825         linextr(case_bottom_z, epp2i[1])
826         hull() {
827           for (dxs = [-1,+1])
828             circleat([ prop_x_pos + dxs * prop_caserecess_buildout_r,
829                        -epp2o[0] ],
830                      r = epp2o[0] - prcp2[0]);
831         }
832     }
833
834     // slot for keeper
835     Flip_rhs(keeper_side)
836       translate([0, -phone_cnr_rad, 0])
837       rotate([90, 0, 0])
838       linear_extrude(height = phone_height + phone_cnr_rad * 2)
839       minkowski(){
840         KeeperProfile();
841         rectfromto([ -keeper_gap_x,    -keeper_gap_z_bot ],
842                    [ keeper_gap_x_holes,    +keeper_gap_z_top ]);
843       }
844
845     // front camera
846     RearCameraAperture();
847
848     // struts (invisible, because they're buried in the case)
849     Struts(epp2i[0], epp2i[1] - case_th_bottom, case_th_bottom);
850
851     Buttons(){
852       mirror([1,0,0])
853         rotate([90,0,90]) {
854           if (!($button_leg_only && enable_support))
855           intersection(){
856             translate([0,0,-10])
857               linear_extrude(height= 20)
858               ButtonPlan($button_l, 0,1);
859             if ($button_leg_only)
860               rotate([-90,90,0])
861                 translate([phone_width/2, -400, kppe[1]])
862                 mirror([1-abs($rhsflip - keeper_side),0,0])
863                 cube([400, 800, 50]);
864             if (enable_support)
865               rotate([-90,90,0])
866               translate([-400, -400, kppd[1]])
867                 mirror([0,0,1])
868                 cube([800,800,100]);
869           }
870           translate([0,0, -bppR[0]])
871             linear_extrude(height= 20)
872             ButtonPlan($button_l, 1,1);
873         }
874       
875     }
876
877     // apertures along top edge
878     CaseAperture(jack_pos, jack_dia, 8);
879     Flip_rhs(1)
880       CaseAperture(noisecancelmic_pos, noisecancelmic_dia, 8);
881
882     OrdinaryRearApertures();
883
884     MicroUSB();
885
886     // gaps for the lid's hinge arms
887     HingePortion(hex20 - hinge_x_arms_gap,
888                  hex21 + hinge_x_arms_gap)
889       minkowski(){
890         HingeLidProfile();
891         circle(r= hinge_r_arms_gap, $fn= 8);
892       }
893
894     // screw holes in the hinge arms
895     HingeScrews();
896
897     // catch striker
898     CatchPortion(catch_width + catch_side_gap*2)
899       CatchCutProfile();
900
901     // prop recess
902     Flip_rhs(1)
903       translate([prop_x_pos,0,0])
904       mirror([0,1,0])
905       rotate([90,0,90])
906       linextr(-prop_recess_hw, +prop_recess_hw)
907       hull(){
908         for (d=[ [0,0], [0,-1], [+1,-1/prop_caserecess_taper] ])
909           circleat(prcp1 + 20*d,
910                    prc_r3);
911       }
912   }
913 }
914
915 module LidAdhocMultiprintFrame(phase){
916   if (led_window_style >= 3) {
917     AdhocMultiprintFrame(phase, lpp13[1], -1);
918   }
919 }
920
921 module Lid(){ ////toplevel
922   difference(){
923     union(){
924       AroundEdges(lpp10[1], lpp13[1] - lpp10[1], 0)
925         LidEdgeProfile();
926
927       // button covers
928       Buttons(){
929         intersection(){
930           rotate([90,0,90])
931             translate([0,0,-10])
932             linear_extrude(height= 20)
933             ButtonPlan($button_l, 1,0);
934           rotate([90,0,0])
935              translate([0,0,-100])
936             linear_extrude(height= 200)
937             ButtonCoverProfile();
938         }
939       }
940
941       // hinge arms
942       HingePortion(hex20, hex21) HingeLidProfile();
943
944       // catch
945       CatchPortion(catch_width)
946         CatchCatchProfile();
947     }
948     Struts(lpp10[0] + strut_min_at_end, lpp13[1], -case_th_lid);
949
950     // screw holes in the hinge arms
951     HingeScrews();
952
953     // prop recess
954     translate([prop_x_pos, -prlp10[0], prlp10[1]])
955       mirror([0,1,0])
956       rotate([90,0,90])
957       linextr(-prop_recess_hw, +prop_recess_hw)
958       hull()
959       for (pa = prop_angles)
960         PropProfile(pa, 1,1);
961
962     // notification led aperture
963     if (led_window_style)
964       translate([led_pos[0], -led_pos[1], lpp13[1]]) {
965         translate([0,0,-10])
966           cylinder(r=nla_r0, h=20);
967         if (led_window_style >= 2)
968           translate([0,0, -nla_t])
969             cylinder(r=nla_r2, height=20);
970       }
971
972     }
973
974   LidAdhocMultiprintFrame(1);
975 }
976
977 module HingeLever(){ ////toplevel
978   difference() {
979     // outer body, positive
980     HingePortion(hex22, hex22 + phone_width/2)
981       HingeLeverOuterProfile();
982
983     // space for the screws
984     HingePortion(hex26, hex24)
985       HingeLeverInnerProfile();
986
987     // recesses for the nuts
988     HingePortion(hex23, hex26+1)
989       HingeLeverNutProfile();
990
991     // bores for the screws
992     HingeScrews();
993
994     // space for the charging cable
995     MicroUSB();
996     Flip_hinge() MicroUSB();
997   }
998 }
999
1000 module LidWindow(){ ////toplevel
1001   translate([led_pos[0], -led_pos[1], lpp13[1]])
1002     mirror([0,0,1])
1003     cylinder(r= nla_r1, h=nla_t);
1004   LidAdhocMultiprintFrame(0, lpp13[1], -1);
1005 }
1006
1007 module LidWindowPrint(){ ////toplevel
1008   rotate([0,180,0])
1009     LidWindow();
1010 }
1011
1012 module DemoLidWindowSelect(){
1013   translate([led_pos[0], led_pos[1], -100]) {
1014     translate([0, -30, 0]) cube([400, 400, 200]);
1015   }
1016 }
1017
1018 module DemoLidWindow(){ ////toplevel
1019   %Lid();
1020   LidWindow();
1021   translate([0,40,0]){
1022     color("blue") intersection(){ Lid(); DemoLidWindowSelect(); }
1023     color("red") intersection(){ LidWindow(); DemoLidWindowSelect(); }
1024   }
1025 }
1026
1027 module HingeLeverPrint(){ ////toplevel
1028   rotate([-90,0,0])
1029     translate([-phone_width/2, phone_height, 0])
1030     HingeLever();
1031 }
1032
1033 module TestSelectLength(){
1034   translate([-30, -200, -20])
1035     cube([30 + 15, 250, 40]);
1036 }
1037
1038 module TestLength(){ ////toplevel
1039   intersection(){
1040     Case();
1041     TestSelectLength();
1042   }
1043 }
1044
1045 module TestLengthRight(){ ////toplevel
1046   intersection(){
1047     Case();
1048     Flip_rhs(1)
1049       TestSelectLength();
1050   }
1051 }
1052
1053 module TestSelectWidth(){
1054   translate([-30, -(phone_height - 25), -20])
1055     mirror([0, 1, 0])
1056     cube([200, 50, 40]);
1057 }
1058
1059 module TestWidth(){ ////toplevel
1060   intersection(){
1061     Case();
1062     TestSelectWidth();
1063   }
1064 }
1065
1066 module TestLidWidthPrint(){ ////toplevel
1067   rotate([0,180.0]) intersection(){
1068     Lid();
1069     TestSelectWidth();
1070   }
1071 }
1072
1073 module TestSelectRearAperture(){
1074   minkowski(){
1075     union() children();
1076     translate([20, 0,0])
1077       cube([42, 2, 1], center=true);
1078   }
1079 }
1080
1081 module TestSelectCamera(){
1082   minkowski(){
1083     TestSelectRearAperture()
1084       RearCameraAperture();
1085     cube([0.1, 50, 0.1]);
1086   }
1087 }
1088
1089 module TestSelectOrdinaryRearApertures(){
1090   TestSelectRearAperture()
1091     OrdinaryRearApertures();
1092 }
1093
1094 module TestCamera(){ ////toplevel
1095   intersection(){
1096     Case();
1097     TestSelectCamera();
1098   }
1099 }
1100
1101 module TestLidByCamera(){ ////toplevel
1102   intersection(){
1103     Lid();
1104     TestSelectCamera();
1105   }
1106 }
1107
1108 module TestLidByCameraPrint(){ ////toplevel
1109   rotate([180,0,0]) TestLidByCamera();
1110 }
1111
1112 module DemoByCamera(){ ////toplevel
1113   color("blue") TestLidByCamera();
1114   color("red")  TestCamera();
1115 }
1116
1117 module OneKeeper(){ ////toplevel
1118   translate([0, -phone_cnr_rad, 0])
1119     rotate([90, 0, 0])
1120     linear_extrude(height = phone_height - phone_cnr_rad * 2)
1121     KeeperProfile();
1122 }
1123
1124 module OneKeeperPrint(){ ////toplevel
1125   rotate([0,180,0])
1126     OneKeeper();
1127 }
1128
1129 module LidPrint(){ ////toplevel
1130   rotate([0,180,0])
1131     Lid();
1132 }
1133
1134 module TestSelectFrame(){
1135   include = [1,-1] * (epp2i[0] + 4);
1136
1137   difference(){
1138     cube(1000, center=true);
1139     translate([0,0, -100])
1140       linear_extrude(height=200)
1141       rectfromto(include,  inside_br - include);
1142   }
1143 }
1144
1145 module TestSelectLidFrame(){
1146   TestSelectFrame();
1147 }
1148
1149 module TestFrameCase(){ ////toplevel
1150   intersection(){
1151     Case();
1152     union(){
1153       TestSelectFrame();
1154       TestSelectCamera();
1155       TestSelectOrdinaryRearApertures();
1156     }
1157   }
1158 }
1159
1160 module TestTopApertures(){ ////toplevel
1161   intersection(){
1162     Case();
1163     TestSelectFrame();
1164     translate([-100, -35, -100])
1165       cube([400, 100, 200]);
1166   }
1167 }
1168
1169 module TestFrameLidPrint(){ ////toplevel
1170   rotate([0,180,0]) intersection(){
1171     Lid();
1172     TestSelectLidFrame();
1173   }
1174 }
1175
1176 module ButtonPlanForDemo(z, deep, cut){
1177   translate([0,0,z])
1178     ButtonPlan(8, deep, cut);
1179 }
1180
1181 module HingeScrews(){
1182   Flip_rhs() Flip_bot(1){
1183     for (c= [ hppT, hppB ])
1184       translate([ hex20,
1185                   -c[0],
1186                   c[1] ]){
1187         rotate([0,90,0])
1188           translate([0,0,-.2])
1189           cylinder( r= hingescrew_shaft_dia/2,
1190                     h = hingescrew_shaft_len+0.2 );
1191         rotate([0,-90,0])
1192           translate([0,0,+.1])
1193           cylinder( r= hingescrew_head_dia/2, h = hingescrew_head_th );
1194       }
1195   }
1196 }
1197
1198 module DemoPropAngleSelect(c){
1199   color(c) difference(){
1200     union(){ children(); }
1201     translate([ prop_x_pos, -400, -200 ])
1202       cube([ 400,800,400 ]);
1203   }
1204 }
1205
1206 module DemoPropAngle(ang){
1207   hL = [0, -(phone_height - hppT[0]), hppT[1] - hp_k*2];
1208   hC = [0, -(phone_height - hppB[0]), hppB[1]];
1209
1210   translate(hL)
1211     rotate([ang/2,0,0])
1212     translate(-hL)
1213     translate(hC)
1214     rotate([ang/2,0,0])
1215     translate(-hC) {
1216       DemoPropAngleSelect("red") Case();
1217
1218       color("orange")
1219         translate([prop_x_pos, -prcp1[0], prcp1[1]])
1220         PropProfileAssignments(ang) {
1221           echo($prpp1);
1222           rotate([-$prp_theta, 0, 0])
1223           translate([0, $prpp1[0], -$prpp1[1]])
1224           rotate([90,0,-90])
1225           Prop();
1226         }
1227     }
1228
1229   translate([0,0, -hp_k*2])
1230     DemoPropAngleSelect("blue")
1231     Lid();
1232 }
1233
1234 module DemoPropAngles(){ ////toplevel
1235   for (i=[0 : len(prop_angles)-1])
1236     translate(i * [0, -100, 100])
1237     DemoPropAngle(prop_angles[i]);
1238 }
1239
1240 module DemoFrame(){ ////toplevel
1241   color("red") TestFrameCase();
1242   color("blue") intersection(){ Lid(); TestSelectLidFrame(); }
1243   color("black") HingeScrews();
1244   %HingeLever();
1245 }
1246
1247 module DemoHingedFrame(){ ///toplevel
1248   color("red") TestFrameCase();
1249   translate([0,0, -2*hp_k])
1250   color("blue") intersection(){ Lid(); TestSelectLidFrame(); }
1251
1252   Flip_hinge(){
1253     color("orange") HingeLever();
1254     color("black") HingeScrews();
1255   }
1256 }
1257
1258 module DemoHinge(){ ////toplevel
1259   translate([ -0.5*phone_width, phone_height, hp_k*3 ]) {
1260     DemoFrame();
1261     translate([0,0, -hp_k*3])
1262       DemoHingedFrame();
1263   }
1264 }
1265
1266 module DemoProfiles(){ ////toplevel
1267   LidEdgeProfile();
1268   %EdgeProfile();
1269   KeeperProfile();
1270   translate([0,0,-1]) color("black") KeeperProfile(1);
1271
1272   translate([20,0]) {
1273     LidEdgeProfile();
1274     %EdgeProfile();
1275
1276     demopoint_QR = [ bppS[0], bppQ[1] - 0.1];
1277   
1278     color("blue") ButtonCoverProfile();
1279     color("red") {
1280       rectfromto(bppQ, demopoint_QR);
1281       rectfromto(bppR, demopoint_QR);
1282     }
1283   }
1284
1285   translate([-20,0]) {
1286     color("black") ButtonPlanForDemo(-2, 0,1);
1287     color("red" )  ButtonPlanForDemo(-4, 1,1);
1288     color("blue")  ButtonPlanForDemo(-6, 1,0);
1289   }
1290
1291   translate([0, -30]) {
1292     %LidEdgeProfile();
1293     %EdgeProfile();
1294     color("blue") HingeLidProfile();
1295     color("red")  HingeBaseProfile();
1296     color("black") translate([0,0,-2]) HingeLeverOuterProfile();
1297   }
1298
1299   for (f=[0,1]) {
1300     translate([-30, -60 + 30*f]) {
1301       translate([0,0,-4]) EdgeProfile();
1302       %translate([0,0,-10]) HingeBaseProfile();
1303       translate([0,-2] * f * hp_k) {
1304         translate([0,0,-4]) LidEdgeProfile();
1305         %translate([0,0,-10]) %HingeLidProfile();
1306       }
1307       translate(+hppB) rotate([0,0,180*f]) translate(-hppB) {
1308         translate([0,0,-2]) color("black") HingeLeverOuterProfile(); 
1309         translate([0,0,0]) color("red") difference(){
1310           HingeLeverOuterProfile();
1311           HingeLeverInnerProfile();
1312         }
1313         translate([0,0,3]) color("yellow") HingeLeverNutProfile();
1314       }
1315     }
1316   }
1317
1318   translate([20,-30]) {
1319     %EdgeProfile();
1320     %LidEdgeProfile();
1321     //translate([0,0,1]) CatchCutProfile();
1322     color("blue") CatchCatchProfile();
1323     color("red") difference(){ EdgeProfile(); CatchCutProfile(); }
1324   }
1325
1326   translate([40,-30]) {
1327     difference(){
1328       LidEdgeProfile();
1329       translate(prlp10)
1330         PropProfile(10, 1, 0);
1331     }
1332     translate(prlp10)
1333       PropProfile(15, 0);
1334   }
1335   translate([60,-30]) {
1336     PropAggregateProfile();
1337   }
1338 }
1339
1340 //EdgeProfile();
1341 //KeeperProfile();
1342 //CaseBase();
1343 //%Case();
1344 //Keeper();
1345 //LidEdgeProfile();
1346 //KeeperProfile();
1347 //DemoProfiles();
1348 //PropRecess();