-
-
Save Yinnon-Haviv/bdedb8b6cc541036eff3ae18f94c7735 to your computer and use it in GitHub Desktop.
Checks that the encoding described in in the following link is resilient to rotations. https://github.com/polyvision/EasyRaceLapTimer/blob/master/docs/ir_pulses.md
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
def encode(n): | |
if (n > 63) | (n < 0): | |
raise Exception('Out of range {0}'.format(n)) | |
ret = bin(n)[2:] # [2:] to chop off the "0b" part # creating initial bit encoding (for bits 3-8) | |
ret += reduce(lambda a,b: '1' if (a != b) else '0', ret) # adding checksum as bit 9 | |
pad = ''.join(['0'] * (9 - len(ret))) | |
return pad + ret # padding with leading zeros by spec (for bits 1-2 and maybe more for lower ids) | |
def decode(arr): | |
if (len(arr) != 9): | |
raise Exception('Should be of size 9, got: {0}'.format(arr)) | |
if (arr[0] != '0') or (arr[1] != '0'): | |
raise Exception('Illegal header, got: {0}'.format(arr)) | |
if (reduce(lambda a,b: '1' if (a != b) else '0', arr[2:-1]) != arr[-1]): | |
raise Exception('Illegal checksum, got: {0}'.format(arr)) | |
ret = 0 | |
for bit in arr[2:-1]: | |
ret = (ret << 1) | (bit == '1') | |
return ret | |
def rotate(l,n): | |
return l[n:] + l[:n] | |
legal = frozenset(map(encode, range(0,63))) | |
colliding = set() | |
for x in legal: | |
for rot in range(1,8): | |
if rotate(x, rot) in legal: | |
print 'Found that {0} in rotation {1} is equal to {2}'.format(x, rot, rotate(x, rot)) | |
colliding.add(x) | |
print('Ids which don’t collide: {0}'.format(map(decode, legal.difference(colliding)))) | |
print('Encodings which collide: {0}'.format(colliding)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment