-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.py
135 lines (112 loc) · 4.67 KB
/
decoder.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import numpy as np
from src.block import Block
from src.block import MetadataBlock
from src.ringbuffer import RingBuffer
from src.frame_detection import FrameDetection
from src.message import MessageModeA
class CIS200decoder(object):
def __init__(self):
# "7D12B0E6"
# 01111101000100101011000011100110
self.__start_pattern = np.array([0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1,
1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0])
self.__start_pattern_inv = 1 - self.__start_pattern
self.__mode_a_start = np.array([])
self.__mode_b_start = np.array([])
self.__block_length = 288
self.__ring_buffer = RingBuffer(buffer_length=self.__block_length)
self.__frame = FrameDetection(self.__start_pattern, self.__block_length, confidence=0.8)
self.__messages = []
self.__valid_block_dict = {}
self.__data = None
self.__blocks = None
self.__blocks_received = 0
self.__processing_done = False
self.__metadata_block = None
self.__metadata_block_found = None
self.ecc_lut = {}
def format_text(self):
print("\n\nDecode Messages:")
for message_id in range(self.__metadata_block.message_count):
try:
message = MessageModeA(self.__metadata_block, self.__valid_block_dict, message_id)
self.__messages.append(message)
except IndexError:
print("missing block -> ignore message {}".format(message_id + 1))
for mes in self.__messages:
print("message {}".format(mes.id))
print(mes.content + "\n")
def decode(self, bit):
self.__ring_buffer.add(bit)
# if self.__ring_buffer.is_full:
self.__frame.perform_detection(self.__ring_buffer.get_data)
if self.__frame.is_valid_frame:
# self.__ring_buffer.flush()
try:
if self.__frame.is_inverted:
self.__process_block(self.__ring_buffer.get_data, is_inverted=True)
else:
self.__process_block(self.__ring_buffer.get_data, is_inverted=False)
except ValueError:
pass
return self.__processing_done
def __process_block(self, data, is_inverted):
if is_inverted:
block = Block(1 - data)
else:
block = Block(data)
if block.is_crc_ok:
# # # # # # # ecc lut # # # # # # #
if block.index in self.ecc_lut:
self.ecc_lut[block.index].append(block.index_ecc)
else:
self.ecc_lut[block.index] = [block.index_ecc]
# # # # # # # # # # # # # # # # # #
try:
if block.is_index_ecc_correct:
if block.is_metadata_block:
if self.__metadata_block is None:
self.__process_metadata_block(block)
else:
if self.__metadata_block_found:
self.__process_message_block(block)
if self.__blocks_received == self.__metadata_block.block_count - 1:
self.__processing_done = True
self.format_text()
except IndexError:
print("unknown block ecc: \n\tindex: {}\n\tecc: {}".format(block.index, block.index_ecc))
return self.__processing_done
def __process_metadata_block(self, block):
self.__metadata_block = MetadataBlock(block)
self.__metadata_block_found = True
self.__create_block_dict(self.__metadata_block.block_count)
try:
self.__valid_block_dict[block.index] = block.original_data
self.__blocks_received += 1
except KeyError:
pass
def __process_message_block(self, block):
print("message block with index ", block.index)
try:
self.__valid_block_dict[block.index] = block.original_data
self.__blocks_received += 1
except KeyError:
pass
def __create_block_dict(self, block_count):
for index in range(1, block_count):
self.__valid_block_dict[index] = np.zeros(144, dtype=np.int)
def __mode_detection(self):
pass
if __name__ == "__main__":
import os
with open(os.path.join("test", "data.txt"), "r") as f:
data_in = np.array([int(x) for x in f.read().strip()])
test = CIS200decoder()
for i in data_in:
all_blocks_found = test.decode(i)
if all_blocks_found:
break
"""
for k, v in test.ecc_lut.items():
print(k, v)
"""