]> git.armaanb.net Git - atreides.git/blob - atreus.rkt
Retrace with moved diodes; add silkscreening.
[atreides.git] / atreus.rkt
1 #lang racket
2
3 (define cols 12)
4 (define rows 4)
5
6 (define x-offset 20)
7 (define y-offset 20)
8
9 (define spacing 19)
10 (define angle 10)
11
12 (define column-offsets '(8 5 0 6 11 59 59 11 6 0 5 8))
13
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)
17     (at ,x ,y ,rotation)
18     (path /543DB910) ; TODO: this is not documented; no idea what it does
19     (fp_text reference ,label (at 0 3.302) (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 Cmts.User) (width 0.381))
23     (fp_line (start 6.35 -6.35) (end 6.35 6.35)
24              (layer Cmts.User) (width 0.381))
25     (fp_line (start 6.35 6.35) (end -6.35 6.35)
26              (layer Cmts.User) (width 0.381))
27     (fp_line (start -6.35 6.35) (end -6.35 -6.35)
28              (layer Cmts.User) (width 0.381))
29     (pad 0 np_thru_hole circle (at 0 0) (size 3.9878 3.9878)
30          (drill 3.9878) (layers *.Cu)) ; switch hole, no copper
31     (pad 0 thru_hole circle (at -5.08 0) (size 1.7018 1.7018)
32          (drill 1.7018) (layers *.Cu)) ; board-mount hole, no copper
33     (pad 0 thru_hole circle (at 5.08 0) (size 1.7018 1.7018)
34          (drill 1.7018) (layers *.Cu)) ; 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)))
43
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))
47     (path /543DB90F)
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 Cmts.User) (width 0.2032))
52     (fp_line (start 1.524 -1.143) (end 1.524 1.143)
53              (layer Cmts.User) (width 0.2032))
54     (fp_line (start 1.524 1.143) (end -1.524 1.143)
55              (layer Cmts.User) (width 0.2032))
56     (fp_line (start -1.524 1.143) (end -1.524 -1.143)
57              (layer Cmts.User) (width 0.2032))
58     (fp_line (start -3.81 0) (end -1.6637 0) (layer Back) (width 0.6096))
59     (fp_line (start 1.6637 0) (end 3.81 0) (layer Back) (width 0.6096))
60     (fp_line (start -3.81 0) (end -1.6637 0) (layer Front) (width 0.6096))
61     (fp_line (start 1.6637 0) (end 3.81 0) (layer Front) (width 0.6096))
62
63     (pad 1 thru_hole circle (at -3.81 0 180) (size 1.651 1.651)
64          (drill 0.9906) (layers *.Cu *.SilkS *.Mask) ,net-neg)
65     (pad 2 thru_hole rect (at 3.81 0 ,rotation) (size 1.651 1.651)
66          (drill 0.9906) (layers *.Cu *.SilkS *.Mask) ,net-pos)
67     (pad 99 smd rect (at -1.6637 0 ,rotation) (size 0.8382 0.8382)
68          (layers Front F.Paste F.Mask))
69     (pad 99 smd rect (at -1.6637 0 ,rotation) (size 0.8382 0.8382)
70          (layers Back B.Paste B.Mask))
71     (pad 99 smd rect (at 1.6637 0 ,rotation) (size 0.8382 0.8382)
72          (layers Front F.Paste F.Mask))
73     (pad 99 smd rect (at 1.6637 0 ,rotation) (size 0.8382 0.8382)
74          (layers Back B.Paste B.Mask))))
75
76 (define microcontroller-module
77   `(module A_STAR (layer Front) (tedit 4FDC31C8) (tstamp 543EF800)
78     (at 134 70 270)
79     (path /543EEB02)
80     (fp_text value A-STAR (at -3 0 270) (layer F.SilkS)
81              (effects (font (size 3.048 2.54) (thickness 0.4572))))
82     (fp_line (start -15.24 -7.62) (end -15.24 7.62) (layer Dwgs.User) (width 0.381))
83     (fp_line (start -15.24 7.62) (end 10.1 7.62) (layer Dwgs.User) (width 0.381))
84     (fp_line (start 10.1 7.62) (end 10.1 -7.62) (layer Dwgs.User) (width 0.381))
85     (fp_line (start 10.1 -7.62) (end -15.24 -7.62) (layer Dwgs.User) (width 0.381))
86
87     (pad B5 thru_hole circle (at -13.97 6.35 270) (size 1.7526 1.7526)
88          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 7 N-col-2))
89     (pad B4 thru_hole circle (at -11.43 6.35 270) (size 1.7526 1.7526)
90          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 8 N-col-3))
91     (pad E6 thru_hole circle (at -8.89 6.35 270) (size 1.7526 1.7526)
92          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 9 N-col-4))
93     (pad D7 thru_hole circle (at -6.35 6.35 270) (size 1.7526 1.7526)
94          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 5 N-col-0))
95     (pad C6 thru_hole circle (at -3.81 6.35 270) (size 1.7526 1.7526)
96          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 6 N-col-1))
97     (pad D4 thru_hole circle (at -1.27 6.35 270) (size 1.7526 1.7526)
98          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 10 N-col-5))
99
100     (pad D0 thru_hole circle (at 1.27 6.35 270) (size 1.7526 1.7526)
101          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 1 N-row-0))
102     (pad D1 thru_hole circle (at 3.81 6.35 270) (size 1.7526 1.7526)
103          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 2 N-row-1))
104     (pad D3 thru_hole circle (at 6.35 6.35 270) (size 1.7526 1.7526)
105          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 3 N-row-2))
106     (pad D2 thru_hole circle (at 8.89 6.35 270) (size 1.7526 1.7526)
107          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 4 N-row-3))
108
109     (pad F7 thru_hole circle (at -13.97 -6.35 270) (size 1.7526 1.7526)
110          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 13 N-col-8))
111     (pad F6 thru_hole circle (at -11.43 -6.35 270) (size 1.7526 1.7526)
112          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 12 N-col-7))
113     (pad B6 thru_hole circle (at -8.89 -6.35 270) (size 1.7526 1.7526)
114          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 11 N-col-6))
115     (pad B7 thru_hole circle (at -6.35 -6.35 270) (size 1.7526 1.7526)
116          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 15 N-col-10))
117     (pad D6 thru_hole circle (at -3.81 -6.35 270) (size 1.7526 1.7526)
118          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 14 N-col-9))))
119
120 (define nets
121   `((net 0 "")
122     (net 1 N-row-0)
123     (net 2 N-row-1)
124     (net 3 N-row-2)
125     (net 4 N-row-3)
126     (net 5  N-col-0)
127     (net 6  N-col-1)
128     (net 7  N-col-2)
129     (net 8  N-col-3)
130     (net 9  N-col-4)
131     (net 10 N-col-5)
132     (net 11 N-col-6)
133     (net 12 N-col-7)
134     (net 13 N-col-8)
135     (net 14 N-col-9)
136     (net 15 N-col-10)
137     ,@(for/list ([s (in-range 42)])
138         (list 'net (+ 16 s) (string->symbol (format "N-diode-~s" s))))))
139
140 (define (net-class nets)
141   (append '(net_class Default "This is the default net class."
142             (clearance 0.254)
143             (trace_width 0.2032)
144             (via_dia 0.889)
145             (via_drill 0.635)
146             (uvia_dia 0.508)
147             (uvia_drill 0.127))
148           (for/list ([n nets])
149             (list 'add_net (last n)))))
150
151 (define (switch row col)
152   (let* ([left? (< col 6)]
153          [rotation (if left? -10 10)]
154          [x (* (+ 1 col) spacing)]
155          [y (+ (list-ref column-offsets col) (* spacing row))]
156          [hypotenuse (sqrt (+ (* x x) (* y y)))]
157          [Θ (atan (/ y x))]
158          [Θ′ (- Θ (degrees->radians rotation))]
159          [x′ (+ (if left? x-offset 5) (* hypotenuse (cos Θ′)))]
160          [y′ (+ (if left? y-offset (+ y-offset 42.885)) (* hypotenuse (sin Θ′)))]
161          [label (format "SW~a:~a" col row)]
162          [diode (+ row (* col 4))]
163          ;; if we try to number nets linearly, kicad segfaults; woo!
164          ;; so we re-use the nets we skipped with the missing col 5/6 diodes
165          [diode (cond [(> diode 44) (- diode 20)]
166                       [(> diode 41) (- diode 21)]
167                       [true diode])]
168          [net-col (if left? col (- col 1))]
169          [diode-net `(net ,(+ 16 diode)
170                       ,(string->symbol (format "N-diode-~s" diode)))]
171          [column-net `(net ,(+ net-col 5)
172                        ,(string->symbol (format "N-col-~s" net-col)))])
173     (switch-module x′ y′ rotation label
174                    (if left? diode-net column-net)
175                    (if left? column-net diode-net))))
176
177 (define (diode row col)
178   (let* ([left? (< col 6)]
179          [rotation (if left? -10 10)]
180          [x (* (+ 1 col) spacing)]
181          [y (+ (list-ref column-offsets col) (* spacing row))]
182          [hypotenuse (sqrt (+ (* x x) (* y y)))]
183          [Θ (atan (/ y x))]
184          [Θ′ (- Θ (degrees->radians rotation))]
185          [x′ (+ (if left? x-offset 5) (* hypotenuse (cos Θ′))
186                 (if left? 9 -9))]
187          [y′ (+ (if left? y-offset (+ y-offset 42.885))
188                 (* hypotenuse (sin Θ′)))]
189          [label (format "D~a:~a" col row)]
190          [diode (+ row (* col 4))]
191          ;; if we try to number nets linearly, kicad segfaults; woo!
192          ;; so we re-use the nets we skipped with the missing col 5/6 diodes
193          [diode (cond [(> diode 44) (- diode 20)]
194                       [(> diode 41) (- diode 21)]
195                       [true diode])]
196          [net-row (cond [(= col 5) 2]
197                         [(= col 6) 3]
198                         [true row])])
199     (diode-module x′ y′ rotation label
200                   `(net ,(+ 16 diode)
201                     ,(string->symbol (format "N-diode-~s" diode)))
202                   `(net ,(+ net-row 1)
203                     ,(string->symbol (format "N-row-~s" net-row))))))
204
205 (define switches+diodes
206   (for/list ([col (in-range cols)]
207              #:when true
208              [row (if (or (= 5 col) (= 6 col))
209                       '(0) (in-range rows))])
210     (list (switch row col) (diode row col))))
211
212 (define edge-cuts
213   (for/list [(s '([31 22] [84 22] [128 30] [128 54] [140 54] [140 30] [185 22]
214                   [237 22] [250 95] [161 112] [107 112] [18 95]))
215              (e '([84 22] [128 30] [128 54] [140 54] [140 30] [185 22]
216                   [237 22] [250 95] [161 112] [107 112] [18 95] [31 22]))]
217     `(gr_line (start ,@s) (end ,@e) (angle 90) (layer Edge.Cuts) (width 0.3))))
218
219 (define board
220   (apply append nets
221          (list (net-class nets))
222          (list microcontroller-module)
223          edge-cuts
224          switches+diodes))
225
226 (define (write-placement filename)
227   (when (file-exists? filename) (delete-file filename))
228   (call-with-output-file filename
229     (λ (op)
230       (display (call-with-input-file "header.rktd"
231                  (curry read-string 9999)) op)
232       ;; kicad has this terrible bug where it's whitespace-sensitive here =(
233       (display "\n" op)
234       (for ([f board])
235         (pretty-print f op 1))
236       (display (call-with-input-file "traces.rktd"
237                  (curry read-string 99999)) op)
238       (display ")" op))))
239
240 (write-placement "/tmp/atreus.kicad_pcb")