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 np_thru_hole circle (at -5.08 0) (size 1.7018 1.7018)
32 (drill 1.7018)) ; board-mount hole, no copper
33 (pad 0 np_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 10.1 7.62) (layer F.SilkS) (width 0.381))
89 (fp_line (start 10.1 7.62) (end 10.1 -7.62) (layer F.SilkS) (width 0.381))
90 (fp_line (start 10.1 -7.62) (end -15.24 -7.62) (layer F.SilkS) (width 0.381))
92 (pad B5 thru_hole circle (at -13.97 6.35 270) (size 1.7526 1.7526)
93 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 7 N-col-2))
94 (pad B4 thru_hole circle (at -11.43 6.35 270) (size 1.7526 1.7526)
95 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 8 N-col-3))
96 (pad E6 thru_hole circle (at -8.89 6.35 270) (size 1.7526 1.7526)
97 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 9 N-col-4))
98 (pad D7 thru_hole circle (at -6.35 6.35 270) (size 1.7526 1.7526)
99 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 5 N-col-0))
100 (pad C6 thru_hole circle (at -3.81 6.35 270) (size 1.7526 1.7526)
101 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 6 N-col-1))
102 (pad D4 thru_hole circle (at -1.27 6.35 270) (size 1.7526 1.7526)
103 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 10 N-col-5))
105 (pad D0 thru_hole circle (at 1.27 6.35 270) (size 1.7526 1.7526)
106 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 1 N-row-0))
107 (pad D1 thru_hole circle (at 3.81 6.35 270) (size 1.7526 1.7526)
108 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 2 N-row-1))
109 (pad D3 thru_hole circle (at 6.35 6.35 270) (size 1.7526 1.7526)
110 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 3 N-row-2))
111 (pad D2 thru_hole circle (at 8.89 6.35 270) (size 1.7526 1.7526)
112 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 4 N-row-3))
114 (pad F7 thru_hole circle (at -13.97 -6.35 270) (size 1.7526 1.7526)
115 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 13 N-col-8))
116 (pad F6 thru_hole circle (at -11.43 -6.35 270) (size 1.7526 1.7526)
117 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 12 N-col-7))
118 (pad B6 thru_hole circle (at -8.89 -6.35 270) (size 1.7526 1.7526)
119 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 11 N-col-6))
120 (pad B7 thru_hole circle (at -6.35 -6.35 270) (size 1.7526 1.7526)
121 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 15 N-col-10))
122 (pad D6 thru_hole circle (at -3.81 -6.35 270) (size 1.7526 1.7526)
123 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 14 N-col-9))
124 (pad RST thru_hole circle (at -1.27 -6.35 270) (size 1.7526 1.7526)
125 (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 0 ""))))
144 ,@(for/list ([s (in-range 42)])
145 (list 'net (+ 16 s) (string->symbol (format "N-diode-~s" s))))))
147 (define (net-class nets)
148 (append '(net_class Default "This is the default net class."
156 (list 'add_net (last n)))))
158 (define (switch row col)
159 (let* ([left? (< col 6)]
160 [rotation (if left? -10 10)]
161 [x (* (+ 1 col) spacing)]
162 [y (+ (list-ref column-offsets col) (* spacing row))]
163 [hypotenuse (sqrt (+ (* x x) (* y y)))]
165 [Θ′ (- Θ (degrees->radians rotation))]
166 [x′ (+ (if left? x-offset 5) (* hypotenuse (cos Θ′)))]
167 [y′ (+ (if left? y-offset (+ y-offset 42.885)) (* hypotenuse (sin Θ′)))]
168 [label (format "SW~a:~a" col row)]
169 [diode (+ row (* col 4))]
170 ;; if we try to number nets linearly, kicad segfaults; woo!
171 ;; so we re-use the nets we skipped with the missing col 5/6 diodes
172 [diode (cond [(> diode 44) (- diode 20)]
173 [(> diode 41) (- diode 21)]
175 [net-col (if left? col (- col 1))]
176 [diode-net `(net ,(+ 16 diode)
177 ,(string->symbol (format "N-diode-~s" diode)))]
178 [column-net `(net ,(+ net-col 5)
179 ,(string->symbol (format "N-col-~s" net-col)))]
180 ;; rotate middle keys additional 90° after calculating position
181 [rotation (cond [(= 5 col) 80]
184 (switch-module x′ y′ rotation label
185 (if left? diode-net column-net)
186 (if left? column-net diode-net))))
188 (define (diode row col)
189 (let* ([left? (< col 6)]
190 [rotation (if left? -10 10)]
191 [x (* (+ 1 col) spacing)]
192 [y (+ (list-ref column-offsets col) (* spacing row))]
193 [hypotenuse (sqrt (+ (* x x) (* y y)))]
195 [Θ′ (- Θ (degrees->radians rotation))]
196 [x′ (+ (if left? x-offset 5) (* hypotenuse (cos Θ′))
198 [y′ (+ (if left? y-offset (+ y-offset 42.885))
199 (* hypotenuse (sin Θ′)))]
200 [label (format "D~a:~a" col row)]
201 [diode (+ row (* col 4))]
202 ;; if we try to number nets linearly, kicad segfaults; woo!
203 ;; so we re-use the nets we skipped with the missing col 5/6 diodes
204 [diode (cond [(> diode 44) (- diode 20)]
205 [(> diode 41) (- diode 21)]
207 [net-row (cond [(= col 5) 2]
210 (diode-module x′ y′ rotation label
212 ,(string->symbol (format "N-diode-~s" diode)))
214 ,(string->symbol (format "N-row-~s" net-row))))))
216 (define switches+diodes
217 (for/list ([col (in-range cols)]
219 [row (if (or (= 5 col) (= 6 col))
220 '(0) (in-range rows))])
221 (list (switch row col) (diode row col))))
224 (for/list [(s '([31 22] [84 22] [128 30] [128 54] [130 54] [130 60] [138 60]
225 [138 54] [140 54] [140 30] [185 22]
226 [237 22] [250 95] [161 112] [107 112] [18 95]))
227 (e '([84 22] [128 30] [128 54] [130 54] [130 60] [138 60] [138 54]
228 [140 54] [140 30] [185 22]
229 [237 22] [250 95] [161 112] [107 112] [18 95] [31 22]))]
230 `(gr_line (start ,@s) (end ,@e) (angle 90) (layer Edge.Cuts) (width 0.3))))
234 (list (net-class nets))
235 (list microcontroller-module)
239 (define (write-placement filename)
240 (when (file-exists? filename) (delete-file filename))
241 (call-with-output-file filename
243 (display (call-with-input-file "header.rktd"
244 (curry read-string 9999)) op)
245 ;; kicad has this terrible bug where it's whitespace-sensitive here =(
248 (pretty-print f op 1))
249 (display (call-with-input-file "traces.rktd"
250 (curry read-string 999999)) op)
253 (write-placement "/tmp/atreus.kicad_pcb")