12 (define column-offsets '(8 5 0 6 11 59 59 11 6 0 5 8))
14 (define (switch-module x y rotation label net-pos net-neg)
15 ;; TODO: set timestamps?
16 `(module MX_FLIP (layer Front) (tedit 4FD81CDD) (tstamp 543EF801)
18 (path /543DB910) ; TODO: this is not documented; no idea what it does
19 (fp_text reference ,label (at 0 3.302 ,rotation) (layer F.SilkS)
20 (effects (font (size 1.524 1.778) (thickness 0.254))))
21 (fp_line (start -6.35 -6.35) (end 6.35 -6.35)
22 (layer F.SilkS) (width 0.381))
23 (fp_line (start 6.35 -6.35) (end 6.35 6.35)
24 (layer F.SilkS) (width 0.381))
25 (fp_line (start 6.35 6.35) (end -6.35 6.35)
26 (layer F.SilkS) (width 0.381))
27 (fp_line (start -6.35 6.35) (end -6.35 -6.35)
28 (layer F.SilkS) (width 0.381))
29 (pad 0 np_thru_hole circle (at 0 0) (size 3.9878 3.9878)
30 (drill 3.9878)) ; switch hole, no copper
31 (pad 0 thru_hole circle (at -5.08 0) (size 1.7018 1.7018)
32 (drill 1.7018)) ; board-mount hole, no copper
33 (pad 0 thru_hole circle (at 5.08 0) (size 1.7018 1.7018)
34 (drill 1.7018)) ; board-mount hole, no copper
35 (pad 1 thru_hole circle (at 2.54 -5.08) (size 2.286 2.286) (drill 1.4986)
36 (layers *.Cu *.SilkS *.Mask) ,net-pos)
37 (pad 1 thru_hole circle (at 3.81 -2.54) (size 2.286 2.286) (drill 1.4986)
38 (layers *.Cu *.SilkS *.Mask) ,net-pos)
39 (pad 2 thru_hole circle (at -2.54 -5.08) (size 2.286 2.286) (drill 1.4986)
40 (layers *.Cu *.SilkS *.Mask) ,net-neg)
41 (pad 2 thru_hole circle (at -3.81 -2.54) (size 2.286 2.286) (drill 1.4986)
42 (layers *.Cu *.SilkS *.Mask) ,net-neg)))
44 (define (diode-module x y rotation label net-pos net-neg)
45 `(module DIODE (layer Front) (tedit 4E0F7A99) (tstamp 543EF854)
46 (at ,x ,y ,(+ 90 rotation))
48 (fp_text reference D2:2 (at 0 0 180) (layer F.SilkS) hide
49 (effects (font (size 1.016 1.016) (thickness 0.2032))))
50 (fp_line (start -1.524 -1.143) (end 1.524 -1.143)
51 (layer F.SilkS) (width 0.2032))
52 (fp_line (start 1.524 -1.143) (end 1.524 1.143)
53 (layer F.SilkS) (width 0.2032))
54 (fp_line (start 0 -1.143) (end 0 1.143)
55 (layer F.SilkS) (width 0.2032))
56 (fp_line (start 0 -1.143) (end -1.524 0)
57 (layer F.SilkS) (width 0.2032))
58 (fp_line (start -1.524 0) (end 0 1.143)
59 (layer F.SilkS) (width 0.2032))
60 (fp_line (start 1.524 1.143) (end -1.524 1.143)
61 (layer F.SilkS) (width 0.2032))
62 (fp_line (start -1.524 1.143) (end -1.524 -1.143)
63 (layer F.SilkS) (width 0.2032))
64 (fp_line (start -3.81 0) (end -1.6637 0) (layer Back) (width 0.6096))
65 (fp_line (start 1.6637 0) (end 3.81 0) (layer Back) (width 0.6096))
66 (fp_line (start -3.81 0) (end -1.6637 0) (layer Front) (width 0.6096))
67 (fp_line (start 1.6637 0) (end 3.81 0) (layer Front) (width 0.6096))
69 (pad 1 thru_hole circle (at -3.81 0 180) (size 1.651 1.651)
70 (drill 0.9906) (layers *.Cu *.SilkS *.Mask) ,net-neg)
71 (pad 2 thru_hole rect (at 3.81 0 ,rotation) (size 1.651 1.651)
72 (drill 0.9906) (layers *.Cu *.SilkS *.Mask) ,net-pos)
73 (pad 99 smd rect (at -1.6637 0 ,rotation) (size 0.8382 0.8382)
74 (layers Front F.Paste F.Mask))
75 (pad 99 smd rect (at -1.6637 0 ,rotation) (size 0.8382 0.8382)
76 (layers Back B.Paste B.Mask))
77 (pad 99 smd rect (at 1.6637 0 ,rotation) (size 0.8382 0.8382)
78 (layers Front F.Paste F.Mask))
79 (pad 99 smd rect (at 1.6637 0 ,rotation) (size 0.8382 0.8382)
80 (layers Back B.Paste B.Mask))))
82 (define microcontroller-module
83 `(module A_STAR (layer Front) (tedit 4FDC31C8) (tstamp 543EF800)
86 (fp_text value A-STAR (at -3 0 270) (layer F.SilkS)
87 (effects (font (size 3.048 2.54) (thickness 0.4572))))
88 (fp_line (start -15.24 -7.62) (end -15.24 7.62) (layer F.SilkS) (width 0.381))
89 (fp_line (start -15.24 7.62) (end 10.1 7.62) (layer F.SilkS) (width 0.381))
90 (fp_line (start 10.1 7.62) (end 10.1 -7.62) (layer F.SilkS) (width 0.381))
91 (fp_line (start 10.1 -7.62) (end -15.24 -7.62) (layer F.SilkS) (width 0.381))
93 (pad B5 thru_hole circle (at -13.97 6.35 270) (size 1.7526 1.7526)
94 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 7 N-col-2))
95 (pad B4 thru_hole circle (at -11.43 6.35 270) (size 1.7526 1.7526)
96 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 8 N-col-3))
97 (pad E6 thru_hole circle (at -8.89 6.35 270) (size 1.7526 1.7526)
98 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 9 N-col-4))
99 (pad D7 thru_hole circle (at -6.35 6.35 270) (size 1.7526 1.7526)
100 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 5 N-col-0))
101 (pad C6 thru_hole circle (at -3.81 6.35 270) (size 1.7526 1.7526)
102 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 6 N-col-1))
103 (pad D4 thru_hole circle (at -1.27 6.35 270) (size 1.7526 1.7526)
104 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 10 N-col-5))
106 (pad D0 thru_hole circle (at 1.27 6.35 270) (size 1.7526 1.7526)
107 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 1 N-row-0))
108 (pad D1 thru_hole circle (at 3.81 6.35 270) (size 1.7526 1.7526)
109 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 2 N-row-1))
110 (pad D3 thru_hole circle (at 6.35 6.35 270) (size 1.7526 1.7526)
111 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 3 N-row-2))
112 (pad D2 thru_hole circle (at 8.89 6.35 270) (size 1.7526 1.7526)
113 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 4 N-row-3))
115 (pad F7 thru_hole circle (at -13.97 -6.35 270) (size 1.7526 1.7526)
116 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 13 N-col-8))
117 (pad F6 thru_hole circle (at -11.43 -6.35 270) (size 1.7526 1.7526)
118 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 12 N-col-7))
119 (pad B6 thru_hole circle (at -8.89 -6.35 270) (size 1.7526 1.7526)
120 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 11 N-col-6))
121 (pad B7 thru_hole circle (at -6.35 -6.35 270) (size 1.7526 1.7526)
122 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 15 N-col-10))
123 (pad D6 thru_hole circle (at -3.81 -6.35 270) (size 1.7526 1.7526)
124 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 14 N-col-9))))
143 ,@(for/list ([s (in-range 42)])
144 (list 'net (+ 16 s) (string->symbol (format "N-diode-~s" s))))))
146 (define (net-class nets)
147 (append '(net_class Default "This is the default net class."
155 (list 'add_net (last n)))))
157 (define (switch row col)
158 (let* ([left? (< col 6)]
159 [rotation (if left? -10 10)]
160 [x (* (+ 1 col) spacing)]
161 [y (+ (list-ref column-offsets col) (* spacing row))]
162 [hypotenuse (sqrt (+ (* x x) (* y y)))]
164 [Θ′ (- Θ (degrees->radians rotation))]
165 [x′ (+ (if left? x-offset 5) (* hypotenuse (cos Θ′)))]
166 [y′ (+ (if left? y-offset (+ y-offset 42.885)) (* hypotenuse (sin Θ′)))]
167 [label (format "SW~a:~a" col row)]
168 [diode (+ row (* col 4))]
169 ;; if we try to number nets linearly, kicad segfaults; woo!
170 ;; so we re-use the nets we skipped with the missing col 5/6 diodes
171 [diode (cond [(> diode 44) (- diode 20)]
172 [(> diode 41) (- diode 21)]
174 [net-col (if left? col (- col 1))]
175 [diode-net `(net ,(+ 16 diode)
176 ,(string->symbol (format "N-diode-~s" diode)))]
177 [column-net `(net ,(+ net-col 5)
178 ,(string->symbol (format "N-col-~s" net-col)))]
179 ;; rotate middle keys additional 90° after calculating position
180 [rotation (cond [(= 5 col) 80]
183 (switch-module x′ y′ rotation label
184 (if left? diode-net column-net)
185 (if left? column-net diode-net))))
187 (define (diode row col)
188 (let* ([left? (< col 6)]
189 [rotation (if left? -10 10)]
190 [x (* (+ 1 col) spacing)]
191 [y (+ (list-ref column-offsets col) (* spacing row))]
192 [hypotenuse (sqrt (+ (* x x) (* y y)))]
194 [Θ′ (- Θ (degrees->radians rotation))]
195 [x′ (+ (if left? x-offset 5) (* hypotenuse (cos Θ′))
197 [y′ (+ (if left? y-offset (+ y-offset 42.885))
198 (* hypotenuse (sin Θ′)))]
199 [label (format "D~a:~a" col row)]
200 [diode (+ row (* col 4))]
201 ;; if we try to number nets linearly, kicad segfaults; woo!
202 ;; so we re-use the nets we skipped with the missing col 5/6 diodes
203 [diode (cond [(> diode 44) (- diode 20)]
204 [(> diode 41) (- diode 21)]
206 [net-row (cond [(= col 5) 2]
209 (diode-module x′ y′ rotation label
211 ,(string->symbol (format "N-diode-~s" diode)))
213 ,(string->symbol (format "N-row-~s" net-row))))))
215 (define switches+diodes
216 (for/list ([col (in-range cols)]
218 [row (if (or (= 5 col) (= 6 col))
219 '(0) (in-range rows))])
220 (list (switch row col) (diode row col))))
223 (for/list [(s '([31 22] [84 22] [128 30] [128 54] [140 54] [140 30] [185 22]
224 [237 22] [250 95] [161 112] [107 112] [18 95]))
225 (e '([84 22] [128 30] [128 54] [140 54] [140 30] [185 22]
226 [237 22] [250 95] [161 112] [107 112] [18 95] [31 22]))]
227 `(gr_line (start ,@s) (end ,@e) (angle 90) (layer Edge.Cuts) (width 0.3))))
231 (list (net-class nets))
232 (list microcontroller-module)
236 (define (write-placement filename)
237 (when (file-exists? filename) (delete-file filename))
238 (call-with-output-file filename
240 (display (call-with-input-file "header.rktd"
241 (curry read-string 9999)) op)
242 ;; kicad has this terrible bug where it's whitespace-sensitive here =(
245 (pretty-print f op 1))
246 (display (call-with-input-file "traces.rktd"
247 (curry read-string 999999)) op)
250 (write-placement "/tmp/atreus.kicad_pcb")