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

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