class LadderDrive::Protocol::Omron::OmronDevice

Constants

SUFFIXES

Attributes

bit[R]
channel[R]
suffix[R]

Public Class Methods

new(a, b = nil, c=nil) click to toggle source
# File lib/ladder_drive/protocol/omron/omron_device.rb, line 37
    def initialize a, b = nil, c=nil
      case a
      when Array
=begin
        case a.size
        when 4
          @suffix = suffix_for_code(a[3])
          @channel = ((a[2] << 8 | a[1]) << 8) | a[0]
        end
=end
      else
        if b
          @suffix = a.upcase if a
          @channel = b.to_i
          @bit = c.to_i if c
        else
          if /^(M|H|D|T|C|A)?([0-9]+)(\.([0-9]{1,2}))?$/i =~ a
            @suffix = $1.upcase if $1
            @channel = $2.to_i
            @bit = $4.to_i if $4
          end
        end
      end
      case @suffix
      when "T", "C"
        raise "#{self.name} is not allowed as a bit device." if @bit
      end
    end

Public Instance Methods

+(value) click to toggle source
# File lib/ladder_drive/protocol/omron/omron_device.rb, line 101
def + value
  if bit
    v = channel * 16 + bit + value
    c = v / 16
    b = v % 16
    self.class.new suffix, c, b
  else
    self.class.new suffix, channel + value
  end
end
-(value) click to toggle source
# File lib/ladder_drive/protocol/omron/omron_device.rb, line 112
def - value
  case value
  when OmronDevice
    d = value
    raise "Can't subtract between different device type." if self.bit_device? ^ d.bit_device?
    if bit
      (channel * 16 + bit) - (d.channel * 16 + d.bit)
    else
      channel - d.channel
    end
  else
    value = value.to_i
    if bit
      v = [channel * 16 + bit - value, 0].max
      c = v / 16
      b = v % 16
      self.class.new suffix, c, b
    else
      self.class.new suffix, [channel - value, 0].max
    end
  end
end
bit_device?() click to toggle source
# File lib/ladder_drive/protocol/omron/omron_device.rb, line 87
def bit_device?
  !!bit
end
channel_device() click to toggle source
# File lib/ladder_drive/protocol/omron/omron_device.rb, line 66
def channel_device
  return self unless bit_device?
  self.class.new suffix, channel
end
name() click to toggle source
# File lib/ladder_drive/protocol/omron/omron_device.rb, line 75
def name
  if bit
    "#{suffix}#{channel}.#{bit.to_s.rjust(2, '0')}"
  else
    "#{suffix}#{channel}"
  end
end
next_device() click to toggle source
# File lib/ladder_drive/protocol/omron/omron_device.rb, line 83
def next_device
  self + 1
end
suffix_code() click to toggle source
# File lib/ladder_drive/protocol/omron/omron_device.rb, line 96
def suffix_code
  index = SUFFIXES.index suffix
  index ? SUFFIX_CODES[index] : 0
end
suffix_for_code(code) click to toggle source
# File lib/ladder_drive/protocol/omron/omron_device.rb, line 91
def suffix_for_code code
  index = SUFFIX_CODES.index code
  index ? SUFFIXES[index] : nil
end
valid?() click to toggle source
# File lib/ladder_drive/protocol/omron/omron_device.rb, line 71
def valid?
  !!channel
end