#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# generated by wxGlade 0.3.5.1 on Wed Jun  8 11:49:27 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.eng_option import eng_option
from optparse import OptionParser
from gnuradio import eng_notation
from gnuradio.wxgui import powermate


ID_SPIN_1 = wx.NewId()
ID_SPIN_2 = wx.NewId()
ID_SPIN_3 = wx.NewId()
ID_SPIN_4 = wx.NewId()
ID_SPIN_5 = wx.NewId()
ID_SPIN_6 = wx.NewId()
ID_SPIN_7 = wx.NewId()
ID_SPIN_8 = wx.NewId()
ID_SPINCTRL_1 = wx.NewId()
ID_SPINCTRL_2 = wx.NewId()
ID_SLIDER_1 = wx.NewId()
ID_SLIDER_2 = wx.NewId()
ID_RADIOBUTTON_1 = wx.NewId()
ID_TEXTCTRL_1 = wx.NewId()
ID_TEXTCTRL_2 = wx.NewId()
ID_TOGGLEBUTTON_1 = wx.NewId()
ID_TOGGLEBUTTON_2 = wx.NewId()
ID_EXIT = 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()
        wxglade_tmp_menu.Append(ID_EXIT, "Exit", "Exit", wx.ITEM_NORMAL)
        self.frame_1_menubar.Append(wxglade_tmp_menu, "File")
        # Menu Bar end
        self.bitmap_1 = wx.StaticBitmap(self, -1, wx.Bitmap("/usr/src/radio/wx/numerals/fancydigitalgreen/0.bmp", wx.BITMAP_TYPE_ANY))
        self.bitmap_2 = wx.StaticBitmap(self, -1, wx.Bitmap("/usr/src/radio/wx/numerals/fancydigitalgreen/0.bmp", wx.BITMAP_TYPE_ANY))
        self.bitmap_3 = wx.StaticBitmap(self, -1, wx.Bitmap("/usr/src/radio/wx/numerals/fancydigitalgreen/0.bmp", wx.BITMAP_TYPE_ANY))
        self.bitmap_4 = wx.StaticBitmap(self, -1, wx.Bitmap("/usr/src/radio/wx/numerals/fancydigitalgreen/0.bmp", wx.BITMAP_TYPE_ANY))
        self.bitmap_5 = wx.StaticBitmap(self, -1, wx.Bitmap("/usr/src/radio/wx/numerals/fancydigitalgreen/0.bmp", wx.BITMAP_TYPE_ANY))
        self.bitmap_6 = wx.StaticBitmap(self, -1, wx.Bitmap("/usr/src/radio/wx/numerals/fancydigitalgreen/0.bmp", wx.BITMAP_TYPE_ANY))
        self.bitmap_7 = wx.StaticBitmap(self, -1, wx.Bitmap("/usr/src/radio/wx/numerals/fancydigitalgreen/0.bmp", wx.BITMAP_TYPE_ANY))
        self.bitmap_8 = wx.StaticBitmap(self, -1, wx.Bitmap("/usr/src/radio/wx/numerals/fancydigitalgreen/0.bmp", wx.BITMAP_TYPE_ANY))
        self.spin_button_1 = wx.SpinButton(self, ID_SPIN_1 )
        self.spin_button_2 = wx.SpinButton(self, ID_SPIN_2 )
        self.spin_button_3 = wx.SpinButton(self, ID_SPIN_3 )
        self.spin_button_4 = wx.SpinButton(self, ID_SPIN_4 )
        self.spin_button_5 = wx.SpinButton(self, ID_SPIN_5 )
        self.spin_button_6 = wx.SpinButton(self, ID_SPIN_6 )
        self.spin_button_7 = wx.SpinButton(self, ID_SPIN_7 )
        self.spin_button_8 = wx.SpinButton(self, ID_SPIN_8 )
        self.spin_ctrl_1 = wx.SpinCtrl(self, ID_SPINCTRL_1, "", min=0, max=64000000)
        self.label_1 = wx.StaticText(self, -1, "Frequency")
        self.slider_1 = wx.Slider(self, ID_SLIDER_1, 0, 0, 64000, style=wx.SL_HORIZONTAL|wx.SL_AUTOTICKS|wx.SL_LABELS)
        self.panel_2 = wx.Panel(self, -1)
        self.label_3 = wx.StaticText(self, -1, "                         Amplitude                               ")
        self.spin_ctrl_2 = wx.SpinCtrl(self, ID_SPINCTRL_2, "", min=0, max=20000)
        self.slider_2 = wx.Slider(self, ID_SLIDER_2, 0, 0, 20000, style=wx.SL_HORIZONTAL|wx.SL_LABELS)
        self.panel_1 = wx.Panel(self, -1)
        self.radio_box_1 = wx.RadioBox(self, ID_RADIOBUTTON_1, "Waveform", choices=["Sine", "Gaussian", "Uniform"], majorDimension=1, style=wx.RA_SPECIFY_COLS)
        self.label_4 = wx.StaticText(self, -1, "   DUC\n  Center\nFrequency")
        self.text_ctrl_1 = wx.TextCtrl(self, ID_TEXTCTRL_1, "", style=wx.TE_PROCESS_ENTER)
        self.label_5 = wx.StaticText(self, -1, "    USRP\nInterpolation\n")
        self.text_ctrl_2 = wx.TextCtrl(self, ID_TEXTCTRL_2, "", style=wx.TE_PROCESS_ENTER)
        self.button_1 = wx.ToggleButton(self, ID_TOGGLEBUTTON_1, "Run")
        self.panel_3 = wx.Panel(self, -1)
        self.button_2 = wx.ToggleButton(self, ID_TOGGLEBUTTON_2, "Auto DUC")
        self.panel_4 = wx.Panel(self, -1)
        self.panel_5 = wx.Panel(self, -1)

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


	# set spin button ranges 
        self.spin_button_1.SetRange(0,6)
        self.spin_button_1.SetValue(0)
        self.spin_button_2.SetRange(-1,10)
        self.spin_button_2.SetValue(0)
        self.spin_button_3.SetRange(-1,10)
        self.spin_button_3.SetValue(0)
        self.spin_button_4.SetRange(-1,10)
        self.spin_button_4.SetValue(0)
        self.spin_button_5.SetRange(-1,10)
        self.spin_button_5.SetValue(0)
        self.spin_button_6.SetRange(-1,10)
        self.spin_button_6.SetValue(0)
        self.spin_button_7.SetRange(-1,10)
        self.spin_button_7.SetValue(0)
        self.spin_button_8.SetRange(-1,10)
        self.spin_button_8.SetValue(0)

	# load numeral bitmaps
        self.n0 = wx.Bitmap("/usr/src/radio/wx/numerals/fancydigitalgreen/0.bmp", wx.BITMAP_TYPE_ANY)
        self.n1 = wx.Bitmap("/usr/src/radio/wx/numerals/fancydigitalgreen/1.bmp", wx.BITMAP_TYPE_ANY)
        self.n2 = wx.Bitmap("/usr/src/radio/wx/numerals/fancydigitalgreen/2.bmp", wx.BITMAP_TYPE_ANY)
        self.n3 = wx.Bitmap("/usr/src/radio/wx/numerals/fancydigitalgreen/3.bmp", wx.BITMAP_TYPE_ANY)
        self.n4 = wx.Bitmap("/usr/src/radio/wx/numerals/fancydigitalgreen/4.bmp", wx.BITMAP_TYPE_ANY)
        self.n5 = wx.Bitmap("/usr/src/radio/wx/numerals/fancydigitalgreen/5.bmp", wx.BITMAP_TYPE_ANY)
        self.n6 = wx.Bitmap("/usr/src/radio/wx/numerals/fancydigitalgreen/6.bmp", wx.BITMAP_TYPE_ANY)
        self.n7 = wx.Bitmap("/usr/src/radio/wx/numerals/fancydigitalgreen/7.bmp", wx.BITMAP_TYPE_ANY)
        self.n8 = wx.Bitmap("/usr/src/radio/wx/numerals/fancydigitalgreen/8.bmp", wx.BITMAP_TYPE_ANY)
        self.n9 = wx.Bitmap("/usr/src/radio/wx/numerals/fancydigitalgreen/9.bmp", wx.BITMAP_TYPE_ANY)


	wx.EVT_MENU(self, ID_EXIT, self.MenuExit)
	wx.EVT_SPIN(self, ID_SPIN_1, self.Spin)
        wx.EVT_SPIN(self, ID_SPIN_2, self.Spin)
        wx.EVT_SPIN(self, ID_SPIN_3, self.Spin)
        wx.EVT_SPIN(self, ID_SPIN_4, self.Spin)
        wx.EVT_SPIN(self, ID_SPIN_5, self.Spin)
        wx.EVT_SPIN(self, ID_SPIN_6, self.Spin)
        wx.EVT_SPIN(self, ID_SPIN_7, self.Spin)
        wx.EVT_SPIN(self, ID_SPIN_8, self.Spin)
	wx.EVT_SLIDER(self, ID_SLIDER_1, self.freqSlider)
	wx.EVT_SLIDER(self, ID_SLIDER_2, self.ampSlider)
	wx.EVT_SPINCTRL(self, ID_SPINCTRL_1, self.SpinCtrl1)
	wx.EVT_SPINCTRL(self, ID_SPINCTRL_2, self.SpinCtrl2)
	wx.EVT_RADIOBOX(self, ID_RADIOBUTTON_1, self.RadioBox1)
	wx.EVT_TEXT_ENTER(self, ID_TEXTCTRL_1, self.TextCtrl1)
	wx.EVT_TEXT_ENTER(self, ID_TEXTCTRL_2, self.TextCtrl2)
	wx.EVT_TOGGLEBUTTON(self, ID_TOGGLEBUTTON_1, self.RunStop)
	wx.EVT_TOGGLEBUTTON(self, ID_TOGGLEBUTTON_2, self.AutoDUC)


        parser = OptionParser (option_class=eng_option)
        parser.add_option ("--sine", dest="type", action="store_const", const=gr.GR_SIN_WAVE,
                       help="generate a complex sinusoid [default]", default=gr.GR_SIN_WAVE)
        parser.add_option ("--const", dest="type", action="store_const", const=gr.GR_CONST_WAVE,
                       help="generate a constant output")
        parser.add_option ("--gaussian", dest="type", action="store_const", const=gr.GR_GAUSSIAN,
                       help="generate Gaussian random output")
        parser.add_option ("--uniform", dest="type", action="store_const", const=gr.GR_UNIFORM,
                       help="generate Uniform random output")

        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=100e3,
                       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 ()

        try:
            self.pm = powermate.powermate(self)
	    POWERMATE = True
        except:
            sys.stderr.write("Unable to find PowerMate or Contour Shuttle\n")
            POWERMATE = False
	if POWERMATE:
            powermate.EVT_POWERMATE_ROTATE(self, self.on_rotate)
            powermate.EVT_POWERMATE_BUTTON(self, self.on_pmButton)



	self.run = False
	self.auto_duc = False
	self.button_2.SetValue(False)

	self.frequency = options.freq
	self.spin_ctrl_1.SetValue(int(self.frequency))
	self.SpinCtrl1(0)

	self.amplitude = options.amplitude
	self.slider_2.SetValue(int(self.amplitude))
	self.spin_ctrl_2.SetValue(int(self.amplitude))

	self.interp = options.interp
	self.text_ctrl_2.SetValue(str(self.interp))
	self.usb_freq = 128e6 / self.interp

	self.type = options.type
	if self.type == gr.GR_UNIFORM:
	   self.radio_box_1.SetSelection(2)
	elif options.type == gr.GR_GAUSSIAN:
	   self.radio_box_1.SetSelection(1)
	else:
	   self.radio_box_1.SetSelection(0)

	self.offset = options.offset

	self.duc_freq = options.duc_freq
	self.text_ctrl_1.SetValue(eng_notation.num_to_str(int(self.duc_freq)))

	self.nchannels = options.nchannels
	self.mux = options.mux


    def __set_properties(self):
        # begin wxGlade: MyFrame.__set_properties
        self.SetTitle("GNURadio Signal Generator")
        self.slider_1.SetSize((400, 50))
        self.slider_2.SetSize((400, 50))
        self.radio_box_1.SetSelection(0)
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MyFrame.__do_layout
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        sizer_10 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_7 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_9 = wx.BoxSizer(wx.VERTICAL)
        sizer_8 = wx.BoxSizer(wx.VERTICAL)
        sizer_5 = wx.BoxSizer(wx.VERTICAL)
        sizer_6 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_4 = wx.BoxSizer(wx.VERTICAL)
        sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
        grid_sizer_1 = wx.GridSizer(2, 8, 0, 0)
        grid_sizer_1.Add(self.bitmap_1, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.bitmap_2, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.bitmap_3, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.bitmap_4, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.bitmap_5, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.bitmap_6, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.bitmap_7, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.bitmap_8, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.spin_button_1, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.spin_button_2, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.spin_button_3, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.spin_button_4, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.spin_button_5, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.spin_button_6, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.spin_button_7, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.spin_button_8, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        sizer_3.Add(grid_sizer_1, 1, wx.EXPAND, 0)
        sizer_3.Add(self.spin_ctrl_1, 0, wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        sizer_2.Add(sizer_3, 1, wx.EXPAND, 0)
        sizer_4.Add(self.label_1, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.FIXED_MINSIZE, 0)
        sizer_4.Add(self.slider_1, 0, wx.FIXED_MINSIZE, 0)
        sizer_4.Add(self.panel_2, 1, wx.EXPAND, 0)
        sizer_2.Add(sizer_4, 1, wx.EXPAND, 0)
        sizer_6.Add(self.label_3, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        sizer_6.Add(self.spin_ctrl_2, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        sizer_5.Add(sizer_6, 1, wx.EXPAND, 0)
        sizer_5.Add(self.slider_2, 0, wx.FIXED_MINSIZE, 0)
        sizer_5.Add(self.panel_1, 1, wx.EXPAND, 0)
        sizer_2.Add(sizer_5, 1, wx.EXPAND, 0)
        sizer_7.Add(self.radio_box_1, 0, wx.FIXED_MINSIZE, 0)
        sizer_8.Add(self.label_4, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        sizer_8.Add(self.text_ctrl_1, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        sizer_7.Add(sizer_8, 1, wx.EXPAND, 0)
        sizer_9.Add(self.label_5, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        sizer_9.Add(self.text_ctrl_2, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        sizer_7.Add(sizer_9, 1, wx.EXPAND, 0)
        sizer_7.Add(self.button_1, 0, wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 0)
        sizer_2.Add(sizer_7, 1, wx.EXPAND, 0)
        sizer_10.Add(self.panel_3, 1, wx.EXPAND, 0)
        sizer_10.Add(self.button_2, 0, wx.FIXED_MINSIZE, 0)
        sizer_10.Add(self.panel_4, 1, wx.EXPAND, 0)
        sizer_10.Add(self.panel_5, 1, wx.EXPAND, 0)
        sizer_2.Add(sizer_10, 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.Close()


    def Spin(self,event):
        n = self.spin_button_8.GetValue()
	if n==-1:
	  self.bitmap_8.SetBitmap(self.n9)
	  self.spin_button_8.SetValue(9)
	  self.spin_button_7.SetValue(self.spin_button_7.GetValue()-1)
        elif n==0: self.bitmap_8.SetBitmap(self.n0)
        elif n==1: self.bitmap_8.SetBitmap(self.n1)
        elif n==2: self.bitmap_8.SetBitmap(self.n2)
        elif n==3: self.bitmap_8.SetBitmap(self.n3)
        elif n==4: self.bitmap_8.SetBitmap(self.n4)
        elif n==5: self.bitmap_8.SetBitmap(self.n5)
        elif n==6: self.bitmap_8.SetBitmap(self.n6)
        elif n==7: self.bitmap_8.SetBitmap(self.n7)
        elif n==8: self.bitmap_8.SetBitmap(self.n8)
        elif n==9: self.bitmap_8.SetBitmap(self.n9)
	elif n==10:
          self.bitmap_8.SetBitmap(self.n0)
          self.spin_button_8.SetValue(0)
          self.spin_button_7.SetValue(self.spin_button_7.GetValue()+1)
        n = self.spin_button_7.GetValue()
	if n==-1:
	  self.bitmap_7.SetBitmap(self.n9)
	  self.spin_button_7.SetValue(9)
	  self.spin_button_6.SetValue(self.spin_button_6.GetValue()-1)
        elif n==0: self.bitmap_7.SetBitmap(self.n0)
        elif n==1: self.bitmap_7.SetBitmap(self.n1)
        elif n==2: self.bitmap_7.SetBitmap(self.n2)
        elif n==3: self.bitmap_7.SetBitmap(self.n3)
        elif n==4: self.bitmap_7.SetBitmap(self.n4)
        elif n==5: self.bitmap_7.SetBitmap(self.n5)
        elif n==6: self.bitmap_7.SetBitmap(self.n6)
        elif n==7: self.bitmap_7.SetBitmap(self.n7)
        elif n==8: self.bitmap_7.SetBitmap(self.n8)
        elif n==9: self.bitmap_7.SetBitmap(self.n9)
	elif n==10:
	  self.bitmap_7.SetBitmap(self.n0)
	  self.spin_button_7.SetValue(0)
	  self.spin_button_6.SetValue(self.spin_button_6.GetValue()+1)
        n = self.spin_button_6.GetValue()
	if n==-1:
	  self.bitmap_6.SetBitmap(self.n9)
	  self.spin_button_6.SetValue(9)
	  self.spin_button_5.SetValue(self.spin_button_5.GetValue()-1)
        elif n==0: self.bitmap_6.SetBitmap(self.n0)
        elif n==1: self.bitmap_6.SetBitmap(self.n1)
        elif n==2: self.bitmap_6.SetBitmap(self.n2)
        elif n==3: self.bitmap_6.SetBitmap(self.n3)
        elif n==4: self.bitmap_6.SetBitmap(self.n4)
        elif n==5: self.bitmap_6.SetBitmap(self.n5)
        elif n==6: self.bitmap_6.SetBitmap(self.n6)
        elif n==7: self.bitmap_6.SetBitmap(self.n7)
        elif n==8: self.bitmap_6.SetBitmap(self.n8)
        elif n==9: self.bitmap_6.SetBitmap(self.n9)
	elif n==10:
	  self.bitmap_6.SetBitmap(self.n0)
	  self.spin_button_6.SetValue(0)
	  self.spin_button_5.SetValue(self.spin_button_5.GetValue()+1)
        n = self.spin_button_5.GetValue()
	if n==-1:
          self.bitmap_5.SetBitmap(self.n9)
          self.spin_button_5.SetValue(9)
          self.spin_button_4.SetValue(self.spin_button_4.GetValue()-1)
        elif n==0: self.bitmap_5.SetBitmap(self.n0)
        elif n==1: self.bitmap_5.SetBitmap(self.n1)
        elif n==2: self.bitmap_5.SetBitmap(self.n2)
        elif n==3: self.bitmap_5.SetBitmap(self.n3)
        elif n==4: self.bitmap_5.SetBitmap(self.n4)
        elif n==5: self.bitmap_5.SetBitmap(self.n5)
        elif n==6: self.bitmap_5.SetBitmap(self.n6)
        elif n==7: self.bitmap_5.SetBitmap(self.n7)
        elif n==8: self.bitmap_5.SetBitmap(self.n8)
        elif n==9: self.bitmap_5.SetBitmap(self.n9)
        elif n==10:
          self.bitmap_5.SetBitmap(self.n0)
          self.spin_button_5.SetValue(0)
          self.spin_button_4.SetValue(self.spin_button_4.GetValue()+1)
        n = self.spin_button_4.GetValue()
        if n==-1:
          self.bitmap_4.SetBitmap(self.n9)
          self.spin_button_4.SetValue(9)
          self.spin_button_3.SetValue(self.spin_button_3.GetValue()-1)
        elif n==0: self.bitmap_4.SetBitmap(self.n0)
        elif n==1: self.bitmap_4.SetBitmap(self.n1)
        elif n==2: self.bitmap_4.SetBitmap(self.n2)
        elif n==3: self.bitmap_4.SetBitmap(self.n3)
        elif n==4: self.bitmap_4.SetBitmap(self.n4)
        elif n==5: self.bitmap_4.SetBitmap(self.n5)
        elif n==6: self.bitmap_4.SetBitmap(self.n6)
        elif n==7: self.bitmap_4.SetBitmap(self.n7)
        elif n==8: self.bitmap_4.SetBitmap(self.n8)
        elif n==9: self.bitmap_4.SetBitmap(self.n9)
        elif n==10:
          self.bitmap_4.SetBitmap(self.n0)
          self.spin_button_4.SetValue(0)
          self.spin_button_3.SetValue(self.spin_button_3.GetValue()+1)
        n = self.spin_button_3.GetValue()
        if n==-1:
          self.bitmap_3.SetBitmap(self.n9)
          self.spin_button_3.SetValue(9)
          self.spin_button_2.SetValue(self.spin_button_2.GetValue()-1)
        elif n==0: self.bitmap_3.SetBitmap(self.n0)
        elif n==1: self.bitmap_3.SetBitmap(self.n1)
        elif n==2: self.bitmap_3.SetBitmap(self.n2)
        elif n==3: self.bitmap_3.SetBitmap(self.n3)
        elif n==4: self.bitmap_3.SetBitmap(self.n4)
        elif n==5: self.bitmap_3.SetBitmap(self.n5)
        elif n==6: self.bitmap_3.SetBitmap(self.n6)
        elif n==7: self.bitmap_3.SetBitmap(self.n7)
        elif n==8: self.bitmap_3.SetBitmap(self.n8)
        elif n==9: self.bitmap_3.SetBitmap(self.n9)
        elif n==10:
          self.bitmap_3.SetBitmap(self.n0)
          self.spin_button_3.SetValue(0)
          self.spin_button_2.SetValue(self.spin_button_2.GetValue()+1)
        n = self.spin_button_2.GetValue()
        if n==-1:
          self.bitmap_2.SetBitmap(self.n9)
          self.spin_button_2.SetValue(9)
          self.spin_button_1.SetValue(self.spin_button_1.GetValue()-1)
        elif n==0: self.bitmap_2.SetBitmap(self.n0)
        elif n==1: self.bitmap_2.SetBitmap(self.n1)
        elif n==2: self.bitmap_2.SetBitmap(self.n2)
        elif n==3: self.bitmap_2.SetBitmap(self.n3)
        elif n==4: self.bitmap_2.SetBitmap(self.n4)
        elif n==5: self.bitmap_2.SetBitmap(self.n5)
        elif n==6: self.bitmap_2.SetBitmap(self.n6)
        elif n==7: self.bitmap_2.SetBitmap(self.n7)
        elif n==8: self.bitmap_2.SetBitmap(self.n8)
        elif n==9: self.bitmap_2.SetBitmap(self.n9)
        elif n==10:
          self.bitmap_2.SetBitmap(self.n0)
          self.spin_button_2.SetValue(0)
          self.spin_button_1.SetValue(self.spin_button_1.GetValue()+1)
        n = self.spin_button_1.GetValue()
        if n==-1:
          self.bitmap_1.SetBitmap(self.n0)
          self.spin_button_1.SetValue(0)
        elif n==0: self.bitmap_1.SetBitmap(self.n0)
        elif n==1: self.bitmap_1.SetBitmap(self.n1)
        elif n==2: self.bitmap_1.SetBitmap(self.n2)
        elif n==3: self.bitmap_1.SetBitmap(self.n3)
        elif n==4: self.bitmap_1.SetBitmap(self.n4)
        elif n==5: self.bitmap_1.SetBitmap(self.n5)
        elif n==6: self.bitmap_1.SetBitmap(self.n6)
        elif n==7:
          self.bitmap_1.SetBitmap(self.n6)
	  self.spin_button_1.SetValue(6)
	self.frequency = 1e7 * self.spin_button_1.GetValue() + \
	  1e6 * self.spin_button_2.GetValue() + \
	  1e5 * self.spin_button_3.GetValue() + \
	  1e4 * self.spin_button_4.GetValue() + \
	  1e3 * self.spin_button_5.GetValue() + \
	  1e2 * self.spin_button_6.GetValue() + \
	  1e1 * self.spin_button_7.GetValue() + \
	  1e0 * self.spin_button_8.GetValue()
	self.slider_1.SetValue(min(64000000,max(0,self.frequency))/1e3)
	self.spin_ctrl_1.SetValue(self.frequency)
	if self.auto_duc:
	  if self.frequency < self.duc_freq - self.usb_freq/2 :
	    self.duc_freq = self.frequency - self.usb_freq/4
	    self.text_ctrl_1.SetValue(eng_notation.num_to_str(int(self.duc_freq)))
	  elif self.frequency > self.duc_freq + self.usb_freq/2 :
	    self.duc_freq = self.frequency + self.usb_freq/4
	    self.text_ctrl_1.SetValue(eng_notation.num_to_str(int(self.duc_freq)))
	if self.run:
	  self.sg.set_waveform_freq (self.frequency - self.duc_freq)
	  self.sg.set_duc_freq (self.duc_freq)

    def freqSlider(self, event):
	self.frequency = 1e3 * self.slider_1.GetValue()
	n = self.frequency
	self.spin_ctrl_1.SetValue(self.frequency)
	d = int(n/1e7)
        self.spin_button_1.SetValue(d)
	n -= d*1e7
	d = int(n/1e6)
        self.spin_button_2.SetValue(d)
	n -= d*1e6
	d = int(n/1e5)
	self.spin_button_3.SetValue(d)
	n -= d*1e5
	d = int(n/1e4)
	self.spin_button_4.SetValue(d)
	n -= d*1e4
	d = int(n/1e3)
	self.spin_button_5.SetValue(d)
	n -= d*1e3
	d = int(n/1e2)
	self.spin_button_6.SetValue(0)
	self.spin_button_7.SetValue(0)
	self.spin_button_8.SetValue(0)
	self.Spin(ID_SPIN_8)

    def ampSlider(self,event):
	self.amplitude = self.slider_2.GetValue()
	self.spin_ctrl_2.SetValue(self.amplitude)
	if self.run:
	   self.sg.set_waveform_ampl (self.amplitude)

    def SpinCtrl1(self,event):
	self.frequency = self.spin_ctrl_1.GetValue()
        n = self.frequency
        d = int(n/1e7)
        self.spin_button_1.SetValue(d)
        n -= d*1e7
        d = int(n/1e6)
        self.spin_button_2.SetValue(d)
        n -= d*1e6
        d = int(n/1e5)
        self.spin_button_3.SetValue(d)
        n -= d*1e5
        d = int(n/1e4)
        self.spin_button_4.SetValue(d)
        n -= d*1e4
        d = int(n/1e3)
        self.spin_button_5.SetValue(d)
        n -= d*1e3
        d = int(n/1e2)
        self.spin_button_6.SetValue(d)
	n -= d*1e2
	d = int(n/1e1)
        self.spin_button_7.SetValue(d)
	n -= d*1e1
	d = int(n/1e0)
        self.spin_button_8.SetValue(d)
	self.Spin(ID_SPIN_8)

    def SpinCtrl2(self,event):
	self.amplitude = self.spin_ctrl_2.GetValue()
	self.slider_2.SetValue(self.amplitude)
	if self.run:
	   self.sg.set_waveform_ampl (self.amplitude)

    def RadioBox1(self,event):
	selection = event.GetSelection()
	self.button_1.SetValue(False)
	if self.run:
	  self.RunStop(0)
	if selection == 0:
	  self.type = gr.GR_SIN_WAVE
	elif selection == 1:
	  self.type = gr.GR_GAUSSIAN
	elif selection == 2:
	  self.type = gr.GR_UNIFORM

    def TextCtrl1(self,event):
	self.duc_freq = eng_notation.str_to_num(self.text_ctrl_1.GetValue())
	if self.run:
	    self.sg.set_duc_freq(self.duc_freq)
	    self.sg.set_waveform_freq (self.frequency - self.duc_freq)

    def TextCtrl2(self,event):
	self.interp = (int(self.text_ctrl_2.GetValue()))
	self.usb_freq = 128e6 / self.interp
	if self.run:
	  self.button_1.SetValue(False)
	  self.RunStop(0)

    def RunStop(self,event):
	state = self.button_1.GetValue()
	if state:
	   self.sg = usrp_siggen.siggen ()
	   self.sg.set_waveform_ampl (self.amplitude)
	   self.sg.set_waveform_freq (self.frequency - self.duc_freq)
	   self.sg.set_interpolator (self.interp)
	   self.sg.set_waveform_type (self.type)
	   self.sg.set_waveform_offset (self.offset)
	   self.sg.set_duc_freq (self.duc_freq)
	   self.sg.usrp.set_nchannels (self.nchannels)
	   self.sg.usrp.set_mux (self.mux)
	   self.sg.fg.start ()
	   self.run = True
	else:
	   self.sg.fg.stop ()
	   del (self.sg)
	   self.run = False

    def AutoDUC(self,event):
	self.auto_duc = self.button_2.GetValue()
        if self.frequency < self.duc_freq - self.usb_freq/2 :
            self.duc_freq = self.frequency - self.usb_freq/4
	    if self.run:
		self.sg.set_duc_freq (self.duc_freq)
	        self.sg.set_waveform_freq (self.frequency - self.duc_freq)
            self.text_ctrl_1.SetValue(eng_notation.num_to_str(int(self.duc_freq)))
        elif self.frequency > self.duc_freq + self.usb_freq/2 :
            self.duc_freq = self.frequency + self.usb_freq/4
	    if self.run:
		self.sg.set_duc_freq (self.duc_freq)
	        self.sg.set_waveform_freq (self.frequency - self.duc_freq)
            self.text_ctrl_1.SetValue(eng_notation.num_to_str(int(self.duc_freq)))
	    

    def on_rotate(self,event):
	self.frequency += event.delta
	self.spin_ctrl_1.SetValue(int(self.frequency))
	self.SpinCtrl1(0)

    def on_pmButton(self,event):
	pass

# end of class MyFrame

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

app = MyApp(0)
app.MainLoop()

