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