| 1 | #!/usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | '''get signals and output them to a port''' |
|---|
| 4 | |
|---|
| 5 | import sources #within the AWG directory. This will generate the signals |
|---|
| 6 | import wx #needed for display stuff |
|---|
| 7 | from omniORB import CORBA #use this for the CORBA orb stuff (pushing packets) |
|---|
| 8 | import sys #for system commands (e.g., argv and argc stuff) |
|---|
| 9 | import CosNaming #narrowing naming context stuff |
|---|
| 10 | import CF, CF__POA #core framework stuff |
|---|
| 11 | import standardInterfaces__POA |
|---|
| 12 | from wx.lib.anchors import LayoutAnchors #used by splitter window |
|---|
| 13 | # import wx.lib.buttons as buttons #for special wx buttons |
|---|
| 14 | |
|---|
| 15 | class AWG: |
|---|
| 16 | '''this class makes the calls to get the actual signal from the sources class''' |
|---|
| 17 | |
|---|
| 18 | def __init__(self, parent): |
|---|
| 19 | self.parent = parent |
|---|
| 20 | |
|---|
| 21 | def get_signal(self, signal_index, file_name): |
|---|
| 22 | #signal I want is hard-coded for now |
|---|
| 23 | self.signal_index = signal_index |
|---|
| 24 | self.signal_len = 8192 #length of packet |
|---|
| 25 | self.frequency = 5 #cycles per packet |
|---|
| 26 | self.rand_type = 'random' |
|---|
| 27 | self.file_name = file_name |
|---|
| 28 | self.delimiter = ',' |
|---|
| 29 | |
|---|
| 30 | my_signal = sources.sources(self) #instance of signals class |
|---|
| 31 | self.the_signal = my_signal.gen_signal() |
|---|
| 32 | |
|---|
| 33 | return self.the_signal |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | def create(parent,namespace, interface, ns_name, port_name): |
|---|
| 37 | #return MainFrame(parent, -1, namespace, interface, ns_name, port_name) |
|---|
| 38 | return MainFrame(parent, -1, "Don't know what this should be", namespace, interface, ns_name, port_name) |
|---|
| 39 | |
|---|
| 40 | #generate wx ids for my wx controls |
|---|
| 41 | [wxID_MAINFRAME, wxID_SPLITTERWINDOW1, wxID_PUSHPACKETBTN, wxID_ISOURCECHOICE, wxID_IFILENAMEEDITOR, wxID_QSOURCECHOICE, wxID_QFILENAMEEDITOR, wxID_ISOURCESTATICTEXT, wxID_QSOURCESTATICTEXT, wxID_IFILESTATICTEXT,wxID_QFILESTATICTEXT] = [wx.NewId() for _init_ctrls in range(11)] |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | class MainFrame(wx.Frame): |
|---|
| 45 | def __init__(self, parent, id, title, namespace, interface, component_name, port_name): |
|---|
| 46 | |
|---|
| 47 | tmp_sources_instance = sources.sources(self) |
|---|
| 48 | self.available_sources = tmp_sources_instance.get_sources_list() |
|---|
| 49 | |
|---|
| 50 | self._init_ctrls(parent) |
|---|
| 51 | |
|---|
| 52 | self.parent = parent |
|---|
| 53 | self.namespace = namespace |
|---|
| 54 | self.interface = interface |
|---|
| 55 | self.my_local_controls = None |
|---|
| 56 | self.component_name = component_name |
|---|
| 57 | self.port_name = port_name |
|---|
| 58 | self.setup_graphics() |
|---|
| 59 | |
|---|
| 60 | # Now Create the menu bar and items |
|---|
| 61 | self.mainmenu = wx.MenuBar() |
|---|
| 62 | |
|---|
| 63 | menu = wx.Menu() |
|---|
| 64 | menu.Append(205, 'E&xit', 'Enough of this already!') |
|---|
| 65 | self.Bind(wx.EVT_MENU, self.OnFileExit, id=205) |
|---|
| 66 | self.mainmenu.Append(menu, '&File') |
|---|
| 67 | |
|---|
| 68 | menu = wx.Menu() |
|---|
| 69 | menu.Append(300, '&About', 'About this thing...') |
|---|
| 70 | self.Bind(wx.EVT_MENU, self.OnHelpAbout, id=300) |
|---|
| 71 | self.mainmenu.Append(menu, '&Help') |
|---|
| 72 | |
|---|
| 73 | self.SetMenuBar(self.mainmenu) |
|---|
| 74 | |
|---|
| 75 | # Bind the close event so we can disconnect the ports |
|---|
| 76 | self.Bind(wx.EVT_CLOSE, self.OnCloseWindow) |
|---|
| 77 | |
|---|
| 78 | self.Show(True) |
|---|
| 79 | |
|---|
| 80 | self.iFileNameEditor.write('i.dat') |
|---|
| 81 | self.qFileNameEditor.write('q.dat') |
|---|
| 82 | |
|---|
| 83 | #set the default source |
|---|
| 84 | #this way if the user presses the "PushPacket" button before selecting |
|---|
| 85 | #a source, the source that is being displayed will be used |
|---|
| 86 | self.iSourceChoice.SetSelection(0) |
|---|
| 87 | self.qSourceChoice.SetSelection(0) |
|---|
| 88 | |
|---|
| 89 | def _init_ctrls(self, prnt): |
|---|
| 90 | wx.Frame.__init__(self, id=wxID_MAINFRAME, name='', parent=prnt, |
|---|
| 91 | pos=wx.Point(1, 570), size=wx.Size(520, 370), |
|---|
| 92 | style=wx.DEFAULT_FRAME_STYLE, title='Arbitrary Waveform Generator') |
|---|
| 93 | |
|---|
| 94 | self.splitterWindow1 = wx.SplitterWindow(id=wxID_SPLITTERWINDOW1, |
|---|
| 95 | name='splitterWindow1', parent=self, point=wx.Point(1, 1), |
|---|
| 96 | size=wx.Size(570, 370), style=wx.SP_3D) |
|---|
| 97 | self.splitterWindow1.SetConstraints(LayoutAnchors(self.splitterWindow1, |
|---|
| 98 | True, True, True, True)) |
|---|
| 99 | |
|---|
| 100 | self.PushPacketBtn = wx.Button(id=wxID_PUSHPACKETBTN, label='Push Packet', |
|---|
| 101 | name='PushPacketBtn', parent=self.splitterWindow1, pos=wx.Point(155, 250), |
|---|
| 102 | size=wx.Size(145, 50)) |
|---|
| 103 | self.PushPacketBtn.SetFont(wx.Font(16, wx.SWISS, wx.NORMAL, wx.BOLD, False)) |
|---|
| 104 | self.PushPacketBtn.SetBackgroundColour("green") |
|---|
| 105 | self.PushPacketBtn.Bind(wx.EVT_BUTTON, self.OnPushPacketBtnButton, |
|---|
| 106 | id=wxID_PUSHPACKETBTN) |
|---|
| 107 | |
|---|
| 108 | ##### |
|---|
| 109 | ## if you want an image on the button |
|---|
| 110 | # button_bmp = wx.Image("../AWG/my_image.bmp", wx.BITMAP_TYPE_BMP).ConvertToBitmap() |
|---|
| 111 | # self.PushPacketBtn = buttons.GenBitmapTextButton(self.splitterWindow1, |
|---|
| 112 | # wxID_PUSHPACKETBTN,button_bmp, 'Push Packet', |
|---|
| 113 | # name='PushPacketBtn', pos=wx.Point(155, 250), |
|---|
| 114 | # size=wx.Size(300, 100), style=0) |
|---|
| 115 | # self.PushPacketBtn.Bind(wx.EVT_BUTTON, self.OnPushPacketBtnButton, |
|---|
| 116 | # id=wxID_PUSHPACKETBTN) |
|---|
| 117 | ##### |
|---|
| 118 | |
|---|
| 119 | self.iSourceChoice = wx.Choice(choices=self.available_sources, |
|---|
| 120 | id=wxID_ISOURCECHOICE, |
|---|
| 121 | name=u'iSourceChoice', parent=self.splitterWindow1, pos=wx.Point(215, 50), |
|---|
| 122 | size=wx.Size(136, 28), style=0) |
|---|
| 123 | self.iSourceChoice.Bind(wx.EVT_CHOICE, self.OnSourceChoiceChoice, |
|---|
| 124 | id=wxID_ISOURCECHOICE) |
|---|
| 125 | |
|---|
| 126 | self.iFileNameEditor = wx.TextCtrl(id=wxID_IFILENAMEEDITOR, |
|---|
| 127 | name=u'iFileNameEditor', parent=self.splitterWindow1, pos=wx.Point(215, 150), |
|---|
| 128 | size=wx.Size(250, 30), style=0, value=u'') |
|---|
| 129 | |
|---|
| 130 | self.qSourceChoice = wx.Choice(choices=self.available_sources, |
|---|
| 131 | id=wxID_QSOURCECHOICE, |
|---|
| 132 | name=u'qSourceChoice', parent=self.splitterWindow1, pos=wx.Point(215, 100), |
|---|
| 133 | size=wx.Size(136, 28), style=0) |
|---|
| 134 | self.qSourceChoice.Bind(wx.EVT_CHOICE, self.OnSourceChoiceChoice, |
|---|
| 135 | id=wxID_QSOURCECHOICE) |
|---|
| 136 | |
|---|
| 137 | self.qFileNameEditor = wx.TextCtrl(id=wxID_QFILENAMEEDITOR, |
|---|
| 138 | name=u'qFileNameEditor', parent=self.splitterWindow1, pos=wx.Point(215, 200), |
|---|
| 139 | size=wx.Size(250, 30), style=0, value=u'') |
|---|
| 140 | |
|---|
| 141 | self.iSourceStaticText = wx.StaticText(id=wxID_ISOURCESTATICTEXT, |
|---|
| 142 | label=u'I channel source:', name='qSourceStaticText', parent=self.splitterWindow1, |
|---|
| 143 | pos=wx.Point(55, 50), size= wx.Size(100, 20), style=0) |
|---|
| 144 | self.iSourceStaticText.SetFont(wx.Font(10,wx.SWISS,wx.NORMAL,wx.BOLD,True,u'Sans')) |
|---|
| 145 | |
|---|
| 146 | self.qSourceStaticText = wx.StaticText(id=wxID_QSOURCESTATICTEXT, |
|---|
| 147 | label=u'Q channel Source:', name='qSourceStaticText', parent=self.splitterWindow1, |
|---|
| 148 | pos=wx.Point(55, 100), size= wx.Size(100, 20), style=0) |
|---|
| 149 | self.qSourceStaticText.SetFont(wx.Font(10,wx.SWISS,wx.NORMAL,wx.BOLD,True,u'Sans')) |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | self.iFileStaticText = wx.StaticText(id=wxID_IFILESTATICTEXT, |
|---|
| 153 | label=u'I channel file:', name='qFileStaticText', parent=self.splitterWindow1, |
|---|
| 154 | pos=wx.Point(55, 150), size= wx.Size(100, 20), style=0) |
|---|
| 155 | self.iFileStaticText.SetFont(wx.Font(10,wx.SWISS,wx.NORMAL,wx.BOLD,True,u'Sans')) |
|---|
| 156 | |
|---|
| 157 | self.qFileStaticText = wx.StaticText(id=wxID_QFILESTATICTEXT, |
|---|
| 158 | label=u'Q channel file:', name='qFileStaticText', parent=self.splitterWindow1, |
|---|
| 159 | pos=wx.Point(55, 200), size= wx.Size(100, 20), style=0) |
|---|
| 160 | self.qFileStaticText.SetFont(wx.Font(10,wx.SWISS,wx.NORMAL,wx.BOLD,True,u'Sans')) |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | def OnSourceChoiceChoice(self,event): |
|---|
| 165 | '''If a source option other than "file" is selected, "grey out" the file input field''' |
|---|
| 166 | |
|---|
| 167 | i_source = self.iSourceChoice.GetStringSelection() |
|---|
| 168 | if i_source == 'file': |
|---|
| 169 | self.iFileNameEditor.Enable(True) |
|---|
| 170 | else: |
|---|
| 171 | self.iFileNameEditor.Enable(False) |
|---|
| 172 | |
|---|
| 173 | q_source = self.qSourceChoice.GetStringSelection() |
|---|
| 174 | if q_source == 'file': |
|---|
| 175 | self.qFileNameEditor.Enable(True) |
|---|
| 176 | else: |
|---|
| 177 | self.qFileNameEditor.Enable(False) |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | def OnPushPacketBtnButton(self,event): |
|---|
| 181 | '''accesses the AWG class to get a signal, then sends the signal''' |
|---|
| 182 | self.i_file_name = self.iFileNameEditor.GetLineText(0) |
|---|
| 183 | self.q_file_name = self.qFileNameEditor.GetLineText(0) |
|---|
| 184 | AWG_instance = AWG(self) |
|---|
| 185 | i_source_selected = self.iSourceChoice.GetSelection() |
|---|
| 186 | q_source_selected = self.qSourceChoice.GetSelection() |
|---|
| 187 | self.I = AWG_instance.get_signal(i_source_selected, self.i_file_name) |
|---|
| 188 | self.Q = AWG_instance.get_signal(q_source_selected, self.q_file_name) |
|---|
| 189 | |
|---|
| 190 | self.I = self.convert_data_type(self.I, self.interface) |
|---|
| 191 | self.Q = self.convert_data_type(self.Q, self.interface) |
|---|
| 192 | |
|---|
| 193 | if len(self.I) == 0 and len(self.Q) == 0: #if there is no data for whatever reason |
|---|
| 194 | print "Packet is empty. Not sending anything." |
|---|
| 195 | else: #there is data, try to send it |
|---|
| 196 | self.PortHandle.pushPacket(self.I,self.Q) |
|---|
| 197 | |
|---|
| 198 | def convert_data_type(self, data, type): |
|---|
| 199 | if type == "complexShort": #short and int are the same in python |
|---|
| 200 | #TODO: test the robustness of the max() module |
|---|
| 201 | #WARNING: if the maximum value of the data is greater than 1, this will probably crash |
|---|
| 202 | #data_max = max(data) |
|---|
| 203 | data_max = 1 |
|---|
| 204 | data = [int(round(float(x)*(32767/data_max))) for x in data] #conver to integer format |
|---|
| 205 | elif type == "complexFloat": |
|---|
| 206 | data = [float(x) for x in data] |
|---|
| 207 | else: |
|---|
| 208 | print "types other than complexFloat and complexShort are not yet supported in sources.convert" |
|---|
| 209 | |
|---|
| 210 | return data |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | def OnFileExit(self, event): |
|---|
| 214 | '''This is what will happen when you select File -> Exit in the menu bar''' |
|---|
| 215 | self.Close() #close the frame |
|---|
| 216 | |
|---|
| 217 | def OnHelpAbout(self, event): |
|---|
| 218 | '''Stuff that gets displayed when you select Help -> About in the menu bar''' |
|---|
| 219 | from wx.lib.dialogs import ScrolledMessageDialog |
|---|
| 220 | about = ScrolledMessageDialog(self, "Arbitrary Waveform Generator.\nA product of Wireless@VT.", "About...") |
|---|
| 221 | about.ShowModal() |
|---|
| 222 | |
|---|
| 223 | def setup_graphics(self): |
|---|
| 224 | self.CORBA_being_used = False |
|---|
| 225 | |
|---|
| 226 | if True: |
|---|
| 227 | self.CORBA_being_used = True |
|---|
| 228 | self.orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID) |
|---|
| 229 | obj = self.orb.resolve_initial_references("NameService") |
|---|
| 230 | rootContext = obj._narrow(CosNaming.NamingContext) |
|---|
| 231 | if rootContext is None: |
|---|
| 232 | print "Failed to narrow the root naming context" |
|---|
| 233 | sys.exit(1) |
|---|
| 234 | name = [CosNaming.NameComponent(self.component_name[0],""), |
|---|
| 235 | CosNaming.NameComponent(self.component_name[1],""), |
|---|
| 236 | CosNaming.NameComponent(self.component_name[2],"")] |
|---|
| 237 | |
|---|
| 238 | try: |
|---|
| 239 | ResourceRef = rootContext.resolve(name) |
|---|
| 240 | |
|---|
| 241 | except: |
|---|
| 242 | print "Required resource not found" |
|---|
| 243 | sys.exit(1) |
|---|
| 244 | |
|---|
| 245 | #connect to an existing port |
|---|
| 246 | ResourceHandle = ResourceRef._narrow(CF.Resource) |
|---|
| 247 | PortReference = ResourceHandle.getPort(self.port_name) |
|---|
| 248 | if PortReference is None: |
|---|
| 249 | print "Failed to get Port reference" |
|---|
| 250 | self.PortHandle = PortReference._narrow(standardInterfaces__POA.complexShort) |
|---|
| 251 | |
|---|
| 252 | def OnCloseWindow(self,event): |
|---|
| 253 | if hasattr(self.parent, 'removeToolFrame'): |
|---|
| 254 | self.parent.removeToolFrame(self) |
|---|
| 255 | self = None |
|---|
| 256 | event.Skip() |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|