More refactoring to get things in own files to help my understanding

This commit is contained in:
nzkarit
2017-09-07 20:15:28 +12:00
parent 475bc5da83
commit f484c7eb63
4 changed files with 94 additions and 46 deletions

View File

@@ -6,6 +6,7 @@ from location import *
from conversions import *
from parity import *
from HackRF import HackRF
from PPM import PPM
import os
###############################################################
@@ -89,7 +90,7 @@ def df17_pos_rep_encode(ca, icao, tc, ss, nicsb, alt, time, lat, lon, surface):
return (df17_even_bytes, df17_odd_bytes)
def frame_1090es_ppm_modulate(even, odd):
"""def frame_1090es_ppm_modulate(even, odd):
ppm = [ ]
for i in range(48): # pause
@@ -121,7 +122,7 @@ def frame_1090es_ppm_modulate(even, odd):
#print '[{}]'.format(', '.join(hex(x) for x in ppm))
return bytearray(ppm)
"""
if __name__ == "__main__":
@@ -153,8 +154,8 @@ if __name__ == "__main__":
#print ''.join(format(x, '02x') for x in df17_even)
#print ''.join(format(x, '02x') for x in df17_odd)
df17_array = frame_1090es_ppm_modulate(df17_even, df17_odd)
ppm = PPM()
df17_array = ppm.frame_1090es_ppm_modulate(df17_even, df17_odd)
#OutFile = open("filename.bin", "wb")
#OutFile.write(df17_array)

43
Encoder.py Normal file
View File

@@ -0,0 +1,43 @@
class Encoder:
"""
Hamming and Manchester Encoding example
Author: Joel Addison
Date: March 2013
Functions to do (7,4) hamming encoding and decoding, including error detection
and correction.
Manchester encoding and decoding is also included, and by default will use
least bit ordering for the byte that is to be included in the array.
"""
def extract_bit(self, byte, pos):
"""
Extract a bit from a given byte using MS ordering.
ie. B7 B6 B5 B4 B3 B2 B1 B0
"""
return (byte >> pos) & 0x01
def manchester_encode(self, byte):
"""
Encode a byte using Manchester encoding. Returns an array of bits.
Adds two start bits (1, 1) and one stop bit (0) to the array.
"""
# Add start bits (encoded 1, 1)
# manchester_encoded = [0, 1, 0, 1]
manchester_encoded = []
# Encode byte
for i in range(7, -1, -1):
if self.extract_bit(byte, i):
manchester_encoded.append(0)
manchester_encoded.append(1)
else:
manchester_encoded.append(1)
manchester_encoded.append(0)
# Add stop bit (encoded 0)
# manchester_encoded.append(1)
# manchester_encoded.append(0)
return manchester_encoded

46
PPM.py Normal file
View File

@@ -0,0 +1,46 @@
import numpy
from Encoder import Encoder
class PPM:
"""The PPM class contains functions about PPM manipulation
"""
def frame_1090es_ppm_modulate(self, even, odd):
"""
Args:
even and odd: The bits you would converted to PPM
Returns:
The bytearray of the PPM data
"""
ppm = [ ]
encoder = Encoder()
for i in range(48): # pause
ppm.append( 0 )
ppm.append( 0xA1 ) # preamble
ppm.append( 0x40 )
for i in range(len(even)):
word16 = numpy.packbits(encoder.manchester_encode(~even[i]))
ppm.append(word16[0])
ppm.append(word16[1])
for i in range(100): # pause
ppm.append( 0 )
ppm.append( 0xA1 ) # preamble
ppm.append( 0x40 )
for i in range(len(odd)):
word16 = numpy.packbits(encoder.manchester_encode(~odd[i]))
ppm.append(word16[0])
ppm.append(word16[1])
for i in range(48): # pause
ppm.append( 0 )
#print '[{}]'.format(', '.join(hex(x) for x in ppm))
return bytearray(ppm)

View File

@@ -1,42 +0,0 @@
"""
Hamming and Manchester Encoding example
Author: Joel Addison
Date: March 2013
Functions to do (7,4) hamming encoding and decoding, including error detection
and correction.
Manchester encoding and decoding is also included, and by default will use
least bit ordering for the byte that is to be included in the array.
"""
def extract_bit(byte, pos):
"""
Extract a bit from a given byte using MS ordering.
ie. B7 B6 B5 B4 B3 B2 B1 B0
"""
return (byte >> pos) & 0x01
def manchester_encode(byte):
"""
Encode a byte using Manchester encoding. Returns an array of bits.
Adds two start bits (1, 1) and one stop bit (0) to the array.
"""
# Add start bits (encoded 1, 1)
# manchester_encoded = [0, 1, 0, 1]
manchester_encoded = []
# Encode byte
for i in range(7, -1, -1):
if extract_bit(byte, i):
manchester_encoded.append(0)
manchester_encoded.append(1)
else:
manchester_encoded.append(1)
manchester_encoded.append(0)
# Add stop bit (encoded 0)
# manchester_encoded.append(1)
# manchester_encoded.append(0)
return manchester_encoded