#!/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.
# 

"""
Digital oscilloscope that uses a USRP as the source of complex samples.
"""
import wxversion
#wxversion.select('2.6') #uncomment this if you have multiple versions of wxpython installed

from gnuradio import gr, gru
from gnuradio import usrp
from gnuradio.eng_option import eng_option
from gnuradio.wxgui import stdgui, fftsink, scopesink
from optparse import OptionParser
import wx

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)
       
        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("-c", "--cordic-freq", type="eng_float", default=0,
                          help="set Rx cordic frequency to FREQ", metavar="FREQ")
        parser.add_option("-m", "--mux", type="intx", default=0x32103210,
                          help="set fpga FR_RX_MUX register to MUX")
        parser.add_option("-n", "--nchan", type="intx", default=1,
                          help="set nchannels to NCHAN")
        parser.add_option("-g", "--gain", type="eng_float", default=0,
                          help="set Rx PGA gain in dB (default 0 dB)")
        parser.add_option("-b", "--bitdepth", type="intx", default=16,
                          help="set usrp bitdepth (16 or 8, default=16)")
        (options, args) = parser.parse_args ()

        mode=usrp.FPGA_MODE_NORMAL
        if (options.bitdepth==8):
          mode=mode | usrp.FPGA_MODE_TRUNC_TO_8BIT
        u = usrp.source_c (0, options.decim, options.nchan, gru.hexint(options.mux), mode )
        u.set_rx_freq (0, options.cordic_freq)
        u.set_pga (0, options.gain)
        u.set_pga (1, options.gain)

        input_rate = u.adc_freq () / u.decim_rate ()

        # You can add this throttle block so that this app doesn't suck down
        # all the CPU available.  You normally wouldn't use it...
        #throttle = gr.throttle(gr.sizeof_gr_complex, input_rate)

        scope = scopesink.scope_sink_c (self, panel, "Secret Data", sample_rate=input_rate)
        vbox.Add (scope.win, 1, wx.EXPAND)

        # wire the blocks together
        #self.connect (u, throttle, scope)
        self.connect (u, scope)

def main ():
    app = stdgui.stdapp (app_flow_graph, "USRP complex O'scope")
    app.MainLoop ()

if __name__ == '__main__':
    main ()

