root/experimental/components/rc2007_gui/rc2007_gui.py @ 4804

Revision 4804, 9.0 KB (checked in by DrewCormier, 7 years ago)

added basic frame for starting wx stuff, orb. also added wx_inits to my install script

  • Property svn:executable set to *
Line 
1#! /usr/bin/env python
2
3'''
4/****************************************************************************
5
6Copyright 2007 Virginia Polytechnic Institute and State University
7
8This file is part of the OSSIE rc2007_gui.
9
10OSSIE rc2007_gui is free software; you can redistribute it and/or modify
11it under the terms of the GNU General Public License as published by
12the Free Software Foundation; either version 2 of the License, or
13(at your option) any later version.
14
15OSSIE rc2007_gui is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with OSSIE rc2007_gui; if not, write to the Free Software
22Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
24****************************************************************************/
25
26'''
27
28from omniORB import CORBA
29from omniORB import URI
30import CosNaming
31import CF, CF__POA
32import standardInterfaces__POA
33import customInterfaces__POA
34import sys
35
36import WorkModule  # module found in the component directory.
37                   # this module is where the main processing
38                   # thread resides. 
39import threading
40import time        # primarily availble for time.sleep() statements
41
42import wx          # gui stuff
43import wx_inits    # my frame init code: specific to this application
44
45
46#-------------------------------------------------------------
47# rc2007_gui_i class definition (main component class)
48#-------------------------------------------------------------
49class rc2007_gui_i(CF__POA.Resource):
50    def __init__(self, uuid, label, poa):
51        CF._objref_Resource.__init__(self._this())
52        print "rc2007_gui_i __init__: " + label
53        self.naming_service_name = label
54        self.poa = poa
55
56        self.inPort0_servant = dataIn_complexShort_i(self, "gui_sound_in")
57        self.inPort0_var = self.inPort0_servant._this()
58
59        self.outPort0_servant = dataOut_complexShort_i(self, "gui_sound_out")
60        self.outPort0_var = self.outPort0_servant._this()
61        self.outPort0_active = False
62
63       
64        self.WorkModule_created = False
65
66        self.propertySet = []
67        self.work_mod = None
68       
69    def start(self):
70        print "rc2007_gui start called"
71       
72    def stop(self):
73        print "rc2007_gui stop called"
74       
75    def getPort(self, id):
76        if str(id) == "gui_sound_in":
77            return self.inPort0_var
78        if str(id) == "gui_sound_out":
79            return self.outPort0_var
80       
81        return None  #port not found in available ports list
82       
83    def initialize(self):
84        print "rc2007_gui initialize called"
85   
86    def configure(self, props):
87        ''' The configure method is called twice by the framework:
88        once to read the default properties in the component.prf
89        file, and once to read the component instance properties
90        storred in the waveform.sad file.  This method should be
91        called before the start method.  This method is where
92        the properties are read in by the component. 
93        '''
94
95        print "rc2007_gui configure called"
96        buffer_size = 0
97       
98        __READ_PROPS__
99   
100        # make sure that only one WorkModule thread is started,
101        # even if configure method is called more than once   
102        if not self.WorkModule_created:
103            self.work_mod = WorkModule.WorkClass(self, buffer_size)
104            self.WorkModule_created = True   
105
106    def query(self, props):
107        return self.propertySet
108   
109    def releaseObject(self):
110        # release the main work module
111        self.work_mod.Release()
112       
113        # release the main process threads for the ports
114        self.outPort0_servant.releasePort()
115               
116        # deactivate the ports
117        iid0 = self.poa.reference_to_id(self.inPort0_var)
118        oid0 = self.poa.reference_to_id(self.outPort0_var)
119
120        self.poa.deactivate_object(iid0)
121        self.poa.deactivate_object(oid0)
122
123
124#------------------------------------------------------------------
125# dataIn_complexShort_i class definition
126#------------------------------------------------------------------
127class dataIn_complexShort_i(standardInterfaces__POA.complexShort):
128    def __init__(self, parent, name):
129        self.parent = parent
130        self.name = name
131
132    # WARNING:  I and Q may have to be changed depending on what data you are receiving (e.g., bytesIn for realChar)
133    def pushPacket(self, I, Q):
134        self.parent.work_mod.AddData(I, Q)
135
136
137#------------------------------------------------------------------
138# dataOut_complexShort_i class definition
139#------------------------------------------------------------------
140class dataOut_complexShort_i(CF__POA.Port):
141    def __init__(self, parent, name):
142        self.parent = parent
143        self.outPorts = {}
144        self.name = name
145       
146        self.data_buffer = []
147        self.data_event = threading.Event()
148        self.data_buffer_lock = threading.Lock()
149       
150        self.is_running = True
151        self.process_thread = threading.Thread(target = self.Process)
152        self.process_thread.start()
153
154    def connectPort(self, connection, connectionId):
155        port = connection._narrow(standardInterfaces__POA.complexShort)
156        self.outPorts[str(connectionId)] = port
157        self.parent.outPort0_active = True
158
159    def disconnectPort(self, connectionId):
160        self.outPorts.pop(str(connectionId))
161        if len(self.outPorts)==0:
162            self.parent.outPort0_active = False
163
164    def releasePort(self):
165        # shut down the Process thread
166        self.is_running = False
167        self.data_event.set()
168
169    # WARNING:  I and Q may have to be changed depending on what data you are receiving (e.g., bytesIn for realChar)
170    def send_data(self, I, Q):
171        self.data_buffer_lock.acquire()
172        self.data_buffer.insert(0, (I,Q))
173        self.data_buffer_lock.release()
174        self.data_event.set()
175
176    def Process(self):
177        while self.is_running:
178            self.data_event.wait()
179            while len(self.data_buffer) > 0:
180                self.data_buffer_lock.acquire()
181                new_data = self.data_buffer.pop()
182                self.data_buffer_lock.release()
183               
184                for port in self.outPorts.values():
185                    port.pushPacket(new_data[0], new_data[1])
186               
187                self.data_event.clear()
188
189
190
191#-------------------------------------------------------------------
192# ORB_Init class definition
193#-------------------------------------------------------------------
194class ORB_Init:
195    """Takes care of initializing the ORB and bind the object"""
196   
197    def __init__(self, uuid, label):
198        # initialize the orb
199        self.orb = CORBA.ORB_init()
200       
201       
202
203def ORB_start_fun():
204        orb_ref = orb
205
206        # get the POA
207        obj_poa = orb_ref.orb.resolve_initial_references("RootPOA")
208        poaManager = obj_poa._get_the_POAManager()
209        poaManager.activate()
210       
211        ns_obj = orb_ref.orb.resolve_initial_references("NameService")
212        rootContext = ns_obj._narrow(CosNaming.NamingContext)
213       
214        # create the main component object
215        orb_ref.USRP_Commander_GUI_Obj = USRP_Commander_GUI_i(orb_ref,
216                                                              uuid,
217                                                              label,
218                                                              obj_poa)
219        USRP_Commander_GUI_var = orb_ref.USRP_Commander_GUI_Obj._this()
220       
221        name = URI.stringToName(label)
222        rootContext.rebind(name, USRP_Commander_GUI_var)
223
224
225        orb_ref.orb.run()
226
227
228#-------------------------------------------------------------------
229# wx.App for the GUI
230#-------------------------------------------------------------------
231class App(wx.App):
232    def OnInit(self):
233        self.frame = wx_inits.Frame()
234        self.frame.Show()
235        self.SetTopWindow(self.frame)
236
237        return True
238
239
240class my_threads:
241    def __init__(self):
242
243        # start the orb that has already been initialized in a thread
244        self.orb_thread = threading.Thread(target=ORB_start_fun)
245        self.orb_thread.start()
246
247        self.app_thread = threading.Thread(target=app.MainLoop)
248        self.app_thread.start()
249
250       
251#-------------------------------------------------------------------
252# Code run when this file is executed
253#-------------------------------------------------------------------
254if __name__ == "__main__":
255    if len(sys.argv) != 3:
256        print sys.argv[0] + " <id> <usage name> "
257   
258    uuid = str(sys.argv[1])
259    label = str(sys.argv[2])
260   
261    print "Identifier - " + uuid + "  Label - " + label
262   
263    # initialize the orb, but do not start it
264    orb = ORB_Init(uuid, label)
265   
266    # initialize the wx stuff
267    app = App()
268
269    # now that orb and app exist, I can start them
270    # note that they must be created first so that they can reference
271    # each other when they start
272    threads_running = my_threads()
273
Note: See TracBrowser for help on using the browser.