]> git.armaanb.net Git - atreides.git/blob - atreus.rkt
Move diodes to the side of the switch.
[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 180) (size 1.651 1.651)
66          (drill 0.9906) (layers *.Cu *.SilkS *.Mask) ,net-pos)
67     (pad 99 smd rect (at -1.6637 0 180) (size 0.8382 0.8382)
68          (layers Front F.Paste F.Mask))
69     (pad 99 smd rect (at -1.6637 0 180) (size 0.8382 0.8382)
70          (layers Back B.Paste B.Mask))
71     (pad 99 smd rect (at 1.6637 0 180) (size 0.8382 0.8382)
72          (layers Front F.Paste F.Mask))
73     (pad 99 smd rect (at 1.6637 0 180) (size 0.8382 0.8382)
74          (layers Back B.Paste B.Mask))))
75
76 (define teensy-module
77   `(module CONTROLLER (layer Front) (tedit 4FDC31C8) (tstamp 543EF800)
78     (at 134 70 270)
79     (path /543EEB02)
80     (fp_text value TEENSY2.0 (at 0 0 270) (layer F.SilkS)
81              (effects (font (size 3.048 2.54) (thickness 0.4572))))
82     (fp_line (start -15.24 -8.89) (end -15.24 8.89) (layer Dwgs.User) (width 0.381))
83     (fp_line (start -15.24 8.89) (end 15.24 8.89) (layer Dwgs.User) (width 0.381))
84     (fp_line (start 15.24 8.89) (end 15.24 -8.89) (layer Dwgs.User) (width 0.381))
85     (fp_line (start 15.24 -8.89) (end -15.24 -8.89) (layer Dwgs.User) (width 0.381))
86     (fp_line (start 0 0) (end 0 0) (layer Dwgs.User) (width 0.0254))
87     (fp_line (start -15.24 -8.89) (end -15.24 8.89) (layer Cmts.User) (width 0.381))
88     (fp_line (start -15.24 8.89) (end 15.24 8.89) (layer Cmts.User) (width 0.381))
89     (fp_line (start 15.24 8.89) (end 15.24 -8.89) (layer Cmts.User) (width 0.381))
90     (fp_line (start 15.24 -8.89) (end -15.24 -8.89) (layer Cmts.User) (width 0.381))
91     (fp_line (start -15.24 -8.89) (end -15.24 8.89) (layer F.SilkS) (width 0.381))
92     (fp_line (start -15.24 8.89) (end 15.24 8.89) (layer F.SilkS) (width 0.381))
93     (fp_line (start 15.24 8.89) (end 15.24 -8.89) (layer F.SilkS) (width 0.381))
94     (fp_line (start 15.24 -8.89) (end -15.24 -8.89) (layer F.SilkS) (width 0.381))
95     (fp_line (start -15.24 6.35) (end -12.7 6.35) (layer F.SilkS) (width 0.381))
96     (fp_line (start -12.7 6.35) (end -12.7 8.89) (layer F.SilkS) (width 0.381))
97
98     ;; Should the placements be determined by a formula?
99     (pad GND thru_hole rect (at -13.97 7.62 270) (size 1.7526 1.7526)
100          (drill 1.0922) (layers *.Cu *.SilkS *.Mask))
101     (pad B0 thru_hole circle (at -11.43 7.62 270) (size 1.7526 1.7526)
102          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 5 N-col-0))
103     (pad B1 thru_hole circle (at -8.89 7.62 270) (size 1.7526 1.7526)
104          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 6 N-col-1))
105     (pad B2 thru_hole circle (at -6.35 7.62 270) (size 1.7526 1.7526)
106          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 7 N-col-2))
107     (pad B3 thru_hole circle (at -3.81 7.62 270) (size 1.7526 1.7526)
108          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 8 N-col-3))
109     (pad B7 thru_hole circle (at -1.27 7.62 270) (size 1.7526 1.7526)
110          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 12 N-col-7))
111     (pad D0 thru_hole circle (at 1.27 7.62 270) (size 1.7526 1.7526)
112          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 1 N-row-0))
113     (pad D1 thru_hole circle (at 3.81 7.62 270) (size 1.7526 1.7526)
114          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 2 N-row-1))
115     (pad D2 thru_hole circle (at 6.35 7.62 270) (size 1.7526 1.7526)
116          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 3 N-row-2))
117     (pad D3 thru_hole circle (at 8.89 7.62 270) (size 1.7526 1.7526)
118          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 4 N-row-3))
119     (pad C6 thru_hole circle (at 11.43 7.62 270) (size 1.7526 1.7526)
120          (drill 1.0922) (layers *.Cu *.SilkS *.Mask))
121     (pad C7 thru_hole circle (at 13.97 7.62 270) (size 1.7526 1.7526)
122          (drill 1.0922) (layers *.Cu *.SilkS *.Mask))
123     (pad D6 thru_hole circle (at 13.97 -7.62 270) (size 1.7526 1.7526)
124          (drill 1.0922) (layers *.Cu *.SilkS *.Mask))
125     (pad D7 thru_hole circle (at 11.43 -7.62 270) (size 1.7526 1.7526)
126          (drill 1.0922) (layers *.Cu *.SilkS *.Mask))
127     (pad B4 thru_hole circle (at 8.89 -7.62 270) (size 1.7526 1.7526)
128          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 9 N-col-4))
129     (pad B5 thru_hole circle (at 6.35 -7.62 270) (size 1.7526 1.7526)
130          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 10 N-col-5))
131     (pad B6 thru_hole circle (at 3.81 -7.62 270) (size 1.7526 1.7526)
132          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 11 N-col-6))
133     (pad F7 thru_hole circle (at 1.27 -7.62 270) (size 1.7526 1.7526)
134          (drill 1.0922) (layers *.Cu *.SilkS *.Mask))
135     (pad F6 thru_hole circle (at -1.27 -7.62 270) (size 1.7526 1.7526)
136          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 13 N-col-8))
137     (pad F5 thru_hole circle (at -3.81 -7.62 270) (size 1.7526 1.7526)
138          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 14 N-col-9))
139     (pad F4 thru_hole circle (at -6.35 -7.62 270) (size 1.7526 1.7526)
140          (drill 1.0922) (layers *.Cu *.SilkS *.Mask) (net 15 N-col-10))
141     (pad F1 thru_hole circle (at -8.89 -7.62 270) (size 1.7526 1.7526)
142          (drill 1.0922) (layers *.Cu *.SilkS *.Mask))
143     (pad F0 thru_hole circle (at -11.43 -7.62 270) (size 1.7526 1.7526)
144          (drill 1.0922) (layers *.Cu *.SilkS *.Mask))
145     (pad Vcc thru_hole circle (at -13.97 -7.62 270) (size 1.7526 1.7526)
146          (drill 1.0922) (layers *.Cu *.SilkS *.Mask))))
147
148 (define nets
149   `((net 0 "")
150     (net 1 N-row-0)
151     (net 2 N-row-1)
152     (net 3 N-row-2)
153     (net 4 N-row-3)
154     (net 5  N-col-0)
155     (net 6  N-col-1)
156     (net 7  N-col-2)
157     (net 8  N-col-3)
158     (net 9  N-col-4)
159     (net 10 N-col-5)
160     (net 11 N-col-6)
161     (net 12 N-col-7)
162     (net 13 N-col-8)
163     (net 14 N-col-9)
164     (net 15 N-col-10)
165     ,@(for/list ([s (in-range 42)])
166         (list 'net (+ 16 s) (string->symbol (format "N-diode-~s" s))))))
167
168 (define (net-class nets)
169   (append '(net_class Default "This is the default net class."
170             (clearance 0.254)
171             (trace_width 0.2032)
172             (via_dia 0.889)
173             (via_drill 0.635)
174             (uvia_dia 0.508)
175             (uvia_drill 0.127))
176           (for/list ([n nets])
177             (list 'add_net (last n)))))
178
179 (define (switch row col)
180   (let* ([left? (< col 6)]
181          [rotation (if left? -10 10)]
182          [x (* (+ 1 col) spacing)]
183          [y (+ (list-ref column-offsets col) (* spacing row))]
184          [hypotenuse (sqrt (+ (* x x) (* y y)))]
185          [Θ (atan (/ y x))]
186          [Θ′ (- Θ (degrees->radians rotation))]
187          [x′ (+ (if left? x-offset 5) (* hypotenuse (cos Θ′)))]
188          [y′ (+ (if left? y-offset (+ y-offset 42.885)) (* hypotenuse (sin Θ′)))]
189          [label (format "SW~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-col (if left? col (- col 1))]
197          [diode-net `(net ,(+ 16 diode)
198                       ,(string->symbol (format "N-diode-~s" diode)))]
199          [column-net `(net ,(+ net-col 5)
200                        ,(string->symbol (format "N-col-~s" net-col)))])
201     (switch-module x′ y′ rotation label
202                    (if left? diode-net column-net)
203                    (if left? column-net diode-net))))
204
205 (define (diode row col)
206   (let* ([left? (< col 6)]
207          [rotation (if left? -10 10)]
208          [x (* (+ 1 col) spacing)]
209          [y (+ (list-ref column-offsets col) (* spacing row))]
210          [hypotenuse (sqrt (+ (* x x) (* y y)))]
211          [Θ (atan (/ y x))]
212          [Θ′ (- Θ (degrees->radians rotation))]
213          [x′ (+ (if left? x-offset 5) (* hypotenuse (cos Θ′))
214                 (if left? 9 -9))]
215          [y′ (+ (if left? y-offset (+ y-offset 42.885))
216                 (* hypotenuse (sin Θ′)))]
217          [label (format "D~a:~a" col row)]
218          [diode (+ row (* col 4))]
219          ;; if we try to number nets linearly, kicad segfaults; woo!
220          ;; so we re-use the nets we skipped with the missing col 5/6 diodes
221          [diode (cond [(> diode 44) (- diode 20)]
222                       [(> diode 41) (- diode 21)]
223                       [true diode])]
224          [net-row (cond [(= col 5) 2]
225                         [(= col 6) 3]
226                         [true row])])
227     (diode-module x′ y′ rotation label
228                   `(net ,(+ 16 diode)
229                     ,(string->symbol (format "N-diode-~s" diode)))
230                   `(net ,(+ net-row 1)
231                     ,(string->symbol (format "N-row-~s" net-row))))))
232
233 (define switches+diodes
234   (for/list ([col (in-range cols)]
235              #:when true
236              [row (if (or (= 5 col) (= 6 col))
237                       '(0) (in-range rows))])
238     (list (switch row col) (diode row col))))
239
240 (define edge-cuts
241   (for/list [(s '([31 22] [84 22] [119 30] [125 54] [143 54] [149 30] [185 22]
242                   [237 22] [250 95] [161 112] [107 112] [18 95]))
243              (e '([84 22] [119 30] [125 54] [143 54] [149 30] [185 22]
244                   [237 22] [250 95] [161 112] [107 112] [18 95] [31 22]))]
245     `(gr_line (start ,@s) (end ,@e) (angle 90) (layer Edge.Cuts) (width 0.3))))
246
247 (define board
248   (apply append nets
249          (list (net-class nets))
250          (list teensy-module)
251          edge-cuts
252          switches+diodes))
253
254 (define (write-placement filename)
255   (when (file-exists? filename) (delete-file filename))
256   (call-with-output-file filename
257     (λ (op)
258       (display (call-with-input-file "header.rktd"
259                  (curry read-string 9999)) op)
260       ;; kicad has this terrible bug where it's whitespace-sensitive here =(
261       (display "\n" op)
262       (for ([f board])
263         (pretty-print f op 1))
264       (display (call-with-input-file "trace.rktd"
265                  (curry read-string 999999)) op)
266       (display ")" op))))
267
268 ;; (write-placement "/tmp/atreus.kicad_pcb")