root/tools/alf_plugins/AWG/branches/AWG-0.6.2/AWG/AWG.py @ 5791

Revision 5791, 11.2 KB (checked in by jgaeddert, 6 years ago)

fixing imports

  • Property svn:eol-style set to native
Line 
1#!/usr/bin/env python
2
3'''get signals and output them to a port'''
4
5import sources    # within the AWG directory.  This will generate the signals
6import wx         # needed for display stuff
7from omniORB import CORBA      # use this for the CORBA orb stuff
8                               # (pushing packets)
9import sys       # for system commands (e.g., argv and argc stuff)
10import CosNaming   # narrowing naming context stuff
11from ossie.cf import CF, CF__POA    # core framework stuff
12from ossie.standardinterfaces import standardInterfaces__POA
13from wx.lib.anchors import LayoutAnchors  # used by splitter window
14
15class AWG:
16    '''this class makes the calls to get the actual signal from the
17       sources class'''
18
19    def __init__(self, parent):       
20        self.parent = parent
21
22    def get_signal(self, signal_type, file_name):
23        #signal I want is hard-coded for now
24        self.signal_type = signal_type
25        self.signal_len = 8192   #length of packet
26        self.frequency = 5   #cycles per packet
27        self.rand_type = 'random'
28        self.file_name = file_name
29        self.delimiter = ','
30
31        my_signal = sources.sources(self)    #instance of signals class
32        self.the_signal = my_signal.gen_signal()
33
34        return self.the_signal             
35   
36
37def create(parent,namespace, interface, ns_name, port_name):
38    return MainFrame(parent, -1, "Don't know what this should be",
39                     namespace, interface, ns_name, port_name)
40
41#generate wx ids for my wx controls
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)]
43
44
45class MainFrame(wx.Frame):
46    def __init__(self, parent, id, title, namespace, interface,
47                 component_name, port_name):
48
49        tmp_sources_instance = sources.sources(self)
50        self.available_sources = tmp_sources_instance.get_sources_list()
51
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)
81       
82        self.iFileNameEditor.write('i.dat')
83        self.qFileNameEditor.write('q.dat')
84
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)
90
91    def _init_ctrls(self, prnt):
92        wx.Frame.__init__(self, id=wxID_MAINFRAME, name='', parent=prnt,
93              pos=wx.Point(1, 570), size=wx.Size(520, 370),
94              style=wx.DEFAULT_FRAME_STYLE, title='Arbitrary Waveform Generator')
95
96        self.splitterWindow1 = wx.SplitterWindow(id=wxID_SPLITTERWINDOW1,
97              name='splitterWindow1', parent=self, point=wx.Point(1, 1),
98              size=wx.Size(570, 370), style=wx.SP_3D)
99        self.splitterWindow1.SetConstraints(LayoutAnchors(self.splitterWindow1,
100              True, True, True, True))
101
102        self.PushPacketBtn = wx.Button(id=wxID_PUSHPACKETBTN, label='Push Packet',
103              name='PushPacketBtn', parent=self.splitterWindow1, pos=wx.Point(155, 250),
104              size=wx.Size(145, 50))
105        self.PushPacketBtn.SetFont(wx.Font(16, wx.SWISS, wx.NORMAL, wx.BOLD, False))
106        self.PushPacketBtn.SetBackgroundColour("green")
107        self.PushPacketBtn.Bind(wx.EVT_BUTTON, self.OnPushPacketBtnButton,
108              id=wxID_PUSHPACKETBTN)
109 
110        self.iSourceChoice = wx.Choice(choices=self.available_sources.keys(),
111              id=wxID_ISOURCECHOICE,
112              name=u'iSourceChoice', parent=self.splitterWindow1, pos=wx.Point(215, 50),
113              size=wx.Size(136, 28), style=0)
114        self.iSourceChoice.Bind(wx.EVT_CHOICE, self.OnSourceChoiceChoice,
115              id=wxID_ISOURCECHOICE)
116
117        self.iFileNameEditor = wx.TextCtrl(id=wxID_IFILENAMEEDITOR,
118              name=u'iFileNameEditor', parent=self.splitterWindow1, pos=wx.Point(215, 150),
119              size=wx.Size(250, 30), style=0, value=u'')
120                         
121        self.qSourceChoice = wx.Choice(choices=self.available_sources.keys(),
122              id=wxID_QSOURCECHOICE,
123              name=u'qSourceChoice', parent=self.splitterWindow1, pos=wx.Point(215, 100),
124              size=wx.Size(136, 28), style=0)
125        self.qSourceChoice.Bind(wx.EVT_CHOICE, self.OnSourceChoiceChoice,
126              id=wxID_QSOURCECHOICE)
127
128        self.qFileNameEditor = wx.TextCtrl(id=wxID_QFILENAMEEDITOR,
129              name=u'qFileNameEditor', parent=self.splitterWindow1, pos=wx.Point(215, 200),
130              size=wx.Size(250, 30), style=0, value=u'')
131
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'))
136
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
155    def OnSourceChoiceChoice(self,event):
156        '''If a source option other than "file" is selected, "grey out" the file input field'''
157
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
171    def OnPushPacketBtnButton(self,event):
172        '''accesses the AWG class to get a signal, then sends the signal'''
173        self.i_file_name = self.iFileNameEditor.GetLineText(0)
174        self.q_file_name = self.qFileNameEditor.GetLineText(0)
175        AWG_instance = AWG(self)
176        i_source_selected = self.iSourceChoice.GetStringSelection()
177        q_source_selected = self.qSourceChoice.GetStringSelection()
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)
180
181
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
199        if len(self.I) == 0 and len(self.Q) == 0:  #if there is no data for whatever reason
200            print "Packet is empty.  Not sending anything."
201        else:  #there is data, try to send it
202            self.PortHandle.pushPacket(self.I,self.Q)
203
204
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
211            SHRT_MAX = 32767    #have to use -1 because round might round up to 32768
212            data = [int(round(float(x)*(SHRT_MAX/data_max))) for x in data]  #conver to integer format
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
221    def OnFileExit(self, event):
222        '''This is what will happen when you select File -> Exit in the menu bar'''
223        self.Close()      #close the frame
224 
225    def OnHelpAbout(self, event):
226        '''Stuff that gets displayed when you select Help -> About in the menu bar'''
227        from wx.lib.dialogs import ScrolledMessageDialog
228        about = ScrolledMessageDialog(self, "Arbitrary Waveform Generator.\nA product of Wireless@VT.", "About...")
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],""),
243         CosNaming.NameComponent(self.component_name[1],""),
244         CosNaming.NameComponent(self.component_name[2],"")]
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
Note: See TracBrowser for help on using the browser.