-
Notifications
You must be signed in to change notification settings - Fork 9
/
core.clj
2587 lines (2081 loc) · 95 KB
/
core.clj
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
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; -*- coding: utf-8 -*-
;; Copyright 2020 Dimitris Papavasiliou
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program 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 Affero General Public License for more details.
;; You should have received a copy of the GNU Affero General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
(ns lagrange-keyboard.core
(:gen-class)
(:refer-clojure :exclude [use import])
(:require [clojure.pprint :refer [pprint]]
[clojure.string :refer [starts-with?]]
[scad-clj.scad :refer :all]
[scad-clj.model :refer :all]))
(def π Math/PI)
(defn degrees [& θ] (let [f (partial * π 1/180)]
(if (> (count θ) 1)
(mapv f θ)
(f (first θ)))))
;; General parameters.
(def place-keycaps? false)
(def keys-depressed? false)
(def place-keyswitches? false)
(def place-pcb? false)
(def draft? true)
(def mock-threads? true)
(def case-test-build? false)
(def case-test-locations [0])
(def case-test-volume [50 50 150, 0 0 0])
(def case-color [0.70588 0.69804 0.67059])
(def interference-test-build? false)
(def thumb-test-build? false)
(def key-test-build? false)
(def key-test-range [1 2, 4 4])
;; Main section parameters.
(def row-count 5)
(def column-count 6)
;; The radius, in mm, for each column, or for each row.
(def row-radius 235)
(def column-radius #(case %
(0 1) 65
2 69
3 66
55))
;; The spacing between rows, in mm, for each column and between
;; successive columns.
(def row-spacing #(case %
(0 1) 7/2
2 13/4
3 13/4
9/2))
(def column-spacing #(case %
0 2
1 0
2 9/2
3 5
3))
;; Column and row rotations in units of keys.
(def row-phase (constantly -8))
(def column-phase (fn [i] (+ -57/25 (cond
(= i 2) -1/4
(= i 3) -1/8
(> i 3) -1/4
:else 0))))
;; Column offsets, in mm, in the Y and Z axes.
(def column-offset #(case %
(0 1) 0
2 8
3 0
-14))
(def column-height #(case %
(0 1) 0
2 -5
3 0
7))
;; The key scale of the outer columns.
(def last-column-scale 3/2)
;; Palm key location tuning offsets, in mm.
(def palm-key-offset [0 -1 -3])
;; Key plate (i.e. switch mount) parameters.
(def keycap-length (* 0.725 25.4)) ; This is essentially 1u in mm.
(def plate-size keycap-length)
(def plate-thickness 3) ; Units of mm.
(def plate-hole-size 14)
;; Place nubs at the switch mount hole edges, meant to engage the tabs
;; on the switches.
(def place-nub? not) ; Which side to place a nub on. Try even?, odd?, or identity.
(def nub-width 1/3) ; How much the nub sticks out into the hole.
(def nub-height 1.5) ; The thickness of the edge of the nub that engages the switch.
;; Thumb section parameters.
(def thumb-offset [5 -12 11]) ; Tuning offsets for the whole thumb section, in mm.
(def thumb-radius 68)
(def thumb-slant 0.85) ; The degree of downward slant.
(def thumb-key-scale 5/4) ; The scale of the top-inner key of the thumb cluster.
;; Per-key phase along the baseline arc, in units of keys, as a pair
;; of numbers: initial phase and per-key phase increment.
(defn thumb-key-phase [column row]
(case row
(1 2) (degrees -37/2 111/2)
(degrees (if (zero? column) 12 10) 55/2)))
;; Per-key offset from the baseline arc, in mm.
(defn thumb-key-offset [column row]
(case row
1 [0 -20 -8]
2 [0 -42 -16]
(case column
0 [0 (* keycap-length 1/2 (- 3/2 thumb-key-scale)) 0]
3 [0 -4 0]
[0 0 0])))
;; Per-key vertical slope.
(defn thumb-key-slope [column row]
(case row
1 (degrees 33)
2 (degrees 6)
0))
;; Height offset for the whole keyboard.
(def global-z-offset 0)
;; Case-related parameters.
(def ^:dynamic wall-thickness 3.2) ; Should probably be a multiple of
; nozzle/line size, if printed with
; walls only.
;; Screw bosses
;; Each boss is a sequence of two key coordinates and, optionally, a
;; pair of parameters. The key coordinates are ultimately passed to
;; key-place; see comments there for their meaning. The boss is placed
;; at a point on the sidewall, between the two key locations. By
;; default it's placed halfway between the given points and inset just
;; enough to clear the outer wall, but this can be tuned via the
;; optional parameters.
(def screw-bosses [[[:key, 5 3, 1 -1] ; Right side
[:key, 5 3, 1 1]
[5/8 1]]
[[:key, 5 0, 1 1]
[:key, 5 0, 1 -1]]
[[:key, 4 0, 0 1] ; Top side
[:key, 4 0, 1/2 1]]
[[:key, 2 0, 0 1]
[:key, 2 0, 1/2 1]]
[[:key, 0 0, 0 1]
[:key, 0 0, -1/2 1]]
[[:key, 0 1, -1 -1] ; Left side
[:key, 0 2, -1 1]]
[[:key, 0 3, -1 -1]
[:thumb, 1 0, -1 1]]
[[:thumb, 2 1, -1 1] ; Front side
[:thumb, 2 1, -1 -1]]
[[:thumb, 0 1, 1 1]
[:thumb, 0 1, 1 -1]]
[[:key, 4 3, 0 -1]
[:key, 4 3, -1 -1]]])
(def ^:dynamic screw-boss-radius 11/2)
(def screw-boss-height 8)
;; Bottom cover parameters.
(def cover-thickness 4)
(def cover-countersink-diameter 9/2) ; Inner diameter of countersunk hole.
(def cover-countersink-height 2.4) ; Head height.
(def cover-countersink-angle (degrees 90)) ; Head angle.
(def cover-fastener-thread [6.5 0.75 8]) ; Cover mount thread diameter, pitch
; and length in mm.
;; Stand parameters.
;; The stand is essentially formed by a rotational extrusion of the
;; bottom cover polygon. To save on material, the inner portion is
;; removed, leaving a strip along the outside, parts of which (the
;; inner, taller tented sections) are also removed in turn, leaving
;; arm-like protrusions which are enough to stabilize the keyboard.
;; These arms can optionally be shortened to save on desktop
;; real estate, although this should be avoided, unless a bridge is
;; installed.
(def stand-tenting-angle (degrees 35)) ; The angle of extrusion.
(def stand-shape-factor 0) ; 0 is radial extrusion, 1 is projection.
(def stand-width 25/2) ; The width of the strip forming the stand cross-section.
(def stand-minimum-thickness [6 4 4/10]) ; Thickness at inner bottom, inner top and outer bottom.
(def stand-boss-indexes [0 3, 1 8]) ; The screw bosses shared by the stand.
(def stand-split-points [15 51]) ; The locations where the arms begin.
(def stand-arm-lengths [3 11, 2 6]) ; Hom much to shorten the arms.
(def stand-arm-slope (degrees 13))
;; Stand boot parameters.
;; The wall thickness is given as a pair of offsets from the stand
;; walls. The total wall thickness is thus the difference of the two
;; and the second number can be used to inset the inner boot wall, to
;; account for material removed from the stand during sanding,
;; printing tolerances, etc.
(def boot-wall-thickness [11/20 -5/20])
(def boot-wall-height 2) ; The height of the sidewalls.
(def boot-bottom-thickness 1) ; The thickness of the boot floor.
;; Bridge parameters.
(def bridge-arch-radius 6)
(def bridge-boss-indexes [4 5 6]) ; The mating boss indexes for the mount link.
(def bridge-bore-expansion 1/5) ; How much to expand the diameter of bearing and link bores.
(def bridge-bearing-separation -10) ; Shortens the separation of the bridge bearings.
;; The thread diameter of the bridge links also determines the
;; geometry of the rod ends. The first value below determines the
;; nominal diameter, on which the rod end geometry is based, while the
;; second value allows increasing the actual thread diameter above the
;; nominal, to adjust the thread fit (loose or tight), while keeping
;; the rest of the geometry constant. The third value is the thread
;; pitch.
(def bridge-link-thread [6 1/2 1])
(def bridge-link-diameter 11) ; Outer diameter of the linkage bars.
;; Link rod end specification as (type, thread length, range). The
;; type can be :fixed or :rotating. The latter type allows adjustment
;; in place, but backlash in the rotational joint makes the linkage
;; less rigid, allowing some motion between the two halves, which can
;; be annoying. The range adjusts the spacing of the pin holes and
;; with it the approximate range of rotation (past the 90° point)
;; before interference with the mating part prohibits further motion.
(def bridge-separation-fork [:fixed 55 (degrees 10)])
(def bridge-toe-fork [:fixed 17 (degrees 10)])
(def bridge-toe-eye [:fixed 7 (degrees 20)])
(def bridge-bracket-size [18 23]) ; Cable bracket aperture size.
;; Controller PCB parameters.
(def pcb-position [-108 32.5 11]) ; PCB mount location.
(def pcb-fastener-thread [5 0.5 6]) ; PCB mount thread diameter,
; pitch and length in mm.
;; Printable keycap parameters.
(def keycap-family-common [:fn-value (if draft? nil 500)
:thickness [9/5 9/5] ; Cap shell width [base top]
:mount-cross-length 4.1 ; Mount cross arm length
:mount-cross-width 1.19 ; Mount cross arm width
:mount-radius 2.75]) ; Mount stem radius
;; SA family row 3 keycap. See:
;; https://pimpmykeyboard.com/template/images/SAFamily.pdf
(def keycap-family-sa (into keycap-family-common [:height (* 0.462 25.4) :radius 33.35]))
(def keycap-family-sa-concave (into keycap-family-sa [:top :spherical-concave
:corner-rounding 0.179]))
(def keycap-family-sa-saddle (into keycap-family-sa
[:top :saddle
:corner-rounding 0.145 :extra-height 3/2
:saddle-aspect 2 :saddle-skew [-6/10 -8/10]]))
;; DSA family keycap. See:
;; https://pimpmykeyboard.com/template/images/DSAFamily.pdf
(def keycap-family-dsa (into keycap-family-common [:height (* 0.291 25.4) :extra-height 1/2
:base-height 1 :radius 36.5
:mount-recess 6/5]))
(def keycap-family-dsa-concave (into keycap-family-dsa [:top :spherical-concave
:corner-rounding 0.446]))
(def keycap-family-dsa-convex (into keycap-family-dsa [:top :spherical-convex
:corner-rounding 0.536]))
(def keycap-family-dsa-fanged (into keycap-family-dsa-convex
[:mount-offset [-0.025 0]
:fang-corner-rounding 0.61 :fang-skew 3/2
:fang-angle (degrees 54)]))
;; An OEM-like keycap, at least functionally (meaning
;; cylindrical top of typical OEM radius and height
;; somewhere between OEM R3 and R4)
(def keycap-family-oem (into keycap-family-common [:top :cylindrical
:height 15/2 :radius 26
:thickness [6/5 9/5]]))
(def keycap-family-oem-flush (into keycap-family-oem [:corner-rounding 0.343]))
(def keycap-family-oem-recessed (into keycap-family-oem [:corner-rounding 0.2
:mount-recess 5/2
:extra-height 5/2]))
;; Route out a keycap legend on the top of the keycap. Must be set to
;; a [character font-family font-size] triplet. The official Lagrange
;; logo keycap was created with ["\\U01D4DB" "Latin Modern Math" 11].
(def keycap-legend nil)
;; Define some utility functions.
(defn one-over-norm [v]
(Math/pow (reduce + (map * v v)) -1/2))
(defn line-normal [a b]
(let [[x y] (map - a b)]
[(- y) x]))
;; Derive some utility variables and set global settings.
(def columns (lazy-seq
(apply range (cond
(or thumb-test-build?
key-test-build?) [(max 0 (key-test-range 0))
(min column-count (inc (key-test-range 2)))]
:else [0 column-count]))))
(def rows (lazy-seq
(apply range (cond
(or thumb-test-build?
key-test-build?) [(max 0 (key-test-range 1))
(min row-count (inc (key-test-range 3)))]
:else [0 row-count]))))
(defn row [i] (if (neg? i) (+ row-count i) i))
(defn column [i] (if (neg? i) (+ column-count i) i))
(defn place-key-at? [[i j]]
(and ((set columns) i)
((set rows) j)
(or (#{2 3 (column -1)} i)
(< j (row -1)))))
(defn key-scale-at [where, i j]
(if (= where :thumb)
[1 (case [i j]
[0 0] thumb-key-scale
([1 0] [2 0]) 3/2
1)]
[(if (= i (column -1)) last-column-scale 1) 1]))
(defn key-options-at [where, i j]
(let [WAN [0.90980 0.90588 0.89020]
VAT [0.57647 0.76078 0.27843]
GQC [0.63137 0.61569 0.56863]
GAH [0.50588 0.50588 0.49412]
BFV [0.36471 0.80392 0.89020]
BBJ [0.00000 0.56078 0.69020]
BBQ [0.00000 0.65098 0.70588]
BO [0.00000 0.29804 0.49804]
RBD [0.82745 0.09804 0.16078]
RAR [0.79608 0.18431 0.16471]]
[(cond ; keycap-family
(= [where, i j] [:thumb 0 1]) keycap-family-dsa-fanged
(= [where, j] [:thumb, 0]) keycap-family-dsa-convex
(= where :thumb) keycap-family-dsa-concave
(= [where, i j] [:main, (column -1) (row -1)]) keycap-family-sa-saddle
:else keycap-family-oem-flush)
(if (= where :thumb) ; color
(case j
0 WAN
WAN)
(cond
(= [i j] [(column -1) (row -1)]) RAR
(= i 5) GQC
:else WAN))]))
(defn scale-to-key [where, i j, x y z]
;; Scale normalized plate coordinates (i.e. in [-1 1]) to world
;; space. We stretch recessed (w.r.t their neighbors) columns in
;; the main section, as well as certain keys in the thumb section,
;; to allow more room for keycaps.
(let [stretch (if (not= where :thumb)
(cond
(or (and (= i 2) (not= j (row -1)))
(and (= i 3) (pos? x))) [6/5 1 1]
:else [1 1 1])
(cond
(and (= [i j] [0 0]) (neg? x) (pos? y)) [1 1.1 1]
(and (= [i j] [1 0]) (pos? y)) [1 1.02 1]
(and (= [i j] [3 0]) (neg? y)) [1 17/16 1]
:else [1 1 1]))]
(map *
[x y z]
stretch
(conj (mapv (partial * 1/2) (key-scale-at where, i j)) 1/2)
[plate-size plate-size plate-thickness])))
(defn thread [D_maj P L & [L_extra]]
(if (or draft? mock-threads?)
(let [r (/ D_maj 2)]
(union
(cylinder r L :center false)
(translate [0 0 L] (cylinder [r 0] (or L_extra r) :center false))))
;; Match the *fs* setting, which is, by definition:
;;
;; *fs* = δθ * r = δθ * D_Maj / 2 = π * D_Maj / (2 * a)
;;
;; Therefore: a = π * D_Maj / (2 * *fs*)
(let [a (int (/ (* π D_maj) 2 scad-clj.model/*fs*))
δθ (/ π a)
N (int (/ (* 2 a L) P))
H (* 1/2 (Math/sqrt 3) P)
D_min (- D_maj (* 10/8 H))]
(polyhedron
(concat
[[0 0 0]]
;; Vertices
(apply concat (for [i (range N)
:let [r (+ D_maj (/ H 4))
[x_1 x_2] (map (partial * 1/2 (Math/cos (* i δθ))) [D_min r])
[y_1 y_2] (map (partial * 1/2 (Math/sin (* i δθ))) [D_min r])
z #(* (+ % (/ (* i δθ) 2 π)) P)]]
[[x_1 y_1 (z -1/2)]
[x_1 y_1 (z -3/8)]
[x_2 y_2 (z 0)]
[x_1 y_1 (z 3/8)]
[x_1 y_1 (z 1/2)]]))
;; Make the top of the thread conical, to ensure no support is
;; needed above it (for female threads).
[[0 0 (+ L (or L_extra (/ D_maj 2)))]])
;; Indices
(concat
[(conj (range 5 0 -1) 5 0)]
(for [i (range (* 2 a))]
[0 (inc (* i 5)) (inc (* (inc i) 5))])
(for [i (range (dec N)) j (range 4)]
(map (partial + 1 (* i 5)) [j (inc j) (+ 6 j) (+ 5 j)]))
(for [i (range (* 2 a))]
(map (partial - (* 5 N)) [-1 (* i 5) (* (inc i) 5)]))
[(map (partial - (* 5 N)) (conj (range 4 -1 -1) -1 0))])))))
;;;;;;;;;;;;;;;;;;;;;;
;; Controller board ;;
;;;;;;;;;;;;;;;;;;;;;;
(def pcb-size [30 65])
(def pcb-thickness 1.6)
;; The radius and position of the board screw hole, measured from the
;; corner of the board.
(def pcb-mount-hole [3/2 4 4])
;; The size and position of the connectors, measured from the
;; upper-left corner of the board to the upper-left corner of the
;; connector.
(def pcb-button-position [4.6 52.7])
(def pcb-button-diameter 2.5)
(def pcb-6p6c-size [16.64 13.59 16.51])
(def pcb-6p6c-position [2.9 17.4])
(def pcb-usb-size [11.46 12.03 15.62])
(def pcb-usb-position [7.8 2.2])
(defn pcb-place [flip? shape]
(cond->> shape
flip? (translate (map * [0 -1] pcb-size))
(not flip?) (mirror [0 1 0])
true (rotate [0 π 0])
true (translate pcb-position)))
(def pcb-button-hole-cutout
(delay
(->> (cylinder (/ pcb-button-diameter 2) 50)
(translate pcb-button-position))))
(def pcb-connector-cutout
(apply union
(for [[size position] [[pcb-usb-size pcb-usb-position]
[pcb-6p6c-size pcb-6p6c-position]]
:let [δ 0.8
[a b c] (map + [δ δ 0] size)
[x y] (map (partial + (/ δ -2)) position)]]
(union
;; Main cutout
(->> (cube a b c :center false)
(translate [x y pcb-thickness]))
;; Chamfer cutout
(->> (square a b)
(extrude-linear {:height 1/2
:scale [(+ 1 (/ 1 a))
(+ 1 (/ 1 b))]
:center false})
(union (translate [0 0 (+ 50/2 1/2)]
(cube (+ a 1) (+ b 1) 50)))
(translate [(+ x (/ a 2))
(+ y (/ b 2))
(+ (last pcb-position) cover-thickness -3/2)]))))))
(def pcb-bosses
(for [s [-1 1] t [-1 1]
:let [[w h] pcb-size
[_ δx δy] pcb-mount-hole
[D P L] pcb-fastener-thread
d 6.8
;; Make the boss a little higher than the thread (here
;; 0.8mm) to allow for a couple of solid layers at the
;; bottom of the boss and a better attachment to the
;; base.
h_b (+ L (/ D 2) 4/5)
z (partial + (last pcb-position))]]
(->> (difference
(->> (cube d d h_b)
(intersection (cylinder 4 h_b))
(translate [0 0 (z (/ h_b -2))]))
(->> (apply thread (update pcb-fastener-thread 2 + P))
(translate [0 0 (z (- (+ h_b P)))])))
(translate [(- (* 1/2 (inc s) w) (* s δx))
(- (* 1/2 (inc t) h) (* t δy))
0]))))
(def pcb
(let [corner-radius 4]
(with-fs 1/2
;; The PCB.
(color [0 0.55 0.29]
(difference
(hull
;; The basic PCB...
(for [s [-1 1] t [-1 1]
:let [[w h] pcb-size]]
(->> (cylinder corner-radius pcb-thickness)
(translate [(- (* 1/2 (inc s) w) (* s corner-radius))
(- (* 1/2 (inc t) h) (* t corner-radius))
(/ pcb-thickness 2)]))))
;; minus the mount holes...
(for [s [-1 1] t [-1 1]
:let [[w h] pcb-size
[r δx δy] pcb-mount-hole]]
(->> (cylinder r (* 3 pcb-thickness))
(translate [(- (* 1/2 (inc s) w) (* s δx))
(- (* 1/2 (inc t) h) (* t δy))
0])))
;; minus the center cutout.
(->> (cylinder 6 (* 3 pcb-thickness))
(union (translate [6 0 0] (cube 12 12 (* 3 pcb-thickness))))
(translate [29 32.5 0]))))
;; The USB/6P6C connectors.
(color (repeat 3 0.75)
(->> (apply cube (conj pcb-usb-size :center false))
(translate (conj pcb-usb-position pcb-thickness)))
(->> (apply cube (conj pcb-6p6c-size :center false))
(translate (conj pcb-6p6c-position pcb-thickness))))
;; The USB cable
(translate (conj (mapv #(+ %1 (/ %2 2))
pcb-usb-position
pcb-usb-size) (+ pcb-thickness 7))
(->> (cube 7 8 12) ; The plug
(color (repeat 3 0.85))
(translate [0 0 6]))
(->> (cube 22 13 16) ; The housing
(color (repeat 3 0.2))
(translate [9/2 0 20]))
(->> (cylinder 4 10) ; The strain relief
(color (repeat 3 0.2))
(rotate [0 (/ π 2) 0])
(translate [41/2 0 21]))
(->> (cylinder 2 30) ; The cable
(color (repeat 3 0.2))
(rotate [0 (/ π 2) 0])
(translate [71/2 0 21])))
;; The board-to-wire connectors.
(color [1 1 1]
(for [y [8 39]]
(translate [25 y -8]
(cube 4.5 18 8 :center false)))
(translate [9 60 -8]
(cube 14 4.5 8 :center false))))))
(def pcb-harness-bracket
(let [δ -5/2
r (first pcb-mount-hole)
h (second pcb-size)]
(difference
(union
(hull
(translate [δ 0 7/2] (cube 2 h 6))
(translate [δ 0 7] (cube 2 (- h 2) 1)))
(for [s [-1 1]
:let [y (* s (- (* 1/2 h) 4))]]
(translate [0 y -1]
(map hull
(partition 2 1 [(translate [δ (* 3/2 s) 1] (cube 2 5 2))
(translate [(+ δ 1) (* 3/2 s) 0] (cube 2 5 2))
(translate [1/2 (* 3/2 s) 0] (cube 2 5 2))]))
(hull (translate [1/2 (* 3/2 s) 0] (cube 2 5 2))
(translate [4 0 0] (cylinder 4 2))))))
(hull
(translate [δ 0 -1/2] (cube 3 (- h 10) 6))
(translate [δ 0 3] (cube 3 (- h 12) 1)))
(for [s [-1 1]
:let [y (* s (- (* 1/2 h) 4))]]
(translate [4 y 0] (cylinder (+ r 1/3) 10))))))
;;;;;;;;;;
;; Keys ;;
;;;;;;;;;;
(defn key-plate [where, i j]
(let [key-scale (key-scale-at where, i j)]
(difference
(union
(translate [0 0 (/ plate-thickness -2)]
(difference
(apply hull
(for [s [-1 1] t [-1 1] u [1 -1]
:let [[x y z] (scale-to-key where, i j, s t u)]]
(->> (sphere (/ plate-thickness 4 (Math/cos (/ π 8))))
(with-fn 8)
(rotate [0 0 (/ π 8)])
(translate [x y 0])
(translate (map (partial * (/ plate-thickness -4)) [s t]))
(translate [0 0 (/ z 2)]))))
(cube plate-hole-size plate-hole-size (* 2 plate-thickness))))
(for [i (range 4)
:let [nub-length 5
[a b] ((if (even? i) identity reverse)
(mapv * (repeat plate-size) key-scale))]]
(rotate [0 0 (* i π 1/2)]
(union
(when (place-nub? i)
(->> (cube (* nub-width 11/10) nub-length nub-height :center false)
(mirror [0 0 1])
(translate [(- (+ (/ plate-hole-size 2) (* nub-width 1/10)))
(/ nub-length -2)
0])))))))
;; Enlarge the hole below the nubs, to provide some space for
;; the tabs on the switch to extend into.
(let [d 5 ; The depth of the keyswitch below the top of the plate.
l 3/4 ; Extend the hole by this much on each side.
m (+ nub-height l)
c (+ plate-hole-size (* 2 l))
h (- plate-thickness m)]
(translate [0 0 (- m)]
(->> (square c c)
(extrude-linear {:height (+ l nub-width)
:scale (repeat 2 (/ (- plate-hole-size (* 2 nub-width)) c))
:center false}))
(translate [0 0 (/ (- m d) 2)]
(cube c c (- d m))))))))
(def keyswitch-socket
(translate [(+ -2.275 -2.54) ; Kaihua PG1511 keyswitch socket.
(+ -1.675 -5.08)
(+ -5 -3.05)]
(with-fn 30
(color (repeat 3 0.2)
(difference
(cube 10.9 5.89 1.80 :center false) ; Main body
(translate [10.9 0 0] ; Bottom-right fillet cutout
(difference
(cube 4 4 10)
(translate [-2 2 0]
(cylinder 2 10))))
(translate [0 5.89 0] ; Top cutout
(union
(cube 10.8 4 10)
(translate [5.4 0 0]
(cylinder 2 10)))))
(apply union ; Switch pin contacts
(for [r [[2.275 1.675 0]
[8.625 4.215 0]]]
(translate r (cylinder (/ 2.9 2) 3.05 :center false)))))
(color (repeat 3 0.75) ; Board contacts
(apply union
(for [r [[-1.8 (- 1.675 (/ 1.68 2)) 0]
[10.9 (- 4.215 (/ 1.68 2)) 0]]]
(translate r (cube 1.8 1.68 1.85 :center false))))))))
(defn orient-keyswitch [where, i j, shape]
(cond->> shape
(or (and (not= where :thumb) (not= i 2) (not= i 5) (not= j 0))
(and (= where :thumb) (not= j 0))) (rotate [0 0 π])))
(defn keyswitch [where, i j]
(orient-keyswitch
where, i j
(union
(color (repeat 3 0.2)
(hull ; Casing, below the plate.
(translate [0 0 -5/4]
(cube 13.95 13.95 5/2))
(translate [0 0 -15/4]
(cube 12.5 13.95 5/2)))
(translate [0 0 -5.4] ; Center locating pin.
(cylinder (/ 3.85 2) 2))
(->> (square 13.95 15.6) ; Casing, above the plate.
(translate [0 2 0])
(extrude-linear {:height 6.2 :scale [2/3 2/3] :center false})
(translate [0 -2 0])))
(color [0.1 0.5 1]
(translate [0 0 8] ; Stem
(cube 6 6 4)))
(color [0.8 0.5 0.2] ; Electrical terminals.
(translate [-2.54 -5.08 -6.30]
(cube 1.5 0.2 3))
(translate [3.81 -2.54 -5.85]
(cube 1.5 0.2 4))))))
;; A basic keycap shape. Cylindrical sides, spherical top, rounded
;; corners. Can be configured to yield SA and DSA style keycaps.
;; Additionally supports a couple more exotic shapes.
;;
;; Where:
;; h is the height of keycap (at top center),
;; h_0 is the height of vertical (i.e. not curved) section at bottom,
;; ρ is the shared radius of sides, top and corner rounding,
;; φ determines how deeply to round the corners.
(defn base-keycap-shape [size h h_0 ρ & {:keys [fn-value shape top corner-rounding
saddle-aspect saddle-skew fang-angle
fang-skew fang-corner-rounding]}]
(let [[minus-or-plus intersection-or-difference]
(case top
(:spherical-convex :saddle) [- intersection]
[+ difference])
d_0 (/ keycap-length 2) ; Half-length at base
d_1 (* 1/4 25.4) ; Half-length at top
;; h_1 is height at center of each crest
h_1 (case top
:saddle h
:cylindrical (minus-or-plus h (- ρ (Math/sqrt (- (* ρ ρ) (* d_1 d_1)))))
(minus-or-plus h (* ρ (- 1 (Math/cos (Math/asin (/ d_1 ρ)))))))
;; Consider two points on the center of the base and top of a
;; side of the key. O is the center of a circle of the given
;; radius passing through them. This will be used to form the
;; sides.
p [d_0 h_0]
q [d_1 h_1]
v (map (partial * 1/2) (map - q p))
a (one-over-norm v)
b (Math/pow (- (* ρ ρ) (/ 1 a a)) 1/2)
O (map + p v [(* -1 b a (second v)) (* b a (first v))])]
;; We need to set *fn* explicitly, since extrude-rotate below
;; doesn't seem to respect the *fa*/*fs* settings.
(union
(with-fn (or fn-value (and draft? 80) 160)
(cond-> (intersection-or-difference
(intersection
;; Initial block.
(or shape
(union
(translate [0 0 h] (apply cube (conj size (* 2 h))))
(when fang-angle ; Extend the side circularly.
(->> (square (size 1) (* 2 h))
(translate [0 h])
(intersection (translate O (circle ρ)))
(translate [(/ (size 1) 2) 0])
(intersection ; Errors from the previous intersection can extend
; the shape to span the Y axis, so we clip it.
(translate [50 0] (square 100 100)))
(extrude-rotate {:angle (/ fang-angle π 1/180)})
(rotate (/ π 2) [0 0 1])
(translate (map * (repeat -1/2) size))))))
;; Cylindrical sides.
(for [s [-1 1] t [0 1]
:let [fang (and fang-angle
(= [s t] [-1 0]))]]
(cond->> (cylinder ρ (+ (apply max size) 100))
true (rotate (/ π 2) [1 0 0])
true (translate [(* s (+ (first O) (/ (- (size t) keycap-length) 2)))
0
(second O)])
true (rotate (* t π 1/2) [0 0 1])
fang (translate (map * (repeat 1/2) size))
fang (rotate fang-angle [0 0 1])
fang (translate (map * (repeat -1/2) size)))))
;; Plus or minus the top.
(case top
;; Torroidal top.
:saddle (->> (circle ρ)
(translate [(* saddle-aspect ρ) 0])
(extrude-rotate {:angle 360})
(rotate [(/ π 2) (/ π 2) 0])
(translate [0 0 (* (- saddle-aspect 1) ρ)])
(translate (map * (concat saddle-skew [1]) (repeat h))))
:cylindrical (->> (cylinder ρ 100)
(scale [(/ (first size) keycap-length) 1 1])
(rotate [(/ π 2) 0 0])
(translate [0 0 (+ ρ h)]))
;; Spherical top (rotated to avoid poles).
(apply hull (for [s [-1/2 1/2] t [-1/2 1/2]
:let [δ (map -
(if (and fang-angle (neg? s))
(map * [fang-skew 1] size) size)
(repeat keycap-length))]]
(->> (sphere ρ)
(rotate [(/ π 2) 0 0])
(translate (conj (mapv * [s t] δ) (minus-or-plus h ρ))))))))
;; Rounded corner.
corner-rounding
(difference
(for [s [0 1] t [0 1]
:when (or (not fang-angle)
(not= [s t] [0 0]))
:let [fang (and fang-angle (zero? s))
ρ_0 2
ρ_1 ρ]]
(cond->> (difference (circle 10)
(circle ρ_0)
(union
(translate [-50 0] (square 100 100 :center false))
(translate [0 -50] (square 100 100 :center false))))
true (rotate [0 0 (/ π -4)])
true (translate [(- ρ_1) 0])
true (extrude-rotate {:angle 90})
true (translate [(+ ρ_0 ρ_1) 0])
true (rotate [(/ π -2) (if fang fang-corner-rounding corner-rounding) (/ π 4)])
true (translate (conj (mapv (partial * -1/2) size) h_0))
true (mirror [s 0])
true (mirror [0 t])
fang (translate (map * (repeat 1/2) size))
fang (rotate fang-angle [0 0 1])
fang (translate (map * (repeat -1/2) size))))))))))
(defn keycap-shape [size & rest]
(let [{h :height ; Keycap height (measured at top-center).
h_add :extra-height ; Additional (wrt profile) height.
h_0 :base-height ; Height of vertical part of base.
ρ :radius ; Radius of sides and top.
:or {h_add 0
h_0 0}} rest]
(apply base-keycap-shape size (+ h h_add) h_0 ρ rest)))
(defn keycap [where, i j]
;; For distance from key plate to released keycap (for Cherry MX
;; switches, and assuming stem flush with keycap bottom), see:
;;
;; https://www.cherrymx.de/en/dev.html
(let [[family-options color-triplet] (key-options-at where, i j)]
(->> (apply keycap-shape
(mapv (partial * keycap-length) (key-scale-at where, i j))
family-options)
(translate [0 0 (if keys-depressed? 3 6.6)])
(color color-triplet))))
(defn printable-keycap [scale & rest]
(let [size (mapv (partial * keycap-length) scale)
{w :thickness
a :mount-cross-length
b :mount-cross-width
r :mount-radius
δ :mount-offset
h :height
h_add :extra-height
h_0 :mount-recess
h_1 :homing-bar-height
:or {δ [0 0]
h_add 0
h_0 0
h_1 0}
} rest
δ_1 3/2
δ_2 0.4
δ_3 (mapv (partial * keycap-length) δ)]
(difference
(union
;; The shell
(difference
(union
(apply keycap-shape size rest)
(when (pos? h_1)
(->> (apply keycap-shape size rest)