Neato XV-11: Difference between revisions
| Line 423: | Line 423: | ||
The algorithm is as follows, provided that `data` is the list of the 20 first bytes, in the same order they arrived in. | The algorithm is as follows, provided that `data` is the list of the 20 first bytes, in the same order they arrived in. | ||
<br /> | |||
def checksum(data): | def checksum(data): | ||
“”“Compute and return the checksum as an int.”“” | “”“Compute and return the checksum as an int.”“” | ||
# group the data by word, little-endian | # group the data by word, little-endian | ||
data_list = [] | data_list = [] | ||
for t in range(10): | for t in range(10): | ||
data_list.append( data2*t + (data2*t+1<<8) ) | data_list.append( data2*t + (data2*t+1<<8) ) | ||
# compute the checksum on 32 bits | # compute the checksum on 32 bits | ||
chk32 = 0 | chk32 = 0 | ||
for d in data_list: | for d in data_list: | ||
chk32 = (chk32 << 1) + d | chk32 = (chk32 << 1) + d | ||
# return a value wrapped around on 15bits, and truncated to still fit into 15 bits | # return a value wrapped around on 15bits, and truncated to still fit into 15 bits | ||
checksum = (chk32 & 0x7FFF) + ( chk32 >> 15 ) # wrap around to fit into 15 bits | checksum = (chk32 & 0x7FFF) + ( chk32 >> 15 ) # wrap around to fit into 15 bits | ||
checksum = checksum & 0x7FFF # truncate to 15 bits | checksum = checksum & 0x7FFF # truncate to 15 bits | ||
return int( checksum ) | return int( checksum ) | ||
<br /> | <br /> | ||