chiark / gitweb /
f0de5d9a390753e2c801652eef38be31dc70f08f
[reprap-play.git] / fairphone-case.scad
1 // -*- C -*-
2
3 phone_height = 146.5 - .80;
4 phone_width = 76.75;
5
6 phone_cnr_rad = 6.0;
7
8 button_cutout_depth = 9;
9
10 phone_edge_thick = 9.0;
11 phone_total_thick = 12.0;
12 phone_backside_slope_inner = 1.5; // larger means shallower
13 phone_backside_slope_outer = 1.0; // larger means shallower
14
15 camera_pos_tl = [  7.0, 13.0 ]; // measured from tl corner
16 camera_pos_br = [ 24.0, 39.5 ]; // tl/br as seen from back
17
18 case_th_bottom = 2.5;
19 case_th_lid = 2.5;
20 case_th_side = 2;
21 case_th_lip = 1.2;
22
23 case_struts_count = 6;
24 case_struts_solid_below = 0.75;
25 case_struts_solid_above = 1.00;
26 case_struts_width = 0.10;
27
28 keeper_th_z = 0.75;
29 keeper_th_x = 0.75;
30 keeper_inner_width = 1.75;
31 keeper_inner_height = 1.75;
32
33 keeper_gap_z_top = 0.25;
34 keeper_gap_z_bot = 0.75;
35 keeper_gap_x     = 0.25;
36 keeper_gap_x_holes = 0.75;
37
38 case_lip = 1.25;
39
40 lid_gap_x = 0.25;
41 lid_gap_z = 0.25;
42 lid_lip = 1.75;
43
44 $fa = 5;
45 $fs = 0.1;
46
47 // ---------- calculated ----------
48
49 // ----- could be changed -----
50 lid_buttoncover_gap = lid_gap_x;
51 lid_buttoncover_overlap = case_th_lip + keeper_gap_z_top;
52
53 phone_backside_slope_thick = phone_total_thick - phone_edge_thick;
54
55 //lid_lip_overlap_width xxx bad name = ;
56 //lid_lip_inner_slope = [ 5, 5 ]; // xxx
57
58 epp0 = [0,0];
59 epp1 = [0, -phone_edge_thick];
60 epp2i = epp1 + phone_backside_slope_thick * [ phone_backside_slope_inner, -1 ];
61 epp2o = epp1 + phone_backside_slope_thick * [ phone_backside_slope_outer, -1 ];
62 epp3 = epp2i + [10, 0];
63 epp5 = epp0 + [0,1] * (keeper_th_z + keeper_gap_z_top + case_lip);
64 epp4 = epp5 + [-1,0] * case_th_side;
65
66 kppe = [0,0];
67 kppd = kppe + [1,0] * keeper_inner_width;
68 kppc = kppd + [0,1] * keeper_th_z;
69 kppb = [ kppe[0] - keeper_th_x, kppc[1] ];
70 kppf = kppe - [0,1] * keeper_inner_height;
71 kppa = [ kppb[0], kppf[1] ];
72
73 lpp10 = [ epp5[0] + lid_gap_x, kppc[1] + lid_gap_z ];
74 lpp11 = [ lpp10[0],            epp5[1] + lid_gap_z ];
75 lpp12 = [ epp4[0] + lid_lip,   lpp11[1] ];
76 lpp13 = [ lpp12[0],            lpp12[1] + lid_lip ];
77
78 // button profile
79 bppM = epp4 + [0,5];
80 bppN = [ 0.5 * (epp0[0] + epp4[0]), bppM[1] ];
81 bppR = [ bppN[0] + lid_buttoncover_gap, -button_cutout_depth ];
82 bppS = [ epp1[0], bppR[1] ];
83 bppQ = [ bppM[0], bppR[1] - lid_buttoncover_overlap ];
84 bppP = bppQ + [0,1] * lid_buttoncover_gap;
85 bppO = [ bppN[0], bppP[1] ];
86 bppL = lpp10 + [5,0];
87 bppK = [ bppL[0], bppN[1] ];
88 bppJ = [ bppN[0], bppL[1] ];
89
90 module rectfromto(a,b) {
91   ab = b - a;
92   translate([min(a[0], b[0]), min(a[1], b[1])])
93     square([abs(ab[0]), abs(ab[1])]);
94 }
95 module circleat(c,r) { translate(c) circle(r); }
96
97 module KeeperProfile(){
98   polygon([kppe, kppd, kppc, kppb, kppa, kppf]);
99 }
100
101 module EdgeProfile(){
102   difference(){
103     hull(){
104       translate(epp3) square(case_th_bottom*2, center=true);
105       circleat(epp2o, r=case_th_bottom);
106       circleat(epp1, r=case_th_side);
107       rectfromto(epp0, epp4);
108     }
109     polygon([ epp5 + [0,10],
110               epp1,
111               epp2i,
112               epp3 + [10,0] ]);
113   }
114 }
115
116 module LidEdgeProfile(){
117   polygon([ lpp10,
118             lpp11,
119             lpp12,
120             lpp13,
121             lpp13 + [10, 0],
122             lpp10 + [10, 0]
123             ]);
124   intersection(){
125     circleat(lpp12, r=lid_lip);
126     rectfromto( lpp12 + [-10,   0],
127                 lpp12 + [+10, +10] );
128   }
129 }
130
131 module ButtonCoverProfile(){
132   intersection(){
133     polygon([ bppM, bppP, bppO, bppJ, bppL, bppK ]);
134     hull(){
135       EdgeProfile();
136       LidEdgeProfile();
137     }
138   }
139 }
140
141 module ButtonPlan(l, bigger, d_D1_y){
142   C = [0,0]; // by definition
143   T = [ 0, epp4[1] ];
144   G = T + [0,10];
145   B0 = T + [0,-1] * button_cutout_depth;
146   r0 = 0.5 * (T[1] - B0[1]);
147   A = [  -l/2 + r0, 0.5 * (T[1] + B0[1]) ];
148   r1 = r0 + bigger;
149   // | D1 - A | = r1 * 2
150   // D1_y = T_y - r1 + d_D1_y
151   // =>    4 * r1^2   =  (r1-r0 + d_D1_y)^2 + (D1_x - A_x)^2
152   // =>    D1_x =  A_x + v/~ [  4 * r1^2   -   (r1-r0 + d_D1_y)^2 ]
153   D1  = [ A[0] + sqrt( 4*r1*r1 - pow(bigger + d_D1_y, 2) ),
154           T[1] - r1 + d_D1_y ];
155   E1  = 0.5 * (D1 + A);
156   F1  = D1 + [0,10];
157
158   hull(){
159     for (m=[0,1]) mirror([m,0])
160       circleat(A, r1);
161   }
162   for (m=[0,1]) mirror([m,0]) {
163     difference(){
164       polygon([ E1,
165                 A,
166                 B0,
167                 G,
168                 F1,
169                 D1
170                 ]);
171       circleat(D1, r1);
172     }
173   }
174 }
175
176 module CaseBase_rhsflip(yn=[0,1]) {
177   for (rhs=yn) {
178     translate([phone_width/2, 0, 0])
179       mirror([rhs,0,0])
180       translate([-phone_width/2, 0, 0])
181       children();
182   }
183 }
184
185 module CaseBase_botflip() {
186   for (bot=[0,1]) {
187     translate([0, -phone_height/2, 0])
188       mirror([0, bot, 0])
189       translate([0, phone_height/2, 0])
190       children();
191   }
192 }  
193
194 module AroundEdges(fill_zstart, fill_th, fill_downwards=0){
195   // sides
196   CaseBase_rhsflip(){
197     translate([0, -phone_cnr_rad, 0])
198       rotate([90,0,0])
199       linear_extrude(height = phone_height - phone_cnr_rad*2)
200       children(0);
201   }
202   // corners
203   CaseBase_rhsflip() CaseBase_botflip() {
204     translate([+1,-1] * phone_cnr_rad)
205       intersection(){
206         rotate_extrude()
207           intersection(){
208             mirror([1,0,0])
209               translate([-1,0] * phone_cnr_rad)
210               children(0);
211             rectfromto([0,-20],[10,20]);
212           }
213         translate([-10, 0, -20] + 0.01 * [+1,-1, 0] )
214           cube([10,10,40]);
215       }
216   }
217   // top and bottom
218   CaseBase_botflip(){
219     translate([ phone_width - phone_cnr_rad, 0,0 ])
220       rotate([90,0,-90])
221       linear_extrude(height = phone_width - phone_cnr_rad*2)
222       children(0);
223   }
224   // fill
225   translate([0,0, fill_zstart])
226     mirror([0,0, fill_downwards])
227     linear_extrude(height = fill_th)
228     rectfromto([+1,-1] * phone_cnr_rad,
229                [phone_width, -phone_height] + [-1,+1] * phone_cnr_rad);
230 }
231
232 module CaseBase(){
233   AroundEdges(epp3[1], case_th_bottom, 1)
234     EdgeProfile();
235 }
236
237 module Case(){ ////toplevel
238   difference(){
239     union(){
240       CaseBase();
241
242       // ledge (fixed keeper)
243       intersection(){
244         rotate([90, 0, 0])
245           linear_extrude(height = phone_height + phone_cnr_rad * 2)
246           KeeperProfile();
247
248         // outline of the whole case, to stop it protruding
249         translate([0,0, -25])
250           linear_extrude(height = 50)
251           hull()
252           // CaseBase_rhsflip() // actually, we only care about the LH
253           CaseBase_botflip()
254           circleat([+1,-1] * phone_cnr_rad, phone_cnr_rad + case_th_side/2);
255       }
256     }
257
258     // slot for keeper
259     CaseBase_rhsflip(1)
260       translate([0, -phone_cnr_rad, 0])
261       rotate([90, 0, 0])
262       linear_extrude(height = phone_height + phone_cnr_rad * 2)
263       minkowski(){
264         KeeperProfile();
265         rectfromto([ -keeper_gap_x,    -keeper_gap_z_bot ],
266                    [ keeper_gap_x_holes,    +keeper_gap_z_top ]);
267       }
268
269     // front camera
270     CaseBase_rhsflip([1])
271       mirror([0, 0, 1])
272       linear_extrude(height = 20)
273       mirror([0, 1, 0])
274       rectfromto(camera_pos_tl, camera_pos_br);
275
276     // struts (invisible, because they're buried in the case)
277     for (i= [1 : 1 : case_struts_count]) {
278       translate([0,
279                  -phone_height * i / (case_struts_count+1),
280                  epp2i[1] - case_th_bottom + case_struts_solid_below])
281         linear_extrude(height= case_th_bottom
282                        -(case_struts_solid_below+case_struts_solid_above))
283         rectfromto([               epp2i[0], -0.5 * case_struts_width ],
284                    [ phone_width - epp2i[0], +0.5 * case_struts_width ]);
285     }
286   }
287 }
288
289 module Lid(){ ////toplevel
290   AroundEdges(lpp10[1], lpp13[1] - lpp10[1], 0)
291     LidEdgeProfile();
292 }
293
294 module TestLength(){ ////toplevel
295   intersection(){
296     Case();
297     translate([-30, -200, -20])
298     cube([30 + 15, 250, 40]);
299   }
300 }
301
302 module TestWidth(){ ////toplevel
303   intersection(){
304     Case();
305     translate([-30, -(phone_height - 25), -20])
306       mirror([0, 1, 0])
307       cube([200, 50, 40]);
308   }
309 }
310
311 module TestLidWidthPrint(){ ////toplevel
312   rotate([0,180.0])
313     intersection(){
314       Lid();
315       translate([-30, -(phone_height - 25), -20])
316         mirror([0, 1, 0])
317         cube([200, 50, 40]);
318     }
319 }
320
321 module TestCamera(){ ////toplevel
322   intersection(){
323     Case();
324     CaseBase_rhsflip(1)
325       translate([0,0,-25])
326       linear_extrude(height = 50)
327       mirror([0, 1, 0])
328       rectfromto([-20, -20],
329                  camera_pos_br + [ 5, 5 ]);
330   }
331 }
332
333 module OneKeeper(){ ////toplevel
334   translate([0, -phone_cnr_rad, 0])
335     rotate([90, 0, 0])
336     linear_extrude(height = phone_height - phone_cnr_rad * 2)
337     KeeperProfile();
338 }
339
340 module OneKeeperPrint(){ ////toplevel
341   rotate([0,180,0])
342     OneKeeper();
343 }
344
345 module Keeper(){ ////toplevel
346   CaseBase_rhsflip()
347     OneKeeper();
348 }
349
350 module ButtonPlanForDemo(colour, z, gap, overlap){
351   color(colour)
352     translate([0,0,z])
353     ButtonPlan(8,
354                lid_buttoncover_gap * gap +
355                lid_buttoncover_overlap * overlap,
356                lid_buttoncover_gap * (1-gap));
357 }
358
359 module DemoProfiles(){ ////toplevel
360   LidEdgeProfile();
361   %EdgeProfile();
362   KeeperProfile();
363
364   translate([20,0]) {
365     LidEdgeProfile();
366     %EdgeProfile();
367
368     demopoint_QR = [ bppS[0], bppQ[1] - 0.1];
369   
370     color("blue") ButtonCoverProfile();
371     color("red") {
372       rectfromto(bppQ, demopoint_QR);
373       rectfromto(bppR, demopoint_QR);
374     }
375   }
376
377   translate([-20,0]) {
378     ButtonPlanForDemo("blue",    0, 0,0);
379     ButtonPlanForDemo("red",    -1, 1,0);
380     ButtonPlanForDemo("yellow", -2, 0,1);
381     ButtonPlanForDemo("blue",   -3, 1,1);
382   }
383 }
384
385 //EdgeProfile();
386 //KeeperProfile();
387 //CaseBase();
388 //%Case();
389 //Keeper();
390 //LidEdgeProfile();
391 //KeeperProfile();
392 //DemoProfiles();