Package motors :: Module crc7
[hide private]
[frames] | no frames]

Source Code for Module motors.crc7

 1  # 
 2  # motor/pololu/crc7.py 
 3  # 
 4  # Basic algorithm thanks to Jean-Loup Le Roux 
 5  # https://code.google.com/p/robotter/source/browse/code/crc7.py?repo=charon 
 6  # 
 7  # Modifications by Carl J. Nobile 
 8  # 
 9   
10  CRC7_POLY = 0x91 
11   
12 -def byte_crc7(v):
13 """ 14 Compute CRC of a single byte. 15 """ 16 for i in range(8): 17 if v & 1: 18 v ^= CRC7_POLY 19 20 v >>= 1 21 22 return v
23 24 CRC7_TABLE = tuple(byte_crc7(i) for i in range(256)) 25
26 -def crc7(data):
27 """ 28 Compute CRC of a whole message. 29 """ 30 crc = 0 31 32 for c in data: 33 crc = CRC7_TABLE[crc ^ c] 34 35 return crc
36 37 38 if __name__ == '__main__': 39 import sys 40 41 if len(sys.argv) > 1: 42 data = sys.argv[1] 43 else: 44 data = sys.stdin.read() 45 46 data = [int(b) for b in data.replace('\n', ',').split(',') if b != ''] 47 print '\n{0:#x}, {0}'.format(crc7(data)) 48