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