#!/usr/bin/env python
#
# Copyright 2004 Free Software Foundation, Inc.
# 
# This file is part of GNU Radio
# 
# GNU Radio is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# 
# GNU Radio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with GNU Radio; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
# 


from gnuradio import gr
from gnuradio import eng_notation
from gnuradio.eng_option import eng_option
from gnuradio.wxgui import stdgui

# change according to your location of the file_hdr_source
from gnuradio import vircomm

from optparse import OptionParser
import wx
import random
import threading
import time

class random_hdr_gen (threading.Thread):
    def __init__(self, file_hdr_source_obj):
        threading.Thread.__init__(self)
        self.file_hdr_source_obj = file_hdr_source_obj
        self.keep_running = True
        self.start()

    def run(self):
        while(self.keep_running):
            # set a random size
            s = random.randint(1,10)

            t_list = []
            
            # generate list
            for i in range(s):
                t_list.append(random.randint(1,100))

            # convert list to tuple
            t = tuple(t_list)

            # print tuple
            print "LEN:",s,"TUPLE: ",t

            # OK, let's set it.
            self.file_hdr_source_obj.set_header(t)

            # ZZZ
            time.sleep(2)

class app_flow_graph (stdgui.gui_flow_graph):
    def __init__(self, frame, panel, vbox, argv):
        stdgui.gui_flow_graph.__init__ (self, frame, panel, vbox, argv)

        self.frame = frame
        self.panel = panel
        self.additional_decim = 1
        
        parser = OptionParser (option_class=eng_option)
        parser.add_option ("-d", "--decim", type="int", default=8,
                           help="set fgpa decimation rate to DECIM")
        parser.add_option ("-m", "--mux", type="intx", default=0x32103210,
                           help="set fpga FR_RX_MUX register to MUX")
        parser.add_option ("-f", "--freq", type="eng_float", default=55.25e6,
                           help="set Rx Frequency", metavar="FREQ")
        parser.add_option ("-g", "--usrp_gain", type="eng_float", default=20,
                           help="set Rx PGA gain in dB (default 0 dB)")
        parser.add_option ("-G", "--frontend_gain", type="eng_float", default=300,
                           help="set Frontend Gain")

        (options, args) = parser.parse_args ()

        ####################################################################################
        
        # Now create the vector (data) source
        data = (1,2,3,4,5,6,7,8)
        self.vsource = gr.vector_source_c(data,True);

        
        src_data = (1,2,0xFFFFFFFF)
        dst = vircomm.file_hdr_sink (src_data, gr.sizeof_gr_complex, "testfile")

        self.rhg = random_hdr_gen(dst)

        # connect it up
        self.connect(self.vsource, dst)


def main ():
    app = stdgui.stdapp (app_flow_graph, "USRP FFT")
    app.MainLoop ()

if __name__ == '__main__':
    main ()
