chiark / gitweb /
fairphone-case: move a rotate into CaseRecess (nfc)
[reprap-play.git] / fairphone-case.scad
1 // -*- C -*-
2
3 include <utils.scad>
4
5 phone = [ 75.0, 145.0 ];
6
7 prop_lid_posns = [ 58, 140 ]; // measured from bottom of phone
8
9 bumper = [ 0.250, -0.025 ];
10 // ^ One side.  Overall size is increased by twice this.
11 // If no bumpers, is the gap around the phone.
12
13 enable_support = 1;
14
15 phone_cnr_rad = 6.0;
16
17 button_cutout_depth = 9;
18
19 phone_edge_thick = 9.0;
20 phone_total_thick = 12.0;
21 phone_backside_slope_inner = 1.5; // larger means shallower
22 phone_backside_slope_outer = 1.0; // larger means shallower
23
24 camera_pos_tl = [  6.450, 12.750 ]; // measured from tl corner
25 camera_pos_br = [ 22.300, 37.600 ]; // tl/br as seen from back
26
27 jack_pos = [ 14.38, 7.96 ];
28 jack_dia = 10.64 + .5; // some jack I had lying around
29
30 noisecancelmic_pos = [ 19.54, 7.37 ];   // from rhs
31 noisecancelmic_dia = 4.00;
32
33 //fingerpushhole_dias = [ 15, 18 ];
34 fingerpushhole_dias = [];
35
36 rearspeaker_pos_bl = [ 12.64, 18.72 ];
37 rearspeaker_size   = [  3.76,  7.36 ];
38
39 microusb_above = 3.27 - 0.25;
40 microusb_below = 0.0;
41 microusb_width = 16.12 + 1.25;
42
43 case_th_bottom = 2.5;
44 case_th_lid = 2.5;
45 case_th_side = 2;
46 case_th_lip = 1.2;
47
48 case_struts_count = 6;
49 case_struts_solid_below = 1.00;
50 case_struts_solid_above = 0.75;
51 case_struts_width = 0.10;
52
53 keeper_th_z = 0.75;
54 keeper_th_x = 0.75;
55 keeper_inner_width = 2.75;
56 keeper_inner_height = 2.75;
57 keeper_slant_slope = 2; // larger means steeper
58
59 keeper_gap_z_top = 0.25;
60 keeper_gap_z_bot = 0.75;
61 keeper_gap_x     = 0.25;
62 keeper_gap_x_holes = 0.75;
63
64 keeper_side = 0; // 0 = lhs; 1 = rhs
65
66 case_lip = 1.25;
67
68 lid_gap_x = 0.25;
69 lid_gap_z = 0.25;
70 lid_lip = 1.75;
71
72 catch_slop = 0.50;
73
74 foldover_gap = 0.50;
75 foldover_lever_gap = 0.50;
76
77 hingescrew_shaft_dia = 2.0 + 0.25; // M2 x 12mm machine screw
78 hingescrew_shaft_len = 12;
79 hingescrew_nut_thick = 1.93 + 0.20; // incl. washer
80 hingescrew_nut_dia = 4.72 + 0.50; // washer, actually
81 hingescrew_head_th = 1.38 + 0.75;
82 hingescrew_head_dia = 3.92;
83 lever_cover_th = 0.75;
84 hingemount_th = 2.5;
85
86 $fa = 5;
87 $fs = 0.1;
88
89 button_l_fudge = 4.4;
90 buttonishleg_default_l_is_fudge = 10;
91
92 hinge_base_slope = 1.5; // bigger is steeper
93
94 strut_min_at_end = 1.5;
95
96 hinge_x_gap = 0.125;
97 hinge_x_postscrew_gap = 0.75;
98 hinge_x_arms_gap = 0.35;
99 hinge_r_arms_gap = 0.55;
100
101 rearspeaker_gap    = [ 2.0, 2.0 ]; // each side
102
103 catch_len = 7.5;
104 catch_width = 15;
105 catch_thickness = 1.0;
106 catch_side_gap = 0.75; // each side
107
108 catch_depth = 0.75;
109 catch_height = 0.35;
110 catch_finger_height = 1.5;
111 catch_finger_depth = 2.5;
112
113 prop_main_width = 3;
114 prop_main_th = 3;
115 prop_wing_len = 3;
116 prop_nose_slope = 1.5; // bigger means pointier
117 prop_side_gap = 0.75; // each side
118 prop_fin_height = 1.5;
119 prop_fin_width = 0.75;
120 prop_max_angle = 45; // bigger means at worse angle
121 prop_recess_under = 0.30;
122 prop_backfwd_gap = 1.0; // total
123
124 // ---------- calculated ----------
125
126 phone_width =  (phone + bumper*2)[0];
127 phone_height = (phone + bumper*2)[1];
128
129 inside_br = [phone_width, -phone_height];
130
131 //echo(camera_pos_tl + bumper,
132 //     camera_pos_br + bumper);
133
134 // ----- could be changed -----
135 lid_buttoncover_gap = lid_gap_x;
136 lid_buttoncover_overlap = case_th_lip + keeper_gap_z_top;
137
138 phone_backside_slope_thick = phone_total_thick - phone_edge_thick;
139
140 prop_nose_len = case_th_lid - prop_recess_under;
141 prop_recess_slope = tan(prop_max_angle); // bigger means steeper
142 prop_recess_depth = case_th_lid - prop_recess_under;
143 prop_recess_width = prop_main_th / cos(prop_max_angle) + prop_backfwd_gap;
144
145 //lid_lip_overlap_width xxx bad name = ;
146 //lid_lip_inner_slope = [ 5, 5 ]; // xxx
147
148 epp0 = [0,0];
149 epp1 = [0, -phone_edge_thick];
150 epp2i = epp1 + phone_backside_slope_thick * [ phone_backside_slope_inner, -1 ];
151 epp2o = epp1 + phone_backside_slope_thick * [ phone_backside_slope_outer, -1 ];
152 epp3 = epp2i + [10, 0];
153 epp5 = epp0 + [0,1] * (keeper_th_z + keeper_gap_z_top + case_lip);
154 epp4 = epp5 + [-1,0] * case_th_side;
155
156 kppe = [0,0];
157 kppd = kppe + [1,0] * keeper_inner_width;
158 kppc = kppd + [0,1] * keeper_th_z;
159 kppb = [ kppe[0] - keeper_th_x, kppc[1] ];
160 kppf = kppe - [0,1] * keeper_inner_height;
161 kppa = [ kppb[0], kppf[1] ];
162
163 lpp10 = [ epp5[0] + lid_gap_x, kppc[1] + lid_gap_z ];
164 lpp11 = [ lpp10[0],            epp5[1] + lid_gap_z ];
165
166 lp_r12 = case_th_lid - (lpp11[1] - lpp10[1]);
167
168 lpp12 = [ epp4[0] + lp_r12,    lpp11[1] ];
169 lpp13 = [ lpp12[0],            lpp12[1] + lp_r12 ];
170
171 // button profile
172 bppM = epp4 + [0,5];
173 bppN = [ 0.5 * (epp0[0] + epp4[0]), bppM[1] ];
174 bppR = [ bppN[0] + lid_buttoncover_gap, -button_cutout_depth ];
175 bppS = [ epp1[0], bppR[1] ];
176 bppQ = [ bppM[0], bppR[1] - lid_buttoncover_overlap ];
177 bppP = bppQ + [0,1] * lid_buttoncover_gap;
178 bppO = [ bppN[0], bppP[1] ];
179 bppL = lpp10 + [5,0];
180 bppK = [ bppL[0], bppN[1] ];
181 bppJ = [ bppN[0], bppL[1] ];
182
183 // hinge plan
184 hp_rn = hingescrew_nut_dia/2;
185 hp_r2_min = hp_rn + lever_cover_th;
186 hp_rs = hingescrew_shaft_dia/2;
187 hp_r1_min = hp_rs + hingemount_th;
188
189 hp_r1 = max(hp_r1_min, hp_r2_min);
190 hp_r2 = hp_r1;
191
192 hppU = lpp13;
193 hppS = epp2o + [0,-1] * case_th_bottom;
194 hp_k = 0.5 * (hppU[1] - hppS[1] + foldover_gap);
195
196 hppM = [ epp4[0] - foldover_lever_gap - hp_r2,
197          0.5 * (hppU + hppS)[1] ];
198 hppT = [ hppM[0], hppU[1] - hp_r1 ];
199 hppB = hppT + [0,-1] * hp_k;
200
201 hppE_y = epp2o[1] - case_th_bottom + hp_r1;
202 hppE_x = hppB[0] + (hppB[1] - hppE_y) * hinge_base_slope;
203 hppE = [ hppE_x, hppE_y ];
204
205 // hinge elevation x coords
206
207 hingescrew_portion_len =
208   0.5* (hingescrew_shaft_len - hingescrew_nut_thick - hinge_x_gap);
209
210 hex20 = max(epp2o[0],
211             phone_cnr_rad,
212             kppd[0] + hingescrew_head_th + keeper_gap_x_holes);
213 hex21 = hex20 + hingescrew_portion_len;
214 hex22 = hex21 + hinge_x_gap;
215 hex23 = hex22 + hingescrew_portion_len
216   - hingescrew_nut_thick; // bodge, need to divvy this up more sensibly
217 hex24 = hex20 + hingescrew_shaft_len + hinge_x_postscrew_gap;
218 //echo(hex20, hex21, hex22, hex23, hex24);
219 //echo(hingescrew_portion_len);
220
221 // catch
222
223 cppJ = [ epp4[0] + catch_thickness, lpp10[1] ];
224 cppA = cppJ + [lid_gap_x, -lid_gap_z];
225 cppB = [ epp0[0], cppA[1] ];
226 cppP = [ epp4[0], cppJ[1] ];
227
228 cppS = cppJ + [0,-1] * catch_len;
229 cppD = [ cppA[0], cppS[1] + catch_slop ];
230 cppC = [ cppB[0], cppD[1] ];
231 cppT = cppS + [1,0] * catch_depth;
232 cppU = cppT + [0,-1] * catch_height;
233 cppV = [ cppS[0], cppU[1] - catch_depth ];
234
235 cppR = 0.5*(cppP + cppJ);
236
237 cp_rQ = 0.5 * (cppJ[0] - cppP[0]);
238 cppQ = [ cppR[0],
239          cppV[1] - (catch_finger_height - cp_rQ) ];
240 cppF = [ cppV[0] + catch_finger_depth, cppC[1] ];
241
242 // prop recess
243
244 prp1 = [0,0]; // by definition
245 prp2 = prp1 + [ -prop_recess_slope, -1 ] * prop_recess_depth;
246 prp4 = prp1 + [1,0] * prop_recess_width;
247 prp3 = [ prp4[0], prp2[1] ];
248 prp5 = prp4 + [1,0] * prop_recess_depth;
249
250 // ---------- modules ----------
251
252 module KeeperProfile(slant=0){
253   use_e = kppe + [0,-1] * slant * keeper_inner_width / keeper_slant_slope;
254   polygon([use_e, kppd, kppc, kppb, kppa, kppf]);
255 }
256
257 module EdgeProfile(){
258   difference(){
259     hull(){
260       translate(epp3) square(case_th_bottom*2, center=true);
261       circleat(epp2o, r=case_th_bottom);
262       circleat(epp1, r=case_th_side);
263       rectfromto(epp0, epp4);
264     }
265     polygon([ epp5 + [0,10],
266               epp1,
267               epp2i,
268               epp3 + [10,0] ]);
269   }
270 }
271
272 module LidEdgeProfile(){
273   polygon([ lpp10,
274             lpp11,
275             lpp12,
276             lpp13,
277             lpp13 + [10, 0],
278             lpp10 + [10, 0]
279             ]);
280   intersection(){
281     circleat(lpp12, r=lp_r12);
282     rectfromto( lpp12 + [-10,   0],
283                 lpp12 + [+10, +10] );
284   }
285 }
286
287 module ButtonCoverProfile(){
288   intersection(){
289     polygon([ bppM, bppP, bppO, bppJ, bppL, bppK ]);
290     hull(){
291       EdgeProfile();
292       LidEdgeProfile();
293     }
294   }
295 }
296
297 module ButtonPlan(l, deep, cut){
298   epsilon =
299     (cut  ? 0 : lid_buttoncover_gap);
300
301   delta =
302     (deep ? lid_buttoncover_overlap : 0);
303
304   C = [0,0]; // by definition
305   T = [ 0, epp4[1] ];
306   G = T + [0,10];
307
308   B0 = C + [0,-1] * button_cutout_depth;
309   B1 = B0 + [0,1] * epsilon;
310
311   r0 = 0.5 * (T[1] - B0[1]);
312   A = [  -(l + button_l_fudge)/2 + r0, 0.5 * (T[1] + B0[1]) ];
313   H = A + [0,-1] * delta;
314
315   D = A + [-2,0] * r0;
316   F = D + [0,10];
317
318   E0 = 0.5 * (D + A);
319   E1 = E0 + [1,0] * epsilon;
320
321   I0 = [ E0[0], H[1] ];
322   I1 = [ E1[0], H[1] ];
323
324   hull(){
325     for (m=[0,1]) mirror([m,0])
326       circleat(H, r0 - epsilon);
327   }
328   for (m=[0,1]) mirror([m,0]) {
329     difference(){
330       polygon([ E1,
331                 I1,
332                 H,
333                 B1,
334                 G,
335                 F,
336                 D
337                 ]);
338       circleat(D, r0 + epsilon);
339     }
340   }
341 }
342
343 module CatchCatchProfile(){
344   hull(){
345     for (c=[ cppR, cppQ ])
346       circleat(c, cp_rQ);
347   }
348   hull(){
349     circleat(lpp12, lp_r12);
350     circleat(lpp12 + [5,0], lp_r12);
351     rectfromto(cppP, cppP + [5,0.1]);
352   }
353   polygon([cppJ, cppS, cppT, cppU, cppV, cppQ, cppR]);
354 }
355
356 module CatchCutProfile(){
357   polygon([ cppB,
358             cppA,
359             cppD,
360             cppF,
361             cppF + [0,-10],
362             cppF + [-10,-10],
363             lpp12 + [-10,0],
364             lpp12 + [10,0]
365             ]);
366 }
367
368 module Flip_rhs(yn=[0,1]) {
369   for ($rhsflip=yn) {
370     translate([phone_width/2, 0, 0])
371       mirror([$rhsflip,0,0])
372       translate([-phone_width/2, 0, 0])
373       children();
374   }
375 }
376
377 module Flip_bot(yn=[0,1]) {
378   for ($botflip=yn) {
379     translate([0, -phone_height/2, 0])
380       mirror([0, $botflip, 0])
381       translate([0, phone_height/2, 0])
382       children();
383   }
384 }  
385
386 module AroundEdges(fill_zstart, fill_th, fill_downwards=0){
387   // sides
388   Flip_rhs(){
389     translate([0, -phone_cnr_rad, 0])
390       rotate([90,0,0])
391       linear_extrude(height = phone_height - phone_cnr_rad*2)
392       children(0);
393   }
394   // corners
395   Flip_rhs() Flip_bot() {
396     translate([+1,-1] * phone_cnr_rad)
397       intersection(){
398         rotate_extrude()
399           intersection(){
400             mirror([1,0,0])
401               translate([-1,0] * phone_cnr_rad)
402               children(0);
403             rectfromto([0,-20],[10,20]);
404           }
405         translate([-10, 0, -20] + 0.01 * [+1,-1, 0] )
406           cube([10,10,40]);
407       }
408   }
409   // top and bottom
410   Flip_bot(){
411     translate([ phone_width - phone_cnr_rad, 0,0 ])
412       rotate([90,0,-90])
413       linear_extrude(height = phone_width - phone_cnr_rad*2)
414       children(0);
415   }
416   // fill
417   translate([0,0, fill_zstart])
418     mirror([0,0, fill_downwards])
419     linear_extrude(height = fill_th)
420     rectfromto([+1,-1] * phone_cnr_rad,
421                [phone_width, -phone_height] + [-1,+1] * phone_cnr_rad);
422 }
423
424 module CaseAperture(pos, dia, $fn) {
425   theta = 180/$fn;
426   translate([ pos[0] + bumper[0],
427               -epp2i[0],
428               -pos[1] ])
429     rotate([-90, theta, 0])
430     cylinder(r = dia/2 / cos(theta),
431              h = 60);
432 }
433
434 module SideButton(y, y_ref_sign, l){
435   // y_ref_sign:
436   //   +1  measured from top    of actual phone to top    of button
437   //   -1  measured from bottom of actual phone to bottom of button
438   //    0  y is centre of button in coordinate system
439   $button_l= l;
440   eff_y = y_ref_sign > 0 ?         -bumper [1] -y -l/2 :
441           y_ref_sign < 0 ? (-phone -bumper)[1] +y +l/2 :
442           y;
443   //echo(eff_y);
444   translate([0, eff_y, 0])
445     children();
446 }
447
448 module LidButtonishLeg(y, y_ref_sign, l=buttonishleg_default_l_is_fudge) {
449   $button_leg_only = true;
450   SideButton(y, y_ref_sign, l) children();
451 }
452
453 module Buttons(){
454   Flip_rhs(1) SideButton(15.580, +1, 8.9) children(); // power
455   Flip_rhs(1) SideButton(48.700, -1, 8.920) children(); // camera
456   Flip_rhs(0) SideButton(30.800, +1, 21.96) children(); // volume
457   Flip_rhs(   ) LidButtonishLeg(14, -1) children();
458 //  Flip_rhs(0) LidButtonishLeg(20, +1, 20) children();
459 }
460
461 module Struts(x_start, z_min, th){
462   // if th is negative, starts at z_min and works towards -ve z
463   // and object should then be printed other way up
464   for (i= [1 : 1 : case_struts_count]) {
465     translate([0,
466                0,
467                z_min])
468       mirror([0,0, th<0 ? 1 : 0])
469       translate([0,
470                  -phone_height * i / (case_struts_count+1),
471                  case_struts_solid_below])
472       linear_extrude(height= abs(th)
473                      -(case_struts_solid_below+case_struts_solid_above))
474       rectfromto([               x_start, -0.5 * case_struts_width ],
475                  [ phone_width - x_start, +0.5 * case_struts_width ]);
476   }
477 }
478
479 module OrdinaryRearAperture(rhs,bot, pos){
480   Flip_rhs(rhs) Flip_bot(bot)
481     linextr(-20, 20)
482     mirror([0,1])
483     translate(pos + bumper)
484     children();
485 }
486
487 module MicroUSB(){
488   Flip_bot(1){
489     rotate([90,0,0])
490       mirror([0,0,1])
491       linextr(-epp2i[0], 60)
492       translate([0.5 * phone_width, 0, 0])
493       rectfromto([-microusb_width/2, epp2i[1] + microusb_below],
494                  [+microusb_width/2, epp0[1] + -microusb_above]);
495   }
496 }
497
498 module OrdinaryRearApertures(){
499   // rear speaker
500   OrdinaryRearAperture(1,1, rearspeaker_pos_bl)
501     rectfromto(-rearspeaker_gap,
502                rearspeaker_size + rearspeaker_gap);
503
504   // finger hole to remove phone
505   if (len(fingerpushhole_dias))
506     OrdinaryRearAperture(1,0, [ fingerpushhole_dias[0]/2 + epp2i[0],
507                                 phone[1]/2 ])
508     scale(fingerpushhole_dias)
509     circle(r= 0.5 );
510 }
511
512 module RearCameraAperture(){
513   Flip_rhs(1)
514     mirror([0, 0, 1])
515     linear_extrude(height = 20)
516     mirror([0, 1, 0])
517     translate(bumper)
518     rectfromto(camera_pos_tl, camera_pos_br);
519 }
520
521 module HingeLidProfile(){
522   hull(){
523     circleat(hppT, hp_r1);
524     circleat(lpp12, lp_r12);
525     polygon([lpp10,
526              lpp13 + [2,0],
527              lpp12,
528              hppT]);
529   }
530 }
531
532 module HingeBaseProfile(){
533   difference(){
534     hull(){
535       circleat(hppB, hp_r1);
536       circleat(hppE, hp_r1);
537       circleat(epp2o, case_th_bottom);
538       circleat(hppB + [10,0], hp_r1);
539     }
540     polygon([epp5, epp1, epp2i, epp3, bppL]);
541   }
542 }
543
544 module HingeLeverOuterProfile(){
545   hull(){
546     circleat(hppT, hp_r2);
547     circleat(hppB, hp_r2);
548   }
549 }
550
551 module HingeLeverInnerProfile(){
552   for (c = [hppT, hppB]) {
553     hull()
554       for (x=[-20,20])
555         for (y=[0, c[1] - hppM[1]])
556           translate([x,y])
557             circleat(c, hp_rn);
558   }
559 }
560
561 module Flip_hinge(){
562   hinge_origin = [0, -(phone_height - hppB[0]), hppB[1]];
563   translate(hinge_origin)
564     rotate([180,0,0])
565     translate(-hinge_origin)
566     children();
567 }
568
569 module HingePortion(x0,x1){
570   Flip_rhs() Flip_bot(1)
571     translate([x0,0,0])
572     mirror([1,0,0])
573     rotate([90,0,-90])
574     linear_extrude(height=x1-x0)
575     children(0);
576 }
577
578 module CatchPortion(width){
579   translate([phone_width/2, 0,0])
580     rotate([90,0,-90])
581     linextr(-width/2, width/2)
582     children(0);
583 }
584
585 module PropRecess(){
586   // origin is nonstandard
587   hwd345 = 0.5*prop_main_width + prop_side_gap;
588   hwd35  = 0.5*prop_fin_width + prop_side_gap;
589   rotate([90,0,90]){
590     linextr(-hwd345, +hwd345)
591       polygon([ prp1,
592                 prp2,
593                 prp3,
594                 prp4,
595                 prp4 + [0,10],
596                 prp1 + [0,10],
597                 ]);
598     linextr(-hwd35, +hwd35)
599       polygon([ prp1,
600                 prp3,
601                 prp5,
602                 prp5 + [0,10],
603                 prp1 + [0,10],
604                 ]);
605   }
606 }
607
608 module CaseBase(){
609   AroundEdges(epp3[1], case_th_bottom, 1)
610     EdgeProfile();
611 }
612
613 module Case(){ ////toplevel
614   difference(){
615     union(){
616       CaseBase();
617
618       // ledge (fixed keeper)
619       Flip_rhs(1-keeper_side) intersection(){
620         rotate([90, 0, 0])
621           linear_extrude(height = phone_height + phone_cnr_rad * 2)
622           KeeperProfile(1);
623
624         // outline of the whole case, to stop it protruding
625         translate([0,0, -25])
626           linear_extrude(height = 50)
627           hull()
628           Flip_bot()
629           circleat([+1,-1] * phone_cnr_rad, phone_cnr_rad + case_th_side/2);
630       }
631
632       // hinge
633       HingePortion(hex20, hex21) HingeBaseProfile();
634     }
635
636     // slot for keeper
637     Flip_rhs(keeper_side)
638       translate([0, -phone_cnr_rad, 0])
639       rotate([90, 0, 0])
640       linear_extrude(height = phone_height + phone_cnr_rad * 2)
641       minkowski(){
642         KeeperProfile();
643         rectfromto([ -keeper_gap_x,    -keeper_gap_z_bot ],
644                    [ keeper_gap_x_holes,    +keeper_gap_z_top ]);
645       }
646
647     // front camera
648     RearCameraAperture();
649
650     // struts (invisible, because they're buried in the case)
651     Struts(epp2i[0], epp2i[1] - case_th_bottom, case_th_bottom);
652
653     Buttons(){
654       mirror([1,0,0])
655         rotate([90,0,90]) {
656           if (!($button_leg_only && enable_support))
657           intersection(){
658             translate([0,0,-10])
659               linear_extrude(height= 20)
660               ButtonPlan($button_l, 0,1);
661             if ($button_leg_only)
662               rotate([-90,90,0])
663                 translate([phone_width/2, -400, kppe[1]])
664                 mirror([1-abs($rhsflip - keeper_side),0,0])
665                 cube([400, 800, 50]);
666             if (enable_support)
667               rotate([-90,90,0])
668               translate([-400, -400, kppd[1]])
669                 mirror([0,0,1])
670                 cube([800,800,100]);
671           }
672           translate([0,0, -bppR[0]])
673             linear_extrude(height= 20)
674             ButtonPlan($button_l, 1,1);
675         }
676     }
677
678     // apertures along top edge
679     CaseAperture(jack_pos, jack_dia, 8);
680     Flip_rhs(1)
681       CaseAperture(noisecancelmic_pos, noisecancelmic_dia, 8);
682
683     OrdinaryRearApertures();
684
685     MicroUSB();
686
687     // gaps for the lid's hinge arms
688     HingePortion(hex20 - hinge_x_arms_gap,
689                  hex21 + hinge_x_arms_gap)
690       minkowski(){
691         HingeLidProfile();
692         circle(r= hinge_r_arms_gap, $fn= 8);
693       }
694
695     // screw holes in the hinge arms
696     HingeScrews();
697
698     // catch striker
699     CatchPortion(catch_width + catch_side_gap*2)
700       CatchCutProfile();
701   }
702 }
703
704 module Lid(){ ////toplevel
705   difference(){
706     union(){
707       AroundEdges(lpp10[1], lpp13[1] - lpp10[1], 0)
708         LidEdgeProfile();
709
710       // button covers
711       Buttons(){
712         intersection(){
713           rotate([90,0,90])
714             translate([0,0,-10])
715             linear_extrude(height= 20)
716             ButtonPlan($button_l, 1,0);
717           rotate([90,0,0])
718              translate([0,0,-100])
719             linear_extrude(height= 200)
720             ButtonCoverProfile();
721         }
722       }
723
724       // hinge arms
725       HingePortion(hex20, hex21) HingeLidProfile();
726
727       // catch
728       CatchPortion(catch_width)
729         CatchCatchProfile();
730     }
731     Struts(lpp10[0] + strut_min_at_end, lpp13[1], -case_th_lid);
732
733     // screw holes in the hinge arms
734     HingeScrews();
735
736     // prop recesses
737     Flip_bot(1)
738       for (y = prop_lid_posns)
739         translate([ phone_width/2, -y, lpp13[1] ])
740           PropRecess();
741   }
742 }
743
744 module HingeLever(){ ////toplevel
745   difference() {
746     // outer body, positive
747     HingePortion(hex22, hex22 + phone_width/2)
748       HingeLeverOuterProfile();
749
750     // space for the screws
751     HingePortion(hex23, hex24)
752       HingeLeverInnerProfile();
753
754     // bores for the screws
755     HingeScrews();
756
757     // space for the charging cable
758     MicroUSB();
759     Flip_hinge() MicroUSB();
760   }
761 }
762
763 module HingeLeverPrint(){ ////toplevel
764   rotate([90,0,0])
765     HingeLever();
766 }
767
768 module TestSelectLength(){
769   translate([-30, -200, -20])
770     cube([30 + 15, 250, 40]);
771 }
772
773 module TestLength(){ ////toplevel
774   intersection(){
775     Case();
776     TestSelectLength();
777   }
778 }
779
780 module TestLengthRight(){ ////toplevel
781   intersection(){
782     Case();
783     Flip_rhs(1)
784       TestSelectLength();
785   }
786 }
787
788 module TestSelectWidth(){
789   translate([-30, -(phone_height - 25), -20])
790     mirror([0, 1, 0])
791     cube([200, 50, 40]);
792 }
793
794 module TestWidth(){ ////toplevel
795   intersection(){
796     Case();
797     TestSelectWidth();
798   }
799 }
800
801 module TestLidWidthPrint(){ ////toplevel
802   rotate([0,180.0]) intersection(){
803     Lid();
804     TestSelectWidth();
805   }
806 }
807
808 module TestSelectRearAperture(){
809   minkowski(){
810     union() children();
811     translate([20, 0,0])
812       cube([42, 2, 1], center=true);
813   }
814 }
815
816 module TestSelectCamera(){
817   minkowski(){
818     TestSelectRearAperture()
819       RearCameraAperture();
820     cube([0.1, 50, 0.1]);
821   }
822 }
823
824 module TestSelectOrdinaryRearApertures(){
825   TestSelectRearAperture()
826     OrdinaryRearApertures();
827 }
828
829 module TestCamera(){ ////toplevel
830   intersection(){
831     Case();
832     TestSelectCamera();
833   }
834 }
835
836 module TestLidByCamera(){ ////toplevel
837   intersection(){
838     Lid();
839     TestSelectCamera();
840   }
841 }
842
843 module TestLidByCameraPrint(){ ////toplevel
844   rotate([180,0,0]) TestLidByCamera();
845 }
846
847 module DemoByCamera(){ ////toplevel
848   color("blue") TestLidByCamera();
849   color("red")  TestCamera();
850 }
851
852 module OneKeeper(){ ////toplevel
853   translate([0, -phone_cnr_rad, 0])
854     rotate([90, 0, 0])
855     linear_extrude(height = phone_height - phone_cnr_rad * 2)
856     KeeperProfile();
857 }
858
859 module OneKeeperPrint(){ ////toplevel
860   rotate([0,180,0])
861     OneKeeper();
862 }
863
864 module LidPrint(){ ////toplevel
865   rotate([0,180,0])
866     Lid();
867 }
868
869 module TestSelectPropRecesses(posns){
870   linextr(-100,100){
871     translate([phone_width/2, -phone_height]){
872       square([ 3, 500 ], center=true);
873       for (y=posns)
874         hull()
875           for (dy=[ -prp5[0], -prp2[0] ])
876             translate([0,y+dy])
877               square([prop_main_width+5, 5], center=true);
878     }
879   }
880 }
881
882 module TestSelectFrame(){
883   include = [1,-1] * (epp2i[0] + 4);
884
885   difference(){
886     cube(1000, center=true);
887     translate([0,0, -100])
888       linear_extrude(height=200)
889       rectfromto(include,  inside_br - include);
890   }
891 }
892
893 module TestSelectLidFrame(){
894   TestSelectFrame();
895   TestSelectPropRecesses(prop_lid_posns);
896 }
897
898 module TestFrameCase(){ ////toplevel
899   intersection(){
900     Case();
901     union(){
902       TestSelectFrame();
903       TestSelectCamera();
904       TestSelectOrdinaryRearApertures();
905     }
906   }
907 }
908
909 module TestFrameLidPrint(){ ////toplevel
910   rotate([0,180,0]) intersection(){
911     Lid();
912     TestSelectLidFrame();
913   }
914 }
915
916 module ButtonPlanForDemo(z, deep, cut){
917   translate([0,0,z])
918     ButtonPlan(8, deep, cut);
919 }
920
921 module HingeScrews(){
922   Flip_rhs() Flip_bot(1){
923     for (c= [ hppT, hppB ])
924       translate([ hex20,
925                   -c[0],
926                   c[1] ]){
927         rotate([0,90,0])
928           translate([0,0,-.2])
929           cylinder( r= hingescrew_shaft_dia/2,
930                     h = hingescrew_shaft_len+0.2 );
931         rotate([0,-90,0])
932           translate([0,0,+.1])
933           cylinder( r= hingescrew_head_dia/2, h = hingescrew_head_th );
934       }
935   }
936 }
937
938 module DemoFrame(){ ////toplevel
939   color("red") TestFrameCase();
940   color("blue") intersection(){ Lid(); TestSelectLidFrame(); }
941   color("black") HingeScrews();
942   %HingeLever();
943 }
944
945 module DemoHingedFrame(){ ///toplevel
946   color("red") TestFrameCase();
947   translate([0,0, -2*hp_k])
948   color("blue") intersection(){ Lid(); TestSelectLidFrame(); }
949
950   Flip_hinge(){
951     color("orange") HingeLever();
952     color("black") HingeScrews();
953   }
954 }
955
956 module DemoHinge(){ ////toplevel
957   DemoFrame();
958   translate([0,0, -hp_k*3])
959     DemoHingedFrame();
960 }
961
962 module DemoProfiles(){ ////toplevel
963   LidEdgeProfile();
964   %EdgeProfile();
965   KeeperProfile();
966   translate([0,0,-1]) color("black") KeeperProfile(1);
967
968   translate([20,0]) {
969     LidEdgeProfile();
970     %EdgeProfile();
971
972     demopoint_QR = [ bppS[0], bppQ[1] - 0.1];
973   
974     color("blue") ButtonCoverProfile();
975     color("red") {
976       rectfromto(bppQ, demopoint_QR);
977       rectfromto(bppR, demopoint_QR);
978     }
979   }
980
981   translate([-20,0]) {
982     color("black") ButtonPlanForDemo(-2, 0,1);
983     color("red" )  ButtonPlanForDemo(-4, 1,1);
984     color("blue")  ButtonPlanForDemo(-6, 1,0);
985   }
986
987   translate([0, -30]) {
988     %LidEdgeProfile();
989     %EdgeProfile();
990     color("blue") HingeLidProfile();
991     color("red")  HingeBaseProfile();
992     color("black") translate([0,0,-2]) HingeLeverOuterProfile();
993     color("orange") translate([0,0,-1]) HingeLeverInnerProfile();
994   }
995
996   translate([20,-30]) {
997     %EdgeProfile();
998     %LidEdgeProfile();
999     //translate([0,0,1]) CatchCutProfile();
1000     color("blue") CatchCatchProfile();
1001     color("red") difference(){ EdgeProfile(); CatchCutProfile(); }
1002   }
1003 }
1004
1005 //EdgeProfile();
1006 //KeeperProfile();
1007 //CaseBase();
1008 //%Case();
1009 //Keeper();
1010 //LidEdgeProfile();
1011 //KeeperProfile();
1012 //DemoProfiles();
1013 //PropRecess();