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