Class: F4R::CRC16

Inherits:
Object
  • Object
show all
Defined in:
lib/f4r.rb

Overview

See CRC section in the FIT SDK for more info and CRC16 examples.

Constant Summary collapse

@@table =

CRC16 table

[
  0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401,
  0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400
].freeze

Class Method Summary collapse

Class Method Details

.crc(io) ⇒ crc

Compute checksum over given IO.

Parameters:

  • io (IO)

Returns:

  • (crc)

    crc Checksum of lower and upper four bits for all bytes in IO



678
679
680
681
682
683
684
685
686
# File 'lib/f4r.rb', line 678

def self.crc(io)
  crc = 0
  io.each_byte do |byte|
    [byte, (byte >> 4)].each do |sb|
      crc = ((crc >> 4) & 0x0FFF) ^ @@table[(crc ^ sb) & 0xF]
    end
  end
  crc
end