]> git.armaanb.net Git - atreides.git/blob - case/openscad/atreus_case.scad
OpenSCAD design with (optional) 2 thumb keys per hand.
[atreides.git] / case / openscad / atreus_case.scad
1 // -*- mode: c -*-
2 /* All distances are in mm. */
3
4 /* set output quality */
5 $fn = 50;
6
7 /* Distance between key centers. */
8 column_spacing   = 19;
9 row_spacing      = column_spacing;
10
11 /* This number should exceed row_spacing and column_spacing. The
12  default gives a 1mm = (20mm - 19mm) gap between keycaps and cuts in
13  the top plate.*/
14 key_hole_size = 20;
15
16 /* rotation angle; the angle between the halves is twice this
17    number */
18 angle = 10;
19
20 /* The radius of screw holes. Holes will be slightly bigger due
21    to the cut width. */
22 screw_hole_radius = 1.5;
23 /* Each screw hole is a hole in a "washer". How big these "washers"
24    should be depends on the material used: this parameter and the
25    `switch_hole_size` determine the spacer wall thickness. */
26 washer_radius     = 4 * screw_hole_radius;
27
28 /* This constant allows tweaking the location of the screw holes near
29    the USB cable. Only useful with small `angle` values. Try the value
30    of 10 with angle=0. */
31 back_screw_hole_offset = 0;
32
33 /* Distance between halves. */
34 hand_separation        = 0;
35
36 /* The approximate size of switch holes. Used to determine how
37    thick walls can be, i.e. how much room around each switch hole to
38    leave. See spacer(). */
39 switch_hole_size = 14;
40
41 /* Sets whether the case should use notched holes. As far as I can
42    tell these notches are not all that useful... */
43 use_notched_holes = true;
44
45 /* Number of rows and columns in the matrix. You need to update
46    staggering_offsets if you change n_cols. */
47 n_rows = 4;
48 n_cols = 5;
49
50 /* Number of thumb keys (per hand), try 1 or 2. */
51 n_thumb_keys = 1;
52
53 /* The width of the USB cable hole in the spacer. */
54 cable_hole_width = 4;
55
56 /* Vertical column staggering offsets. The first element should
57    be zero. */
58 staggering_offsets = [0, 5, 11, 6, 3];
59
60 module rz(angle, center=undef) {
61   /* Rotate children `angle` degrees around `center`. */
62   translate(center) {
63     rotate(angle) {
64       translate(-center) {
65         for (i=[0:$children-1])
66           child(i);
67       }
68     }
69   }
70 }
71
72 /* Compute coordinates of a point obtained by rotating p angle degrees
73    around center. Used to compute locations of screw holes near the
74    USB cable hole. */
75 function rz_fun(p, angle, center) = [cos(angle) * (p[0] - center[0]) - sin(angle) * (p[1] - center[1]) + center[0],
76                                      sin(angle) * (p[0] - center[0]) + cos(angle) * (p[1] - center[1])+ center[1]];
77
78 module switch_hole(position, notches=use_notched_holes) {
79   /* Cherry MX switch hole with the center at `position`. Sizes come
80      from the ErgoDox design. */
81   hole_size    = 13.97;
82   notch_width  = 3.5001;
83   notch_offset = 4.2545;
84   notch_depth  = 0.8128;
85   translate(position) {
86     union() {
87       square([hole_size, hole_size], center=true);
88       if (notches == true) {
89         translate([0, notch_offset]) {
90           square([hole_size+2*notch_depth, notch_width], center=true);
91         }
92         translate([0, -notch_offset]) {
93           square([hole_size+2*notch_depth, notch_width], center=true);
94         }
95       }
96     }
97   }
98 };
99
100 module regular_key(position, size) {
101   /* Create a hole for a regular key. */
102   translate(position) {
103     square([size, size], center=true);
104   }
105 }
106
107 module thumb_key(position, size) {
108   /* Create a hole for a 1x1.5 unit thumb key. */
109   translate(position) {
110     scale([1, 1.5]) {
111       translate(-position) {
112         regular_key(position, size);
113       }
114     }
115   }
116 }
117
118 module column (bottom_position, switch_holes, key_size=key_hole_size) {
119   /* Create a column of keys. */
120   translate(bottom_position) {
121     for (i = [0:(n_rows-1)]) {
122       if (switch_holes == true) {
123         switch_hole([0, i*column_spacing]);
124       } else {
125         regular_key([0, i*column_spacing], key_size);
126       }
127     }
128   }
129 }
130
131 module rotate_half() {
132   /* Rotate the right half of the keys around the top left corner of
133      the thumb key. Assumes that the thumb key is a 1x1.5 key and that
134      it is shifted 0.5*column_spacing up relative to the nearest column. */
135   rotation_y_offset = 1.75 * column_spacing;
136   for (i=[0:$children-1]) {
137     rz(angle, [hand_separation, rotation_y_offset]) {
138       child(i);
139     }
140   }
141 }
142
143 module add_hand_separation() {
144   /* Shift everything right to get desired hand separation. */
145   for (i=[0:$children-1]) {
146     translate([0.5*hand_separation, /* we get back the full separation
147                                        because of mirroring */
148                0]) child(i);
149   }
150 }
151
152 module right_half (switch_holes=true, key_size=key_hole_size) {
153   /* Create switch holes or key holes for the right half of the
154      keyboard. Different key_sizes are used in top_plate() and
155      spacer(). */
156   x_offset = 0.5 * row_spacing;
157   y_offset = 0.5 * column_spacing;
158   thumb_key_offset = y_offset + 0.5 * column_spacing;
159   rotate_half() {
160     add_hand_separation() {
161       for (j=[0:(n_thumb_keys-1)]) {
162         if (switch_holes == true) {
163           switch_hole([x_offset + j*row_spacing, thumb_key_offset]);
164         } else {
165           thumb_key([x_offset + j*row_spacing, thumb_key_offset], key_size);
166         }
167       }
168       for (j=[0:(n_cols-1)]) {
169         column([x_offset + (j+n_thumb_keys)*row_spacing, y_offset + staggering_offsets[j]], switch_holes, key_size);
170       }
171     }
172   }
173 }
174
175 module screw_hole(radius, offset_radius, position, direction) {
176   /* Create a screw hole of radius `radius` at a location
177      `offset_radius` from `position`, (diagonally), in the direction
178      `direction`. Oh, what a mess this is. */
179   /* direction is the 2-element vector specifying to which side of
180      position to move to, [-1, -1] for bottom left, etc. */
181
182   /* radius_offset is the offset in the x (or y) direction so that
183      we're offset_radius from position */
184   radius_offset = offset_radius / sqrt(2);
185   /* key_hole_offset if the difference between key spacing and key
186      hole edge */
187   key_hole_offset = 0.5*(row_spacing - key_hole_size);
188   x = position[0] + (radius_offset - key_hole_offset) * direction[0];
189   y = position[1] + (radius_offset - key_hole_offset) * direction[1];
190   translate([x,y]) {
191     circle(radius);
192   }
193 }
194
195 module right_screw_holes(hole_radius) {
196   /* coordinates of the back right screw hole before rotation... */
197   back_right = [(n_cols+n_thumb_keys)*row_spacing,
198                staggering_offsets[n_cols-1] + n_rows * column_spacing];
199   /* and after */
200   tmp = rz_fun(back_right, angle, [0, 2.25*column_spacing]);
201
202   nudge = 0.75;
203
204   rotate_half() {
205     add_hand_separation() {
206       screw_hole(hole_radius, washer_radius,
207                  [row_spacing, 0],
208                  [-nudge, -nudge]);
209       screw_hole(hole_radius, washer_radius,
210                  [(n_cols+n_thumb_keys)*row_spacing, staggering_offsets[n_cols-1]],
211                  [nudge, -nudge]);
212       screw_hole(hole_radius, washer_radius,
213                  back_right,
214                  [nudge, nudge]);
215     }
216   }
217
218   /* add the screw hole near the cable hole */
219   translate([cable_hole_width + washer_radius - tmp[0],
220              back_screw_hole_offset]) {
221     rotate_half() {
222       add_hand_separation() {
223         screw_hole(hole_radius,
224                    washer_radius,
225                    back_right,
226                    [nudge, nudge]);
227       }
228     }
229   }
230 }
231
232 module screw_holes(hole_radius) {
233   /* Create all the screw holes. */
234   right_screw_holes(hole_radius);
235   mirror ([1,0,0]) { right_screw_holes(hole_radius); }
236 }
237
238 module left_half(switch_holes=true, key_size=key_hole_size) {
239   mirror ([1,0,0]) { right_half(switch_holes, key_size); }
240 }
241
242 module bottom_plate() {
243   /* bottom layer of the case */
244   difference() {
245     hull() { screw_holes(washer_radius); }
246     screw_holes(screw_hole_radius);
247   }
248 }
249
250 module top_plate() {
251   /* top layer of the case */
252   difference() {
253     bottom_plate();
254     right_half(false);
255     left_half(false);
256   }
257 }
258
259 module switch_plate() {
260   /* the switch plate */
261   difference() {
262     bottom_plate();
263     right_half();
264     left_half();
265   }
266 }
267
268 module spacer() {
269   /* Create a spacer. */
270   difference() {
271     union() {
272       difference() {
273         bottom_plate();
274         hull() {
275           right_half(switch_holes=false, key_size=switch_hole_size + 3);
276           left_half(switch_holes=false, key_size=switch_hole_size + 3);
277         }
278       }
279       screw_holes(washer_radius);
280     }
281     screw_holes(screw_hole_radius);
282     /* add the USB cable hole: */
283     translate([-0.5*cable_hole_width, 2*column_spacing]) {
284       square([cable_hole_width, (2*n_rows) * column_spacing]);
285     }
286   }
287 }
288
289 /* Create all four layers. */
290 top_plate();
291 translate([300, 150]) { spacer(); }
292 translate([300, 0]) { switch_plate(); }
293 translate([0, 150]) { bottom_plate(); }