#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# generated by wxGlade 0.3.5.1 on Wed Aug 17 11:24:48 2005
#
# Copyright 2005 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.
#


import wx
import usrp_siggen
from gnuradio import gr
from gnuradio import usrp
from gnuradio.eng_option import eng_option
from optparse import OptionParser
from gnuradio import eng_notation
import math


ID_ABOUT = wx.NewId()
ID_EXIT = wx.NewId()
ID_STARTFREQ = wx.NewId()
ID_STEPFREQ = wx.NewId()
ID_STOPFREQ = wx.NewId()
ID_STEPTIME = wx.NewId()
ID_AMPLITUDE = wx.NewId()
ID_REPORT = wx.NewId()
ID_START = wx.NewId()
ID_STOP = wx.NewId()


class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        
        # Menu Bar
        self.frame_1_menubar = wx.MenuBar()
        self.SetMenuBar(self.frame_1_menubar)
        wxglade_tmp_menu = wx.Menu()
        self.About = wx.MenuItem(wxglade_tmp_menu, ID_ABOUT, "About", "About", wx.ITEM_NORMAL)
        wxglade_tmp_menu.AppendItem(self.About)
        self.Exit = wx.MenuItem(wxglade_tmp_menu, ID_EXIT, "Exit", "Exit", wx.ITEM_NORMAL)
        wxglade_tmp_menu.AppendItem(self.Exit)
        self.frame_1_menubar.Append(wxglade_tmp_menu, "File")
        # Menu Bar end
        self.label_5 = wx.StaticText(self, -1, " Start Freq: ")
        self.text_ctrl_4 = wx.TextCtrl(self, ID_STARTFREQ, "", style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
        self.label_8 = wx.StaticText(self, -1, "  Step Freq:")
        self.text_ctrl_7 = wx.TextCtrl(self, ID_STEPFREQ, "", style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
        self.label_6 = wx.StaticText(self, -1, " Stop Freq: ")
        self.text_ctrl_5 = wx.TextCtrl(self, ID_STOPFREQ, "", style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
        self.label_9 = wx.StaticText(self, -1, "  Step Time (ms): ")
        self.text_ctrl_8 = wx.TextCtrl(self, ID_STEPTIME, "", style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
        self.label_7 = wx.StaticText(self, -1, " Amplitude: ")
        self.text_ctrl_6 = wx.TextCtrl(self, ID_AMPLITUDE, "", style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
        self.panel_1 = wx.Panel(self, -1)
        self.panel_2 = wx.Panel(self, -1)
        self.label_4 = wx.StaticText(self, -1, "  Report file:  ")
        self.text_ctrl_3 = wx.TextCtrl(self, ID_REPORT, "", style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
        self.button_1 = wx.Button(self, ID_START, "Start")
        self.button_2 = wx.Button(self, ID_STOP, "Stop")
        self.label_1 = wx.StaticText(self, -1, "     Status:    ")
        self.label_2 = wx.StaticText(self, -1, "\nFrequency Out:\n")
        self.text_ctrl_1 = wx.TextCtrl(self, -1, "", style=wx.TE_READONLY)
        self.label_3 = wx.StaticText(self, -1, "\nAmplitude In:\n")
        self.text_ctrl_2 = wx.TextCtrl(self, -1, "", style=wx.TE_READONLY)

        self.__set_properties()
        self.__do_layout()
        # end wxGlade

        wx.EVT_MENU(self, ID_EXIT, self.MenuExit)
        wx.EVT_MENU(self, ID_ABOUT, self.MenuAbout)
        wx.EVT_TEXT_ENTER(self, ID_STARTFREQ, self.SetStartFreq)
	wx.EVT_TEXT_ENTER(self, ID_STOPFREQ, self.SetStopFreq)
	wx.EVT_TEXT_ENTER(self, ID_STEPFREQ, self.SetStepFreq)
	wx.EVT_TEXT_ENTER(self, ID_STEPTIME, self.SetStepTime)
	wx.EVT_TEXT_ENTER(self, ID_AMPLITUDE, self.SetAmplitude)
	wx.EVT_TEXT_ENTER(self, ID_REPORT, self.OpenReportFile)
	wx.EVT_BUTTON(self, ID_START, self.Start)
	wx.EVT_BUTTON(self, ID_STOP, self.Stop)

        parser = OptionParser (option_class=eng_option)

        parser.add_option ("-i", "--interp", dest="interp", type="int", default=64,
		       help="set fgpa interpolation rate to INTERP")
        parser.add_option ("-f", "--freq", type="eng_float", default=500e3,
                       help="set waveform frequency to FREQ")
        parser.add_option ("-a", "--amplitude", type="eng_float", default=16e3,
                       help="set waveform amplitude to AMPLITUDE", metavar="AMPL")
        parser.add_option ("-o", "--offset", type="eng_float", default=0,
                       help="set waveform offset to OFFSET")
        parser.add_option ("-c", "--duc-freq", type="eng_float", default=0,
                       help="set Tx DUC frequency to FREQ", metavar="FREQ")
        parser.add_option ("-n", "--nchannels", type="int", default=1,
                       help="set number of output channels to NCHANNELS")
        parser.add_option ("-m", "--mux", type="intx", default=0x98,
                       help="set output mux register")
        (options, args) = parser.parse_args ()


	self.run = False
	self.rpt_open = False
	self.mux = options.mux
	self.nchannels = options.nchannels
	self.duc_freq = 0.
	self.interp = options.interp
	self.offset = options.offset

	self.amplitude = options.amplitude
	self.text_ctrl_6.SetValue(eng_notation.num_to_str(self.amplitude))

        self.frequency = options.freq
	self.text_ctrl_4.SetValue(eng_notation.num_to_str(self.frequency))

	self.type = gr.GR_SIN_WAVE

	self.stop_freq = 2e6
	self.text_ctrl_5.SetValue(eng_notation.num_to_str(self.stop_freq))
	self.step_freq = 5e3
	self.text_ctrl_7.SetValue(eng_notation.num_to_str(self.step_freq))
	self.step_time = 100
	self.text_ctrl_8.SetValue(eng_notation.num_to_str(self.step_time))
        self.timer = UpdateTimer(self,self.step_time)



    def __set_properties(self):
        # begin wxGlade: MyFrame.__set_properties
        self.SetTitle("Automatic Testing - Sweep")
        self.text_ctrl_3.SetSize((200, 25))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MyFrame.__do_layout
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_5 = wx.BoxSizer(wx.VERTICAL)
        sizer_4 = wx.BoxSizer(wx.VERTICAL)
        sizer_6 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_7 = wx.BoxSizer(wx.VERTICAL)
        sizer_8 = wx.BoxSizer(wx.HORIZONTAL)
        grid_sizer_1 = wx.FlexGridSizer(3, 4, 0, 0)
        grid_sizer_1.Add(self.label_5, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.text_ctrl_4, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.label_8, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.text_ctrl_7, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.label_6, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.text_ctrl_5, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.label_9, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.text_ctrl_8, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.label_7, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.text_ctrl_6, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.panel_1, 1, wx.EXPAND, 0)
        grid_sizer_1.Add(self.panel_2, 1, wx.EXPAND, 0)
        sizer_7.Add(grid_sizer_1, 1, wx.EXPAND, 0)
        sizer_8.Add(self.label_4, 0, wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        sizer_8.Add(self.text_ctrl_3, 0, wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        sizer_7.Add(sizer_8, 1, wx.EXPAND, 0)
        sizer_2.Add(sizer_7, 1, wx.EXPAND, 0)
        sizer_6.Add(self.button_1, 0, wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        sizer_6.Add(self.button_2, 0, wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        sizer_2.Add(sizer_6, 1, wx.EXPAND, 0)
        sizer_3.Add(self.label_1, 0, wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        sizer_4.Add(self.label_2, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.FIXED_MINSIZE, 0)
        sizer_4.Add(self.text_ctrl_1, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.FIXED_MINSIZE, 0)
        sizer_3.Add(sizer_4, 1, wx.EXPAND, 0)
        sizer_5.Add(self.label_3, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.FIXED_MINSIZE, 0)
        sizer_5.Add(self.text_ctrl_2, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.FIXED_MINSIZE, 0)
        sizer_3.Add(sizer_5, 1, wx.EXPAND, 0)
        sizer_2.Add(sizer_3, 1, wx.EXPAND, 0)
        sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
        self.SetAutoLayout(True)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        sizer_1.SetSizeHints(self)
        self.Layout()
        # end wxGlade

    def MenuExit(self, event):
        if self.run:
           self.sg.fg.stop()
	   self.rx_fg.stop()
	if self.rpt_open:
	   self.rpt.close()
        self.Close()

    def MenuAbout(self, event):
        dlg = wx.MessageDialog(self, "Automatic Testing\n"
                              "with GnuRadio / USRP\n",
			      "Automatic Testing", wx.OK | wx.ICON_INFORMATION)
        dlg.ShowModal()
        dlg.Destroy()

    def SetStartFreq(self,event):
	self.frequency = eng_notation.str_to_num(self.text_ctrl_4.GetValue())

    def SetStopFreq(self,event):
	self.stop_freq = eng_notation.str_to_num(self.text_ctrl_5.GetValue())

    def SetStepFreq(self,event):
	self.step_freq = eng_notation.str_to_num(self.text_ctrl_7.GetValue())

    def SetStepTime(self,event):
	self.step_time = eng_notation.str_to_num(self.text_ctrl_8.GetValue())
	self.timer = UpdateTimer(self,self.step_time)

    def SetAmplitude(self,event):
	self.amplitude = eng_notation.str_to_num(self.text_ctrl_6.GetValue())

    def OpenReportFile(self,event):
	self.rpt = open(self.text_ctrl_3.GetValue(),"w")
	self.rpt_open = True

    def Start(self,event):
	if self.run == False:
	  # setup input
	  self.rx_fg = gr.flow_graph()
	  self.rx = usrp.source_c(0, self.interp, 1, 0x32103210, 0)
	  rx_freq = self.frequency + 1e3
	  self.rx.set_rx_freq(0, -rx_freq )
	  self.rx.set_pga(0,0)
	  self.meter = gr.signal_level_cc(1,.05)
	  self.sink = gr.null_sink(gr.sizeof_gr_complex)
	  self.rx_fg.connect (self.rx,self.meter,self.sink)
	  self.rx_fg.start()
	  # setup output
	  self.sg = usrp_siggen.siggen ()
	  self.sg.usrp.set_nchannels (self.nchannels)
	  self.sg.usrp.set_mux(self.mux)
	  self.sg.set_interpolator(self.interp)
	  self.sg.set_waveform_offset(self.offset)
	  self.sg.set_waveform_type(self.type)
	  self.sg.set_waveform_ampl(self.amplitude)
	  self.sg.set_duc_freq (self.duc_freq)
	  self.error = self.sg.usrp.tx_freq(0) - self.duc_freq
	  self.sg.set_waveform_freq(self.error)
	  self.sg.fg.start()
	  self.run = True

    def Stop(self,event):
	if self.run == True:
	  self.rx_fg.stop()
	  self.sg.fg.stop()
	  del (self.sg,self.rx,self.rx_fg)
	  self.run = False

    def OnUpdate(self):
	if self.run:
	  self.frequency += self.step_freq
	  if self.frequency > self.stop_freq:
	    self.Stop(0)
	  else:
	    rpt_input = self.meter.level()
	    if rpt_input > 0:
	      log_rpt_input = math.log10(rpt_input)
	      self.text_ctrl_2.SetValue(eng_notation.num_to_str(log_rpt_input))
	    if (self.duc_freq != 0) & (self.rpt_open == True):
	      self.rpt.write(str(self.duc_freq)+" "+str(log_rpt_input)+"\n")
	    self.duc_freq = self.frequency
	    self.sg.set_duc_freq(self.duc_freq)
	    self.error = self.sg.usrp.tx_freq(0) - self.duc_freq
	    self.sg.set_waveform_freq(self.error)
	    self.text_ctrl_1.SetValue(eng_notation.num_to_str(self.duc_freq))
	    rx_freq = self.frequency + 1e3
	    self.rx.set_rx_freq (0, -rx_freq)


# end of class MyFrame

class UpdateTimer(wx.Timer):
    def __init__(self, target, dur=1000):
        wx.Timer.__init__(self)
        self.target = target
        self.Start(dur)

    def Notify(self):
        """Called every timer interval"""
        if self.target:
            self.target.OnUpdate()


class MyApp(wx.App):
  def OnInit(self):
    frame = MyFrame(None, -1, "Automatic Testing")
    frame.Show(True)
    self.SetTopWindow(frame)
    return True

app = MyApp(0)
app.MainLoop()

