forked from nophead/NopSCADlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcb.scad
945 lines (803 loc) · 32.3 KB
/
pcb.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
//
// NopSCADlib Copyright Chris Palmer 2018
// nop.head@gmail.com
// hydraraptor.blogspot.com
//
// This file is part of NopSCADlib.
//
// NopSCADlib is free software: you can redistribute it and/or modify it under the terms of the
// GNU General Public License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// NopSCADlib is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with NopSCADlib.
// If not, see <https://www.gnu.org/licenses/>.
//
//
//! PCBs and perfboard with optional components. The shape can be a rectangle with optionally rounded corners or a polygon for odd shapes like Arduino.
//
panel_clearance = 0.2;
include <../core.scad>
include <buttons.scad>
include <green_terminals.scad>
include <pin_headers.scad>
use <microswitch.scad>
use <../utils/rounded_cylinder.scad>
use <../utils/dogbones.scad>
use <../utils/thread.scad>
use <../utils/tube.scad>
use <d_connector.scad>
function pcb_name(type) = type[1]; //! Description
function pcb_length(type) = type[2]; //! Length
function pcb_width(type) = type[3]; //! Width
function pcb_thickness(type) = type[4]; //! Thickness
function pcb_radius(type) = type[5]; //! Corner radius
function pcb_hole_d(type) = type[6]; //! Mounting hole diameter
function pcb_land_d(type) = type[7]; //! Pad around mounting hole
function pcb_colour(type) = type[8]; //! Colour of the subtrate
function pcb_parts_on_bom(type) = type[9]; //! True if the parts should be separate BOM items
function pcb_holes(type) = type[10]; //! List of hole positions
function pcb_components(type) = type[11]; //! List of components
function pcb_accessories(type) = type[12]; //! List of accessories to go on the BOM, SD cards, USB cables, etc.
function pcb_grid(type) = type[13]; //! Grid if a perfboard
function pcb_polygon(type) = type[14]; //! Optional outline polygon for odd shaped boards
function pcb_screw(type, cap = hs_cap) = Len(type[15]) ? type[15] : find_screw(cap, screw_smaller_than(pcb_hole_d(type))); //! Mounting screw type
function pcb_grid_pos(type, x, y, z = 0) = //! Returns a pcb grid position
[-pcb_length(type) / 2 + pcb_grid(type).x + 2.54 * x,
-pcb_width(type) / 2 + pcb_grid(type).y + 2.54 * y, pcb_thickness(type) + z];
module pcb_grid(type, x, y, z = 0) //! Positions children at specified grid position
translate(pcb_grid_pos(type, x, y, z))
children();
// allows negative ordinates to represent offsets from the far edge
function pcb_coord(type, p) = let(l = pcb_length(type), w = pcb_width(type)) //! Convert offsets from the edge to coordinates relative to the centre
[(p.x >= 0 ? p.x : l + p.x) - l / 2,
(p.y >= 0 ? p.y : w + p.y) - w / 2];
module pcb_hole_positions(type, all = true) { // Positition children at the hole positions, including holes not used for screws
holes = pcb_holes(type);
for($i = [0 : 1 : len(holes) - 1]) {
hole = holes[$i];
if(len(hole) == 2 || all)
translate(pcb_coord(type, hole))
children();
}
}
module pcb_screw_positions(type) //! Positions children at the mounting hole positions
pcb_hole_positions(type, false) children();
module chip(length, width, thickness, colour, cutout = false) //! Draw a coloured cube to represent a chip, or other rectangular component
if(!cutout)
color(colour)
translate_z(thickness / 2) cube([length, width, thickness], center = true);
module usb_Ax1(cutout = false) { //! Draw USB type A single socket
usb_A(h = 6.5, v_flange_l = 4.5, bar = 0, cutout = cutout);
}
module usb_Ax2(cutout = false) { //! Draw USB type A dual socket
usb_A(h = 15.6, v_flange_l = 12.15, bar = 3.4, cutout = cutout);
}
module usb_A(h, v_flange_l, bar, cutout) {
l = 17;
w = 13.25;
flange_t = 0.4;
h_flange_h = 0.8;
h_flange_l = 11;
v_flange_h = 1;
socket_h = (h - 2 * flange_t - bar) / 2;
translate_z(h / 2)
if(cutout)
rotate([90, 0, 90])
rounded_rectangle([w + 2 * v_flange_h + 2 * panel_clearance,
h + 2 * h_flange_h + 2 * panel_clearance, 100], r = cnc_bit_r, center = false);
else
color("silver") rotate([0, 90, 0]) {
linear_extrude(height = l, center = true)
difference() {
square([h, w], center = true);
for(s = [-1, 1])
translate([s * (bar / 2 + socket_h / 2), 0])
square([socket_h, w - 2 * flange_t], center = true);
}
translate_z(-l / 2 + 0.5)
cube([h, w, 1], center = true);
translate_z(l / 2 - flange_t)
linear_extrude(height = flange_t) difference() {
union() {
square([h + 2 * h_flange_h, h_flange_l], center = true);
square([v_flange_l, w + 2 * v_flange_h], center = true);
}
square([h - eps, w - eps], center = true);
}
}
}
module rj45(cutout = false) { //! Draw RJ45 Ethernet connector
l = 21;
w = 16;
h = 13.5;
plug_h = 6.8;
plug_w = 12;
plug_z = 4;
tab_z = 0.8;
tab_w = 4;
translate_z(h / 2)
if(cutout)
rotate([90, 0, 90])
dogbone_rectangle([w + 2 * panel_clearance, h + 2 * panel_clearance, 100], center = false);
else {
rotate([0, 90, 0]) {
mouth = plug_z + plug_h - tab_z;
color("silver") {
linear_extrude(height = l, center = true)
difference() {
square([h, w], center = true);
translate([h / 2 - tab_z - mouth / 2, 0])
square([mouth + 0.1, plug_w + 0.1], center = true);
}
translate_z(-l / 2)
cube([h, w, eps], center = true);
}
color(grey30) {
linear_extrude(height = l - 0.2, center = true)
difference() {
square([h - 0.1, w - 0.1], center = true);
translate([h / 2 - plug_z - plug_h / 2, 0])
square([plug_h, plug_w - 0.1], center = true);
translate([h / 2 - tab_z - plug_h / 2, 0])
square([plug_h, tab_w], center = true);
}
translate_z(-l / 2 + 1)
cube([h - 0.1, w - 0.1, 0.1], center = true);
}
}
}
}
module jack(cutout = false) { //! Draw 3.5mm jack
l = 12;
w = 7;
h = 6;
d = 6;
ch = 2.5;
translate_z(h / 2)
if(cutout)
rotate([0, 90, 0])
cylinder(d = d + 2 * panel_clearance, h = 100);
else
color(grey20)
rotate([0, 90, 0]) {
linear_extrude(height = l / 2)
difference() {
square([h, w], center = true);
circle(d = 3.5);
}
tube(or = d / 2, ir = 3.5 / 2, h = l / 2 + ch, center = false);
translate_z(-l / 4)
cube([h, w, l / 2], center = true);
}
}
module buzzer(height, diameter, colour) { //! Draw PCB buzzer with specified height, diameter and color
color (colour)
tube(or = diameter / 2, ir = height > 5 ? 1 : 0.75, h = height, center = false);
color("white")
cylinder(d = 2, h = max(height - 3 , 0.5));
}
module potentiometer(h1, h2) {
color("silver") {
baseSize = [12, 11, 6];
translate_z(baseSize.z / 2)
cube(baseSize, center = true);
translate_z(baseSize.z) {
cylinder(d = 5, h = h1 - 0.5);
if (show_threads)
male_metric_thread(6, metric_coarse_pitch(5), length = h1 - 0.5, center = false);
}
translate_z(baseSize.z + h1 - 0.5)
cylinder(d = 3, h = 0.5);
translate_z(baseSize.z + h1)
linear_extrude(h2)
difference() {
circle(d=5);
square([0.75,5], center = true);
}
}
}
function hdmi_depth(type) = type[2]; //! Front to back depth
function hdmi_width1(type) = type[3]; //! Inside width at the top
function hdmi_width2(type) = type[4]; //! Inside width at the bottom
function hdmi_height1(type) = type[5]; //! Inside height at the sides
function hdmi_height2(type) = type[6]; //! Inside height in the middle
function hdmi_height(type) = type[7]; //! Outside height above the PCB
function hdmi_thickness(type) = type[8]; //! Wall thickness of the metal
hdmi_full = [ "hdmi_full", "HDMI socket", 12, 14, 10, 3, 4.5, 6.5, 0.5 ];
hdmi_mini = [ "hdmi_mini", "Mini HDMI socket", 7.5, 10.5, 8.3, 1.28, 2.5, 3.2, 0.35 ];
module hdmi(type, cutout = false) { //! Draw HDMI socket
vitamin(str("hdmi(", type[0], "): ", type[1]));
l = hdmi_depth(type);
iw1 = hdmi_width1(type);
iw2 = hdmi_width2(type);
ih1 = hdmi_height1(type);
ih2 = hdmi_height2(type);
h = hdmi_height(type);
t = hdmi_thickness(type);
module D() {
hull() {
translate([-iw1 / 2, h - t - ih1])
square([iw1, ih1]);
translate([-iw2 / 2, h - t - ih2])
square([iw2, ih2]);
}
}
if(cutout)
rotate([90, 0, 90])
linear_extrude(height = 100)
offset(t + panel_clearance)
D();
else
color("silver")
rotate([90, 0, 90]) {
linear_extrude(height = l, center = true)
difference() {
offset(t)
D();
D();
}
translate_z(-l / 2)
linear_extrude(height = 1)
offset(t)
D();
}
}
module usb_uA(cutout = false) { //! Draw USB micro A connector
l = 6;
iw1 = 7;
iw2 = 5.7;
ih1 = 1;
ih2 = 1.85;
h = 2.65;
t = 0.4;
flange_h = 3;
flange_w = 8;
module D() {
hull() {
translate([-iw1 / 2, h - t - ih1])
square([iw1, ih1]);
translate([-iw2 / 2, h - t - ih2])
square([iw2, ih2]);
}
}
if(cutout)
rotate([90, 0, 90])
linear_extrude(height = 100)
offset((flange_h - ih2) / 2 + 2 * panel_clearance)
D();
else
color("silver") rotate([90, 0, 90]) {
linear_extrude(height = l, center = true)
difference() {
offset(t)
D();
D();
}
translate_z(-l / 2)
linear_extrude(height = 1)
offset(t)
D();
translate_z(l / 2 - t)
linear_extrude(height = t) difference() {
union() {
translate([0, h - t - ih1 / 2])
square([flange_w, ih1], center = true);
translate([0, h / 2 + flange_h / 4])
square([iw1, flange_h / 2], center = true);
translate([0, h / 2 - flange_h / 4])
square([iw2, flange_h / 2], center = true);
}
D();
}
}
}
module usb_B(cutout = false) { //! Draw USB B connector
l = 16.4;
w = 12.2;
h = 11;
tab_w = 5.6;
tab_h = 3.2;
d_h = 7.78;
d_w = 8.45;
d_w2 = 5;
d_h2 = d_h - (d_w - d_w2) / 2;
module D()
hull() {
translate([-d_w / 2, 0])
square([d_w, d_h2]);
translate([-d_w2 /2, 0])
square([d_w2, d_h]);
}
if(cutout)
translate([50, 0, h / 2 - panel_clearance])
cube([100, w + 2 * panel_clearance, h + 2 * panel_clearance], center = true);
else
translate_z(h / 2) rotate([90, 0, 90]) {
color("silver") {
linear_extrude(height = l, center = true)
difference() {
square([w, h], center = true);
translate([0, -d_h / 2])
offset(delta = 0.2)
D();
}
translate_z(-l / 2 + 0.1)
cube([w, h, 0.2], center = true);
}
color("white") {
linear_extrude(height = l - 0.4, center = true)
difference() {
square([w - 0.2, h - 0.2], center = true);
translate([0, -d_h / 2])
difference() {
D();
translate([0, d_h / 2])
square([tab_w, tab_h], center = true);
}
}
translate_z( -(l - 0.4) / 2 + 1)
cube([w - 0.2, h - 0.2, 2], center = true);
}
}
}
module barrel_jack(cutout = false) { //! Draw barrel power jack
l = 13.2;
w = 8.89;
h = 11;
bore_d = 6.3;
bore_h = 6.5;
bore_l = 11.8;
pin_d = 2;
front = 3.3;
r = 0.5;
contact_d = 2;
contact_w = 4;
inset = 1;
if(cutout)
;
else {
color(grey20) rotate([0, 90, 0]) {
linear_extrude(height = l, center = true) {
difference() {
translate([-h / 2, 0])
rounded_square([h, w], r);
translate([-bore_h, 0])
circle(d = bore_d);
translate([-h / 2 - bore_h, 0])
square([h, w], center = true);
}
}
translate_z(l / 2 - front)
linear_extrude(height = front) {
difference() {
translate([-h / 2, 0])
rounded_square([h, w], r);
translate([-bore_h, 0])
circle(d = bore_d);
}
}
translate([-bore_h, 0])
tube(or = w / 2 - 0.5, ir = bore_d / 2, h = l);
translate([-bore_h, 0, -l / 2])
cylinder(d = w -1, h = l - bore_l);
}
color("silver") {
translate([l / 2 - inset - pin_d / 2, 0, bore_h])
hull() {
sphere(pin_d / 2);
rotate([0, -90, 0])
cylinder(d = pin_d, h = bore_l - inset);
}
hull() {
translate([l / 2 - inset - contact_d / 2, 0, bore_h - bore_d / 2])
rotate([90, 0, 0])
cylinder(d = contact_d, h = contact_w, center = true);
translate([l / 2 - bore_l, 0, bore_h - bore_d / 2 + contact_d / 4])
cube([eps, contact_w, eps], center = true);
}
}
}
}
module uSD(size, cutout = false) { //! Draw uSD socket
min_w = 12;
w = size.x - min_w;
t = 0.15;
if(cutout)
;
else
translate_z(size.z / 2) {
color("silver")
rotate([90, 0, 90]) {
linear_extrude(height = size.y, center = true)
difference() {
square([size.x, size.z], center = true);
square([size.x - 2 * t, size.z - 2 * t], center = true);
}
translate_z(-size.y / 2 + t / 2)
cube([size.x, size.z, t], center = true);
}
if(w > 0)
color(grey20)
rotate([90, 0, 90])
translate_z(t)
linear_extrude(height = size.y - t, center = true)
difference() {
square([size.x - 2 * t, size.z - 2 * t], center = true);
translate([-size.x / 2 + min_w / 2 + 0.7, size.z / 2 - t])
square([min_w, 2.2], center = true);
}
}
}
module flex(cutout = false) { //! Draw flexistrip connector
l = 20.6;
w = 3;
h = 5.6;
top_l = 22.4;
top_t = 1.1;
tab_l = 13;
tab_w = 1;
slot_l = 16.4;
slot_w = 0.7;
slot_offset = 0.6;
if(cutout)
;
else {
color(grey30) {
translate_z(0.5)
cube([l, w, 1], center = true);
linear_extrude(height = h)
difference() {
square([l, w], center = true);
translate([0, -w / 2 + slot_offset + slot_w / 2])
square([slot_l, slot_w], center = true);
}
translate_z(h - top_t)
linear_extrude(height = top_t)
difference() {
union() {
square([top_l, w], center = true);
hull() {
translate([0, -w / 2 + (w + tab_w) / 2])
square([tab_l - 1, w + tab_w], center = true);
square([tab_l, w], center = true);
}
}
translate([0, -w / 2 + slot_offset + slot_w / 2])
square([slot_l, slot_w], center = true);
}
}
}
}
module flat_flex(cutout = false) { //! Draw flat flexistrip connector as used on RPI0
l1 = 17;
w1 = 1.4;
h1 = 1.2;
l2 = 15.4;
w2 = 1.6;
h2 = 1.0;
l3 = 16;
w3 = 1.1;
h3 = 1.2;
l4 = 12;
slot_l = 11.8;
slot_h = 0.9;
w = w1 + w2 + w3;
if(cutout)
;
else {
color(grey30) {
translate([w / 2 - w1, 0, h1 / 2])
rotate([90, 0, 90])
linear_extrude(height = w1)
difference() {
square([l1, h1], center = true);
translate([0, -h1 / 2])
square([slot_l, slot_h * 2], center = true);
}
}
color(grey90) {
translate([-w / 2 + w3 / 2, 0, h3 / 2])
cube([w3, l3, h3], center = true);
translate([-w / 2 + w3 + w2 / 2, 0, h2 / 2])
cube([w2, l2, h2], center = true);
translate([-w / 2 + w3 + w2 / 2, 0, h3 / 2])
cube([w2, l4, h3], center = true);
}
}
}
module terminal_35(ways, colour = "blue") { //! Draw 3.5mm terminal block
vitamin(str("terminal_35(", ways, "): Terminal block ", ways, " way 3.5mm"));
pitch = 3.5;
width = ways * pitch;
depth = 7;
height = 8.3;
chamfer_h = 3;
chamfer_d = 1;
box_z = 0.5;
box_w = 2.88;
box_h = 4.1;
wire_z = 2;
wire_d = 2;
pin_l = 4.2;
pin_d = 0.9;
module single() {
screw_r = 1;
color(colour) {
rotate([90, 0, 0])
linear_extrude(height = pitch, center = true)
polygon(points = [
[ depth / 2, 0],
[ depth / 2, box_z],
[-depth / 2 + 1, box_z],
[-depth / 2 + 1, box_z + box_h],
[ depth / 2, box_z + box_h],
[ depth / 2, height - chamfer_h],
[ depth / 2 - chamfer_d, height],
[ -screw_r - eps, height],
[ -screw_r - eps, box_z + box_h],
[ screw_r + eps, box_z + box_h],
[ screw_r + eps, height],
[-depth / 2, height],
[-depth / 2, 0],
]);
linear_extrude(height = box_z + box_h)
difference() {
square([depth, pitch], center = true);
translate([1, 0])
square([depth, box_w], center = true);
}
translate_z(box_z + box_h)
linear_extrude(height = height - box_z - box_h)
difference() {
square([2 * screw_r + 0.1, pitch], center = true);
circle(screw_r);
}
}
color("silver") {
screw_z = box_z + box_h;
translate_z(screw_z) {
cylinder(r = screw_r, h = height - screw_z - 1); // screw
linear_extrude(height = height - screw_z - 0.5)
difference() {
circle(1);
square([4, 0.5], center = true); // screw slot
square([0.5, 1.7], center = true); // second screw slot
}
}
translate_z(box_z - pin_l)
cylinder(d = pin_d, h = pin_l + box_z, $fn = 16); // pin
translate_z(box_z + box_h / 2) // terminal
rotate([0, -90, 0]) {
linear_extrude(height = depth - 2, center = true)
difference() {
square([box_h, box_w], center = true);
translate([wire_z - box_z - box_h / 2, 0])
circle(d = wire_d);
}
translate_z(depth / 2 - 1.5)
cube([box_h, box_w, 1], center = true);
}
}
}
for(i = [0: ways -1])
translate([0, i * pitch - width / 2 + pitch / 2])
single();
}
module molex_254(ways) { //! Draw molex header
vitamin(str("molex_254(", ways, "): Molex KK header ", ways, " way"));
pitch = 2.54;
width = ways * pitch - 0.1;
depth = 6.35;
height = 8.15;
base = 3.18;
back = 1;
below = 2.3;
above = 9;
color("white")
union() {
translate([ -depth / 2, -width / 2,])
cube([depth, width, base]);
w = width - pitch;
translate([- depth / 2, -w / 2])
cube([back, w, height]);
}
color("silver")
for(i = [0: ways -1])
translate([0, i * pitch - width / 2 + pitch / 2, (above + below) / 2 - below])
cube([0.44, 0.75, above + below], center = true);
}
module standoff(h, d, h2, d2) {
color("white") {
cylinder(d = d, h = h);
hull() {
translate_z(-(h2 - h) / 2 + d2 / 2)
sphere(d = d2);
translate_z(h +(h2 - h) / 2 - d2 / 2)
sphere(d = d2);
}
}
}
module pcb_component(comp, cutouts = false, angle = undef) { //! Draw pcb component from description
function show(comp, part) = (comp[3] == part || comp[3] == str("-",part)) && (!cutouts || angle == undef || angle == comp.z);
function param(n, default = 0) = len(comp) > n ? comp[n] : default;
rotate(comp.z) {
if(show(comp, "2p54header")) pin_header(2p54header, comp[4], comp[5], param(6), cutouts, colour = param(7, undef));
if(show(comp, "2p54boxhdr")) box_header(2p54header, comp[4], comp[5], param(6), cutouts);
if(show(comp, "2p54socket")) pin_socket(2p54header, comp[4], comp[5], param(6, false), param(7), param(8, false), cutouts, param(9, undef));
if(show(comp, "chip")) chip(comp[4], comp[5], comp[6], param(7, grey30), cutouts);
if(show(comp, "rj45")) rj45(cutouts);
if(show(comp, "usb_A")) usb_Ax1(cutouts);
if(show(comp, "usb_Ax2")) usb_Ax2(cutouts);
if(show(comp, "usb_uA")) usb_uA(cutouts);
if(show(comp, "usb_B")) usb_B(cutouts);
if(show(comp, "buzzer")) buzzer(param(4, 9), param(5, 12), param(6, grey20));
if(show(comp, "potentiometer")) potentiometer(param(4, 5), param(5, 9));
if(show(comp, "jack")) jack(cutouts);
if(show(comp, "barrel_jack")) barrel_jack(cutouts);
if(show(comp, "hdmi")) hdmi(hdmi_full, cutouts);
if(show(comp, "mini_hdmi")) hdmi(hdmi_mini, cutouts);
if(show(comp, "flex")) flex(cutouts);
if(show(comp, "flat_flex")) flat_flex(cutouts);
if(show(comp, "D_plug")) if(!cutouts) translate_z(d_pcb_offset(comp[4])) d_plug(comp[4], pcb = true);
if(show(comp, "molex_hdr")) if(!cutouts) molex_254(comp[4]);
if(show(comp, "jst_xh")) if(!cutouts) jst_xh_header(jst_xh_header, comp[4], param(5, false), param(6, "white"), param(7, undef));
if(show(comp, "term254")) if(!cutouts) green_terminal(gt_2p54,comp[4], comp[5], param(6,"lime"));
if(show(comp, "gterm")) if(!cutouts) green_terminal(comp[4], comp[5], comp[6], param(7,"lime"));
if(show(comp, "gterm35")) if(!cutouts) green_terminal(gt_3p5, comp[4], comp[5], param(6,"lime"));
if(show(comp, "gterm635")) if(!cutouts) green_terminal(gt_6p35, comp[4], comp[5], param(6,"lime"));
if(show(comp, "term35")) if(!cutouts) terminal_35(comp[4], param(5,"blue"));
if(show(comp, "transition")) if(!cutouts) idc_transition(2p54header, comp[4], comp[5]);
if(show(comp, "block"))
color(comp[7]) if(!cutouts) translate_z(comp[6] / 2) cube([comp[4], comp[5], comp[6]], center = true);
else if(comp[8]) translate([-50, 0, comp[6] / 2 - panel_clearance]) cube([100, comp[5] + 2 * panel_clearance, comp[6] + 2 * panel_clearance], center = true);
if(show(comp, "button_6mm")) square_button(button_6mm);
if(show(comp, "microswitch")) translate_z(microswitch_thickness(comp[4])/2) microswitch(comp[4]);
if(show(comp, "pcb")) if(!cutouts) translate_z(comp[4]) pcb(comp[5]);
if(show(comp, "standoff")) if(!cutouts) standoff(comp[4], comp[5], comp[6], comp[7]);
if(show(comp, "uSD")) uSD(comp[4], cutouts);
}
}
function pcb_component_position(type, name, index = 0) = //! Return x y position of specified component
[for(comp = pcb_components(type), p = [pcb_coord(type, [comp.x, comp.y])]) if(comp[3] == name) [p.x, p.y]][index];
module pcb_component_position(type, name) { //! Position child at the specified component position
for(comp = pcb_components(type)) {
p = pcb_coord(type, [comp.x, comp.y]);
if(comp[3][0] == "-") {
if(comp[3] == str("-", name))
translate([p.x, p.y])
vflip()
children();
}
else
if(comp[3] == name)
translate([p.x, p.y, pcb_thickness(type)])
children();
}
}
module pcb_components(type, cutouts = false, angle = undef) { //! Draw list of PCB components on the PCB
not_on_bom(pcb_parts_on_bom(type))
for(comp = pcb_components(type)) {
p = pcb_coord(type, [comp.x, comp.y]);
if(comp[3][0] == "-")
translate([p.x, p.y])
vflip()
pcb_component(comp, cutouts, angle);
else
translate([p.x, p.y, pcb_thickness(type)])
pcb_component(comp, cutouts, angle);
}
}
module pcb_cutouts(type, angle = undef) pcb_components(type, true, angle); //! Make cut outs to clear components on a PCB
module pcb_grid_positions(type) {
grid = pcb_grid(type);
x0 = grid.x;
y0 = grid.y;
cols = is_undef(grid[2]) ? round((pcb_length(type) - 2 * x0) / inch(0.1)) : grid[2] - 1;
rows = is_undef(grid[3]) ? round((pcb_width(type) - 2 * y0) / inch(0.1)) : grid[3] - 1;
for(x = [0 : cols], y = [0 : rows])
pcb_grid(type, x, y)
children();
}
module pcb(type) { //! Draw specified PCB
grid = pcb_grid(type);
t = pcb_thickness(type);
if(pcb_name(type))
vitamin(str("pcb(", type[0], "): ", pcb_name(type)));
for(part = pcb_accessories(type))
vitamin(part);
pcb_components(type);
color(pcb_colour(type)) linear_extrude(height = t) difference() {
if(Len(pcb_polygon(type)))
polygon(pcb_polygon(type));
else
rounded_square([pcb_length(type), pcb_width(type)], r = pcb_radius(type));
pcb_hole_positions(type)
circle(d = pcb_hole_d(type) + eps);
if(Len(grid))
pcb_grid_positions(type)
circle(d = 1 + eps);
}
color("silver")
translate_z(t / 2)
pcb_hole_positions(type)
tube(or = max(pcb_land_d(type), 1) / 2, ir = pcb_hole_d(type) / 2, h = t + 2 * eps);
fr4 = pcb_colour(type) != "sienna";
plating = 0.15;
color(pcb_colour(type) == "green" ? "silver" : "gold")
translate_z(-plating)
linear_extrude(height = fr4 ? t + 2 * plating : plating)
if(Len(grid)) {
pcb_grid_positions(type)
difference() {
circle(d = 2);
circle(d = 1);
}
if(fr4 && len(grid) < 3) { // oval lands at the ends
screw_x = pcb_coord(type, pcb_holes(type)[0]).x;
y0 = pcb_grid(type).y;
rows = round((pcb_width(type) - 2 * y0) / inch(0.1));
for(end = [-1, 1], y = [1 : rows - 1])
translate([end * screw_x, y0 + y * inch(0.1) - pcb_width(type) / 2])
hull()
for(x = [-1, 1])
translate([x * 1.6 / 2, 0])
circle(d = 2);
}
}
}
module pcb_spacer(screw, height, wall = 1.8, taper = 0) { //! Generate STL for PCB spacer
stl(str("pcb_spacer", round(screw_radius(screw) * 20), round(height * 10), taper ? str("_", taper) : ""));
ir = screw_clearance_radius(screw);
or = corrected_radius(ir) + wall;
if(height > taper)
linear_extrude(height = height - taper)
poly_ring(or, ir);
if(taper)
linear_extrude(height = height)
poly_ring(ir + 2 * extrusion_width, ir);
}
module pcb_base(type, height, thickness, wall = 2) { //! Generate STL for a base with PCB spacers
screw = pcb_screw(type);
ir = screw_clearance_radius(screw);
or = corrected_radius(ir) + wall;
union() {
linear_extrude(height = thickness)
difference() {
hull()
pcb_screw_positions(type)
poly_ring(or, ir);
pcb_screw_positions(type)
poly_circle(ir);
}
linear_extrude(height = height)
pcb_screw_positions(type)
poly_ring(or, ir);
}
}
module pcb_assembly(type, height, thickness) { //! Draw PCB assembly with spaces and fasteners in place
translate_z(height)
pcb(type);
screw = pcb_screw(type);
if(!is_undef(screw)) {
washer = screw_washer(screw);
nut = screw_nut(screw);
screw_length = screw_longer_than(height + thickness + pcb_thickness(type) + washer_thickness(washer) + nut_thickness(nut, true));
taper = screw_smaller_than(pcb_hole_d(type)) > 2 * screw_radius(screw); // Arduino?
pcb_screw_positions(type) {
translate_z(height + pcb_thickness(type))
screw(screw, screw_length);
color(pp1_colour)
if(taper)
pcb_spacer(screw, height, taper = 2);
else
pcb_spacer(screw, height);
translate_z(-thickness)
vflip()
nut_and_washer(nut, true);
}
}
}