Sha256: f9bbfaac873556188690e9a40d92e1759ababbb63ee7566b0bc7cf1adbead9e4

Contents?: true

Size: 695 Bytes

Versions: 2

Compression:

Stored size: 695 Bytes

Contents

module Senec
  class Value
    def initialize(data)
      @data = data
    end

    def to_f
      decoded_value
    end

    def to_i
      decoded_value.round
    end

    private

    def decoded_value
      parts  = @data.split('_')
      prefix = parts[0]
      value  = parts[1]

      case prefix
      when 'fl'
        hex2float(value)
      when 'i3', 'u1', 'u3'
        hex2int(value)
      # TODO: There are some more prefixes to handle
      else
        raise ArgumentError, "#{@data} cannot be decoded!"
      end
    end

    def hex2float(hex)
      ["0x#{hex}".to_i(16)].pack('L').unpack1('F').round(1)
    end

    def hex2int(hex)
      "0x#{hex}".to_i(16)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
senec-0.5.0 lib/senec/value.rb
senec-0.4.0 lib/senec/value.rb