/
keyboard-case.scad
242 lines (218 loc) · 9.76 KB
/
keyboard-case.scad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
unit = 19.05; // Unit size of spacing between keys
cherry_plate_width = 14; // Width of hole in plate for key insertion. Adjust if needed
plate_thickness = 4; // Fairly thick for strength, ideally print this section with high infill
top_case_raised_height = 7.2 + 1; // Distance between plate and bottom of keycap plus a little extra, for raised top case
bottom_case_height = 13; // Enough room to house electonics
wall_thickness = 2; // Sides and bottom of case
keyswitch_color = "#aa3333";
themes = [
// Keycap case
["#130044", "#101010"],
["#73d373", "#180054"],
["#73d373", "#bbbbff"],
["#252525", "#bbbbff"],
];
theme = 2;
keycap_color = themes[theme].x;
case_color = themes[theme].y;
// Create a hole where a switch can be inserted. The top of the hole is at 0
module switch_hole(size, depth = 5) {
clip_recess_z = 1.5; // How far below the top the recess begins
clip_recess_x = 6; // How much of the side is taken up by the recess
clip_recess_y = 1.4; // How far in does the clip go
translate([0, 0, -depth]) {
linear_extrude(height = depth + 0.01, center = false, convexity = 3)
square([cherry_plate_width, cherry_plate_width], center = true);
linear_extrude(height = depth - clip_recess_z, center = false, convexity = 3)
for (r = [0, 90])
rotate([0, 0, r])
square([cherry_plate_width + 2 * clip_recess_y, clip_recess_x], center = true);
}
}
module cherry_keyswitch() {
cherry_switch_width = 14;
cherry_switch_depth = 5.2;
color(keyswitch_color) translate([0, 0, 5]) {
translate([0, 0, 0.5]) cube([7, 5.7, 1], center = true);
translate([0, 0, 1 + 1.8])
for (r = [0, 90])
rotate([0, 0, r])
cube([4.0, 1.2, 3.6], center = true);
}
color("#222222") {
hull() {
translate([0, 0, 0.99]) linear_extrude(height = 0.01, center = false, convexity = 3)
square([14, 14], center = true);
translate([0, 0, 5.6]) linear_extrude(height = 0.01, center = false, convexity = 3)
square([11, 11], center = true);
}
translate([0, 0, 0.5]) cube([15.6, 15.6, 1], center = true);
translate([0, 0, -8.3]) cylinder(r = 3.8 / 2, h = 5, $fn = 8);
hull() {
linear_extrude(height = 0.01, center = false, convexity = 3)
square([cherry_switch_width, cherry_switch_width], center = true);
translate([0, 0, -cherry_switch_depth]) linear_extrude(height = 0.01, center = false, convexity = 3)
square([cherry_switch_width-0.5, cherry_switch_width-0.5], center = true);
}
}
}
module simple_keycap(size) {
color(keycap_color) translate([0, 0, 7.2]) hull() {
linear_extrude(height = 0.01, center = false, convexity = 3)
offset(delta = -0.4, chamfer = false) square(size * unit, center = true);
translate([0, 0, 8]) linear_extrude(height = 0.01, center = false, convexity = 3)
offset(r = 2, $fn = 16) offset(delta = -5.5) square(size * unit, center = true);
}
}
// Negative space for a keycap for raised top cases
module keycap_hole(size, depth = top_case_raised_height) {
linear_extrude(height = depth + 1, center = false, convexity = 3)
offset(delta = 0.4) square(size * unit, center = true);
}
// Negative space for a key switch plus keycap
module case_switch_hole(size) {
keycap_hole(size);
switch_hole(size);
}
// For subtracting a hole in the bottom case for a microswitch
module reset_microswitch(hole = true) {
translate([0, 0, wall_thickness]) {
color("#202020") cube([14, 6, 6], center = false);
color("red") translate([4, 0.01, 1]) cube([2, hole ? 10 : 7, 4], center = false);
}
}
// Typical mini USB breakout boards from e.g. Aliexpress
mini_usb_screw_dia = 3.0;
mini_usb_screw_rad = (mini_usb_screw_dia - 0.6) / 2; // Smaller than M3 to tap into
mini_usb_screw_sep = 20;
mini_usb_hole_height = 7.5;
module mini_usb_hole(hole = true) {
translate([0, 0, wall_thickness + 0.05]) {
color("green") translate([0, -11, pcb_thickness/2]) cube([25.5, 19.5, pcb_thickness], center = true);
if (hole) {
translate([0, 0, mini_usb_hole_height/2]) rotate([90, 0, 0]) roundedcube([10, mini_usb_hole_height, 10], r=1.5, center=true, $fs=1);
}
color("silver") translate([0, -5, mini_usb_hole_height/2]) rotate([90, 0, 0]) cube([7.6, 3.7, 9.2], r=1.5, center=true, $fs=1);
for (i = [-1,1], j = [0, 14]) {
translate([i*mini_usb_screw_sep/2, -4-j, -5]) polyhole(r=mini_usb_screw_rad, h=10);
}
}
}
// Typical micro USB breakout boards from e.g. Aliexpress
micro_usb_screw_dia = 3.0;
micro_usb_screw_rad = (micro_usb_screw_dia - 0.6) / 2; // Smaller than M3 to tap into
micro_usb_screw_sep = 9;
micro_usb_hole_height = 7.5;
micro_usb_socket_height = 2.5;
pcb_thickness = 2;
module micro_usb_hole(hole = true) {
translate([0, 0, wall_thickness + 0.01]) {
color("green") translate([0, -8, pcb_thickness/2]) cube([14, 14, pcb_thickness], center = true);
color("silver") {
if (hole) {
translate([0, 1, micro_usb_hole_height/2]) rotate([90, 0, 0]) roundedcube([11, micro_usb_hole_height, 10], r=1.5, center=true, $fs=1);
}
translate([0, -3, pcb_thickness + micro_usb_socket_height / 2]) rotate([90, 0, 0]) cube([7.5, micro_usb_socket_height, 7], r = 1.5, center = true, $fs = 1);
}
for (i = [-1,1]) {
translate([i * micro_usb_screw_sep/2, -8, -5]) polyhole(r = micro_usb_screw_rad, h = 15);
}
}
}
// You can use this for things that don't need to vary each child according to the key (e.g. size)
module key_positions(keys) {
// Mirror because KLE has Y axis reversed
mirror([0, 0, 1]) for (key = keys)
position_key(key)
children();
}
module key_holes(keys, type = "both") {
//echo("Number of keys:",len(keys));
// Mirror because KLE has Y axis reversed
mirror([0, 1, 0]) for (key = keys) {
position_key(key)
if (type == "both") {
case_switch_hole(key_size(key));
} else if (type == "plate") {
switch_hole(key_size(key));
} else if (type == "switch") {
cherry_keyswitch();
} else if (type == "keycap") {
simple_keycap(key_size(key));
}
}
}
module screw_positions(screws) {
for (screw = screws) translate(screw) children();
}
module screw_holes(screws, screw_depth = 8, screw_head_depth = plate_thickness + top_case_raised_height) {
screw_positions(screws) {
mirror([0, 0, 1]) translate([0, 0, -screw_head_depth]) bolthole(r1=6/2, r2=3.2/2, h1=screw_head_depth, h2=screw_depth);
}
}
// children should be a 2d polygon specifying the outer border of case
module top_case(keys, screws, raised = false, chamfer_height = 2.5, chamfer_width, chamfer_faces = [false, true]) {
screw_offset = 3;
chamfer_w = chamfer_width == undef ? chamfer_height : chamfer_width;
total_depth = plate_thickness + (raised ? top_case_raised_height : 0);
color(case_color) difference() {
render() chamfer_extrude(height = total_depth, chamfer = chamfer_height, width = chamfer_w, faces = chamfer_faces, $fn = 25) children();
translate([0, 0, screw_offset]) screw_holes(screws);
translate([0, 0, plate_thickness]) key_holes(keys);
}
}
// M5 bolt tenting
boltRad = 5 / 2;
nutRad = 9.4 / 2;
nutHeight = 3.5;
module tent_support(position) {
base_chamfer = 2.5;
off = apothem(nutRad, 6)+0.5;
lift = 0;
height = bottom_case_height - lift;
translate([position[0], position[1], lift]) rotate([0, 0, position[2]]) {
difference() {
chamfer_extrude(height=height, chamfer=base_chamfer, faces = [true, false]) {
hull() {
translate([-5,0]) square([0.1, 35], center=true);
translate([off, 0]) circle(r=boltRad+base_chamfer+1.5);
}
}
//translate([-10,-20, -0.1]) cube([10-base_chamfer, 40, bottom_case_height+1], center=false);
// Screw hole
translate([off, 0, -0.1]) polyhole(r=boltRad, h=height+1);
// Nut hole
translate([off, 0, height-nutHeight]) rotate([0, 0, 60/2]) cylinder(r=nutRad, h=nutHeight+0.1, $fn=6);
}
}
}
// children should be a 2d polygon specifying the outer border of case
module bottom_case(screws, tent_positions = [], chamfer_height = 2.5, chamfer_width) {
screw_offset = 3;
screw_rad = 3;
standoff_rad = 7 / 2;
chamfer_w = chamfer_width == undef ? chamfer_height : chamfer_width;
color(case_color) difference() {
union() {
render() chamfer_extrude(height = bottom_case_height, chamfer = chamfer_height, width = chamfer_w, faces = [true, false], $fn = 25)
children();
for(tent = tent_positions) {
tent_support(tent);
}
}
difference() {
render() translate([0, 0, wall_thickness])
chamfer_extrude(height = bottom_case_height, chamfer = chamfer_height * 0.7, width = chamfer_w * 0.7, faces = [true, false], $fn = 25)
offset(delta = -wall_thickness) children();
screw_positions(screws)
hull() {
translate([0, 0, bottom_case_height]) polyhole(r = standoff_rad, h = 0.1);
polyhole(r = standoff_rad + 2, h = 0.1);
}
}
translate([0, 0, wall_thickness]) screw_positions(screws) polyhole(r = 2.4 / 2, h = 50);
}
}
// Requires my utility functions in your OpenSCAD lib or as local submodule
// https://github.com/Lenbok/scad-lenbok-utils.git
use<Lenbok_Utils/utils.scad>