diff options
Diffstat (limited to 'library.rb')
| -rw-r--r-- | library.rb | 66 |
1 files changed, 61 insertions, 5 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 | ||
