Class: Denko::OneWire::Helper
- Inherits:
-
Object
- Object
- Denko::OneWire::Helper
- Defined in:
- lib/denko/one_wire/helper.rb
Class Method Summary collapse
Class Method Details
.address_to_bytes(address) ⇒ Object
4 5 6 7 8 |
# File 'lib/denko/one_wire/helper.rb', line 4 def self.address_to_bytes(address) bytes = [] 8.times { |i| bytes[i] = address >> (8*i) & 0xFF } bytes end |
.calculate_crc(data) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/denko/one_wire/helper.rb', line 15 def self.calculate_crc(data) if data.class == Integer bytes = address_to_bytes(data) else bytes = data end crc = 0b00000000 bytes.take(bytes.length - 1).each do |byte| for bit in (0..7) xor = ((byte >> bit) & 0b1) ^ (crc & 0b1) crc = crc ^ ((xor * (2 ** 3)) | (xor * (2 ** 4))) crc = crc >> 1 crc = crc | (xor * (2 ** 7)) end end [crc, bytes.last] end |
.crc(data) ⇒ Object
10 11 12 13 |
# File 'lib/denko/one_wire/helper.rb', line 10 def self.crc(data) calculated, received = self.calculate_crc(data) calculated == received end |