/
byte2sym.py
347 lines (293 loc) · 14.4 KB
/
byte2sym.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# SPDX-License-Identifier: GPL-3.0
#
# GNU Radio Python Flow Graph
# Title: Byte to Float Symbols
# Author: Peter Mathys
# Description: Conversion from bytes to M-ary symbols
# GNU Radio version: 3.8.1.0
from distutils.version import StrictVersion
if __name__ == '__main__':
import ctypes
import sys
if sys.platform.startswith('linux'):
try:
x11 = ctypes.cdll.LoadLibrary('libX11.so')
x11.XInitThreads()
except:
print("Warning: failed to XInitThreads()")
from PyQt5 import Qt
from PyQt5.QtCore import QObject, pyqtSlot
from gnuradio import qtgui
from gnuradio.filter import firdes
import sip
from gnuradio import blocks
from gnuradio import digital
from gnuradio import gr
import sys
import signal
from argparse import ArgumentParser
from gnuradio.eng_arg import eng_float, intx
from gnuradio import eng_notation
from gnuradio.qtgui import Range, RangeWidget
import pmt
from gnuradio import qtgui
class byte2sym(gr.top_block, Qt.QWidget):
def __init__(self, bit_endianess=1, bits_to_use_per_byte_mask=255, gain=1):
gr.top_block.__init__(self, "Byte to Float Symbols")
Qt.QWidget.__init__(self)
self.setWindowTitle("Byte to Float Symbols")
qtgui.util.check_set_qss()
try:
self.setWindowIcon(Qt.QIcon.fromTheme('gnuradio-grc'))
except:
pass
self.top_scroll_layout = Qt.QVBoxLayout()
self.setLayout(self.top_scroll_layout)
self.top_scroll = Qt.QScrollArea()
self.top_scroll.setFrameStyle(Qt.QFrame.NoFrame)
self.top_scroll_layout.addWidget(self.top_scroll)
self.top_scroll.setWidgetResizable(True)
self.top_widget = Qt.QWidget()
self.top_scroll.setWidget(self.top_widget)
self.top_layout = Qt.QVBoxLayout(self.top_widget)
self.top_grid_layout = Qt.QGridLayout()
self.top_layout.addLayout(self.top_grid_layout)
self.settings = Qt.QSettings("GNU Radio", "byte2sym")
try:
if StrictVersion(Qt.qVersion()) < StrictVersion("5.0.0"):
self.restoreGeometry(self.settings.value("geometry").toByteArray())
else:
self.restoreGeometry(self.settings.value("geometry"))
except:
pass
##################################################
# Parameters
##################################################
self.bit_endianess = bit_endianess
self.bits_to_use_per_byte_mask = bits_to_use_per_byte_mask
self.gain = gain
##################################################
# Variables
##################################################
self.bits_per_symbol = bits_per_symbol = 1
self.tag = tag = gr.tag_utils.python_to_tag((0, pmt.intern("Z"), pmt.intern("0x5a"), pmt.intern("Vsrc")))
self.samp_rate = samp_rate = 32000
self.polar = polar = 1
self.invert = invert = 0
self.baud_delay = baud_delay = 0
self.M = M = 2**bits_per_symbol
##################################################
# Blocks
##################################################
# Create the options list
self._polar_options = (1, 1, )
# Create the labels list
self._polar_labels = ('unipolar', 'polar', )
# Create the combo box
self._polar_tool_bar = Qt.QToolBar(self)
self._polar_tool_bar.addWidget(Qt.QLabel('polar' + ": "))
self._polar_combo_box = Qt.QComboBox()
self._polar_tool_bar.addWidget(self._polar_combo_box)
for _label in self._polar_labels: self._polar_combo_box.addItem(_label)
self._polar_callback = lambda i: Qt.QMetaObject.invokeMethod(self._polar_combo_box, "setCurrentIndex", Qt.Q_ARG("int", self._polar_options.index(i)))
self._polar_callback(self.polar)
self._polar_combo_box.currentIndexChanged.connect(
lambda i: self.set_polar(self._polar_options[i]))
# Create the radio buttons
self.top_grid_layout.addWidget(self._polar_tool_bar)
# Create the options list
self._invert_options = (0, 1, )
# Create the labels list
self._invert_labels = ('No Invert', 'Invert', )
# Create the combo box
self._invert_tool_bar = Qt.QToolBar(self)
self._invert_tool_bar.addWidget(Qt.QLabel('Invert Bits' + ": "))
self._invert_combo_box = Qt.QComboBox()
self._invert_tool_bar.addWidget(self._invert_combo_box)
for _label in self._invert_labels: self._invert_combo_box.addItem(_label)
self._invert_callback = lambda i: Qt.QMetaObject.invokeMethod(self._invert_combo_box, "setCurrentIndex", Qt.Q_ARG("int", self._invert_options.index(i)))
self._invert_callback(self.invert)
self._invert_combo_box.currentIndexChanged.connect(
lambda i: self.set_invert(self._invert_options[i]))
# Create the radio buttons
self.top_grid_layout.addWidget(self._invert_tool_bar)
self._bits_per_symbol_range = Range(1, 256, 1, 1, 256)
self._bits_per_symbol_win = RangeWidget(self._bits_per_symbol_range, self.set_bits_per_symbol, 'Bits per symbol', "counter_slider", int)
self.top_grid_layout.addWidget(self._bits_per_symbol_win)
self._baud_delay_range = Range(0, 8, 1, 0, 8)
self._baud_delay_win = RangeWidget(self._baud_delay_range, self.set_baud_delay, 'Baud delay', "counter_slider", int)
self.top_grid_layout.addWidget(self._baud_delay_win)
self.qtgui_time_sink_x_0 = qtgui.time_sink_f(
1024, #size
samp_rate, #samp_rate
"", #name
1 #number of inputs
)
self.qtgui_time_sink_x_0.set_update_time(0.10)
self.qtgui_time_sink_x_0.set_y_axis(-1, 1)
self.qtgui_time_sink_x_0.set_y_label('Amplitude', "")
self.qtgui_time_sink_x_0.enable_tags(True)
self.qtgui_time_sink_x_0.set_trigger_mode(qtgui.TRIG_MODE_TAG, qtgui.TRIG_SLOPE_POS, 0.0, 0, 0, "Z")
self.qtgui_time_sink_x_0.enable_autoscale(True)
self.qtgui_time_sink_x_0.enable_grid(False)
self.qtgui_time_sink_x_0.enable_axis_labels(True)
self.qtgui_time_sink_x_0.enable_control_panel(True)
self.qtgui_time_sink_x_0.enable_stem_plot(False)
labels = ['', '', '', '', '',
'', '', '', '', '']
widths = [1, 1, 1, 1, 1,
1, 1, 1, 1, 1]
colors = ['blue', 'red', 'green', 'black', 'cyan',
'magenta', 'yellow', 'dark red', 'dark green', 'dark blue']
alphas = [1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0]
styles = [1, 1, 1, 1, 1,
1, 1, 1, 1, 1]
markers = [0, -1, -1, -1, -1,
-1, -1, -1, -1, -1]
for i in range(1):
if len(labels[i]) == 0:
self.qtgui_time_sink_x_0.set_line_label(i, "Data {0}".format(i))
else:
self.qtgui_time_sink_x_0.set_line_label(i, labels[i])
self.qtgui_time_sink_x_0.set_line_width(i, widths[i])
self.qtgui_time_sink_x_0.set_line_color(i, colors[i])
self.qtgui_time_sink_x_0.set_line_style(i, styles[i])
self.qtgui_time_sink_x_0.set_line_marker(i, markers[i])
self.qtgui_time_sink_x_0.set_line_alpha(i, alphas[i])
self._qtgui_time_sink_x_0_win = sip.wrapinstance(self.qtgui_time_sink_x_0.pyqwidget(), Qt.QWidget)
self.top_grid_layout.addWidget(self._qtgui_time_sink_x_0_win)
self.digital_map_bb_1_0 = digital.map_bb(invert*list(2**8-1-i for i in range(2**8))+(1-invert)*list(range(2**8)))
self.digital_map_bb_1 = digital.map_bb(invert*list(2**8-1-i for i in range(2**8))+(1-invert)*list(range(2**8)))
self.blocks_vector_source_x_0 = blocks.vector_source_b(list(ord(i) for i in 'Zombie'), True, 1, [tag])
self.blocks_unpacked_to_packed_xx_0 = blocks.unpacked_to_packed_bb(bits_per_symbol, gr.GR_LSB_FIRST)
self.blocks_throttle_0 = blocks.throttle(gr.sizeof_float*1, samp_rate,True)
self.blocks_packed_to_unpacked_xx_0 = blocks.packed_to_unpacked_bb(bits_per_symbol, gr.GR_LSB_FIRST)
self.blocks_multiply_const_vxx_0 = blocks.multiply_const_ff(gain)
self.blocks_float_to_char_0 = blocks.float_to_char(1, polar*0.5+(1-polar))
self.blocks_file_sink_0 = blocks.file_sink(gr.sizeof_char*1, '/dev/pts/1', False)
self.blocks_file_sink_0.set_unbuffered(False)
self.blocks_delay_0 = blocks.delay(gr.sizeof_char*1, baud_delay)
self.blocks_char_to_float_0 = blocks.char_to_float(1, polar*0.5+(1-polar))
self.blocks_and_const_xx_0_0 = blocks.and_const_bb(bits_to_use_per_byte_mask)
self.blocks_and_const_xx_0 = blocks.and_const_bb(bits_to_use_per_byte_mask)
self.blocks_add_const_vxx_1 = blocks.add_const_ff(polar*(M-1)-(1-polar)*0)
self.blocks_add_const_vxx_0 = blocks.add_const_ff(-polar*(M-1)+(1-polar)*0)
##################################################
# Connections
##################################################
self.connect((self.blocks_add_const_vxx_0, 0), (self.blocks_throttle_0, 0))
self.connect((self.blocks_add_const_vxx_1, 0), (self.blocks_float_to_char_0, 0))
self.connect((self.blocks_and_const_xx_0, 0), (self.digital_map_bb_1, 0))
self.connect((self.blocks_and_const_xx_0_0, 0), (self.blocks_file_sink_0, 0))
self.connect((self.blocks_char_to_float_0, 0), (self.blocks_add_const_vxx_0, 0))
self.connect((self.blocks_delay_0, 0), (self.blocks_unpacked_to_packed_xx_0, 0))
self.connect((self.blocks_float_to_char_0, 0), (self.blocks_delay_0, 0))
self.connect((self.blocks_multiply_const_vxx_0, 0), (self.blocks_add_const_vxx_1, 0))
self.connect((self.blocks_packed_to_unpacked_xx_0, 0), (self.blocks_char_to_float_0, 0))
self.connect((self.blocks_throttle_0, 0), (self.blocks_multiply_const_vxx_0, 0))
self.connect((self.blocks_throttle_0, 0), (self.qtgui_time_sink_x_0, 0))
self.connect((self.blocks_unpacked_to_packed_xx_0, 0), (self.digital_map_bb_1_0, 0))
self.connect((self.blocks_vector_source_x_0, 0), (self.blocks_and_const_xx_0, 0))
self.connect((self.digital_map_bb_1, 0), (self.blocks_packed_to_unpacked_xx_0, 0))
self.connect((self.digital_map_bb_1_0, 0), (self.blocks_and_const_xx_0_0, 0))
def closeEvent(self, event):
self.settings = Qt.QSettings("GNU Radio", "byte2sym")
self.settings.setValue("geometry", self.saveGeometry())
event.accept()
def get_bit_endianess(self):
return self.bit_endianess
def set_bit_endianess(self, bit_endianess):
self.bit_endianess = bit_endianess
def get_bits_to_use_per_byte_mask(self):
return self.bits_to_use_per_byte_mask
def set_bits_to_use_per_byte_mask(self, bits_to_use_per_byte_mask):
self.bits_to_use_per_byte_mask = bits_to_use_per_byte_mask
self.blocks_and_const_xx_0.set_k(self.bits_to_use_per_byte_mask)
self.blocks_and_const_xx_0_0.set_k(self.bits_to_use_per_byte_mask)
def get_gain(self):
return self.gain
def set_gain(self, gain):
self.gain = gain
self.blocks_multiply_const_vxx_0.set_k(self.gain)
def get_bits_per_symbol(self):
return self.bits_per_symbol
def set_bits_per_symbol(self, bits_per_symbol):
self.bits_per_symbol = bits_per_symbol
self.set_M(2**self.bits_per_symbol)
def get_tag(self):
return self.tag
def set_tag(self, tag):
self.tag = tag
self.blocks_vector_source_x_0.set_data(list(ord(i) for i in 'Zombie'), [self.tag])
def get_samp_rate(self):
return self.samp_rate
def set_samp_rate(self, samp_rate):
self.samp_rate = samp_rate
self.blocks_throttle_0.set_sample_rate(self.samp_rate)
self.qtgui_time_sink_x_0.set_samp_rate(self.samp_rate)
def get_polar(self):
return self.polar
def set_polar(self, polar):
self.polar = polar
self._polar_callback(self.polar)
self.blocks_add_const_vxx_0.set_k(-self.polar*(self.M-1)+(1-self.polar)*0)
self.blocks_add_const_vxx_1.set_k(self.polar*(self.M-1)-(1-self.polar)*0)
self.blocks_char_to_float_0.set_scale(self.polar*0.5+(1-self.polar))
self.blocks_float_to_char_0.set_scale(self.polar*0.5+(1-self.polar))
def get_invert(self):
return self.invert
def set_invert(self, invert):
self.invert = invert
self._invert_callback(self.invert)
def get_baud_delay(self):
return self.baud_delay
def set_baud_delay(self, baud_delay):
self.baud_delay = baud_delay
self.blocks_delay_0.set_dly(self.baud_delay)
def get_M(self):
return self.M
def set_M(self, M):
self.M = M
self.blocks_add_const_vxx_0.set_k(-self.polar*(self.M-1)+(1-self.polar)*0)
self.blocks_add_const_vxx_1.set_k(self.polar*(self.M-1)-(1-self.polar)*0)
def argument_parser():
description = 'Conversion from bytes to M-ary symbols'
parser = ArgumentParser(description=description)
parser.add_argument(
"--bit-endianess", dest="bit_endianess", type=intx, default=1,
help="Set LSB first [default=%(default)r]")
parser.add_argument(
"--bits-to-use-per-byte-mask", dest="bits_to_use_per_byte_mask", type=intx, default=255,
help="Set Bits to use per byte mask [default=%(default)r]")
parser.add_argument(
"--gain", dest="gain", type=intx, default=1,
help="Set Gain [default=%(default)r]")
return parser
def main(top_block_cls=byte2sym, options=None):
if options is None:
options = argument_parser().parse_args()
if StrictVersion("4.5.0") <= StrictVersion(Qt.qVersion()) < StrictVersion("5.0.0"):
style = gr.prefs().get_string('qtgui', 'style', 'raster')
Qt.QApplication.setGraphicsSystem(style)
qapp = Qt.QApplication(sys.argv)
tb = top_block_cls(bit_endianess=options.bit_endianess, bits_to_use_per_byte_mask=options.bits_to_use_per_byte_mask, gain=options.gain)
tb.start()
tb.show()
def sig_handler(sig=None, frame=None):
Qt.QApplication.quit()
signal.signal(signal.SIGINT, sig_handler)
signal.signal(signal.SIGTERM, sig_handler)
timer = Qt.QTimer()
timer.start(500)
timer.timeout.connect(lambda: None)
def quitting():
tb.stop()
tb.wait()
qapp.aboutToQuit.connect(quitting)
qapp.exec_()
if __name__ == '__main__':
main()