2323from python_hackrf import pyhackrf
2424
2525
26- def pyhackrf_operacake_info (device : pyhackrf .PyHackrfDevice = None ,
26+ def pyhackrf_operacake_info (device : pyhackrf .PyHackrfDevice | None = None ,
2727 serial_number : str | None = None ,
2828 print_to_console : bool = True ,
2929 ) -> str | None :
@@ -39,8 +39,12 @@ def pyhackrf_operacake_info(device: pyhackrf.PyHackrfDevice = None,
3939 device = pyhackrf .pyhackrf_open ()
4040
4141 operacake_info = ''
42- boards = device .pyhackrf_get_operacake_boards ()
43- if len (boards ):
42+ boards = []
43+
44+ if device :
45+ boards = device .pyhackrf_get_operacake_boards ()
46+
47+ if device and len (boards ):
4448 operacake_info += 'Opera Cakes found:\n '
4549 for i in range (len (boards )):
4650 address = boards [i ]
@@ -50,7 +54,8 @@ def pyhackrf_operacake_info(device: pyhackrf.PyHackrfDevice = None,
5054 operacake_info = 'Opera Cakes found: None'
5155
5256 if initialize :
53- device .pyhackrf_close ()
57+ if device :
58+ device .pyhackrf_close ()
5459 pyhackrf .pyhackrf_exit ()
5560
5661 if print_to_console :
@@ -63,7 +68,7 @@ def pyhackrf_operacake_info(device: pyhackrf.PyHackrfDevice = None,
6368def pyhackrf_set_operacake_mode (address : int ,
6469 mode : str ,
6570 serial_number : str | None = None ,
66- device : pyhackrf .PyHackrfDevice = None ,
71+ device : pyhackrf .PyHackrfDevice | None = None ,
6772 ) -> None :
6873
6974 initialize = True if device is None else False
@@ -77,22 +82,24 @@ def pyhackrf_set_operacake_mode(address: int,
7782 device = pyhackrf .pyhackrf_open ()
7883
7984 if mode == 'frequency' :
80- mode = pyhackrf .py_operacake_switching_mode .OPERACAKE_MODE_FREQUENCY
85+ operacake_mode = pyhackrf .py_operacake_switching_mode .OPERACAKE_MODE_FREQUENCY
8186 elif mode == 'time' :
82- mode = pyhackrf .py_operacake_switching_mode .OPERACAKE_MODE_TIME
87+ operacake_mode = pyhackrf .py_operacake_switching_mode .OPERACAKE_MODE_TIME
8388 else :
84- mode = pyhackrf .py_operacake_switching_mode .OPERACAKE_MODE_MANUAL
89+ operacake_mode = pyhackrf .py_operacake_switching_mode .OPERACAKE_MODE_MANUAL
8590
86- device .pyhackrf_set_operacake_mode (address , mode )
91+ if device :
92+ device .pyhackrf_set_operacake_mode (address , operacake_mode )
8793
8894 if initialize :
89- device .pyhackrf_close ()
95+ if device :
96+ device .pyhackrf_close ()
9097 pyhackrf .pyhackrf_exit ()
9198
9299
93- def pyhackrf_set_operacake_freq_ranges (freq_ranges : list ,
100+ def pyhackrf_set_operacake_freq_ranges (freq_ranges : list [ tuple [ int , int , int ]] ,
94101 serial_number : str | None = None ,
95- device : pyhackrf .PyHackrfDevice = None ,
102+ device : pyhackrf .PyHackrfDevice | None = None ,
96103 ) -> None :
97104
98105 initialize = True if device is None else False
@@ -105,16 +112,18 @@ def pyhackrf_set_operacake_freq_ranges(freq_ranges: list,
105112 else :
106113 device = pyhackrf .pyhackrf_open ()
107114
108- device .pyhackrf_set_operacake_freq_ranges (freq_ranges )
115+ if device :
116+ device .pyhackrf_set_operacake_freq_ranges (freq_ranges )
109117
110118 if initialize :
111- device .pyhackrf_close ()
119+ if device :
120+ device .pyhackrf_close ()
112121 pyhackrf .pyhackrf_exit ()
113122
114123
115- def pyhackrf_set_operacake_dwell_times (dwell_times : list ,
124+ def pyhackrf_set_operacake_dwell_times (dwell_times : list [ tuple [ int , int ]] ,
116125 serial_number : str | None = None ,
117- device : pyhackrf .PyHackrfDevice = None ,
126+ device : pyhackrf .PyHackrfDevice | None = None ,
118127 ) -> None :
119128
120129 initialize = True if device is None else False
@@ -127,18 +136,20 @@ def pyhackrf_set_operacake_dwell_times(dwell_times: list,
127136 else :
128137 device = pyhackrf .pyhackrf_open ()
129138
130- device .pyhackrf_set_operacake_dwell_times (dwell_times )
139+ if device :
140+ device .pyhackrf_set_operacake_dwell_times (dwell_times )
131141
132142 if initialize :
133- device .pyhackrf_close ()
143+ if device :
144+ device .pyhackrf_close ()
134145 pyhackrf .pyhackrf_exit ()
135146
136147
137148def pyhackrf_set_operacake_ports (address : int ,
138149 port_a : str ,
139150 port_b : str ,
140151 serial_number : str | None = None ,
141- device : pyhackrf .PyHackrfDevice = None ,
152+ device : pyhackrf .PyHackrfDevice | None = None ,
142153 ) -> None :
143154
144155 initialize = True if device is None else False
@@ -151,16 +162,18 @@ def pyhackrf_set_operacake_ports(address: int,
151162 else :
152163 device = pyhackrf .pyhackrf_open ()
153164
154- device .pyhackrf_set_operacake_ports (address , port_a , port_b )
165+ if device :
166+ device .pyhackrf_set_operacake_ports (address , port_a , port_b )
155167
156168 if initialize :
157- device .pyhackrf_close ()
169+ if device :
170+ device .pyhackrf_close ()
158171 pyhackrf .pyhackrf_exit ()
159172
160173
161174def pyhackrf_operacake_gpio_test (address : int ,
162175 serial_number : str | None = None ,
163- device : pyhackrf .PyHackrfDevice = None ,
176+ device : pyhackrf .PyHackrfDevice | None = None ,
164177 ) -> None :
165178
166179 initialize = True if device is None else False
@@ -173,30 +186,34 @@ def pyhackrf_operacake_gpio_test(address: int,
173186 else :
174187 device = pyhackrf .pyhackrf_open ()
175188
176- test_result = device .pyhackrf_operacake_gpio_test (address )
177- if test_result == 0xFFFF :
178- print ('GPIO mode disabled.' )
179- print ('Remove additional add-on boards and retry.' )
180-
181- elif test_result :
182- reg , mask = 0x7 , 0x7
183-
184- print ("u2ctrl1\t %d\t %d\t %d\n " , (reg >> 2 ) & 1 , (reg >> 1 ) & 1 , reg & 1 )
185- test_result >>= 3
186- reg = test_result & mask
187- print ("u2ctrl0\t %d\t %d\t %d" , (reg >> 2 ) & 1 , (reg >> 1 ) & 1 , reg & 1 )
188- test_result >>= 3
189- reg = test_result & mask
190- print ("u3ctrl1\t %d\t %d\t %d" , (reg >> 2 ) & 1 , (reg >> 1 ) & 1 , reg & 1 )
191- test_result >>= 3
192- reg = test_result & mask
193- print ("u3ctrl0\t %d\t %d\t %d" , (reg >> 2 ) & 1 , (reg >> 1 ) & 1 , reg & 1 )
194- test_result >>= 3
195- reg = test_result & mask
196- print ("u1ctrl \t %d\t %d\t %d\n " , (reg >> 2 ) & 1 , (reg >> 1 ) & 1 , reg & 1 )
189+ if device :
190+ test_result = device .pyhackrf_operacake_gpio_test (address )
191+ if test_result == 0xFFFF :
192+ print ('GPIO mode disabled.' )
193+ print ('Remove additional add-on boards and retry.' )
194+
195+ elif test_result :
196+ reg , mask = 0x7 , 0x7
197+
198+ print ("u2ctrl1\t %d\t %d\t %d\n " , (reg >> 2 ) & 1 , (reg >> 1 ) & 1 , reg & 1 )
199+ test_result >>= 3
200+ reg = test_result & mask
201+ print ("u2ctrl0\t %d\t %d\t %d" , (reg >> 2 ) & 1 , (reg >> 1 ) & 1 , reg & 1 )
202+ test_result >>= 3
203+ reg = test_result & mask
204+ print ("u3ctrl1\t %d\t %d\t %d" , (reg >> 2 ) & 1 , (reg >> 1 ) & 1 , reg & 1 )
205+ test_result >>= 3
206+ reg = test_result & mask
207+ print ("u3ctrl0\t %d\t %d\t %d" , (reg >> 2 ) & 1 , (reg >> 1 ) & 1 , reg & 1 )
208+ test_result >>= 3
209+ reg = test_result & mask
210+ print ("u1ctrl \t %d\t %d\t %d\n " , (reg >> 2 ) & 1 , (reg >> 1 ) & 1 , reg & 1 )
211+ else :
212+ print ('GPIO test passed' )
197213 else :
198- print ('GPIO test passed ' )
214+ print ('device not found ' )
199215
200216 if initialize :
201- device .pyhackrf_close ()
217+ if device :
218+ device .pyhackrf_close ()
202219 pyhackrf .pyhackrf_exit ()
0 commit comments