diff options
Diffstat (limited to '')
| -rw-r--r-- | library.rb | 66 | ||||
| -rwxr-xr-x | test.rb | 58 |
2 files changed, 73 insertions, 51 deletions
| @@ -1,23 +1,79 @@ | |||
| 1 | require "./lace" | 1 | require "./lace" |
| 2 | 2 | ||
| 3 | $smd = "0805" | 3 | $passive = "0805" |
| 4 | 4 | ||
| 5 | def r(ohms, footprint=$smd) | 5 | def 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)}Ω") |
| 9 | end | 9 | end |
| 10 | 10 | ||
| 11 | def c(farads, footprint=$smd) | 11 | def 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") |
| 15 | end | 15 | end |
| 16 | 16 | ||
| 17 | def d(spec, footprint=$smd) | 17 | def d(spec, footprint=$passive) |
| 18 | comp("D", footprint, "diode", spec) | 18 | comp("D", footprint, "diode", spec) |
| 19 | end | 19 | end |
| 20 | 20 | ||
| 21 | def led(spec, ohms, footprint=$smd) | 21 | def 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) |
| 23 | end | 23 | end |
| 24 | |||
| 25 | $ic = "TSSOP" | ||
| 26 | |||
| 27 | IC = Struct.new(:value, :pincount, :subparts, :automatic) | ||
| 28 | |||
| 29 | $ic_definitions = {} | ||
| 30 | $ic_available_parts = {} | ||
| 31 | |||
| 32 | def define_ic(name, value, pincount, *subparts, &automatic) | ||
| 33 | $ic_definitions[name] = IC.new(value, pincount, subparts, automatic) | ||
| 34 | end | ||
| 35 | |||
| 36 | def 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 | ||
| 41 | end | ||
| 42 | |||
| 43 | def 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 | ||
| 62 | end | ||
| 63 | |||
| 64 | def 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 | ||
| 72 | end | ||
| 73 | |||
| 74 | def 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]) | ||
| 79 | end | ||
| @@ -3,57 +3,23 @@ | |||
| 3 | require "./lace" | 3 | require "./lace" |
| 4 | require "./library" | 4 | require "./library" |
| 5 | 5 | ||
| 6 | def led(color="red") | 6 | nl = Lace::Netlist.new do |
| 7 | comp("D", "0805", "LED", "1.5V 10mA #{color}") - r(220) | 7 | vcc = net("vcc") |
| 8 | end | 8 | gnd = net("gnd") |
| 9 | |||
| 10 | def 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 | ||
| 25 | end | ||
| 26 | |||
| 27 | def 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]) | ||
| 32 | end | ||
| 33 | 9 | ||
| 34 | def 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 |
| 42 | end | ||
| 43 | |||
| 44 | nl = 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 |
| 57 | end | 23 | end |
| 58 | 24 | ||
| 59 | nl.kicad | 25 | nl.kicad |
