eletro 0.0.2 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +34 -0
- data/VERSION +1 -1
- data/eletro.gemspec +17 -2
- data/lib/eletro.rb +10 -1
- data/lib/eletro/capacitor.rb +59 -0
- data/lib/eletro/diode.rb +12 -0
- data/lib/eletro/helpers/color_code.rb +91 -0
- data/lib/eletro/ic.rb +8 -0
- data/lib/eletro/inductor.rb +7 -0
- data/lib/eletro/logic.rb +9 -0
- data/lib/eletro/part.rb +71 -0
- data/lib/eletro/resistor.rb +14 -87
- data/lib/eletro/source.rb +84 -0
- data/lib/eletro/transistor.rb +8 -0
- data/spec/eletro/capacitor_spec.rb +29 -0
- data/spec/eletro/ohm_spec.rb +3 -3
- data/spec/eletro/part_spec.rb +22 -0
- data/spec/eletro/resistor_spec.rb +24 -24
- data/spec/eletro/source_spec.rb +113 -0
- metadata +18 -3
data/README.rdoc
CHANGED
@@ -16,8 +16,42 @@ Eletric Stuff, Ohm Law, Resistor Colors, Karnaugh Maps...
|
|
16
16
|
-> 1kΩ --BKR--
|
17
17
|
|
18
18
|
|
19
|
+
eletro 4k7
|
20
|
+
|
21
|
+
-> 4.7kΩ --YVR--
|
22
|
+
|
23
|
+
|
24
|
+
|
19
25
|
With *SH colors!
|
20
26
|
|
27
|
+
|
28
|
+
== Power Supply (Source)
|
29
|
+
|
30
|
+
Calculates load network on a PS.
|
31
|
+
Simbling loads mean serial, simbling arrays parallel.
|
32
|
+
|
33
|
+
s = Source.new("12dc")
|
34
|
+
s << Resistor.new("100")
|
35
|
+
s << [Resistor.new("100"), Resistor.new("100")]
|
36
|
+
s << [Resistor.new("100"), Resistor.new("200")]
|
37
|
+
|
38
|
+
s.sum_net
|
39
|
+
=> Resistor 54.5454Ω
|
40
|
+
|
41
|
+
s.i
|
42
|
+
=> 0.220mA
|
43
|
+
|
44
|
+
s << Resistor.new("100")
|
45
|
+
s << [Resistor.new("100"), Resistor.new("100")]
|
46
|
+
s << [Resistor.new("100"), [Resistor.new("100"), Resistor.new("100")]]
|
47
|
+
|
48
|
+
s.sum_net
|
49
|
+
=> Resistor 33.3333Ω
|
50
|
+
|
51
|
+
s.i
|
52
|
+
=> 0.360mA
|
53
|
+
|
54
|
+
|
21
55
|
== Ohm`s Law
|
22
56
|
|
23
57
|
TBF
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/eletro.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{eletro}
|
8
|
-
s.version = "0.0
|
8
|
+
s.version = "0.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Marcos Piccinini"]
|
12
|
-
s.date = %q{2010-11-
|
12
|
+
s.date = %q{2010-11-11}
|
13
13
|
s.default_executable = %q{eletro}
|
14
14
|
s.description = %q{Eletric Stuff, Ohm Law, Resistor Colors, Karnaugh Maps and other gems on Ruby}
|
15
15
|
s.email = %q{x@nofxx.com}
|
@@ -26,11 +26,23 @@ Gem::Specification.new do |s|
|
|
26
26
|
"bin/eletro",
|
27
27
|
"eletro.gemspec",
|
28
28
|
"lib/eletro.rb",
|
29
|
+
"lib/eletro/capacitor.rb",
|
30
|
+
"lib/eletro/diode.rb",
|
31
|
+
"lib/eletro/helpers/color_code.rb",
|
32
|
+
"lib/eletro/ic.rb",
|
33
|
+
"lib/eletro/inductor.rb",
|
34
|
+
"lib/eletro/logic.rb",
|
29
35
|
"lib/eletro/ohm.rb",
|
36
|
+
"lib/eletro/part.rb",
|
30
37
|
"lib/eletro/resistor.rb",
|
38
|
+
"lib/eletro/source.rb",
|
39
|
+
"lib/eletro/transistor.rb",
|
31
40
|
"spec/colour_test",
|
41
|
+
"spec/eletro/capacitor_spec.rb",
|
32
42
|
"spec/eletro/ohm_spec.rb",
|
43
|
+
"spec/eletro/part_spec.rb",
|
33
44
|
"spec/eletro/resistor_spec.rb",
|
45
|
+
"spec/eletro/source_spec.rb",
|
34
46
|
"spec/eletro_spec.rb",
|
35
47
|
"spec/spec_helper.rb"
|
36
48
|
]
|
@@ -41,8 +53,11 @@ Gem::Specification.new do |s|
|
|
41
53
|
s.summary = %q{Eletric Helpers on Ruby}
|
42
54
|
s.test_files = [
|
43
55
|
"spec/spec_helper.rb",
|
56
|
+
"spec/eletro/source_spec.rb",
|
57
|
+
"spec/eletro/capacitor_spec.rb",
|
44
58
|
"spec/eletro/ohm_spec.rb",
|
45
59
|
"spec/eletro/resistor_spec.rb",
|
60
|
+
"spec/eletro/part_spec.rb",
|
46
61
|
"spec/eletro_spec.rb"
|
47
62
|
]
|
48
63
|
|
data/lib/eletro.rb
CHANGED
@@ -1,6 +1,15 @@
|
|
1
1
|
|
2
|
+
require "eletro/helpers/color_code"
|
2
3
|
require "eletro/ohm"
|
4
|
+
require "eletro/part"
|
5
|
+
require "eletro/diode"
|
6
|
+
require "eletro/logic"
|
7
|
+
require "eletro/source"
|
3
8
|
require "eletro/resistor"
|
9
|
+
require "eletro/inductor"
|
10
|
+
require "eletro/capacitor"
|
11
|
+
require "eletro/transistor"
|
12
|
+
require "eletro/ic"
|
4
13
|
|
5
14
|
module Eletro
|
6
15
|
|
@@ -8,7 +17,7 @@ module Eletro
|
|
8
17
|
params = args.join
|
9
18
|
if params =~ /A|mA/ # need to improve
|
10
19
|
else
|
11
|
-
puts Resistor.new(params).
|
20
|
+
puts Resistor.new(params).pretty_output
|
12
21
|
end
|
13
22
|
end
|
14
23
|
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
module Eletro
|
4
|
+
|
5
|
+
class Capacitor < Part
|
6
|
+
attr_accessor :polarized
|
7
|
+
|
8
|
+
include ColorCode
|
9
|
+
|
10
|
+
def initialize(*args)
|
11
|
+
parse_color_code(args.join)
|
12
|
+
end
|
13
|
+
|
14
|
+
def parse txt
|
15
|
+
if m = txt.match(/#{mult.keys.join('|')}/)
|
16
|
+
txt.gsub!(/#{m[0]}/, ".")
|
17
|
+
m = mult[m[0]]
|
18
|
+
else
|
19
|
+
st, nd, m = txt.split(//)
|
20
|
+
m ||= 0
|
21
|
+
m = m.to_i #(10**-(m.to_i))
|
22
|
+
end
|
23
|
+
num = ("#{st}#{nd}#{'0' * m}").to_f * mult['p']
|
24
|
+
num
|
25
|
+
end
|
26
|
+
|
27
|
+
def unit
|
28
|
+
"F"
|
29
|
+
end
|
30
|
+
|
31
|
+
def mult
|
32
|
+
{ 'n' => 10e-6, 'u' => 10e-9, 'p' => 10e-12 }
|
33
|
+
end
|
34
|
+
|
35
|
+
def format
|
36
|
+
m = 'p'
|
37
|
+
mult.keys.reverse.each do |k|
|
38
|
+
m = k if value >= mult[k]
|
39
|
+
end
|
40
|
+
# v = if value > mult[0]
|
41
|
+
# "%g" % @value
|
42
|
+
# elsif value < 1000000
|
43
|
+
# "%gk" % (@value/1000)
|
44
|
+
# else
|
45
|
+
# "%gm" % (@value/1000000)
|
46
|
+
# end
|
47
|
+
out = "%g#{m}#{unit}" % (value/mult[m]).round
|
48
|
+
out += " ± #{@precision}%" if @precision
|
49
|
+
out
|
50
|
+
end
|
51
|
+
|
52
|
+
def polarized?
|
53
|
+
!!@polarized
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
end
|
data/lib/eletro/diode.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
module Eletro
|
3
|
+
|
4
|
+
COLOR = true
|
5
|
+
|
6
|
+
module ColorCode
|
7
|
+
attr_reader :color
|
8
|
+
|
9
|
+
CODE = [:k, :b, :r, :o, :y, :g, :u, :v, :a, :w, :l, :s]
|
10
|
+
COLORS = {
|
11
|
+
:k => :black,
|
12
|
+
:b => :brown,
|
13
|
+
:r => :red,
|
14
|
+
:o => :orange,
|
15
|
+
:y => :yellow,
|
16
|
+
:g => :green,
|
17
|
+
:u => :blue,
|
18
|
+
:v => :violet,
|
19
|
+
:a => :gray,
|
20
|
+
:w => :white,
|
21
|
+
:l => :gold,
|
22
|
+
:s => :silver}
|
23
|
+
|
24
|
+
PRECISION = [nil, 1, 2, nil, nil, 0.5, 0.25, 01, 0.05, nil, 5, 10, 20]
|
25
|
+
|
26
|
+
alias :colors :color
|
27
|
+
|
28
|
+
def parse_color_code(params)
|
29
|
+
return if params.empty?
|
30
|
+
if params =~ /\d/
|
31
|
+
# m = params.match(/(\d*[,|{k|m|g}|\.]?\d*)\s*(k|m|g)?/)
|
32
|
+
# return unless m
|
33
|
+
@value = parse(params)
|
34
|
+
@color = value2color(@value)
|
35
|
+
else
|
36
|
+
@color = params.split(//)
|
37
|
+
@value = color2value(@color)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def calc char
|
42
|
+
CODE.index(char.downcase.to_sym)
|
43
|
+
end
|
44
|
+
|
45
|
+
def color2value chars
|
46
|
+
out = calc(chars[0])
|
47
|
+
out = (out.to_s + calc(chars[1]).to_s).to_f
|
48
|
+
out *= (10 ** (calc(chars[2])))
|
49
|
+
if chars.size > 3
|
50
|
+
@precision = PRECISION[calc(chars[3])]
|
51
|
+
else
|
52
|
+
#out *= (10 ** (calc(chars[2])))
|
53
|
+
end
|
54
|
+
out
|
55
|
+
end
|
56
|
+
|
57
|
+
def value2color value
|
58
|
+
st, nd, *rest = value.to_s.split(//)
|
59
|
+
out = [CODE[st.to_i], CODE[nd.to_i]]
|
60
|
+
index = 1 if rest.size == 3 # ugly... sleepy... fix....
|
61
|
+
index ||= (value ** 0.1).round
|
62
|
+
out << CODE[index]
|
63
|
+
out
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
def rgblize color
|
68
|
+
s = case color.to_s.downcase.to_sym
|
69
|
+
when :k then "\e[40m"
|
70
|
+
when :b then "\e[0;33m"
|
71
|
+
when :r then "\e[41m"
|
72
|
+
when :g then "\e[42m"
|
73
|
+
when :y then "\e[1;33m"
|
74
|
+
when :o then "\e[43m"
|
75
|
+
when :u then "\e[44m"
|
76
|
+
when :v then "\e[45m"
|
77
|
+
when :w then "\e[47m"
|
78
|
+
when :a then "\e[31m"
|
79
|
+
else ".."
|
80
|
+
end
|
81
|
+
s += "#{color.to_s.upcase}\e[0m"
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
def bold(txt); "\e[2m#{txt}\e[0m"; end
|
86
|
+
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
end
|
data/lib/eletro/ic.rb
ADDED
data/lib/eletro/logic.rb
ADDED
data/lib/eletro/part.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
module Eletro
|
2
|
+
|
3
|
+
class Part
|
4
|
+
attr_accessor :value, :unit, :p0, :p1, :v
|
5
|
+
|
6
|
+
def p0
|
7
|
+
@p0 ||= :gnd
|
8
|
+
end
|
9
|
+
|
10
|
+
def p1
|
11
|
+
@p1 ||= :pwd
|
12
|
+
end
|
13
|
+
|
14
|
+
def *(other)
|
15
|
+
self.class.new(self.value*other)
|
16
|
+
end
|
17
|
+
|
18
|
+
def +(other)
|
19
|
+
self.class.new(self.value+other)
|
20
|
+
end
|
21
|
+
|
22
|
+
def /(other)
|
23
|
+
self.class.new(self.value/other)
|
24
|
+
end
|
25
|
+
|
26
|
+
def -(other)
|
27
|
+
self.class.new(self.value-other)
|
28
|
+
end
|
29
|
+
|
30
|
+
def abs
|
31
|
+
self.value.abs
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_f; @value; end
|
35
|
+
def to_i; @value.to_i; end
|
36
|
+
|
37
|
+
def unit; end
|
38
|
+
def to_s
|
39
|
+
"#{@value}#{unit}"
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
def coerce(other); [other, @value]; end
|
44
|
+
|
45
|
+
def round
|
46
|
+
# todo, table standard values, find nearest...
|
47
|
+
end
|
48
|
+
|
49
|
+
def method_missing(*args)
|
50
|
+
txt = args.join
|
51
|
+
if txt =~ /p\d+/
|
52
|
+
if txt =~ /=/
|
53
|
+
var, value = txt.split("=")
|
54
|
+
instance_variable_set("@#{var}", value)
|
55
|
+
self.class.instance_eval do
|
56
|
+
define_method(var, lambda { instance_variable_get("@#{var}") })
|
57
|
+
end
|
58
|
+
else
|
59
|
+
respond_to?(txt) ? send(args.join) : nil
|
60
|
+
end
|
61
|
+
else
|
62
|
+
raise NoMethodError.new("undefined method `#{args.join}' " +
|
63
|
+
"for #{self.inspect}:#{self.class.name}")
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
end
|
data/lib/eletro/resistor.rb
CHANGED
@@ -4,80 +4,30 @@
|
|
4
4
|
|
5
5
|
module Eletro
|
6
6
|
|
7
|
-
|
7
|
+
class Resistor < Part
|
8
8
|
|
9
|
-
|
10
|
-
attr_reader :value, :color
|
11
|
-
|
12
|
-
UNIT = 'Ω'
|
13
|
-
MULT = /k|m|g/
|
14
|
-
|
15
|
-
CODE = [:k, :b, :r, :o, :y, :g, :u, :v, :a, :w, :l, :s]
|
16
|
-
COLORS = {
|
17
|
-
:k => :black,
|
18
|
-
:b => :brown,
|
19
|
-
:r => :red,
|
20
|
-
:o => :orange,
|
21
|
-
:y => :yellow,
|
22
|
-
:g => :green,
|
23
|
-
:u => :blue,
|
24
|
-
:v => :violet,
|
25
|
-
:a => :gray,
|
26
|
-
:w => :white,
|
27
|
-
:l => :gold,
|
28
|
-
:s => :silver}
|
29
|
-
|
30
|
-
PRECISION = [nil, 1, 2, nil, nil, 0.5, 0.25, 01, 0.05, nil, 5, 10, 20]
|
9
|
+
include ColorCode
|
31
10
|
|
32
11
|
def initialize(*args)
|
33
|
-
|
34
|
-
if params =~ /\d/
|
35
|
-
# m = params.match(/(\d*[,|{k|m|g}|\.]?\d*)\s*(k|m|g)?/)
|
36
|
-
# return unless m
|
37
|
-
@value = parse(params)
|
38
|
-
@color = value2color(@value)
|
39
|
-
else
|
40
|
-
@color = params.split(//)
|
41
|
-
@value = color2value(@color)
|
42
|
-
end
|
12
|
+
parse_color_code(args.join)
|
43
13
|
end
|
44
14
|
|
45
15
|
def parse txt
|
46
|
-
if
|
47
|
-
txt.gsub!(/#{
|
48
|
-
|
16
|
+
if m = txt.match(/#{mult.keys.join('|')}/)
|
17
|
+
txt.gsub!(/#{m[0]}/, ".")
|
18
|
+
m = mult[m[0]]
|
49
19
|
end
|
50
20
|
num = txt.to_f
|
51
|
-
num *=
|
21
|
+
num *= m if m
|
52
22
|
num
|
53
23
|
end
|
54
24
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
def calc char
|
59
|
-
CODE.index(char.downcase.to_sym)
|
25
|
+
def unit
|
26
|
+
"Ω"
|
60
27
|
end
|
61
28
|
|
62
|
-
def
|
63
|
-
|
64
|
-
out = (out.to_s + calc(chars[1]).to_s).to_f
|
65
|
-
out *= (10 ** (calc(chars[2])))
|
66
|
-
if chars.size > 3
|
67
|
-
@precision = PRECISION[calc(chars[3])]
|
68
|
-
else
|
69
|
-
#out *= (10 ** (calc(chars[2])))
|
70
|
-
end
|
71
|
-
out
|
72
|
-
end
|
73
|
-
|
74
|
-
def value2color value
|
75
|
-
st, nd, *rest = value.to_s.split(//)
|
76
|
-
out = [CODE[st.to_i], CODE[nd.to_i]]
|
77
|
-
index = 1 if rest.size == 3 # ugly... sleepy... fix....
|
78
|
-
index ||= (value ** 0.1).round
|
79
|
-
out << CODE[index]
|
80
|
-
out.map(&:upcase)
|
29
|
+
def mult
|
30
|
+
{ 'k' => 10e2, 'm' => 10e5 }
|
81
31
|
end
|
82
32
|
|
83
33
|
def format
|
@@ -88,40 +38,17 @@ module Eletro
|
|
88
38
|
else
|
89
39
|
"%gm" % (@value/1000000)
|
90
40
|
end
|
91
|
-
out = "#{v}#{
|
41
|
+
out = "#{v}#{unit}"
|
92
42
|
out += " ± #{@precision}%" if @precision
|
93
43
|
out
|
94
44
|
end
|
95
45
|
|
96
|
-
def
|
46
|
+
def pretty_output
|
97
47
|
out = format
|
98
48
|
out += " --"
|
99
|
-
@color.each
|
100
|
-
out += COLOR ? rgblize(c) : c
|
101
|
-
end
|
49
|
+
@color.each { |c| out += COLOR ? rgblize(c) : c }
|
102
50
|
out + "--"
|
103
|
-
|
104
51
|
end
|
105
52
|
|
106
|
-
|
107
|
-
def rgblize color
|
108
|
-
s = case color.downcase.to_sym
|
109
|
-
when :k then "\e[40m"
|
110
|
-
when :b then "\e[0;33m"
|
111
|
-
when :r then "\e[41m"
|
112
|
-
when :g then "\e[42m"
|
113
|
-
when :y then "\e[1;33m"
|
114
|
-
when :o then "\e[43m"
|
115
|
-
when :u then "\e[44m"
|
116
|
-
when :v then "\e[45m"
|
117
|
-
when :w then "\e[47m"
|
118
|
-
when :a then "\e[31m"
|
119
|
-
else ".."
|
120
|
-
end
|
121
|
-
s += "#{color}\e[0m"
|
122
|
-
end
|
123
|
-
|
124
|
-
def bold(txt); "\e[2m#{txt}\e[0m"; end
|
125
|
-
|
126
53
|
end
|
127
54
|
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module Eletro
|
2
|
+
|
3
|
+
class Source < Part
|
4
|
+
|
5
|
+
attr_accessor :hz, :v, :circuits
|
6
|
+
|
7
|
+
|
8
|
+
def initialize(*args)
|
9
|
+
params = args.join
|
10
|
+
@v = params.match(/(\d*)/)[1].to_i
|
11
|
+
@hz = params =~ /ac|AC/ ? 60 : 0
|
12
|
+
@circuits = []
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
def <<(part, circuit=nil)
|
18
|
+
if circuit
|
19
|
+
|
20
|
+
else
|
21
|
+
circuits << (part.is_a?(Array) ? part : [part])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def i
|
26
|
+
@v / sum_net.to_f
|
27
|
+
end
|
28
|
+
|
29
|
+
def w
|
30
|
+
@v * i
|
31
|
+
end
|
32
|
+
|
33
|
+
#
|
34
|
+
# Logic:
|
35
|
+
#
|
36
|
+
# Resistor on an array are in series, sum. [100, 100] => [200]
|
37
|
+
# Arrays in arrays are parallel connections. [[100], [100]] => [50]
|
38
|
+
#
|
39
|
+
# Mixed example (rounded):
|
40
|
+
#
|
41
|
+
# [[100], [100, 100], [100, 100]]
|
42
|
+
# [[100], [200], [200]]
|
43
|
+
# [[66], [200]]
|
44
|
+
# [[50]] => 50 ohms
|
45
|
+
#
|
46
|
+
def sum_net
|
47
|
+
# Memoize
|
48
|
+
@sum_net ||= calc_sum_net
|
49
|
+
end
|
50
|
+
|
51
|
+
def calc_sum_net(ary=circuits)
|
52
|
+
ary.map! do |i|
|
53
|
+
if i.is_a?(Array)
|
54
|
+
i = [calc_sum_net(i)] unless i.select { |c| c.is_a?(Array) }.empty?
|
55
|
+
i.size > 1 ? serial_sum(i) : i[0]#.value
|
56
|
+
else; i
|
57
|
+
end
|
58
|
+
end
|
59
|
+
ary.size == 1 ? ary[0] : parallel_sum(*ary)
|
60
|
+
end
|
61
|
+
|
62
|
+
def parallel_sum(r1, r2, *rest)
|
63
|
+
sum = r1 * r2 / (r1 + r2)
|
64
|
+
rest.empty? ? sum : parallel_sum(*rest.unshift(sum))
|
65
|
+
end
|
66
|
+
|
67
|
+
def serial_sum(ary)
|
68
|
+
ary.reduce(0) { |i, c| i + c }
|
69
|
+
end
|
70
|
+
|
71
|
+
#just for fun
|
72
|
+
def acdc
|
73
|
+
dc? || (@v /= 2)
|
74
|
+
end
|
75
|
+
|
76
|
+
def dc?
|
77
|
+
@hz.zero?
|
78
|
+
end
|
79
|
+
|
80
|
+
def ac?
|
81
|
+
!dc?
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe "Eletro::Capacitor" do
|
4
|
+
|
5
|
+
let(:rs) { Capacitor.new }
|
6
|
+
|
7
|
+
describe "Value" do
|
8
|
+
|
9
|
+
it "should not be polarized" do
|
10
|
+
c = Capacitor.new
|
11
|
+
c.should_not be_polarized
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should instantiate 47" do
|
15
|
+
r = Capacitor.new("47")
|
16
|
+
r.value.should be_within(220.0).of(0.1)
|
17
|
+
r.format.should eql("47pF")
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should instantiate 102" do
|
21
|
+
r = Capacitor.new("102")
|
22
|
+
r.value.should be_within(220.0).of(0.1)
|
23
|
+
r.format.should eql("1uF")
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
data/spec/eletro/ohm_spec.rb
CHANGED
@@ -33,19 +33,19 @@ describe "Eletro::Ohm" do
|
|
33
33
|
it "should calc V if I have [I, R]" do
|
34
34
|
o.i = 0.048
|
35
35
|
o.r = 250
|
36
|
-
o.v.should
|
36
|
+
o.v.should be_within(12.0).of(0.01)
|
37
37
|
end
|
38
38
|
|
39
39
|
it "should calc I if I have [V, R]" do
|
40
40
|
o.v = 12
|
41
41
|
o.r = 250
|
42
|
-
o.i.should
|
42
|
+
o.i.should be_within(0.048).of(0.001)
|
43
43
|
end
|
44
44
|
|
45
45
|
it "should calc V if I have [I, R]" do
|
46
46
|
o.i = 0.048
|
47
47
|
o.r = 250
|
48
|
-
o.v.should
|
48
|
+
o.v.should be_within(12.0).of(0.01)
|
49
49
|
end
|
50
50
|
# o.r.should eql(1)
|
51
51
|
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe "Eletro::Part" do
|
4
|
+
|
5
|
+
let(:p) { Part.new}
|
6
|
+
|
7
|
+
it "should have pins" do
|
8
|
+
p.p0.should eql(:gnd)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should get dynamic" do
|
12
|
+
p.p99.should be_nil
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should set dynamic" do
|
16
|
+
p.p99.should be_nil
|
17
|
+
p.p99 = :fu
|
18
|
+
p.p99.should eql('fu')
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
end
|
@@ -9,86 +9,86 @@ describe "Eletro::Resistor" do
|
|
9
9
|
|
10
10
|
it "should instantiate 220 ohms" do
|
11
11
|
r = Resistor.new("220")
|
12
|
-
r.value.should
|
12
|
+
r.value.should be_within(220.0).of(0.1)
|
13
13
|
r.format.should eql("220Ω")
|
14
14
|
end
|
15
15
|
|
16
16
|
it "should instantiate 100R ohms" do
|
17
17
|
r = Resistor.new("100R")
|
18
|
-
r.value.should
|
18
|
+
r.value.should be_within(100.0).of(0.1)
|
19
19
|
r.format.should eql("100Ω")
|
20
20
|
end
|
21
21
|
|
22
22
|
it "should instantiate 1k ohms" do
|
23
23
|
r = Resistor.new("1k")
|
24
|
-
r.value.should
|
24
|
+
r.value.should be_within(1000.0).of(0.1)
|
25
25
|
r.format.should eql("1kΩ")
|
26
26
|
end
|
27
27
|
|
28
28
|
it "should instantiate 1k ohms" do
|
29
29
|
r = Resistor.new("1000.0")
|
30
|
-
r.value.should
|
30
|
+
r.value.should be_within(1000.0).of(0.1)
|
31
31
|
r.format.should eql("1kΩ")
|
32
32
|
end
|
33
33
|
|
34
34
|
it "should instantiate 1k ohms" do
|
35
35
|
r = Resistor.new("1100.0")
|
36
|
-
r.value.should
|
36
|
+
r.value.should be_within(1100.0).of(0.1)
|
37
37
|
r.format.should eql("1.1kΩ")
|
38
38
|
end
|
39
39
|
|
40
40
|
it "should instantiate 1k ohms" do
|
41
41
|
r = Resistor.new("BBR")
|
42
|
-
r.value.should
|
42
|
+
r.value.should be_within(1100.0).of(0.1)
|
43
43
|
r.format.should eql("1.1kΩ")
|
44
44
|
end
|
45
45
|
|
46
46
|
it "should instantiate 3.3k ohms" do
|
47
47
|
r = Resistor.new("3.3k")
|
48
|
-
r.value.should
|
48
|
+
r.value.should be_within(3300.0).of(0.1)
|
49
49
|
r.format.should eql("3.3kΩ")
|
50
50
|
end
|
51
51
|
|
52
52
|
it "should instantiate 4k7 ohms" do
|
53
53
|
r = Resistor.new("4k7")
|
54
|
-
r.value.should
|
54
|
+
r.value.should be_within(4700.0).of(0.1)
|
55
55
|
r.format.should eql("4.7kΩ")
|
56
56
|
end
|
57
57
|
|
58
58
|
it "should instantiate 1m5 ohms" do
|
59
59
|
r = Resistor.new("1m5")
|
60
|
-
r.value.should
|
61
|
-
r.format.should eql("
|
60
|
+
r.value.should be_within(15000000.0).of(0.1)
|
61
|
+
r.format.should eql("1.5mΩ")
|
62
62
|
end
|
63
63
|
|
64
64
|
it "should get colors for value" do
|
65
65
|
r = Resistor.new("1k")
|
66
|
-
r.colors.join.should eql("
|
66
|
+
r.colors.join.should eql("bkr")
|
67
67
|
end
|
68
68
|
|
69
69
|
it "should get colors for value" do
|
70
70
|
r = Resistor.new("10k")
|
71
|
-
r.colors.join.should eql("
|
71
|
+
r.colors.join.should eql("bko")
|
72
72
|
end
|
73
73
|
|
74
74
|
it "should get colors for value" do
|
75
75
|
r = Resistor.new("1.1k")
|
76
|
-
r.colors.join.should eql("
|
76
|
+
r.colors.join.should eql("bbr")
|
77
77
|
end
|
78
78
|
|
79
79
|
it "should get colors for value" do
|
80
80
|
r = Resistor.new("4k7")
|
81
|
-
r.colors.join.should eql("
|
81
|
+
r.colors.join.should eql("yvr")
|
82
82
|
end
|
83
83
|
|
84
84
|
it "should get colors for value" do
|
85
85
|
r = Resistor.new("220")
|
86
|
-
r.colors.join.should eql("
|
86
|
+
r.colors.join.should eql("rrb")
|
87
87
|
end
|
88
88
|
|
89
89
|
it "should get colors for value" do
|
90
90
|
r = Resistor.new("110")
|
91
|
-
r.colors.join.should eql("
|
91
|
+
r.colors.join.should eql("bbb")
|
92
92
|
end
|
93
93
|
|
94
94
|
end
|
@@ -97,37 +97,37 @@ describe "Eletro::Resistor" do
|
|
97
97
|
|
98
98
|
it "should instantiate 3 chars" do
|
99
99
|
r = Resistor.new("BKR")
|
100
|
-
r.value.should
|
100
|
+
r.value.should be_within(1000.0).of(0.01)
|
101
101
|
r.format.should eql("1kΩ")
|
102
102
|
end
|
103
103
|
|
104
104
|
it "should instantiate 3 chars" do
|
105
105
|
r = Resistor.new("BKO")
|
106
|
-
r.value.should
|
106
|
+
r.value.should be_within(10000.0).of(0.01)
|
107
107
|
r.format.should eql("10kΩ")
|
108
108
|
end
|
109
109
|
|
110
110
|
it "should instantiate 3 chars" do
|
111
111
|
r = Resistor.new("YVR")
|
112
|
-
r.value.should
|
112
|
+
r.value.should be_within(4700.0).of(0.01)
|
113
113
|
r.format.should eql("4.7kΩ")
|
114
114
|
end
|
115
115
|
|
116
116
|
it "should instantiate 3 chars" do
|
117
117
|
r = Resistor.new("YVR")
|
118
|
-
r.value.should
|
118
|
+
r.value.should be_within(4700.0).of(0.01)
|
119
119
|
r.format.should eql("4.7kΩ")
|
120
120
|
end
|
121
121
|
|
122
122
|
it "should instantiate 4 chars" do
|
123
123
|
r = Resistor.new("WWRS")
|
124
|
-
r.value.should
|
124
|
+
r.value.should be_within(9900.0).of(0.01)
|
125
125
|
r.format.should eql("9.9kΩ ± 10%")
|
126
126
|
end
|
127
127
|
|
128
128
|
it "should instantiate 4 chars" do
|
129
129
|
r = Resistor.new("BKRL")
|
130
|
-
r.value.should
|
130
|
+
r.value.should be_within(1000.0).of(0.01)
|
131
131
|
r.format.should eql("1kΩ ± 5%")
|
132
132
|
end
|
133
133
|
|
@@ -138,8 +138,8 @@ describe "Eletro::Resistor" do
|
|
138
138
|
|
139
139
|
it "should print nicely" do
|
140
140
|
r = Resistor.new("BKR")
|
141
|
-
r.value.should
|
142
|
-
r.
|
141
|
+
r.value.should be_within(1000.0).of(0.01)
|
142
|
+
r.pretty_output.should eql("1kΩ --\e[0;33mB\e[0m\e[40mK\e[0m\e[41mR\e[0m--")
|
143
143
|
end
|
144
144
|
|
145
145
|
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe "Eletro::Source" do
|
4
|
+
|
5
|
+
let(:ss) { Source.new }
|
6
|
+
|
7
|
+
describe "Instantiate" do
|
8
|
+
|
9
|
+
it "should instantiate 220 v" do
|
10
|
+
s = Source.new("220ac")
|
11
|
+
s.v.should eql(220)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should instantiate 220 AC" do
|
15
|
+
s = Source.new("220ac")
|
16
|
+
s.should be_ac
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should instantiate 12vdc" do
|
20
|
+
s = Source.new("12vdc")
|
21
|
+
s.v.should eql(12)
|
22
|
+
s.should be_dc
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should default to DC" do
|
26
|
+
s = Source.new("9")
|
27
|
+
s.should be_dc
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "Circuits" do
|
34
|
+
|
35
|
+
describe "Loads" do
|
36
|
+
|
37
|
+
it "should add loads 1k" do
|
38
|
+
s = Source.new("12")
|
39
|
+
s << Resistor.new("1k")
|
40
|
+
s.i.should be_within(0.012).of(0.0001)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should add loads 100 ohm" do
|
44
|
+
s = Source.new("12")
|
45
|
+
s << Resistor.new("100")
|
46
|
+
s.i.should be_within(0.12).of(0.001)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should add two parallel loads" do
|
50
|
+
s = Source.new("12")
|
51
|
+
s << Resistor.new("2k")
|
52
|
+
s << Resistor.new("2k")
|
53
|
+
s.sum_net.should be_within(1000.0).of(0.01)
|
54
|
+
s.i.should be_within(0.012).of(0.0001)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should add three parallel loads" do
|
58
|
+
s = Source.new("12")
|
59
|
+
s << Resistor.new("1k")
|
60
|
+
s << Resistor.new("1k")
|
61
|
+
s << Resistor.new("100")
|
62
|
+
s.sum_net.should be_within(83.333).of(0.001)
|
63
|
+
s.i.should be_within(0.144).of(0.001)
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should add mixed parallel/serial" do
|
67
|
+
s = Source.new("12")
|
68
|
+
s << Resistor.new("100")
|
69
|
+
s << [Resistor.new("100"), Resistor.new("100")]
|
70
|
+
s << [Resistor.new("100"), Resistor.new("100")]
|
71
|
+
s.circuits.should have(3).items
|
72
|
+
s.sum_net.should be_within(50.0).of(0.01)
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should add mixed parallel/serial" do
|
76
|
+
s = Source.new("12")
|
77
|
+
s << Resistor.new("100")
|
78
|
+
s << [Resistor.new("100"), Resistor.new("100")]
|
79
|
+
s << [Resistor.new("100"), Resistor.new("200")]
|
80
|
+
s.circuits.should have(3).items
|
81
|
+
s.sum_net.should be_within(54.5454).of(0.01)
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
it "should add mixed parallel/serial" do
|
86
|
+
s = Source.new("12")
|
87
|
+
s << Resistor.new("100")
|
88
|
+
s << [Resistor.new("100"), Resistor.new("100")]
|
89
|
+
s << [[Resistor.new("100")], [Resistor.new("200")]]
|
90
|
+
s.circuits.should have(3).items
|
91
|
+
s.sum_net.should be_within(33.333).of(0.001)
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should add mixed parallel series with more depth" do
|
95
|
+
s = Source.new("12")
|
96
|
+
s << Resistor.new("100")
|
97
|
+
s << [Resistor.new("100"), Resistor.new("100")]
|
98
|
+
s << [Resistor.new("100"), [Resistor.new("100"), Resistor.new("100")]]
|
99
|
+
s.circuits.should have(3).items
|
100
|
+
s.sum_net.should be_within(33.333).of(0.001)
|
101
|
+
end
|
102
|
+
|
103
|
+
# it "should add loads calcs w" do
|
104
|
+
# s = Source.new("12")
|
105
|
+
# s << Resistor.new("40")
|
106
|
+
# s.w.should be_within(3.5999, 0.0001)
|
107
|
+
# end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
+
- 1
|
7
8
|
- 0
|
8
|
-
|
9
|
-
version: 0.0.2
|
9
|
+
version: 0.1.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Marcos Piccinini
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-11-
|
17
|
+
date: 2010-11-11 00:00:00 -02:00
|
18
18
|
default_executable: eletro
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -64,11 +64,23 @@ files:
|
|
64
64
|
- bin/eletro
|
65
65
|
- eletro.gemspec
|
66
66
|
- lib/eletro.rb
|
67
|
+
- lib/eletro/capacitor.rb
|
68
|
+
- lib/eletro/diode.rb
|
69
|
+
- lib/eletro/helpers/color_code.rb
|
70
|
+
- lib/eletro/ic.rb
|
71
|
+
- lib/eletro/inductor.rb
|
72
|
+
- lib/eletro/logic.rb
|
67
73
|
- lib/eletro/ohm.rb
|
74
|
+
- lib/eletro/part.rb
|
68
75
|
- lib/eletro/resistor.rb
|
76
|
+
- lib/eletro/source.rb
|
77
|
+
- lib/eletro/transistor.rb
|
69
78
|
- spec/colour_test
|
79
|
+
- spec/eletro/capacitor_spec.rb
|
70
80
|
- spec/eletro/ohm_spec.rb
|
81
|
+
- spec/eletro/part_spec.rb
|
71
82
|
- spec/eletro/resistor_spec.rb
|
83
|
+
- spec/eletro/source_spec.rb
|
72
84
|
- spec/eletro_spec.rb
|
73
85
|
- spec/spec_helper.rb
|
74
86
|
has_rdoc: true
|
@@ -105,6 +117,9 @@ specification_version: 3
|
|
105
117
|
summary: Eletric Helpers on Ruby
|
106
118
|
test_files:
|
107
119
|
- spec/spec_helper.rb
|
120
|
+
- spec/eletro/source_spec.rb
|
121
|
+
- spec/eletro/capacitor_spec.rb
|
108
122
|
- spec/eletro/ohm_spec.rb
|
109
123
|
- spec/eletro/resistor_spec.rb
|
124
|
+
- spec/eletro/part_spec.rb
|
110
125
|
- spec/eletro_spec.rb
|