summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--library.rb66
-rwxr-xr-xtest.rb58
2 files changed, 73 insertions, 51 deletions
diff --git a/library.rb b/library.rb
index 1fb682b..0ba7cbc 100644
--- a/library.rb
+++ b/library.rb
@@ -1,23 +1,79 @@
1require "./lace" 1require "./lace"
2 2
3$smd = "0805" 3$passive = "0805"
4 4
5def r(ohms, footprint=$smd) 5def r(ohms, footprint=$passive)
6 ohms = ohms.to_s 6 ohms = ohms.to_s
7 ohms.sub!(/(ohms?|Ω)$/i, "") 7 ohms.sub!(/(ohms?|Ω)$/i, "")
8 comp("R", footprint, "resistor", "#{Lace.number(ohms)}Ω") 8 comp("R", footprint, "resistor", "#{Lace.number(ohms)}Ω")
9end 9end
10 10
11def c(farads, footprint=$smd) 11def c(farads, footprint=$passive)
12 farads = farads.to_s 12 farads = farads.to_s
13 farads.sub!(/f(arads?)?$/i, "") 13 farads.sub!(/f(arads?)?$/i, "")
14 comp("C", footprint, "capacitor", "#{Lace.number(farads)}F") 14 comp("C", footprint, "capacitor", "#{Lace.number(farads)}F")
15end 15end
16 16
17def d(spec, footprint=$smd) 17def d(spec, footprint=$passive)
18 comp("D", footprint, "diode", spec) 18 comp("D", footprint, "diode", spec)
19end 19end
20 20
21def led(spec, ohms, footprint=$smd) 21def led(ohms="1K", spec="red", footprint=$passive)
22 comp("D", footprint, "LED", spec) - self.r(ohms, footprint) 22 comp("D", footprint, "LED", spec) - self.r(ohms, footprint)
23end 23end
24
25$ic = "TSSOP"
26
27IC = Struct.new(:value, :pincount, :subparts, :automatic)
28
29$ic_definitions = {}
30$ic_available_parts = {}
31
32def define_ic(name, value, pincount, *subparts, &automatic)
33 $ic_definitions[name] = IC.new(value, pincount, subparts, automatic)
34end
35
36def stock_ic(name, value, &automatic)
37 case value
38 when /74.*00/; define_ic(name, value, 14, [1, 2, 3], [4, 5, 6], [13, 12, 11], [10, 9, 8], &automatic)
39 when /74.*04/; define_ic(name, value, 14, [1, 2], [3, 4], [5, 6], [13, 12], [11, 10], [9, 8], &automatic)
40 end
41end
42
43def ic(name)
44 parts = $ic_available_parts.fetch(name, [])
45 if parts.size < 1
46 raise "IC #{name} not defined!" unless $ic_definitions.member?(name)
47 info = $ic_definitions[name]
48 ic = comp("U", "#{$ic}-#{info.pincount}", name, info.value)
49 info.automatic.call(ic)
50 subs = info.subparts.map { | sub | sub.map { | pin | ic[pin] } }
51 subs.each do | sub |
52 if sub.size == 2
53 parts << pair(*sub)
54 else
55 parts << sub
56 end
57 end
58 end
59 part = parts.shift
60 $ic_available_parts[name] = parts
61 return part
62end
63
64def probe(attenuation=20, impedance=50)
65 jack = comp("J", "SMB", "SMB jack", "#{attenuation}x #{impedance}Ω probe")
66 jack[2] - jack[3] - jack[4] - jack[5]
67 if attenuation == 1
68 pair(jack[1], jack[2])
69 else
70 pair(r((attenuation-1)*impedance) - jack[1], jack[2])
71 end
72end
73
74def btn(color="black")
75 sw = comp("SW", "BTN", "SPST button", color)
76 sw[1] - sw[4]
77 sw[2] - sw[3]
78 pair(sw[1], sw[3])
79end
diff --git a/test.rb b/test.rb
index b524539..d6cfbe2 100755
--- a/test.rb
+++ b/test.rb
@@ -3,57 +3,23 @@
3require "./lace" 3require "./lace"
4require "./library" 4require "./library"
5 5
6def led(color="red") 6nl = Lace::Netlist.new do
7 comp("D", "0805", "LED", "1.5V 10mA #{color}") - r(220) 7 vcc = net("vcc")
8end 8 gnd = net("gnd")
9
10def inverter()
11 $inverters ||= []
12 if $inverters.empty?
13 u = comp("U", "TSOP-14", "74HCT04", "hex inverter")
14 $vcc - u[14] - c("100n") - u[7] - $gnd
15 $inverters += [
16 pair(u[1], u[2]),
17 pair(u[3], u[4]),
18 pair(u[5], u[6]),
19 pair(u[9], u[8]),
20 pair(u[11], u[10]),
21 pair(u[13], u[12]),
22 ]
23 end
24 $inverters.shift
25end
26
27def btn(color="black")
28 sw = comp("SW", "BTN", "SPST button", color)
29 sw[1] - sw[4]
30 sw[2] - sw[3]
31 pair(sw[1], sw[3])
32end
33 9
34def probe(attenuation=20, impedance=50) 10 stock_ic("inverter", "CD74HC04") do | ic |
35 jack = comp("J", "SMB", "SMB jack", "#{attenuation}x #{impedance}Ω probe") 11 vcc - ic[14] - c("100nF") - ic[7] - gnd
36 jack[2] - jack[3] - jack[4] - jack[5]
37 if attenuation == 1
38 pair(jack[1], jack[2])
39 else
40 pair(r((attenuation-1)*impedance) - jack[1], jack[2])
41 end 12 end
42end
43
44nl = Lace::Netlist.new do
45 $vcc = net("vcc")
46 $gnd = net("gnd")
47 13
48 $vcc - led - $gnd 14 vcc - led(220) - gnd
49 15
50 $vcc - btn - led("green") - $gnd 16 vcc - btn - led(220, "green") - gnd
51 $vcc - btn("orange") - inverter() - led("white") - $gnd 17 vcc - btn("orange") - ic("inverter") - led(220, "white") - gnd
52 18
53 x = $vcc - btn("white") 19 x = vcc - btn("white")
54 x - net("x") - led("amber") - $gnd 20 x - net("x") - led(220, "amber") - gnd
55 x - inverter() - net("x̅") - led("blue") - $gnd 21 x - ic("inverter") - net("xbar") - led(220, "blue") - gnd
56 x - probe - $gnd 22 x - probe - gnd
57end 23end
58 24
59nl.kicad 25nl.kicad