forked from Matioupi/realtime-adsb-out
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathADSBLowLevelEncoder.py
102 lines (83 loc) · 3.06 KB
/
ADSBLowLevelEncoder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from CustomDecorators import *
import numpy
@Singleton
class ADSBLowLevelEncoder:
"""
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 __init__(self):
self.adsb_frame_preamble = [0xA1,0x40]
self.adsb_frame_pause = [0]*70
###############################################################
# Further work on fork
# Copyright (C) 2017 David Robinson
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.
"""
manchester_encoded = []
# Encode byte
for i in range(7, -1, -1):
if self.extract_bit(byte, i):
manchester_encoded.extend([0,1])
else:
manchester_encoded.extend([1,0])
return manchester_encoded
def frame_1090es_ppm_modulate(self, even, odd = []):
"""
Args:
even and odd: The data frames that need to be converted to PPM
Returns:
The bytearray of the PPM data
"""
ppm = [ ]
length_even = len(even)
length_odd = len(odd)
if (length_even != 0):
ppm.extend(self.adsb_frame_pause) # pause
ppm.extend(self.adsb_frame_preamble) # preamble
for i in range(length_even):
word16 = numpy.packbits(self.manchester_encode(~even[i]))
ppm.extend(word16[0:2])
ppm.extend(self.adsb_frame_pause) # pause
if (length_odd != 0):
ppm.extend(self.adsb_frame_pause) # pause
ppm.extend(self.adsb_frame_preamble) # preamble
for i in range(length_odd):
word16 = numpy.packbits(self.manchester_encode(~odd[i]))
ppm.extend(word16[0:2])
ppm.extend(self.adsb_frame_pause) # pause
return bytearray(ppm)
def hackrf_raw_IQ_format(self, ppm):
"""
Args:
ppm: this is some data in ppm (pulse position modulation) which will be converted into
hackRF raw IQ sample format, ready to be broadcasted
Returns:
bytearray: containing the IQ data
"""
signal = []
bits = numpy.unpackbits(numpy.asarray(ppm, dtype=numpy.uint8))
for bit in bits:
if bit == 1:
I = 127
Q = 127
else:
I = 0
Q = 0
signal.append(I)
signal.append(Q)
return bytearray(signal)