#!/usr/bin/env python
##################################################
# Gnuradio Python Flow Graph
# Title: Top Block
# Author: Jakub Kakona
# Generated: Sat May  3 15:10:31 2014
##################################################

from gnuradio import analog
from gnuradio import audio
from gnuradio import blocks
from gnuradio import eng_notation
from gnuradio import filter
from gnuradio import gr
from gnuradio import wxgui
from gnuradio.eng_option import eng_option
from gnuradio.fft import window
from gnuradio.filter import firdes
from gnuradio.wxgui import forms
from gnuradio.wxgui import scopesink2
from gnuradio.wxgui import waterfallsink2
from grc_gnuradio import wxgui as grc_wxgui
from optparse import OptionParser
import wx

class top_block(grc_wxgui.top_block_gui):

    def __init__(self):
        grc_wxgui.top_block_gui.__init__(self, title="Top Block")
        _icon_path = "/usr/local/share/icons/hicolor/32x32/apps/gnuradio-grc.png"
        self.SetIcon(wx.Icon(_icon_path, wx.BITMAP_TYPE_ANY))

        ##################################################
        # Variables
        ##################################################
        self.variable_slider_0 = variable_slider_0 = 1
        self.samp_rate = samp_rate = 5e6

        ##################################################
        # Blocks
        ##################################################
        self.notebook_0 = self.notebook_0 = wx.Notebook(self.GetWin(), style=wx.NB_TOP)
        self.notebook_0.AddPage(grc_wxgui.Panel(self.notebook_0), "RF signal")
        self.notebook_0.AddPage(grc_wxgui.Panel(self.notebook_0), "Scope")
        self.notebook_0.AddPage(grc_wxgui.Panel(self.notebook_0), "Demod")
        self.Add(self.notebook_0)
        _variable_slider_0_sizer = wx.BoxSizer(wx.VERTICAL)
        self._variable_slider_0_text_box = forms.text_box(
                parent=self.notebook_0.GetPage(2).GetWin(),
                sizer=_variable_slider_0_sizer,
                value=self.variable_slider_0,
                callback=self.set_variable_slider_0,
                label='variable_slider_0',
                converter=forms.float_converter(),
                proportion=0,
        )
        self._variable_slider_0_slider = forms.slider(
                parent=self.notebook_0.GetPage(2).GetWin(),
                sizer=_variable_slider_0_sizer,
                value=self.variable_slider_0,
                callback=self.set_variable_slider_0,
                minimum=0,
                maximum=10,
                num_steps=100,
                style=wx.SL_HORIZONTAL,
                cast=float,
                proportion=1,
        )
        self.notebook_0.GetPage(2).Add(_variable_slider_0_sizer)
        self.wxgui_waterfallsink2_2 = waterfallsink2.waterfall_sink_f(
                self.notebook_0.GetPage(2).GetWin(),
                baseband_freq=0,
                dynamic_range=100,
                ref_level=0,
                ref_scale=2.0,
                sample_rate=48e3,
                fft_size=512,
                fft_rate=15,
                average=False,
                avg_alpha=None,
                title="Demodulated Audio",
                win=window.hamming,
        )
        self.notebook_0.GetPage(2).Add(self.wxgui_waterfallsink2_2.win)
        self.wxgui_waterfallsink2_1 = waterfallsink2.waterfall_sink_c(
                self.notebook_0.GetPage(2).GetWin(),
                baseband_freq=0,
                dynamic_range=100,
                ref_level=0,
                ref_scale=2.0,
                sample_rate=500e3,
                fft_size=512,
                fft_rate=15,
                average=False,
                avg_alpha=None,
                title="IF Plot",
                win=window.blackmanharris,
        )
        self.notebook_0.GetPage(2).Add(self.wxgui_waterfallsink2_1.win)
        self.wxgui_waterfallsink2_0_0 = waterfallsink2.waterfall_sink_c(
                self.notebook_0.GetPage(0).GetWin(),
                baseband_freq=0,
                dynamic_range=100,
                ref_level=0,
                ref_scale=2.0,
                sample_rate=samp_rate,
                fft_size=4096,
                fft_rate=15,
                average=False,
                avg_alpha=None,
                title="ADC1 Waterfall Plot",
                size=(4096, 500),
        )
        self.notebook_0.GetPage(0).Add(self.wxgui_waterfallsink2_0_0.win)
        self.wxgui_waterfallsink2_0 = waterfallsink2.waterfall_sink_c(
                self.notebook_0.GetPage(0).GetWin(),
                baseband_freq=0,
                dynamic_range=100,
                ref_level=0,
                ref_scale=2.0,
                sample_rate=samp_rate,
                fft_size=512,
                fft_rate=15,
                average=False,
                avg_alpha=None,
                title="ADC2 Waterfall Plot",
                win=window.hanning,
        )
        self.notebook_0.GetPage(0).Add(self.wxgui_waterfallsink2_0.win)
        self.wxgui_scopesink2_1 = scopesink2.scope_sink_f(
                self.notebook_0.GetPage(1).GetWin(),
                title="Scope Plot",
                sample_rate=samp_rate/2,
                v_scale=0,
                v_offset=0,
                t_scale=1e-2,
                ac_couple=False,
                xy_mode=False,
                num_inputs=2,
                trig_mode=wxgui.TRIG_MODE_AUTO,
                y_axis_label="Counts",
        )
        self.notebook_0.GetPage(1).Add(self.wxgui_scopesink2_1.win)
        self.wxgui_scopesink2_0 = scopesink2.scope_sink_f(
                self.notebook_0.GetPage(1).GetWin(),
                title="Scope Plot",
                sample_rate=samp_rate,
                v_scale=0,
                v_offset=0,
                t_scale=0,
                ac_couple=False,
                xy_mode=False,
                num_inputs=4,
                trig_mode=wxgui.TRIG_MODE_AUTO,
                y_axis_label="Counts",
        )
        self.notebook_0.GetPage(1).Add(self.wxgui_scopesink2_0.win)
        self.rational_resampler_xxx_0 = filter.rational_resampler_fff(
                interpolation=48,
                decimation=500,
                taps=None,
                fractional_bw=None,
        )
        self.low_pass_filter_0 = filter.fir_filter_ccf(int(samp_rate/500e3), firdes.low_pass(
                1, samp_rate, 100e3, 1e6, firdes.WIN_HAMMING, 6.76))
        self.blocks_vector_to_streams_0 = blocks.vector_to_streams(gr.sizeof_short*1, 10)
        self.blocks_throttle_2 = blocks.throttle(gr.sizeof_short*10, samp_rate/2)
        self.blocks_short_to_float_2_0 = blocks.short_to_float(1, 1)
        self.blocks_short_to_float_2 = blocks.short_to_float(1, 1)
        self.blocks_short_to_float_1_0 = blocks.short_to_float(1, 1)
        self.blocks_short_to_float_1 = blocks.short_to_float(1, 1)
        self.blocks_short_to_float_0_0 = blocks.short_to_float(1, 1)
        self.blocks_short_to_float_0 = blocks.short_to_float(1, 1)
        self.blocks_multiply_const_vxx_0 = blocks.multiply_const_vff((variable_slider_0, ))
        self.blocks_interleave_0_2 = blocks.interleave(gr.sizeof_short*1)
        self.blocks_interleave_0_1 = blocks.interleave(gr.sizeof_short*1)
        self.blocks_interleave_0_0 = blocks.interleave(gr.sizeof_short*1)
        self.blocks_interleave_0 = blocks.interleave(gr.sizeof_short*1)
        self.blocks_float_to_complex_0_0 = blocks.float_to_complex(1)
        self.blocks_float_to_complex_0 = blocks.float_to_complex(1)
        self.blocks_file_source_0 = blocks.file_source(gr.sizeof_short*10, "/home/kaklik/GNU_radio/2014.04.15.17.02.48.dat", True)
        self.audio_sink_0 = audio.sink(48000, "", True)
        self.analog_wfm_rcv_0 = analog.wfm_rcv(
                quad_rate=500e3,
                audio_decimation=1,
        )

        ##################################################
        # Connections
        ##################################################
        self.connect((self.blocks_vector_to_streams_0, 2), (self.blocks_interleave_0_2, 0))
        self.connect((self.blocks_vector_to_streams_0, 3), (self.blocks_interleave_0_2, 1))
        self.connect((self.blocks_vector_to_streams_0, 4), (self.blocks_interleave_0_1, 0))
        self.connect((self.blocks_vector_to_streams_0, 5), (self.blocks_interleave_0_1, 1))
        self.connect((self.blocks_vector_to_streams_0, 6), (self.blocks_interleave_0_0, 0))
        self.connect((self.blocks_vector_to_streams_0, 7), (self.blocks_interleave_0_0, 1))
        self.connect((self.blocks_vector_to_streams_0, 8), (self.blocks_interleave_0, 0))
        self.connect((self.blocks_vector_to_streams_0, 9), (self.blocks_interleave_0, 1))
        self.connect((self.blocks_short_to_float_1, 0), (self.blocks_float_to_complex_0, 1))
        self.connect((self.blocks_short_to_float_0, 0), (self.blocks_float_to_complex_0, 0))
        self.connect((self.blocks_short_to_float_0_0, 0), (self.blocks_float_to_complex_0_0, 0))
        self.connect((self.blocks_short_to_float_1_0, 0), (self.blocks_float_to_complex_0_0, 1))
        self.connect((self.blocks_vector_to_streams_0, 0), (self.blocks_short_to_float_2, 0))
        self.connect((self.blocks_vector_to_streams_0, 1), (self.blocks_short_to_float_2_0, 0))
        self.connect((self.blocks_short_to_float_2, 0), (self.wxgui_scopesink2_1, 0))
        self.connect((self.blocks_short_to_float_2_0, 0), (self.wxgui_scopesink2_1, 1))
        self.connect((self.blocks_short_to_float_0_0, 0), (self.wxgui_scopesink2_0, 0))
        self.connect((self.blocks_short_to_float_1_0, 0), (self.wxgui_scopesink2_0, 1))
        self.connect((self.blocks_short_to_float_0, 0), (self.wxgui_scopesink2_0, 2))
        self.connect((self.blocks_short_to_float_1, 0), (self.wxgui_scopesink2_0, 3))
        self.connect((self.blocks_interleave_0_2, 0), (self.blocks_short_to_float_0_0, 0))
        self.connect((self.blocks_interleave_0_1, 0), (self.blocks_short_to_float_1_0, 0))
        self.connect((self.blocks_interleave_0_0, 0), (self.blocks_short_to_float_0, 0))
        self.connect((self.blocks_interleave_0, 0), (self.blocks_short_to_float_1, 0))
        self.connect((self.blocks_throttle_2, 0), (self.blocks_vector_to_streams_0, 0))
        self.connect((self.blocks_file_source_0, 0), (self.blocks_throttle_2, 0))
        self.connect((self.blocks_float_to_complex_0_0, 0), (self.wxgui_waterfallsink2_0_0, 0))
        self.connect((self.blocks_float_to_complex_0, 0), (self.wxgui_waterfallsink2_0, 0))
        self.connect((self.blocks_float_to_complex_0_0, 0), (self.low_pass_filter_0, 0))
        self.connect((self.low_pass_filter_0, 0), (self.analog_wfm_rcv_0, 0))
        self.connect((self.analog_wfm_rcv_0, 0), (self.rational_resampler_xxx_0, 0))
        self.connect((self.rational_resampler_xxx_0, 0), (self.blocks_multiply_const_vxx_0, 0))
        self.connect((self.blocks_multiply_const_vxx_0, 0), (self.audio_sink_0, 0))
        self.connect((self.low_pass_filter_0, 0), (self.wxgui_waterfallsink2_1, 0))
        self.connect((self.blocks_multiply_const_vxx_0, 0), (self.wxgui_waterfallsink2_2, 0))


