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