Debian Codegraph @

noctar_io/pv_noctar_io.py

raw symbols SCIP
1 #!/usr/bin/python2
2 #----------------------------------------------------------------------------
3 # Name: pv_noctar_io.py
4 # Purpose: This is the IO Panel for Noctar!
5 #
6 # Author: Victor Wollesen
7 #
8 # Created: 2012-12-12
9 # Copyright: (c) 2012 Per Vices Corporation
10 #----------------------------------------------------------------------------
11
12
13 import wx
14 import wx.lib.inspection
15 import wx.lib.wordwrap
16 import wx.lib.mixins.inspection
17 import sys, os
18 import time
19 import subprocess, multiprocessing
20 #import math, numpy, decimal, random
21 #import types
22 import math
23 #import io
24 import string #, StringIO
25
26 #----------------------------------------------------------------------------
27 #
28 ## This is the data collection command line generation function.
29 ## It returns a properly formatted command for execution.
30 #
31
32 def worker_def(self, lockA, stream, cmd):
33 lockA.acquire()
34 try:
35 stream.write('Lock Acquired: '+str(cmd)+'\n')
36 subprocess.check_call( cmd, stdout=stream )
37 except:
38 print 'Error Executing Commands.'
39 finally:
40 stream.write('Lock Releasing \n')
41 lockA.release()
42
43 def open_if_not_exists(filename):
44 try:
45 fd = os.open(filename, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
46 except:
47 print "Ooops - unable to open file."
48 return None
49 fobj = os.fdopen(fd)
50 return fobj
51
52 #----------------------------------------------------------------------------
53
54 class Log:
55 def WriteText(self, text):
56 if text[-1:] == '\n':
57 text = text[:-1]
58 wx.LogMessage(text)
59 write = WriteText
60
61
62 class RunDemoApp(wx.App, wx.lib.mixins.inspection.InspectionMixin):
63 def __init__(self, moduleIO):
64 self.moduleIO = moduleIO
65 wx.App.__init__(self, redirect=False)
66
67 def OnInit(self):
68 wx.Log_SetActiveTarget(wx.LogStderr())
69 self.Init() # InspectionMixin
70
71 frame = wx.Frame(None, -1, title="Per Vices Noctar: IO Panel", pos=(50,50), size=(200,100),
72 style=wx.DEFAULT_FRAME_STYLE, name="Noctar")
73 frame.CreateStatusBar()
74 menuBar = wx.MenuBar()
75
76 self.rx_sr = 125e6 #ADC Samplerate. Note that data rate is twice this.
77 self.tx_sr = 250e6 #DAC Samplerate. The datarate will be twice this..
78
79 #File Menu
80 menu = wx.Menu()
81 item = menu.Append(wx.ID_ABOUT, "&About"," Information about this program")
82 self.Bind(wx.EVT_MENU, self.OnAbout, item)
83 item = menu.Append(-1, "&Widget Inspector\tF6", "Show the wxPython Widget Inspection Tool")
84 self.Bind(wx.EVT_MENU, self.OnWidgetInspector, item)
85 item = menu.Append(-1, "E&xit\tCtrl-Q", "Exit")
86 self.Bind(wx.EVT_MENU, self.OnExitApp, item)
87 menuBar.Append(menu, "&File")
88
89 # Icon and Logo
90 # pvicon = wx.Image(opj('pvlogo.png'), wx.BITMAP_TYPE_ANY)
91 # pvicon = pvicon.ConvertToBitmap()
92 #
93
94 frame.SetMenuBar(menuBar)
95 frame.Show(True)
96 frame.Bind(wx.EVT_CLOSE, self.OnCloseFrame)
97
98 #Get Panels
99 #panelIO = self.moduleIO.initPanel(frame,frame)
100 #panelIO = self.moduleIO.pv_noctar_io_panel(frame)
101 #panelDisplay = self.moduleDisplay.initPanel(frame, frame, Log())
102
103 pv_noctar_util = 'langford_util'
104 pv_noctar_adc_util = 'langford_adc_util'
105 pv_noctar_fsynth = 'langford_rf_fsynth'
106 pv_noctar_vga = 'langford_rx_rf_bb_vga'
107
108 class panelIO_t( self.moduleIO.pv_noctar_io_panel ):
109 #Overriding default form methods
110
111 #Disable Freq if using Baseband stage
112 ##TODO: Implement ddc/duc access using gui)
113 def OnRxReset(self, event):
114 print 'Rx Reset Initiated!'
115 c_pv_rxreset_cmd = []
116 c_pv_rxreset_cmd.append( [pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'RxDspEn', '0' ] )
117 c_pv_rxreset_cmd.append( [pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'RxDspEn', '1' ] )
118 lockRxReset = multiprocessing.Lock() #Acquisition lock
119 getData = []
120 #Initiate all workers
121 for cmd in c_pv_rxreset_cmd:
122 getData.append( multiprocessing.Process(target=worker_def, args=(self, lockRxReset, sys.stdout, cmd) ) )
123 getData[c_pv_rxreset_cmd.index(cmd)].start()
124 getData[c_pv_rxreset_cmd.index(cmd)].join() #No multithreading - wait on child now.
125 print c_pv_rxreset_cmd.index(cmd)
126 print 'Rx Reset Completed.'
127 event.Skip()
128
129 def OnTxReset(self, event):
130 print 'Tx Reset Initiated!'
131 c_pv_txreset_cmd = []
132 c_pv_txreset_cmd.append( [pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'TxDspEn', '0' ] )
133 c_pv_txreset_cmd.append( [pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'TxDspEn', '1' ] )
134 lockTxReset = multiprocessing.Lock() #Acquisition lock
135 getData = []
136 #Initiate all workers
137 for cmd in c_pv_txreset_cmd:
138 getData.append( multiprocessing.Process(target=worker_def, args=(self, lockTxReset, sys.stdout, cmd) ) )
139 getData[c_pv_txreset_cmd.index(cmd)].start()
140 getData[c_pv_txreset_cmd.index(cmd)].join() #No multithreading - wait on child now.
141 print c_pv_txreset_cmd.index(cmd)
142 print 'Tx Reset Completed.'
143 event.Skip()
144
145 def OnDisableRxFreq(self, event):
146 panelIO.pv_noctar_rx_freq.Enable(not( bool( panelIO.pv_noctar_rx_branch_sel.GetSelection() ) ))
147 panelIO.pv_noctar_rx_dec.SetSelection(not( bool( panelIO.pv_noctar_rx_branch_sel.GetSelection() ) ))
148 panelIO.pv_noctar_rx_dec.Enable(not( bool( panelIO.pv_noctar_rx_branch_sel.GetSelection() ) ))
149 panelIO.pv_noctar_rx_dXc.Enable(not( bool( panelIO.pv_noctar_rx_branch_sel.GetSelection() ) ))
150 panelIO.pv_noctar_rx_adc_clk.SetSelection(( bool( panelIO.pv_noctar_rx_branch_sel.GetSelection() ) ))
151 event.Skip()
152
153 def OnDisableTxFreq(self, event):
154 panelIO.pv_noctar_tx_freq.Enable(not( bool( panelIO.pv_noctar_tx_branch_sel.GetSelection() ) ))
155 panelIO.pv_noctar_tx_int.SetSelection(not( bool( panelIO.pv_noctar_tx_branch_sel.GetSelection() ) ))
156 ##panelIO.pv_noctar_tx_int.Enable(not( bool( panelIO.pv_noctar_tx_branch_sel.GetSelection() ) ))
157 panelIO.pv_noctar_tx_dXc.Enable(not( bool( panelIO.pv_noctar_tx_branch_sel.GetSelection() ) ))
158
159 event.Skip()
160
161 def OnApply(self, event):
162 c_pv_cmd = []
163
164 #This could be implemented in a far better and more classful way. Too late...
165 #The following returns the pin and value necessary to pass to the appropriate utility program
166 #Receive Parameters
167 c_pv_noctar_rx_branch_sel = [pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'NASRxA', str( panelIO.pv_noctar_rx_branch_sel.GetSelection() ) ]
168 c_pv_cmd.append(c_pv_noctar_rx_branch_sel)
169
170 #If the branch is Low, enable the VGA/LNA. Otherwise, disable it
171 if( not(bool( panelIO.pv_noctar_rx_branch_sel.GetSelection() )) ):
172 c_pv_noctar_rx_freq = [ pv_noctar_fsynth, str(panelIO.pv_noctar_dev_path.GetValue()), 'rx', str( float(panelIO.pv_noctar_rx_freq.GetValue()) ), '2' ]
173 c_pv_cmd.append(c_pv_noctar_rx_freq)
174 c_pv_noctar_rx_rf_gain = [ pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'N21at0', str( int( panelIO.pv_noctar_rx_rf_gain.GetValue()) ) ]
175 c_pv_cmd.append(c_pv_noctar_rx_rf_gain)
176 #We're inverting the VGA outputs in order to always have a higher number correspond to higher gain. This is strictly
177 #to present a consistent user interface. Reference the LMH6521 for actual power specifications.
178 c_pv_noctar_rx_rf_vga0 = [ pv_noctar_vga, str(panelIO.pv_noctar_dev_path.GetValue()), '0', str( float( panelIO.pv_noctar_rx_rf_vga0.GetValue()*(-1)+63 )/2 ), '2' ]
179 c_pv_cmd.append(c_pv_noctar_rx_rf_vga0)
180 c_pv_noctar_rx_rf_vga1 = [ pv_noctar_vga, str(panelIO.pv_noctar_dev_path.GetValue()), '1', str( float( panelIO.pv_noctar_rx_rf_vga1.GetValue()*(-1)+63)/2 ), '2' ]
181 c_pv_cmd.append(c_pv_noctar_rx_rf_vga1)
182 c_pv_noctar_rx_dec = [ pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'RXDecEn', str( hex(pow(2,panelIO.pv_noctar_rx_dec.GetSelection()) - 1) ) ]
183 c_pv_cmd.append(c_pv_noctar_rx_dec)
184 if (float(panelIO.pv_noctar_rx_dXc.GetValue()) > 0):
185 c_pv_noctar_rx_dXcc = [ pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'RXRevFreq', '0' ]
186 c_pv_cmd.append(c_pv_noctar_rx_dXcc)
187 elif (float(panelIO.pv_noctar_rx_dXc.GetValue()) < 0):
188 c_pv_noctar_rx_dXcc = [ pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'RXRevFreq', '1' ]
189 c_pv_cmd.append(c_pv_noctar_rx_dXcc)
190 c_pv_noctar_rx_dXc = [ pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'RXPhase', str( abs(int(float(panelIO.pv_noctar_rx_dXc.GetValue())*(pow(2,32))/125e6))) ]
191 c_pv_cmd.append(c_pv_noctar_rx_dXc)
192 #Interleave Clocking
193 c_pv_noctar_rx_adc_clk_A = [ pv_noctar_adc_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'A', 'ClkSel', str( int( panelIO.pv_noctar_rx_adc_clk.GetSelection()) ) ]
194 c_pv_cmd.append(c_pv_noctar_rx_adc_clk_A)
195 c_pv_noctar_rx_adc_clk_B = [ pv_noctar_adc_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'B', 'ClkSel', str( int( 0 ) ) ]
196 c_pv_cmd.append(c_pv_noctar_rx_adc_clk_B)
197 #Set Decimation and Sample Rate
198 panelIO.pv_noctar_fb_rx_sr.SetValue('{:g}'.format(float(250e6/(2**panelIO.pv_noctar_rx_dec.GetSelection()))))
199
200 elif ( bool ( panelIO.pv_noctar_rx_branch_sel.GetSelection() ) ):
201 c_pv_noctar_rx_bb_mode_sel = [ pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'N3HILO', str( panelIO.pv_noctar_rx_bb_mode_sel.GetSelection() ) ]
202 c_pv_cmd.append(c_pv_noctar_rx_bb_mode_sel)
203 c_pv_noctar_rx_bb_en = [ pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'N3ENB', str( int( panelIO.pv_noctar_rx_branch_sel.GetSelection() ) ) ]
204 c_pv_cmd.append(c_pv_noctar_rx_bb_en)
205 c_pv_noctar_rx_bb_gain = [ pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'N3GAIN', str( int( panelIO.pv_noctar_rx_bb_gain.GetValue()) ) ]
206 c_pv_cmd.append(c_pv_noctar_rx_bb_gain)
207 #Interleave Clocking
208 c_pv_noctar_rx_adc_clk_A = [ pv_noctar_adc_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'A', 'ClkSel', str( int( panelIO.pv_noctar_rx_adc_clk.GetSelection()) ) ]
209 c_pv_cmd.append(c_pv_noctar_rx_adc_clk_A)
210 c_pv_noctar_rx_adc_clk_B = [ pv_noctar_adc_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'B', 'ClkSel', str( int( 0 ) ) ]
211 c_pv_cmd.append(c_pv_noctar_rx_adc_clk_B)
212
213 #Determine Sample Rate and display
214 panelIO.pv_noctar_fb_rx_sr.SetValue('{:g}'.format(float(250e6)))
215
216 #Transmission Parameters
217 c_pv_noctar_tx_branch_sel = [ pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'NASTxA', str( panelIO.pv_noctar_tx_branch_sel.GetSelection() ) ]
218 c_pv_cmd.append(c_pv_noctar_tx_branch_sel)
219
220 #Tx High
221 if( not(bool( panelIO.pv_noctar_tx_branch_sel.GetSelection() )) ):
222 c_pv_noctar_tx_freq = [ pv_noctar_fsynth, str(panelIO.pv_noctar_dev_path.GetValue()), 'tx', str( float(panelIO.pv_noctar_tx_freq.GetValue()) ), '2' ]
223 c_pv_cmd.append(c_pv_noctar_tx_freq)
224 c_pv_noctar_tx_int = [ pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'TXIntEn', str( hex(pow(2,panelIO.pv_noctar_tx_int.GetSelection()) - 1) ) ]
225 c_pv_cmd.append(c_pv_noctar_tx_int)
226 c_pv_noctar_tx_rf_gain = [ pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'N22at0', str( int( panelIO.pv_noctar_tx_rf_gain.GetValue()) ) ]
227 c_pv_cmd.append(c_pv_noctar_tx_rf_gain)
228 if (float(panelIO.pv_noctar_tx_dXc.GetValue()) > 0):
229 c_pv_noctar_tx_dXcc = [ pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'TXRevFreq', '0' ]
230 c_pv_cmd.append(c_pv_noctar_tx_dXcc)
231 elif (float(panelIO.pv_noctar_tx_dXc.GetValue()) < 0):
232 c_pv_noctar_tx_dXcc = [ pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'TXRevFreq', '1' ]
233 c_pv_cmd.append(c_pv_noctar_tx_dXcc)
234 c_pv_noctar_tx_dXc = [ pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'TXPhase', str( abs(int(float(panelIO.pv_noctar_tx_dXc.GetValue())*(pow(2,32))/125e6))) ]
235 c_pv_cmd.append(c_pv_noctar_tx_dXc)
236 #Determine Sample Rate and display
237 panelIO.pv_noctar_fb_tx_sr.SetValue('{:g}'.format(float(125e6/(2**panelIO.pv_noctar_tx_int.GetSelection()))))
238 #Tx Low - No additional parameters.
239 elif ( bool( panelIO.pv_noctar_tx_branch_sel.GetSelection() )):
240 c_pv_noctar_tx_int = [ pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'TXIntEn', str( hex(pow(2,panelIO.pv_noctar_tx_int.GetSelection()) - 1) ) ]
241 c_pv_cmd.append(c_pv_noctar_tx_int)
242 #Determine Sample Rate and display
243 panelIO.pv_noctar_fb_tx_sr.SetValue('{:g}'.format(float(125e6/(2**panelIO.pv_noctar_tx_int.GetSelection()))))
244
245 #Clock and Timing
246 c_pv_noctar_clk_sel = [ pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'NRXTALSEL', str( panelIO.pv_noctar_clk_sel.GetSelection() ) ]
247 c_pv_cmd.append(c_pv_noctar_clk_sel)
248 c_pv_noctar_clk_vcobypass = [ pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'NRVCOSEL', str( panelIO.pv_noctar_clk_vcobypass.GetSelection() ) ]
249 c_pv_cmd.append(c_pv_noctar_clk_vcobypass)
250
251 #Device/DSP related
252 c_pv_noctar_rx_dsp_en = [ pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'RxDspEn', str( panelIO.pv_noctar_rx_dsp_en.GetSelection() ) ]
253
254 #Setting RxDspEn to 0 is not recommended.
255 #c_pv_cmd.append(c_pv_noctar_rx_dsp_en)
256 c_pv_noctar_tx_dsp_en = [ pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'TxDspEn', str( panelIO.pv_noctar_tx_dsp_en.GetSelection() ) ]
257 #Setting TxDspEn to 0 is not recommended.
258 #c_pv_cmd.append(c_pv_noctar_tx_dsp_en)
259 c_pv_noctar_rx_adc0_pwr = [ pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'ADCAPD', str( panelIO.pv_noctar_rx_adc0_pwr.GetSelection() ) ]
260 #Setting ADC0 power to 0 is not recommended.
261 #c_pv_cmd.append(c_pv_noctar_rx_adc0_pwr)
262 c_pv_noctar_rx_adc1_pwr = [ pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'ADCBPD', str( panelIO.pv_noctar_rx_adc1_pwr.GetSelection() ) ]
263 #Setting ADC1 power to 0 is not recommended.
264 #c_pv_cmd.append(c_pv_noctar_rx_adc1_pwr)
265 c_pv_noctar_tx_dac_pwr = [ pv_noctar_util, str(panelIO.pv_noctar_dev_path.GetValue()), 'DACSDO', str( panelIO.pv_noctar_tx_dac_pwr.GetSelection()) ]
266 c_pv_cmd.append(c_pv_noctar_tx_dac_pwr)
267
268 self.c_pv_cmd = c_pv_cmd
269
270 ### Multiprocessing parameters ###
271 #This needs to be fixed; this code is only here because
272 #I know it works; it's copypasta from an earlier project.
273 lockA = multiprocessing.Lock() #Acquisition lock
274 getData = []
275 #Initiate all workers
276 for cmd in c_pv_cmd:
277 getData.append( multiprocessing.Process(target=worker_def, args=(self, lockA, sys.stdout, cmd) ) )
278 getData[c_pv_cmd.index(cmd)].start()
279 getData[c_pv_cmd.index(cmd)].join() #No multithreading - wait on child now.
280 print c_pv_cmd.index(cmd)
281
282 event.Skip()
283
284
285
286 panelIO = panelIO_t( frame )
287
288 self.Bind(wx.EVT_BUTTON, self.OnApply, panelIO.g_pv_io_buttonApply)
289
290 panelIO.WindowStyle = wx.SUNKEN_BORDER
291 box = wx.BoxSizer(wx.VERTICAL)
292
293 box.Add(panelIO, 0, wx.CENTER)
294 frame.SetAutoLayout(True)
295 frame.SetSizerAndFit(box)
296
297 #Set File Configuration Parameters
298 self.SetTopWindow(frame)
299 self.frame = frame
300 self.panelIO = panelIO
301 return True
302
303 def OnApply(self, evt):
304 print 'Caught against button.'
305
306
307 def OnAbout(self,evt):
308 # First we create and fill the info object
309 info = wx.AboutDialogInfo()
310 info.Name = "Per Vices Noctar IO Panel"
311 info.Version = "0.1"
312 info.Copyright = "(C) 2012 Per Vices Corporation"
313 info.Description = wx.lib.wordwrap.wordwrap(
314 "This utility provides a demonstration interface to the Noctar transceiver"
315 "and demonstrates its functionality."
316 "\n\nThis program is dual licensed under the GPLv3 and commerical licenses."
317 "\n",
318 400, wx.ClientDC(self.frame))
319 info.WebSite = ("http://www.pervices.com", "www.pervices.com")
320 info.Developers = [ "Per Vices Corporation\n\t\t[email protected]" #,
321 #"Engineering\n\n\t\tYi Yao\n\t\[email protected]\n"
322 ]
323 #info.License
324
325 # Then we call wx.AboutBox giving it that info object
326 wx.AboutBox(info)
327
328 def OnExitApp(self, evt):
329 self.frame.Close(True)
330
331 def OnCloseFrame(self, evt):
332 if hasattr(self, "window") and hasattr(self.window, "ShutdownDemo"):
333 self.window.ShutdownDemo()
334 evt.Skip()
335
336 def OnWidgetInspector(self, evt):
337 #wx.lib.inspection.InspectionTool().Show()
338 evt.Skip()
339
340
341 #----------------------------------------------------------------------------
342 def opj(path):
343 """Convert paths to the platform-specific separator"""
344 st = apply(os.path.join, tuple(path.split('/')))
345 # HACK: on Linux, a leading / gets lost...
346 if path.startswith('/'):
347 st = '/' + st
348 return st
349 #----------------------------------------------------------------------------
350
351 def main(argv):
352 #if len(argv) < 2:
353 # print "Please specify a demo module name on the command-line"
354 # raise SystemExit
355
356 name0, ext0 = os.path.splitext('pv_noctar_io_panel.py')
357 module0 = __import__(name0)
358
359 app = RunDemoApp(module0)
360 app.MainLoop()
361
362 if __name__ == "__main__":
363 main(sys.argv)