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