Created
April 22, 2014 09:21
-
-
Save GeorgeDewar/11171561 to your computer and use it in GitHub Desktop.
Checksum calculation example for IR codes of a Fujitsu heat pump (remote AR-RAH1E)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sample data | |
data = [] | |
data[0] = '00101000110001100000000000001000 00001000011111111001000000001100 00001101100000001000000000000000 00000000000000000000010001111111' | |
data[1] = '00101000110001100000000000001000 00001000011111111001000000001100 00000011100000001000000000000000 00000000000000000000010001110111' | |
data[2] = '00101000110001100000000000001000 00001000011111111001000000001100 00001101100011001000000000000000 00001011111110010000010011111010' | |
data[3] = '00101000110001100000000000001000 00001000011111111001000000001100 00001101100000001000100000000000 00000000000000000000010001110111' | |
data[4] = '00101000110001100000000000001000 00001000011111111001000000001100 00001101000000000000100000000000 00000000000000000000010000001111' | |
# Checksum function | |
def checksum(data) | |
bytes = data.gsub(/ /, '').scan(/.{8}/) | |
bytes = bytes[8..14] | |
numbers = bytes.map { |byte| byte.reverse.to_i(2) } | |
sum = numbers.inject { |sum, x| sum + x } | |
(208 - sum) % 256 | |
end | |
data.each do |sample| | |
actualChecksum = sample.gsub(/ /, '').scan(/.{8}/).last | |
calculatedChecksum = checksum(sample).to_s(2).rjust(8, '0').reverse | |
diff = actualChecksum.reverse.to_i(2) - calculatedChecksum.reverse.to_i(2) | |
puts "Calculated: #{calculatedChecksum}, Actual: #{actualChecksum}, Diff: #{diff}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I hate this kind of stuff too. people write stuff in some garbage language like python or go or rust, just looks like !^!@&$^!$^#%&#^@&%*^ and spits out a result and be like "ok just use this"
Nah how about explaining the actual algorithm so we don't have to guess what stuff like
bytes = data.gsub(/ /, '').scan(/.{8}/)
means.god damn.