Spaces:
Sleeping
Sleeping
Lamp (3D print) tab: conduit block, LED bays, music box enclosure, colour plugs, KiCad boards
f9a308d verified Download ling_pcb.py from taellinglin/HatchLing-TuneBox: direct link, hf CLI and curl.
- Browser
- Download file 12.1 kB
-
https://huggingface.co/spaces/taellinglin/HatchLing-TuneBox/resolve/main/ling_pcb.py
- Command line
-
hf download hf://spaces/taellinglin/HatchLing-TuneBox/ling_pcb.py
-
curl -L -o ling_pcb.py https://huggingface.co/spaces/taellinglin/HatchLing-TuneBox/resolve/main/ling_pcb.py
12.1 kB
| """KiCad board files for the music box: a controller carrier, and the LED strips. | |
| controller_pcb() A carrier for a Raspberry Pi Pico (it plays the MIDI or WAV from a microSD card and drives the lights), with sockets for a MAX98357A | |
| I2S amplifier module and a microSD module, a MIDI input (6N138 opto-isolator, a 3-pin header for a TRS jack), two 74AHCT125 level-shifted | |
| LED outputs (5 V data for SK6812/WS2812 strips), a rotary encoder, two buttons and a 5 V power input. It holds the parts, nets, board outline | |
| and mounting holes, and it is NOT routed: open it in KiCad, check the footprints against your parts, and route it (or use Freerouting). | |
| led_strip_pcb() The outline of an LED strip, the 3-pin connector and a marker and number at the place of every LED, for you to put your LED's footprint on. | |
| Footprints for SMD LEDs differ between makers, so the LED pads are left to you. | |
| Files are written in KiCad 7's format (KiCad 8 opens them and upgrades them). They have not been opened in KiCad here: check them before you order. | |
| """ | |
| import math | |
| HEAD = '''(kicad_pcb (version 20221018) (generator hatchling) | |
| (general (thickness 1.6)) | |
| (paper "A4") | |
| (layers | |
| (0 "F.Cu" signal) (31 "B.Cu" signal) | |
| (32 "B.Adhes" user "B.Adhesive") (33 "F.Adhes" user "F.Adhesive") (34 "B.Paste" user) (35 "F.Paste" user) | |
| (36 "B.SilkS" user "B.Silkscreen") (37 "F.SilkS" user "F.Silkscreen") (38 "B.Mask" user) (39 "F.Mask" user) | |
| (40 "Dwgs.User" user "User.Drawings") (41 "Cmts.User" user "User.Comments") (44 "Edge.Cuts" user) (45 "Margin" user) | |
| (46 "B.CrtYd" user "B.Courtyard") (47 "F.CrtYd" user "F.Courtyard") (48 "B.Fab" user) (49 "F.Fab" user)) | |
| (setup (pad_to_mask_clearance 0) (pcbplotparams (layerselection 0x00010fc_ffffffff) (plotframeref false) (outputdirectory ""))) | |
| ''' | |
| class Board: | |
| def __init__(self, ox=100.0, oy=80.0): | |
| self.ox, self.oy = ox, oy; self.nets = {'': 0}; self.items = []; self.n = 0 | |
| def net(self, name): | |
| if name is None or name == '': return 0, '' | |
| if name not in self.nets: self.nets[name] = len(self.nets) | |
| return self.nets[name], name | |
| def line(self, x0, y0, x1, y1, layer='Edge.Cuts', w=0.1): | |
| self.items.append(' (gr_line (start %.3f %.3f) (end %.3f %.3f) (layer "%s") (width %.2f))' % (self.ox + x0, self.oy + y0, self.ox + x1, self.oy + y1, layer, w)) | |
| def text(self, x, y, s, layer='F.SilkS', size=1.0, rot=0): | |
| self.items.append(' (gr_text "%s" (at %.3f %.3f %d) (layer "%s") (effects (font (size %.2f %.2f) (thickness 0.15))))' % (s, self.ox + x, self.oy + y, rot, layer, size, size)) | |
| def circle(self, x, y, r, layer='Dwgs.User', w=0.15): | |
| self.items.append(' (gr_circle (center %.3f %.3f) (end %.3f %.3f) (layer "%s") (width %.2f))' % (self.ox + x, self.oy + y, self.ox + x + r, self.oy + y, layer, w)) | |
| def rect(self, x0, y0, x1, y1, layer='Edge.Cuts'): | |
| for a, b, c, d in ((x0, y0, x1, y0), (x1, y0, x1, y1), (x1, y1, x0, y1), (x0, y1, x0, y0)): self.line(a, b, c, d, layer) | |
| def footprint(self, name, ref, value, x, y, pads, courtyard=None, rot=0): | |
| """pads: (number, kind 'thru_hole'|'np_thru_hole', shape, dx, dy, w, h, drill, net name or None)""" | |
| out = [' (footprint "%s" (layer "F.Cu") (at %.3f %.3f%s)' % (name, self.ox + x, self.oy + y, (' %d' % rot) if rot else ''), | |
| ' (fp_text reference "%s" (at 0 -2.5) (layer "F.SilkS") (effects (font (size 1 1) (thickness 0.15))))' % ref, | |
| ' (fp_text value "%s" (at 0 2.5) (layer "F.Fab") (effects (font (size 1 1) (thickness 0.15))))' % value] | |
| if courtyard: | |
| x0, y0, x1, y1 = courtyard | |
| for a, b, c, d in ((x0, y0, x1, y0), (x1, y0, x1, y1), (x1, y1, x0, y1), (x0, y1, x0, y0)): | |
| out.append(' (fp_line (start %.3f %.3f) (end %.3f %.3f) (layer "F.CrtYd") (width 0.05))' % (a, b, c, d)) | |
| out.append(' (fp_line (start %.3f %.3f) (end %.3f %.3f) (layer "F.SilkS") (width 0.12))' % (a, b, c, d)) | |
| for num, kind, shape, dx, dy, w, h, drill, net in pads: | |
| nid, nname = self.net(net) | |
| layers = '"*.Cu" "*.Mask"' | |
| netpart = ' (net %d "%s")' % (nid, nname) if nid else '' | |
| out.append(' (pad "%s" %s %s (at %.3f %.3f) (size %.3f %.3f) (drill %.3f) (layers %s)%s)' % (num, kind, shape, dx, dy, w, h, drill, layers, netpart)) | |
| out.append(' )'); self.items.append('\n'.join(out)) | |
| def write(self) -> str: | |
| nets = '\n'.join(' (net %d "%s")' % (i, n) for n, i in sorted(self.nets.items(), key=lambda kv: kv[1])) | |
| return HEAD + nets + '\n' + '\n'.join(self.items) + '\n)\n' | |
| def _header(b, ref, value, x, y, nets, vertical=True, pitch=2.54): | |
| n = len(nets); pads = [] | |
| for i, nn in enumerate(nets): | |
| dx, dy = (0.0, i * pitch) if vertical else (i * pitch, 0.0) | |
| pads.append((str(i + 1), 'thru_hole', 'rect' if i == 0 else 'circle', dx, dy, 1.7, 1.7, 1.0, nn)) | |
| cy = ((n - 1) * pitch) | |
| court = (-1.4, -1.4, 1.4, cy + 1.4) if vertical else (-1.4, -1.4, cy + 1.4, 1.4) | |
| b.footprint('HatchLing:Header_1x%02d_P2.54mm' % n, ref, value, x, y, pads, court) | |
| def _dip(b, ref, value, x, y, nets, rows=7.62): | |
| n = len(nets); half = n // 2; pads = [] | |
| for i in range(n): | |
| if i < half: dx, dy = 0.0, i * 2.54 | |
| else: dx, dy = rows, (n - 1 - i) * 2.54 | |
| pads.append((str(i + 1), 'thru_hole', 'rect' if i == 0 else 'oval', dx, dy, 1.6, 1.6, 0.8, nets[i])) | |
| b.footprint('HatchLing:DIP-%d_W7.62mm' % n, ref, value, x, y, pads, (-1.5, -1.8, rows + 1.5, (half - 1) * 2.54 + 1.8)) | |
| def _two_pad(b, ref, value, x, y, a, c, span=7.62, size=1.8, drill=0.9, name='Axial'): | |
| b.footprint('HatchLing:%s_P%.2fmm' % (name, span), ref, value, x, y, [('1', 'thru_hole', 'rect', 0, 0, size, size, drill, a), ('2', 'thru_hole', 'circle', span, 0, size, size, drill, c)], | |
| (-1.3, -1.8, span + 1.3, 1.8)) | |
| PICO_NETS = {1: None, 2: 'MIDI_RX', 3: 'GND', 4: 'LED_A_3V3', 5: 'LED_B_3V3', 6: None, 7: None, 8: 'GND', 9: 'I2S_DIN', 10: 'I2S_BCLK', 11: 'I2S_LRC', 12: None, 13: 'GND', | |
| 14: 'ENC_A', 15: 'ENC_B', 16: 'ENC_SW', 17: 'BTN1', 18: 'GND', 19: 'BTN2', 20: None, | |
| 21: 'SD_MISO', 22: 'SD_CS', 23: 'GND', 24: 'SD_SCK', 25: 'SD_MOSI', 26: None, 27: None, 28: 'GND', 29: None, 30: None, 31: None, 32: None, 33: 'GND', 34: None, 35: None, | |
| 36: '+3V3', 37: None, 38: 'GND', 39: 'VSYS', 40: None} | |
| PICO_GPIO = {1: 'GP0 (UART0 TX, free)', 2: 'GP1 (UART0 RX: MIDI in)', 4: 'GP2 (LED strip A data)', 5: 'GP3 (LED strip B data)', 9: 'GP6 (I2S data)', 10: 'GP7 (I2S bit clock)', | |
| 11: 'GP8 (I2S word clock: must follow the bit clock)', 14: 'GP10 (encoder A)', 15: 'GP11 (encoder B)', 16: 'GP12 (encoder button)', 17: 'GP13 (button 1)', 19: 'GP14 (button 2)', | |
| 21: 'GP16 (SPI0 MISO)', 22: 'GP17 (SPI0 CS)', 24: 'GP18 (SPI0 SCK)', 25: 'GP19 (SPI0 MOSI)'} | |
| def controller_pcb(width=110.0, height=75.0, holes=((4.0, 4.0), (106.0, 4.0), (4.0, 71.0), (106.0, 71.0))) -> str: | |
| """The controller carrier. -> text of a .kicad_pcb""" | |
| b = Board(); b.rect(0, 0, width, height) | |
| for i, (hx, hy) in enumerate(holes): | |
| b.footprint('HatchLing:MountingHole_3.2mm', 'H%d' % (i + 1), 'M3', hx, hy, [('', 'np_thru_hole', 'circle', 0, 0, 3.2, 3.2, 3.2, None)]) | |
| pico = [] | |
| for n in range(1, 41): | |
| if n <= 20: dx, dy = -8.89, -24.13 + (n - 1) * 2.54 | |
| else: dx, dy = 8.89, 24.13 - (n - 21) * 2.54 | |
| pico.append((str(n), 'thru_hole', 'rect' if n == 1 else 'oval', dx, dy, 1.7, 1.7, 1.0, PICO_NETS[n])) | |
| b.footprint('HatchLing:RaspberryPi_Pico_Socket_2x20_P2.54mm', 'U1', 'Raspberry Pi Pico (in two 1x20 sockets, 17.78 mm apart, USB toward the board edge)', 22.0, 38.0, pico, (-11.0, -26.0, 11.0, 26.0)) | |
| _two_pad(b, 'J1', '5 V in (screw terminal, 5.08 mm)', 6.0, 6.0, '+5V', 'GND', span=5.08, size=2.6, drill=1.3, name='Terminal') | |
| _two_pad(b, 'D1', '1N5819 (5 V to VSYS)', 22.0, 7.0, '+5V', 'VSYS', 10.16, name='DO-41') | |
| _two_pad(b, 'C1', '1000 uF 10 V (radial)', 42.0, 5.0, '+5V', 'GND', span=5.0, size=2.2, drill=1.1, name='CP_Radial') | |
| _dip(b, 'U2', '74AHCT125 (level shift: 3.3 V to 5 V)', 45.0, 12.0, ['GND', 'LED_A_3V3', 'LED_A_5V', 'GND', 'LED_B_3V3', 'LED_B_5V', 'GND', | |
| None, 'GND', '+5V', None, 'GND', '+5V', '+5V']) | |
| _dip(b, 'U3', '6N138 (MIDI opto-isolator)', 45.0, 40.0, [None, 'MIDI_OPTO_A', 'MIDI_5', None, 'GND', 'MIDI_RX', None, '+3V3']) | |
| _two_pad(b, 'C2', '100 nF (U2 supply)', 62.0, 12.0, '+5V', 'GND', 5.08, name='Axial') | |
| _two_pad(b, 'C3', '100 nF (U3 supply)', 62.0, 38.0, '+3V3', 'GND', 5.08, name='Axial') | |
| _two_pad(b, 'R1', '220 R (MIDI loop)', 62.0, 45.0, 'MIDI_4', 'MIDI_OPTO_A', 7.62) | |
| _two_pad(b, 'R2', '470 R (6N138 pull-up)', 62.0, 50.0, 'MIDI_RX', '+3V3', 7.62) | |
| _two_pad(b, 'D2', '1N4148 (protects the opto)', 62.0, 55.0, 'MIDI_5', 'MIDI_OPTO_A', 7.62, name='DO-35') | |
| _header(b, 'J2', 'MIDI in: pin 4, pin 5, shield (3.5 mm TRS jack or DIN)', 96.0, 38.0, ['MIDI_4', 'MIDI_5', 'GND']) | |
| _header(b, 'J3', 'MAX98357A module: VIN GND SD GAIN DIN BCLK LRC', 80.0, 12.0, ['+5V', 'GND', '+3V3', None, 'I2S_DIN', 'I2S_BCLK', 'I2S_LRC']) | |
| _header(b, 'J4', 'microSD module (SPI): GND VCC MISO MOSI SCK CS (check your module)', 88.0, 12.0, ['GND', '+3V3', 'SD_MISO', 'SD_MOSI', 'SD_SCK', 'SD_CS']) | |
| _header(b, 'J5', 'LED strip A (left bay): 5V DATA GND', 96.0, 12.0, ['+5V', 'LED_A_5V', 'GND']) | |
| _header(b, 'J6', 'LED strip B (right bay): 5V DATA GND', 102.0, 12.0, ['+5V', 'LED_B_5V', 'GND']) | |
| _header(b, 'J7', 'Encoder EC11: A C B SW SW', 80.0, 64.0, ['ENC_A', 'GND', 'ENC_B', 'ENC_SW', 'GND'], vertical=False) | |
| _header(b, 'J8', 'Buttons: B1 GND B2 GND', 96.0, 64.0, ['BTN1', 'GND', 'BTN2', 'GND'], vertical=False) | |
| b.text(22, 70, 'HatchLing-TuneBox controller', size=1.0) | |
| return b.write() | |
| def pin_table() -> str: | |
| """The Pico's pins, as a table for the notes.""" | |
| rows = ['Pico pin signal'] | |
| for n in sorted(PICO_GPIO): rows.append('%2d %s' % (n, PICO_GPIO[n])) | |
| return '\n'.join(rows) | |
| def led_strip_pcb(lanes, width=16.0, led_x=2.4, margin=7.0, head=11.0, name='strip') -> tuple: | |
| """A strip's outline, the connector and a marker for each LED. lanes: the y of each LED along the strip (mm, any origin). -> (kicad text, length)""" | |
| lo, hi = min(lanes), max(lanes); L = (hi - lo) + margin + head; b = Board(); b.rect(0, 0, width, L) | |
| ys = [margin + (v - lo) for v in lanes] | |
| for i, y in enumerate(ys): | |
| b.line(led_x - 1.2, y, led_x + 1.2, y, 'Dwgs.User', 0.15); b.line(led_x, y - 1.2, led_x, y + 1.2, 'Dwgs.User', 0.15); b.circle(led_x, y, 1.75) | |
| b.text(width - 3.0, y, str(i), 'F.SilkS', 0.9) | |
| cy = margin + (hi - lo) + 6.0 | |
| _header(b, 'J1', '5V DATA GND (to the controller)', width / 2 - 2.54, cy, ['+5V', 'DATA_IN', 'GND'], vertical=False) | |
| b.text(width / 2, cy + 3.2, 'IN', 'F.SilkS', 0.9) | |
| b.text(width / 2, 2.0, 'DATA OUT: leave open', 'F.SilkS', 0.7) | |
| b.text(led_x, L - 1.2, 'LED centres: x = %.1f mm from this edge' % led_x, 'Dwgs.User', 0.6, 0) | |
| return b.write(), L, ys | |
| def check(text: str) -> list: | |
| """Cheap sanity checks on a board file: balanced brackets, every net used declared once. -> problems""" | |
| problems = []; depth = 0; instr = False | |
| for ch in text: | |
| if ch == '"': instr = not instr | |
| elif not instr: | |
| if ch == '(': depth += 1 | |
| elif ch == ')': depth -= 1 | |
| if depth < 0: problems.append('closing bracket without an opening one'); break | |
| if depth != 0: problems.append('brackets are not balanced (%d)' % depth) | |
| import re | |
| declared = re.findall(r'^ \(net (\d+) "', text, flags=re.M); used = set(re.findall(r'\(net (\d+) "', text.split('(footprint', 1)[-1])) | |
| if len(declared) != len(set(declared)): problems.append('a net is declared twice') | |
| for u in used: | |
| if u not in declared: problems.append('net %s is used but not declared' % u) | |
| return problems | |