# QT sink close method reimplementation

    def get_variable_slider_0(self):
        return self.variable_slider_0

    def set_variable_slider_0(self, variable_slider_0):
        self.variable_slider_0 = variable_slider_0
        self.blocks_multiply_const_vxx_0.set_k((self.variable_slider_0, ))
        self._variable_slider_0_slider.set_value(self.variable_slider_0)
        self._variable_slider_0_text_box.set_value(self.variable_slider_0)

    def get_samp_rate(self):
        return self.samp_rate

    def set_samp_rate(self, samp_rate):
        self.samp_rate = samp_rate
        self.blocks_throttle_2.set_sample_rate(self.samp_rate/2)
        self.low_pass_filter_0.set_taps(firdes.low_pass(1, self.samp_rate, 100e3, 1e6, firdes.WIN_HAMMING, 6.76))
        self.wxgui_scopesink2_0.set_sample_rate(self.samp_rate)
        self.wxgui_waterfallsink2_0.set_sample_rate(self.samp_rate)
        self.wxgui_scopesink2_1.set_sample_rate(self.samp_rate/2)
        self.wxgui_waterfallsink2_0_0.set_sample_rate(self.samp_rate)

if __name__ == '__main__':
    import ctypes
    import os
    if os.name == 'posix':
        try:
            x11 = ctypes.cdll.LoadLibrary('libX11.so')
            x11.XInitThreads()
        except:
            print "Warning: failed to XInitThreads()"
    parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
    (options, args) = parser.parse_args()
    tb = top_block()
    tb.Start(True)
    tb.Wait()