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