#!/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, fftsink
from optparse import OptionParser
import wx
import time
import threading

from gnuradio import vircomm

class data_printer (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):
            data = self.file_hdr_source_obj.query_header()
            print "HDR DATA:",  data

            time.sleep(0.5)

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
        
        parser = OptionParser (option_class=eng_option)
        parser.add_option ("-d", "--decim", type="int", default=16,
                           help="set fgpa decimation rate to DECIM")
        parser.add_option ("-f", "--filename", type="string", default="usrp_rx.dat",
                           help="specify a filename to read")
        parser.add_option ("-r", "--repeat", action="store_true",default=False)
        parser.add_option ("-a", "--avg", type="int", default=10,
                           help="set num ffts to average over (default 10)")

        (options, args) = parser.parse_args ()


        # get data from file
        self.file_src = vircomm.file_hdr_source(gr.sizeof_gr_complex, options.filename)
        self.decim = options.decim

        #input_rate = self.file_src.adc_freq () / self.file_src.decim_rate ()
        input_rate = 64e6 / options.decim
        
        navg_frames = 1

        self.dp = data_printer(self.file_src)
        
        # make a fft sink

        ymin=20
        ymax=100


        block = fftsink.fft_sink_c(self, panel, title="example fft", fft_size=512, sample_rate=input_rate,
                                   y_per_div=(ymax - ymin)/8, ref_level=ymax,
                                   average=True, avg_alpha=(0.05), baseband_freq=0)

        fft_win = block.win

        vbox.Add (fft_win, 1, wx.EXPAND)
        self.connect(self.file_src, block)

        # build small control area at bottom
        hbox = wx.BoxSizer (wx.HORIZONTAL)
        hbox.Add ((1, 1), 1, wx.EXPAND)
        hbox.Add (wx.StaticText (panel, -1, "Set ddc freq: "), 0, wx.ALIGN_CENTER)
        self.tc_freq = wx.TextCtrl (panel, -1, "", style=wx.TE_PROCESS_ENTER)
        hbox.Add (self.tc_freq, 0, wx.ALIGN_CENTER)
        wx.EVT_TEXT_ENTER (self.tc_freq, self.tc_freq.GetId(), self.handle_text_enter)
        hbox.Add ((1, 1), 1, wx.EXPAND)
        # add it to the main vbox
        vbox.Add (hbox, 0, wx.EXPAND)

        self.update_status_bar ()

    def handle_text_enter (self, event):
        str = event.GetString ()
        self.tc_freq.Clear ()
        self.u.set_rx_freq (0, eng_notation.str_to_num (str))
        self.update_status_bar ()

    def update_status_bar (self):
        #ddc_freq = self.u.rx_freq (0)
        #decim_rate = self.u.decim_rate ()
        #sample_rate = self.u.adc_freq () / decim_rate
        ddc_freq = 0
        decim_rate = 0
        sample_rate = 0

        msg = "decim: %d  %sS/s  DDC: %s" % (
            decim_rate,
            eng_notation.num_to_str (sample_rate),
            eng_notation.num_to_str (ddc_freq))
            
        self.frame.GetStatusBar().SetStatusText (msg, 1)

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

if __name__ == '__main__':
    main ()



