]> git.armaanb.net Git - atreides.git/blob - atreus.rkt
Link to images of each revision in changelog.
[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 ,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)))
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 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))
68
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))))
81
82 (define microcontroller-module
83   `(module A_STAR (layer Front) (tedit 4FDC31C8) (tstamp 543EF800)
84     (at 134 70 270)
85     (path /543EEB02)
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))
91
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))
104
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))
113
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 ""))
126     (pad GND thru_hole circle (at 6.35 -6.35 270) (size 1.7526 1.7526)
127          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 0 ""))))
128
129 (define nets
130   `((net 0 "")
131     (net 1 N-row-0)
132     (net 2 N-row-1)
133     (net 3 N-row-2)
134     (net 4 N-row-3)
135     (net 5  N-col-0)
136     (net 6  N-col-1)
137     (net 7  N-col-2)
138     (net 8  N-col-3)
139     (net 9  N-col-4)
140     (net 10 N-col-5)
141     (net 11 N-col-6)
142     (net 12 N-col-7)
143     (net 13 N-col-8)
144     (net 14 N-col-9)
145     (net 15 N-col-10)
146     ,@(for/list ([s (in-range 42)])
147         (list 'net (+ 16 s) (string->symbol (format "N-diode-~s" s))))))
148
149 (define (net-class nets)
150   (append '(net_class Default "This is the default net class."
151             (clearance 0.254)
152             (trace_width 0.2032)
153             (via_dia 0.889)
154             (via_drill 0.635)
155             (uvia_dia 0.508)
156             (uvia_drill 0.127))
157           (for/list ([n nets])
158             (list 'add_net (last n)))))
159
160 (define (switch row col)
161   (let* ([left? (< col 6)]
162          [rotation (if left? -10 10)]
163          [x (* (+ 1 col) spacing)]
164          [y (+ (list-ref column-offsets col) (* spacing row))]
165          [hypotenuse (sqrt (+ (* x x) (* y y)))]
166          [Θ (atan (/ y x))]
167          [Θ′ (- Θ (degrees->radians rotation))]
168          [x′ (+ (if left? x-offset 5) (* hypotenuse (cos Θ′)))]
169          [y′ (+ (if left? y-offset (+ y-offset 42.885)) (* hypotenuse (sin Θ′)))]
170          [label (format "SW~a:~a" col row)]
171          [diode (+ row (* col 4))]
172          ;; if we try to number nets linearly, kicad segfaults; woo!
173          ;; so we re-use the nets we skipped with the missing col 5/6 diodes
174          [diode (cond [(> diode 44) (- diode 20)]
175                       [(> diode 41) (- diode 21)]
176                       [true diode])]
177          [net-col (if left? col (- col 1))]
178          [diode-net `(net ,(+ 16 diode)
179                       ,(string->symbol (format "N-diode-~s" diode)))]
180          [column-net `(net ,(+ net-col 5)
181                        ,(string->symbol (format "N-col-~s" net-col)))]
182          ;; rotate middle keys additional 90° after calculating position
183          [rotation (cond [(= 5 col) 80]
184                          [(= 6 col) 280]
185                          [true rotation])])
186     (switch-module x′ y′ rotation label
187                    (if left? diode-net column-net)
188                    (if left? column-net diode-net))))
189
190 (define (diode row col)
191   (let* ([left? (< col 6)]
192          [rotation (if left? -10 10)]
193          [x (* (+ 1 col) spacing)]
194          [y (+ (list-ref column-offsets col) (* spacing row))]
195          [hypotenuse (sqrt (+ (* x x) (* y y)))]
196          [Θ (atan (/ y x))]
197          [Θ′ (- Θ (degrees->radians rotation))]
198          [x′ (+ (if left? x-offset 5) (* hypotenuse (cos Θ′))
199                 (if left? 9 -9))]
200          [y′ (+ (if left? y-offset (+ y-offset 42.885))
201                 (* hypotenuse (sin Θ′)))]
202          [label (format "D~a:~a" col row)]
203          [diode (+ row (* col 4))]
204          ;; if we try to number nets linearly, kicad segfaults; woo!
205          ;; so we re-use the nets we skipped with the missing col 5/6 diodes
206          [diode (cond [(> diode 44) (- diode 20)]
207                       [(> diode 41) (- diode 21)]
208                       [true diode])]
209          [net-row (cond [(= col 5) 2]
210                         [(= col 6) 3]
211                         [true row])])
212     (diode-module x′ y′ rotation label
213                   `(net ,(+ 16 diode)
214                     ,(string->symbol (format "N-diode-~s" diode)))
215                   `(net ,(+ net-row 1)
216                     ,(string->symbol (format "N-row-~s" net-row))))))
217
218 (define switches+diodes
219   (for/list ([col (in-range cols)]
220              #:when true
221              [row (if (or (= 5 col) (= 6 col))
222                       '(0) (in-range rows))])
223     (list (switch row col) (diode row col))))
224
225 (define edge-cuts
226   (for/list [(s '([31 22] [84 22] [127 30] [127 54] [130 54] [130 60] [138 60]
227                   [138 54] [141 54] [141 30] [185 22]
228                   [237 22] [250 95] [161 112] [107 112] [18 95]))
229              (e '([84 22] [127 30] [127 54] [130 54] [130 60] [138 60] [138 54]
230                   [141 54] [141 30] [185 22]
231                   [237 22] [250 95] [161 112] [107 112] [18 95] [31 22]))]
232     `(gr_line (start ,@s) (end ,@e) (angle 90) (layer Edge.Cuts) (width 0.3))))
233
234 (define board
235   (apply append nets
236          (list (net-class nets))
237          (list microcontroller-module)
238          edge-cuts
239          switches+diodes))
240
241 (define (write-placement filename)
242   (when (file-exists? filename) (delete-file filename))
243   (call-with-output-file filename
244     (λ (op)
245       (display (call-with-input-file "header.rktd"
246                  (curry read-string 9999)) op)
247       ;; kicad has this terrible bug where it's whitespace-sensitive here =(
248       (display "\n" op)
249       (for ([f board])
250         (pretty-print f op 1))
251       (display (call-with-input-file "traces.rktd"
252                  (curry read-string 999999)) op)
253       (display ")" op))))
254
255 (write-placement "/tmp/atreus.kicad_pcb")