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 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 5 deletions(-) (limited to 'library.rb') 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 -- cgit v1.2.3