summaryrefslogtreecommitdiff
path: root/library.rb
blob: 0ba7cbc212f21cdb0d5065fb5e15cc1f160117f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
require "./lace"

$passive = "0805"

def r(ohms, footprint=$passive)
  ohms = ohms.to_s
  ohms.sub!(/(ohms?|Ω)$/i, "")
  comp("R", footprint, "resistor", "#{Lace.number(ohms)}Ω")
end

def c(farads, footprint=$passive)
  farads = farads.to_s
  farads.sub!(/f(arads?)?$/i, "")
  comp("C", footprint, "capacitor", "#{Lace.number(farads)}F")
end

def d(spec, footprint=$passive)
  comp("D", footprint, "diode", spec)
end

def led(ohms="1K", spec="red", footprint=$passive)
  comp("D", footprint, "LED", spec) - self.r(ohms, footprint)
end

$ic = "TSSOP"

IC = Struct.new(:value, :pincount, :subparts, :automatic)

$ic_definitions = {}
$ic_available_parts = {}

def define_ic(name, value, pincount, *subparts, &automatic)
  $ic_definitions[name] = IC.new(value, pincount, subparts, automatic)
end

def stock_ic(name, value, &automatic)
  case value
    when /74.*00/; define_ic(name, value, 14, [1, 2, 3], [4, 5, 6], [13, 12, 11], [10, 9, 8], &automatic)
    when /74.*04/; define_ic(name, value, 14, [1, 2], [3, 4], [5, 6], [13, 12], [11, 10], [9, 8], &automatic)
  end
end

def ic(name)
  parts = $ic_available_parts.fetch(name, [])
  if parts.size < 1
    raise "IC #{name} not defined!" unless $ic_definitions.member?(name)
    info = $ic_definitions[name]
    ic = comp("U", "#{$ic}-#{info.pincount}", name, info.value)
    info.automatic.call(ic)
    subs = info.subparts.map { | sub | sub.map { | pin | ic[pin] } }
    subs.each do | sub |
      if sub.size == 2
        parts << pair(*sub)
      else
        parts << sub
      end
    end
  end
  part = parts.shift
  $ic_available_parts[name] = parts
  return part
end

def probe(attenuation=20, impedance=50)
  jack = comp("J", "SMB", "SMB jack", "#{attenuation}x #{impedance}Ω probe")
  jack[2] - jack[3] - jack[4] - jack[5]
  if attenuation == 1
    pair(jack[1], jack[2])
  else
    pair(r((attenuation-1)*impedance) - jack[1], jack[2])
  end
end

def btn(color="black")
  sw = comp("SW", "BTN", "SPST button", color)
  sw[1] - sw[4]
  sw[2] - sw[3]
  pair(sw[1], sw[3])
end