#!/usr/bin/env python
#
# Copyright 2004,2005,2006,2007 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., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
# 


from gnuradio import gr, gru
from gnuradio import eng_notation
from gnuradio.eng_option import eng_option
from gnuradio.wxgui import stdgui, fftsink, waterfallsink, scopesink, form, slider
from optparse import OptionParser
import wx
import sys

from gnuradio import cxadc


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

        self.frame = frame
        self.panel = panel
        
        usage="%prog: [options]"        
        parser = OptionParser(option_class=eng_option, usage=usage) 
        parser.add_option("-n", "--frame-decim", type="int", default=1,
                          help="set oscope frame decimation factor to n [default=1]")
        parser.add_option("-v", "--v-scale", type="eng_float", default=10000,
                          help="set oscope initial V/div to SCALE [default=%default]")
        parser.add_option("-t", "--t-scale", type="eng_float", default=49e-6,
                          help="set oscope initial s/div to SCALE [default=50us]")
        #parser.add_option( "-s","--input-shorts", action="store_true", default=False,
        #                  help="input interleaved shorts in stead of complex floats")
        (options, args) = parser.parse_args()
        if len(args) != 0:
            parser.print_help()
            raise SystemExit, 1

        self.show_debug_info = True
        
        # build the graph
        sampling_freq=27e6
        #// public shared_ptr constructor
        #cxadc_source_sptr
        #cxadc_make_source (double sampling_freq = 27e6,//sampling_freq is ignored at the moment. TODO: implement sampling_freq 
	#	    int do_avg_frame=1,
        #            double frame_avg_alpha=0.01,
	#	    const std::string dev = "/dev/cxadc") throw (std::runtime_error);
        if True: #options.input_shorts:
           self.cxadc=cxadc.source (sampling_freq,1,0.1,"/dev/cxadc")
           self.fsrc=gr.short_to_float()
           #self.csrc= gr.interleaved_short_to_complex()
           self.connect(self.cxadc,self.fsrc)	
        else:
           self.fsrc= cxadc.source_f (sampling_freq = sampling_freq,do_avg_frame=True,frame_avg_alpha=0.01,dev = "/dev/cxadc")

        input_rate = sampling_freq #100.0e6 #self.u.adc_freq() / self.u.decim_rate()

        self.scope = scopesink.scope_sink_f(self, panel, sample_rate=input_rate,
                                            frame_decim=options.frame_decim,
                                            v_scale=options.v_scale,
                                            t_scale=options.t_scale)
        self.connect(self.fsrc, self.scope)

        self._build_gui(vbox)
                        


    def _set_status_msg(self, msg):
        self.frame.GetStatusBar().SetStatusText(msg, 0)

    def _build_gui(self, vbox):            
        vbox.Add(self.scope.win, 10, wx.EXPAND)
        
        # add control area at the bottom
        #self.myform = myform = form.form()
        #hbox = wx.BoxSizer(wx.HORIZONTAL)
        #hbox.Add((5,0), 0, 0)
        #
        #vbox.Add(hbox, 0, wx.EXPAND)


def main ():
    app = stdgui.stdapp(app_flow_graph, "cxadc TV card O'scope", nstatus=1)
    app.MainLoop()

if __name__ == '__main__':
    main ()

