From a0eee238ad656de8fcf2b5634ea1c3c735bbf427 Mon Sep 17 00:00:00 2001 From: Julian Blake Kongslie Date: Fri, 3 Jun 2022 23:55:19 -0700 Subject: More complex library functionality for ICs with subcomponents. --- library.rb | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- test.rb | 58 ++++++++++++------------------------------------------ 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 @@ require "./lace" -$smd = "0805" +$passive = "0805" -def r(ohms, footprint=$smd) +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=$smd) +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=$smd) +def d(spec, footprint=$passive) comp("D", footprint, "diode", spec) end -def led(spec, ohms, footprint=$smd) +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 diff --git a/test.rb b/test.rb index b524539..d6cfbe2 100755 --- a/test.rb +++ b/test.rb @@ -3,57 +3,23 @@ require "./lace" require "./library" -def led(color="red") - comp("D", "0805", "LED", "1.5V 10mA #{color}") - r(220) -end - -def inverter() - $inverters ||= [] - if $inverters.empty? - u = comp("U", "TSOP-14", "74HCT04", "hex inverter") - $vcc - u[14] - c("100n") - u[7] - $gnd - $inverters += [ - pair(u[1], u[2]), - pair(u[3], u[4]), - pair(u[5], u[6]), - pair(u[9], u[8]), - pair(u[11], u[10]), - pair(u[13], u[12]), - ] - end - $inverters.shift -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 +nl = Lace::Netlist.new do + vcc = net("vcc") + gnd = net("gnd") -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]) + stock_ic("inverter", "CD74HC04") do | ic | + vcc - ic[14] - c("100nF") - ic[7] - gnd end -end - -nl = Lace::Netlist.new do - $vcc = net("vcc") - $gnd = net("gnd") - $vcc - led - $gnd + vcc - led(220) - gnd - $vcc - btn - led("green") - $gnd - $vcc - btn("orange") - inverter() - led("white") - $gnd + vcc - btn - led(220, "green") - gnd + vcc - btn("orange") - ic("inverter") - led(220, "white") - gnd - x = $vcc - btn("white") - x - net("x") - led("amber") - $gnd - x - inverter() - net("x̅") - led("blue") - $gnd - x - probe - $gnd + x = vcc - btn("white") + x - net("x") - led(220, "amber") - gnd + x - ic("inverter") - net("xbar") - led(220, "blue") - gnd + x - probe - gnd end nl.kicad -- cgit v1.2.3