Class: Ansible::KNX::DPT9::DPT9_Float

Inherits:
BinData::Primitive
  • Object
show all
Defined in:
lib/ansible/knx/dpt/dpt9.rb

Overview

special Bindata::Primitive class required for non-standard 16-bit floats used by DPT9

Constant Summary collapse

DPT9_Range =
-671088.64..670760.96

Instance Method Summary collapse

Instance Method Details

#getObject



44
45
46
47
48
# File 'lib/ansible/knx/dpt/dpt9.rb', line 44

def get
    # puts "sign=#{sign} exp=#{exp} mant=#{mant}"
    mantissa = (self.sign==1) ? ~(self.mant^2047) : self.mant
    return Math.ldexp((0.01*mantissa), self.exp)
end

#set(v) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ansible/knx/dpt/dpt9.rb', line 52

def set(v)
    raise "Value (#{v}) out of range" unless DPT9_Range === v
    mantissa, exponent = Math.frexp(v)
    #puts "#{self}.set(#{v}) with initial mantissa=#{mantissa}, exponent=#{exponent}"
    # find the minimum exponent that will upsize the normalized mantissa (0,5 to 1 range)
    # in order to fit in 11 bits (-2048..2047)
    max_mantissa = 0
    minimum_exp = exponent.downto(-15).find{ | e |
        max_mantissa = Math.ldexp(100*mantissa, e).to_i
        max_mantissa.between?(-2048, 2047)
    } 
    self.sign = (mantissa < 0) ?  1 :  0 
    self.mant  = (mantissa < 0) ?  ~(max_mantissa^2047) : max_mantissa 
    self.exp = exponent - minimum_exp  
end