From df432a446df8748fac61a9f8e968c9ca7f33ccd1 Mon Sep 17 00:00:00 2001 From: Julian Blake Kongslie Date: Wed, 11 May 2022 20:59:16 -0700 Subject: Don't mysteriously drop nets; allow arbitrary pair construction. --- lace.rb | 114 ++++++++++++++++++++++++++-------------------------------------- test.rb | 47 +++++++++++++++++++++----- 2 files changed, 85 insertions(+), 76 deletions(-) diff --git a/lace.rb b/lace.rb index f6f871d..1d7f2f9 100644 --- a/lace.rb +++ b/lace.rb @@ -1,59 +1,7 @@ -require "bigdecimal" require "set" module Lace - extend self - - def en(series, sigfigs, n) - return BigDecimal((10.0**Math.log10(n.to_f**series.to_f).round.to_f)**(1.0/series.to_f), sigfigs).round - end - - HISTORIC_E24_FIX_TABLE = { - Math.log10(2.6) => Math.log10(2.7), - Math.log10(2.9) => Math.log10(3.0), - Math.log10(3.2) => Math.log10(3.3), - Math.log10(3.5) => Math.log10(3.6), - Math.log10(3.8) => Math.log10(3.9), - Math.log10(4.2) => Math.log10(4.3), - Math.log10(4.6) => Math.log10(4.7), - } - def historic_e24_fix(n) - exp, rem = Math.log10(n).divmod(1) - HISTORIC_E24_FIX_TABLE.each do | accurate, historic | - rem = historic if (rem - accurate).abs <= 0.0001 - end - return (10 ** (exp + rem)).round - end - - def e3(n) - return historic_e24_fix(en(3, 2, n)) - end - - def e6(n) - return historic_e24_fix(en(6, 2, n)) - end - - def e12(n) - return historic_e24_fix(en(12, 2, n)) - end - - def e24(n) - return historic_e24_fix(en(24, 2, n)) - end - - def e48(n) - return en(48, 3, n) - end - - def e96(n) - return en(96, 3, n) - end - - def e192(n) - return en(192, 3, n) - end - class Netlist attr_reader :components @@ -66,18 +14,31 @@ module Lace edit(&p) if p end + def definitive_net(n) + nn = @nets[n.object_id] + return n if nn.object_id == n.object_id + return definitive_net(nn) + end + def merge_nets(a, b) - into = @nets[[a.object_id, b.object_id].min] - if a.name - raise "Nets #{into.name} and #{a.name} merged!" if into.name and into.name != a.name - into.name = a.name + da = definitive_net(a) + db = definitive_net(b) + return da if da.object_id == db.object_id + into = @nets[[da.object_id, db.object_id].min] + if da.name + raise "Nets #{into.name} and #{da.name} merged!" if into.name and into.name != da.name + into.name = da.name end - if b.name - raise "Nets #{into.name} and #{b.name} merged!" if into.name and into.name != b.name - into.name = b.name + if db.name + raise "Nets #{into.name} and #{db.name} merged!" if into.name and into.name != db.name + into.name = db.name + end + @nets.each do | id, n | + dn = definitive_net(n) + if dn.object_id != into.object_id and (dn.object_id == da.object_id or dn.object_id == db.object_id) + @nets[id] = into + end end - @nets[a.object_id] = into - @nets[b.object_id] = into return into end @@ -88,11 +49,18 @@ module Lace def comp(*args) c = Component.new(self, *args) @components[c.object_id] = c + return c end def net(*args) n = Net.new(self, *args) @nets[n.object_id] = n + return n + end + + def pair(left, right) + x = Wireable.new(self, left, right) + return x end def real_refs @@ -126,7 +94,7 @@ module Lace nodes = [] refs.each do | ref, comp | comp.pins.each do | pin, pinnet | - nodes << {:ref => ref, :pin => pin} if @nets[pinnet].object_id == net.object_id + nodes << {:ref => ref, :pin => pin} if definitive_net(pinnet).object_id == net.object_id end end nets[name] = nodes if nodes.size > 1 @@ -225,9 +193,21 @@ END @right = right || left end + def left_net + x = left + return x if x.instance_of?(Net) + return x.left_net + end + + def right_net + x = right + return x if x.instance_of?(Net) + return x.right_net + end + def -(that) - @netlist.merge_nets(@right, that.left) - return Wireable.new(@netlist, @left, that.right) + @netlist.merge_nets(right_net, that.left_net) + return Wireable.new(@netlist, left_net, that.right_net) end end @@ -263,13 +243,13 @@ END def [](pin) unless pins.member?(pin) n = @netlist.net - pins[pin] = n.object_id + pins[pin] = n end - return @netlist.nets[pins[pin]] + return pins[pin] end def desc - return "#{@value} #{@name}" if @name != @value + return "\"#{@value}\" #{@name}" if @name != @value return @name end diff --git a/test.rb b/test.rb index 86d1415..9bb54a1 100755 --- a/test.rb +++ b/test.rb @@ -2,24 +2,53 @@ require "./lace" -def r(size, suffix="ohm") - comp("R", "0805", "resistor", "#{Lace::e12(size)}#{suffix}") +def r(size) + comp("R", "0805", "resistor", size) end -def c(size, suffix="nF") - comp("C", "0805", "capacitor", "#{Lace::e12(size)}#{suffix}") +def c(size) + comp("C", "0805", "capacitor", size) end def led(color="red") - comp("D", "0805", "LED", "1.5V 10mA #{color}") + comp("D", "0805", "LED", "1.5V 10mA #{color}") - r("220ohm") +end + +def inverter() + $inverters ||= [] + if $inverters.empty? + u = comp("U", "TSOP-14", "74HCT04", "hex inverter") + $vcc - u[14] - c("100nF") - 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[4]) end nl = Lace::Netlist.new do - vcc = net("vcc") - gnd = net("gnd") + $vcc = net("vcc") + $gnd = net("gnd") + + $vcc - led - $gnd + + $vcc - btn - led("green") - $gnd - vcc - r((5 - 1.5)/0.010) - led - gnd - vcc - c(100) - gnd + x = $vcc - btn("white") + x - led("amber") - $gnd + x - inverter() - led("blue") - $gnd end nl.kicad -- cgit v1.2.3