Class: Plc::Emulator::EmuDevice
- Inherits:
-
PlcDevice
- Object
- PlcDevice
- Plc::Emulator::EmuDevice
- Defined in:
- lib/plc/emulator/emu_device.rb
Instance Attribute Summary collapse
-
#in_value ⇒ Object
readonly
Returns the value of attribute in_value.
-
#out_value ⇒ Object
readonly
Returns the value of attribute out_value.
-
#plc ⇒ Object
Returns the value of attribute plc.
Instance Method Summary collapse
- #+(value) ⇒ Object
- #-(value) ⇒ Object
- #bool(kind = nil) ⇒ Object
- #bool=(value) ⇒ Object
- #changed? ⇒ Boolean
-
#device_by_suffix_number(suffix, number) ⇒ Object
NOTE: override at subclass It should get from plc.
-
#initialize(a, b = nil) ⇒ EmuDevice
constructor
A new instance of EmuDevice.
- #reset ⇒ Object
- #set_value(value, kind = nil) ⇒ Object
- #set_word(value, kind = nil) ⇒ Object
- #sync_input ⇒ Object
- #sync_output ⇒ Object
- #value(kind = nil) ⇒ Object
- #value=(value) ⇒ Object
- #word(kind = nil) ⇒ Object
- #word=(value) ⇒ Object
Constructor Details
#initialize(a, b = nil) ⇒ EmuDevice
Returns a new instance of EmuDevice.
36 37 38 39 40 41 42 |
# File 'lib/plc/emulator/emu_device.rb', line 36 def initialize a, b = nil super @lock = Mutex.new @in_value = 0 @out_value = 0 @changed = false end |
Instance Attribute Details
#in_value ⇒ Object (readonly)
Returns the value of attribute in_value.
33 34 35 |
# File 'lib/plc/emulator/emu_device.rb', line 33 def in_value @in_value end |
#out_value ⇒ Object (readonly)
Returns the value of attribute out_value.
33 34 35 |
# File 'lib/plc/emulator/emu_device.rb', line 33 def out_value @out_value end |
#plc ⇒ Object
Returns the value of attribute plc.
34 35 36 |
# File 'lib/plc/emulator/emu_device.rb', line 34 def plc @plc end |
Instance Method Details
#+(value) ⇒ Object
163 164 165 166 |
# File 'lib/plc/emulator/emu_device.rb', line 163 def + value d = super plc ? plc.device_by_name(d.name) : d end |
#-(value) ⇒ Object
168 169 170 171 |
# File 'lib/plc/emulator/emu_device.rb', line 168 def - value d = super plc ? plc.device_by_name(d.name) : d end |
#bool(kind = nil) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/plc/emulator/emu_device.rb', line 71 def bool kind=nil v = value kind case v when nil, false, 0 false else true end end |
#bool=(value) ⇒ Object
81 82 83 |
# File 'lib/plc/emulator/emu_device.rb', line 81 def bool= value set_value value end |
#changed? ⇒ Boolean
56 57 58 |
# File 'lib/plc/emulator/emu_device.rb', line 56 def changed? @changed end |
#device_by_suffix_number(suffix, number) ⇒ Object
NOTE: override at subclass
It should get from plc
62 63 64 65 |
# File 'lib/plc/emulator/emu_device.rb', line 62 def device_by_suffix_number suffix, number d = super plc.device_by_name d.name end |
#reset ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/plc/emulator/emu_device.rb', line 44 def reset @lock.synchronize { super @in_value = nil @out_value = 0 } end |
#set_value(value, kind = nil) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/plc/emulator/emu_device.rb', line 132 def set_value value, kind=nil @lock.synchronize { case kind when :in @in_value = value when :out @out_value = value else @changed = true unless @value == value @value = value if @changed end } end |
#set_word(value, kind = nil) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/plc/emulator/emu_device.rb', line 105 def set_word value, kind=nil if bit_device? f = 1 d = self 16.times do d.set_value (value & f) != 0, kind d = d + 1 f <<= 1 end else set_value value, kind end end |
#sync_input ⇒ Object
146 147 148 149 150 151 152 153 154 |
# File 'lib/plc/emulator/emu_device.rb', line 146 def sync_input @lock.synchronize { unless @in_value.nil? @changed = true unless @value == @in_value @value = @in_value if @changed @in_value = nil end } end |
#sync_output ⇒ Object
156 157 158 159 160 161 |
# File 'lib/plc/emulator/emu_device.rb', line 156 def sync_output @lock.synchronize { @out_value = @value @changed = false } end |
#value(kind = nil) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/plc/emulator/emu_device.rb', line 119 def value kind=nil @lock.synchronize { case kind when :in @in_value when :out @out_value else @value end } end |
#value=(value) ⇒ Object
67 68 69 |
# File 'lib/plc/emulator/emu_device.rb', line 67 def value= value set_value value end |
#word(kind = nil) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/plc/emulator/emu_device.rb', line 85 def word kind=nil if bit_device? v = 0 d = self f = 1 16.times do v |= f if d.bool(kind) d = d + 1 f <<= 1 end v else value kind end end |
#word=(value) ⇒ Object
101 102 103 |
# File 'lib/plc/emulator/emu_device.rb', line 101 def word= value set_word value end |