#!/usr/bin/env python

from gnuradio import gr, gru, eng_notation, optfir
from gnuradio import audio
from gnuradio import cxadc
from gnuradio import usrp
from gnuradio import blks
from gnuradio.eng_option import eng_option
from gnuradio.wxgui import slider, powermate
from gnuradio.wxgui import stdgui, fftsink, form
from optparse import OptionParser
import usrp_dbid
import sys
import math
import wx

def pick_subdevice(u):
    """
    The user didn't specify a subdevice on the command line.
    Try for one of these, in order: TV_RX, BASIC_RX, whatever is on side A.

    @return a subdev_spec
    """
    return usrp.pick_subdev(u, (usrp_dbid.TV_RX,
                                usrp_dbid.TV_RX_REV_2,
                                usrp_dbid.BASIC_RX))


class wfm_rx_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("-R", "--rx-subdev-spec", type="subdev", default=None,
                          help="select USRP Rx side A or B (default=A)")
        parser.add_option("-f", "--freq", type="eng_float", default=100.1e6,
                          help="set frequency to FREQ", metavar="FREQ")
        parser.add_option("-i", "--if-freq", type="eng_float", default=5.75e6,
                          help="give IF frequency of rf-frontend [default=5.75e6] set to zero, if you have no frontend.", metavar="FREQ")
        parser.add_option("-g", "--gain", type="eng_float", default=40,
                          help="set gain in dB (default is midpoint)")
        parser.add_option("-V", "--volume", type="eng_float", default=None,
                          help="set volume (default is midpoint)")
        parser.add_option("-O", "--audio-output", type="string", default="",
                          help="pcm device name.  E.g., hw:0,0 or surround51 or /dev/dsp")
        parser.add_option( "-u","--use-usrp-for-tuning", action="store_true", default=False,
                          help="use an usrp to tune a TVRX tuner frontend (connect the IF-output to the video-in of the cx2388x capture card)")
        (options, args) = parser.parse_args()
        if len(args) != 0:
            parser.print_help()
            sys.exit(1)
        
        self.frame = frame
        self.panel = panel
        
        self.vol = 0
        self.state = "FREQ"
        self.freq = 0

        self.use_usrp_for_tuning=options.use_usrp_for_tuning
        # build graph
        if self.use_usrp_for_tuning:
          self.u = usrp.source_c()                    # usrp is only used for tuning TVRX of which you can connect the IF-output to the cx2388xx TV capture card video-in
        expected_crystal_freq=28.63636e6
        actual_crystal_freq=32.0e6 #I modified my board with a 32 MHz crystal, default this is a 28.63636e6 crystal or oscillator
        default_resample_rate=27.0e6
        resample_rate=default_resample_rate
        adc_rate=   resample_rate *  actual_crystal_freq/expected_crystal_freq
        #// 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:
           do_remove_dc=1
           self.cxadc=cxadc.source (adc_rate,do_remove_dc,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 = adc_rate,do_avg_frame=True,frame_avg_alpha=0.01,dev = "/dev/cxadc")

        #adc_rate = self.u.adc_rate()                # 64 MS/s
        cxadc_rate = adc_rate #/ usrp_decim           # 320 kS/s
        factor=1
        chanfilt_decim = 100#100*factor
        demod_rate = cxadc_rate / chanfilt_decim
        audio_decimation = 10#10/factor
        audio_rate = demod_rate / (1*audio_decimation)  # 32 kHz
        
        if self.use_usrp_for_tuning:
          if options.rx_subdev_spec is None:
            options.rx_subdev_spec = pick_subdevice(self.u)
          self.subdev = usrp.selected_subdev(self.u, options.rx_subdev_spec)
          print "Using RX d'board %s" % (self.subdev.side_and_name(),)


        #chan_filt_coeffs = optfir.low_pass (1,           # gain
        #                                    cxadc_rate,   # sampling rate
        #                                    80e3,        # passband cutoff
        #                                    115e3,       # stopband cutoff
        #                                    0.1,         # passband ripple
        #                                    60)          # stopband attenuation
        use_firdes=False
        if use_firdes:
          chan_filt_coeffs = gr.firdes.low_pass  	(   	1,
	  	 cxadc_rate,
	  	125.0e3, #100.0e3
	  	50.0e3   #20.0e3
	  )  	
        else:
          chan_filt_coeffs = optfir.low_pass (1,           # gain
                                              cxadc_rate,   # sampling rate
                                              80e3,        # passband cutoff
                                              1.5*115e3,       # stopband cutoff
                                              10*0.1,         # passband ripple
                                              60)          # stopband attenuation
        #print len(chan_filt_coeffs)
        #chan_filt = gr.fir_filter_ccf (chanfilt_decim, chan_filt_coeffs)
        chan_filt = gr.freq_xlating_fir_filter_fcf (chanfilt_decim, chan_filt_coeffs, options.if_freq, adc_rate) #cxadc_rate)


        self.guts = blks.wfm_rcv (self, demod_rate, audio_decimation)

        self.volume_control = gr.multiply_const_ff(self.vol)

        # sound card as final sink
        audio_sink = audio.sink (int (audio_rate),
                                 options.audio_output,
                                 False)  # ok_to_block
        
        # now wire it all together
        self.connect (self.fsrc, chan_filt, self.guts, self.volume_control, audio_sink)

        self._build_gui(vbox, cxadc_rate, demod_rate, audio_rate)

        if options.gain is None:
            # if no gain was specified, use the mid-point in dB
            if self.use_usrp_for_tuning:
              g = self.subdev.gain_range()
              options.gain = float(g[0]+g[1])/2
            else:
              options.gain =0

        if options.volume is None:
            g = self.volume_range()
            options.volume = float(g[0]+g[1])/2
            
        if abs(options.freq) < 1e6:
            options.freq *= 1e6

        # set initial values

        self.set_gain(options.gain)
        self.set_vol(options.volume)
        if not(self.set_freq(options.freq)):
            self._set_status_msg("Failed to set initial frequency")


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


    def _build_gui(self, vbox, cxadc_rate, demod_rate, audio_rate):

        def _form_set_freq(kv):
            return self.set_freq(kv['freq'])


        if 1:
            self.src_fft = fftsink.fft_sink_f (self, self.panel, title="Data from CXADC",
                                               fft_size=512, sample_rate=cxadc_rate)
            self.connect (self.fsrc, self.src_fft)
            vbox.Add (self.src_fft.win, 4, wx.EXPAND)

        if 1:
            post_filt_fft = fftsink.fft_sink_f (self, self.panel, title="Post Demod", 
                                                fft_size=1024, sample_rate=demod_rate,
                                                y_per_div=10, ref_level=0)
            self.connect (self.guts.fm_demod, post_filt_fft)
            vbox.Add (post_filt_fft.win, 4, wx.EXPAND)

        if 0:
            post_deemph_fft = fftsink.fft_sink_f (self, self.panel, title="Post Deemph",
                                                  fft_size=512, sample_rate=audio_rate,
                                                  y_per_div=10, ref_level=-20)
            self.connect (self.guts.deemph, post_deemph_fft)
            vbox.Add (post_deemph_fft.win, 4, wx.EXPAND)

        
        # control area form at bottom
        self.myform = myform = form.form()

        hbox = wx.BoxSizer(wx.HORIZONTAL)
        hbox.Add((5,0), 0)
        myform['freq'] = form.float_field(
            parent=self.panel, sizer=hbox, label="Freq", weight=1,
            callback=myform.check_input_and_call(_form_set_freq, self._set_status_msg))

        hbox.Add((5,0), 0)
        myform['freq_slider'] = \
            form.quantized_slider_field(parent=self.panel, sizer=hbox, weight=3,
                                        range=(87.9e6, 108.1e6, 0.1e6),
                                        callback=self.set_freq)
        hbox.Add((5,0), 0)
        vbox.Add(hbox, 0, wx.EXPAND)

        hbox = wx.BoxSizer(wx.HORIZONTAL)
        hbox.Add((5,0), 0)

        myform['volume'] = \
            form.quantized_slider_field(parent=self.panel, sizer=hbox, label="Volume",
                                        weight=3, range=self.volume_range(),
                                        callback=self.set_vol)
        hbox.Add((5,0), 1)

        if self.use_usrp_for_tuning:
           self.gain_range=self.subdev.gain_range()
        else:
           self.gain_range=(0,0,1)
        myform['gain'] = \
            form.quantized_slider_field(parent=self.panel, sizer=hbox, label="Gain",
                                        weight=3, range=self.gain_range,
                                        callback=self.set_gain)
        hbox.Add((5,0), 0)
        vbox.Add(hbox, 0, wx.EXPAND)

        try:
            self.knob = powermate.powermate(self.frame)
            self.rot = 0
            powermate.EVT_POWERMATE_ROTATE (self.frame, self.on_rotate)
            powermate.EVT_POWERMATE_BUTTON (self.frame, self.on_button)
        except:
            print "FYI: No Powermate or Contour Knob found"


    def on_rotate (self, event):
        self.rot += event.delta
        if (self.state == "FREQ"):
            if self.rot >= 3:
                self.set_freq(self.freq + .1e6)
                self.rot -= 3
            elif self.rot <=-3:
                self.set_freq(self.freq - .1e6)
                self.rot += 3
        else:
            step = self.volume_range()[2]
            if self.rot >= 3:
                self.set_vol(self.vol + step)
                self.rot -= 3
            elif self.rot <=-3:
                self.set_vol(self.vol - step)
                self.rot += 3
            
    def on_button (self, event):
        if event.value == 0:        # button up
            return
        self.rot = 0
        if self.state == "FREQ":
            self.state = "VOL"
        else:
            self.state = "FREQ"
        self.update_status_bar ()
        

    def set_vol (self, vol):
        g = self.volume_range()
        self.vol = max(g[0], min(g[1], vol))
        self.volume_control.set_k(10**(self.vol/10))
        self.myform['volume'].set_value(self.vol)
        self.update_status_bar ()
                                        
    def set_freq(self, target_freq):
        """
        Set the center frequency we're interested in.

        @param target_freq: frequency in Hz
        @rypte: bool

        Tuning is a two step process.  First we ask the front-end to
        tune as close to the desired frequency as it can.  Then we use
        the result of that operation and our target_frequency to
        determine the value for the digital down converter.
        """
        if self.use_usrp_for_tuning:
          r = usrp.tune(self.u, 0, self.subdev, target_freq)
          
          if r:
            self.freq = target_freq
            self.myform['freq'].set_value(target_freq)         # update displayed value
            self.myform['freq_slider'].set_value(target_freq)  # update displayed value
            self.update_status_bar()
            self._set_status_msg("OK", 0)
            return True

          self._set_status_msg("Failed", 0)
          return False
        else:
          print "Not implemented yet"
          self._set_status_msg("Failed", 0)
          return False

    def set_gain(self, gain):
        self.myform['gain'].set_value(gain)     # update displayed value
        if self.use_usrp_for_tuning:
          self.subdev.set_gain(gain)
        else:
          print "Not implemented yet"

    def update_status_bar (self):
        msg = "Volume:%r  Setting:%s" % (self.vol, self.state)
        self._set_status_msg(msg, 1)
        self.src_fft.set_baseband_freq(self.freq)

    def volume_range(self):
        return (-20.0, 0.0, 0.5)
        

if __name__ == '__main__':
    app = stdgui.stdapp (wfm_rx_graph, "CXADC WFM RX")
    app.MainLoop ()

