Skip to content

Instantly share code, notes, and snippets.

@atx
Created September 20, 2014 12:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atx/c437c65af2501acda9c7 to your computer and use it in GitHub Desktop.
Save atx/c437c65af2501acda9c7 to your computer and use it in GitHub Desktop.
#! /usr/bin/python3
import struct
import sys
def to_int(l, signed=False):
ret = 0
for x in l[1:] if signed else l:
ret = (ret << 1) | x
if signed and l[0]:
ret -= pow(2, len(l) - 1)
return ret
def calc_cksum(p):
base = 0b1111
for x in range(4, len(p), 4):
base += to_int(p[x:x + 4])
return base & 0b1111
def print_packet(p):
print(p, end="")
data = to_int(p[-16:-4], signed=True)
cks = to_int(p[0:4])
ecks = calc_cksum(p)
print("\t%d\t%d\t%d" % (data, cks, ecks), flush=True)
silence = 0
packet = []
while True:
sample = struct.unpack("h", sys.stdin.buffer.read(2))[0]
value = False if abs(sample) < 13000 else True
if value and silence != 0:
if silence < 150:
packet.append(0)
elif silence < 250:
packet.append(1)
else:
if len(packet) == 28:
print_packet(packet)
packet.clear()
silence = 0
elif not value:
silence += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment