Module: Unpack

Included in:
FitObject
Defined in:
lib/fitreader/unpack.rb

Constant Summary collapse

MASKS =
{
  7 => 0b10000000,
  6 => 0b01000000,
  5 => 0b00100000,
  4 => 0b00010000,
  3 => 0b00001000,
  2 => 0b00000100,
  1 => 0b00000010,
  0 => 0b00000001
}.freeze

Instance Method Summary collapse

Instance Method Details

#read_bit(byte, bit) ⇒ Object



20
21
22
# File 'lib/fitreader/unpack.rb', line 20

def read_bit(byte, bit)
  (byte & MASKS[bit]) >> bit
end

#read_bits(byte, range) ⇒ Object



24
25
26
27
28
29
# File 'lib/fitreader/unpack.rb', line 24

def read_bits(byte, range)
  mask = range.first
              .downto(range.last)
              .inject(0) { |sum, i| sum + MASKS[i] }
  (byte & mask) >> range.last
end

#read_multiple(io, char, len, size) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fitreader/unpack.rb', line 6

def read_multiple(io, char, len, size)
  if char == 'Z*'
    readbytes(io, char, len)
  else
    multiples = len / size
    res = io.read(len).unpack(char * multiples)
    if res.length == 1
      res.first
    else
      res
    end
  end
end

#readbytes(io, char, len) ⇒ Object



2
3
4
# File 'lib/fitreader/unpack.rb', line 2

def readbytes(io, char, len)
  io.read(len).unpack(char).first
end