#!/usr/bin/env python
##################################################
# Gnuradio Python Flow Graph
# Title: MLAB ADC Grabber
# Author: Jakub Kakona
# Description: Basic software for reading ADC data from ADCdual01A modules trought ML602 development board.
# Generated: Mon Jun  2 15:58:15 2014
##################################################

from datetime import datetime
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 fftsink2
from gnuradio.wxgui import numbersink2
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 MLAB_ADC_Grabber(grc_wxgui.top_block_gui):

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

        ##################################################
        # Variables
        ##################################################
        self.prefix = prefix = "/home/localuser/gnuradio/"
        self.samp_rate = samp_rate = 5e6
        self.recfile = recfile = prefix + datetime.now().strftime("%Y.%m.%d.%H.%M.%S") + ".dat"

        ##################################################
        # 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), "Time wiev")
        self.notebook_0.AddPage(grc_wxgui.Panel(self.notebook_0), "Frequency  wiev")
        self.notebook_0.AddPage(grc_wxgui.Panel(self.notebook_0), "numbers")
        self.Add(self.notebook_0)
        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=512,
                fft_rate=15,
                average=False,
                avg_alpha=None,
                title="ADC1 Waterfall Plot",
        )
        self.notebook_0.GetPage(0).GridAdd(self.wxgui_waterfallsink2_0_0.win, 1, 1, 1, 1)
        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).GridAdd(self.wxgui_waterfallsink2_0.win, 2, 1, 1, 1)
        self.wxgui_scopesink2_1 = scopesink2.scope_sink_f(
                self.notebook_0.GetPage(0).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(0).GridAdd(self.wxgui_scopesink2_1.win, 2, 2, 1, 1)
        self.wxgui_scopesink2_0 = scopesink2.scope_sink_f(
                self.notebook_0.GetPage(0).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(0).GridAdd(self.wxgui_scopesink2_0.win, 1, 2, 1, 1)
        self.wxgui_numbersink2_1 = numbersink2.number_sink_f(
                self.notebook_0.GetPage(2).GetWin(),
                unit="Units",
                minval=-100,
                maxval=100,
                factor=1.0,
                decimal_places=10,
                ref_level=0,
                sample_rate=samp_rate,
                number_rate=15,
                average=False,
                avg_alpha=None,
                label="Number Plot",
                peak_hold=False,
                show_gauge=True,
        )
        self.notebook_0.GetPage(2).Add(self.wxgui_numbersink2_1.win)
        self.wxgui_numbersink2_0 = numbersink2.number_sink_f(
                self.notebook_0.GetPage(2).GetWin(),
                unit="Units",
                minval=-100,
                maxval=100,
                factor=1.0,
                decimal_places=10,
                ref_level=0,
                sample_rate=samp_rate,
                number_rate=15,
                average=False,
                avg_alpha=None,
                label="Number Plot",
                peak_hold=False,
                show_gauge=True,
        )
        self.notebook_0.GetPage(2).Add(self.wxgui_numbersink2_0.win)
        self.wxgui_fftsink2_1 = fftsink2.fft_sink_c(
                self.notebook_0.GetPage(1).GetWin(),
                baseband_freq=0,
                y_per_div=10,
                y_divs=10,
                ref_level=0,
                ref_scale=2.0,
                sample_rate=samp_rate,
                fft_size=2048,
                fft_rate=15,
                average=False,
                avg_alpha=None,
                title="ADC 1 FFT Plot",
                peak_hold=False,
                win=window.hanning,
        )
        self.notebook_0.GetPage(1).Add(self.wxgui_fftsink2_1.win)
        self.wxgui_fftsink2_0 = fftsink2.fft_sink_c(
                self.notebook_0.GetPage(1).GetWin(),
                baseband_freq=0,
                y_per_div=10,
                y_divs=10,
                ref_level=0,
                ref_scale=2.0,
                sample_rate=samp_rate,
                fft_size=2048,
                fft_rate=15,
                average=False,
                avg_alpha=None,
                title="ADC 2 FFT Plot",
                peak_hold=False,
                win=window.hanning,
        )
        self.notebook_0.GetPage(1).Add(self.wxgui_fftsink2_0.win)
        self.dc_blocker_xx_1 = filter.dc_blocker_cc(32, True)
        self.dc_blocker_xx_0 = filter.dc_blocker_cc(32, True)
        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_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, "/dev/xillybus_data2_r", False)

        ##################################################
        # 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.dc_blocker_xx_0, 0))
        self.connect((self.dc_blocker_xx_0, 0), (self.wxgui_waterfallsink2_0_0, 0))
        self.connect((self.blocks_float_to_complex_0, 0), (self.dc_blocker_xx_1, 0))
        self.connect((self.blocks_float_to_complex_0_0, 0), (self.wxgui_fftsink2_1, 0))
        self.connect((self.blocks_float_to_complex_0, 0), (self.wxgui_fftsink2_0, 0))
        self.connect((self.blocks_short_to_float_0, 0), (self.wxgui_numbersink2_1, 0))
        self.connect((self.blocks_short_to_float_1, 0), (self.wxgui_numbersink2_0, 0))
        self.connect((self.dc_blocker_xx_1, 0), (self.wxgui_waterfallsink2_0, 0))


# QT sink close method reimplementation

    def get_prefix(self):
        return self.prefix

    def set_prefix(self, prefix):
        self.prefix = prefix
        self.set_recfile(self.prefix + datetime.now().strftime("%Y.%m.%d.%H.%M.%S") + ".dat")

    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.wxgui_scopesink2_1.set_sample_rate(self.samp_rate/2)
        self.wxgui_waterfallsink2_0_0.set_sample_rate(self.samp_rate)
        self.wxgui_scopesink2_0.set_sample_rate(self.samp_rate)
        self.wxgui_waterfallsink2_0.set_sample_rate(self.samp_rate)
        self.wxgui_fftsink2_0.set_sample_rate(self.samp_rate)
        self.wxgui_fftsink2_1.set_sample_rate(self.samp_rate)

    def get_recfile(self):
        return self.recfile

    def set_recfile(self, recfile):
        self.recfile = recfile

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()"
    parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
    (options, args) = parser.parse_args()
    tb = MLAB_ADC_Grabber()
    tb.Start(True)
    tb.Wait()