1+ import argparse
2+ import sys
3+
14from .pyhackrf_tools import (
5+ pyhackrf_info ,
26 pyhackrf_operacake ,
3- pyhackrf_transfer ,
47 pyhackrf_sweep ,
5- pyhackrf_info ,
8+ pyhackrf_transfer ,
69)
710from .pylibhackrf import pyhackrf
8- import argparse
9- import sys
1011
1112
12- def main ():
13+ def main () -> None :
1314 parser = argparse .ArgumentParser (
1415 description = 'python_hackrf is a Python wrapper for libhackrf and hackrf-tools.' ,
15- usage = 'python_hackrf [-h] {info, sweep, operacake} ...'
16+ usage = 'python_hackrf [-h] {info, sweep, operacake} ...' ,
1617 )
1718 subparsers = parser .add_subparsers (dest = 'command' , title = 'Available commands' )
1819 subparsers .required = True
1920 pyhackrf_info_parser = subparsers .add_parser (
20- 'info' , help = 'Read device information from HackRF such as serial number and firmware version.' , usage = 'python_hackrf info [-h] [-f] [-s]'
21+ 'info' , help = 'Read device information from HackRF such as serial number and firmware version.' , usage = 'python_hackrf info [-h] [-f] [-s]' ,
2122 )
2223 pyhackrf_info_parser .add_argument ('-f' , '--full' , action = 'store_true' , help = 'show info like in hackrf_info' )
2324 pyhackrf_info_parser .add_argument ('-s' , '--serial_numbers' , action = 'store_true' , help = 'show only founded serial_numbers' )
2425
2526 pyhackrf_operacake_parser = subparsers .add_parser (
26- 'operacake' , help = 'Configure Opera Cake antenna switch connected to HackRF.' , usage = 'python_hackrf operacake [-h] [-d] [-o] [-m] [-a] [-b] [-f] [-t] [-w] [-l] [-g]'
27+ 'operacake' , help = 'Configure Opera Cake antenna switch connected to HackRF.' , usage = 'python_hackrf operacake [-h] [-d] [-o] [-m] [-a] [-b] [-f] [-t] [-w] [-l] [-g]' ,
2728 )
2829 pyhackrf_operacake_parser .add_argument ('-d' , action = 'store' , help = 'serial_number. serial number of desired HackRF' , metavar = '' )
2930 pyhackrf_operacake_parser .add_argument ('-o' , '--address' , action = 'store' , help = 'specify a particular Opera Cake by address. Default is 0' , metavar = '' , default = 0 )
@@ -37,7 +38,7 @@ def main():
3738 pyhackrf_operacake_parser .add_argument ('-g' , '--gpio_test' , action = 'store_true' , help = 'test GPIO functionality of an Opera Cake' )
3839
3940 pyhackrf_sweep_parser = subparsers .add_parser (
40- 'sweep' , help = 'Command-line spectrum analyzer.' , usage = 'python_hackrf sweep [-h] [-d] [-a] [-f] [-p] [-l] [-g] [-w] [-1] [-N] [-B] [-S] [-s] [-b] [-r]'
41+ 'sweep' , help = 'Command-line spectrum analyzer.' , usage = 'python_hackrf sweep [-h] [-d] [-a] [-f] [-p] [-l] [-g] [-w] [-1] [-N] [-B] [-S] [-s] [-b] [-r]' ,
4142 )
4243 pyhackrf_sweep_parser .add_argument ('-d' , action = 'store' , help = 'serial number of desired HackRF' , metavar = '' )
4344 pyhackrf_sweep_parser .add_argument ('-a' , action = 'store_true' , help = 'RX RF amplifier. If specified = Enable' )
@@ -55,7 +56,7 @@ def main():
5556 pyhackrf_sweep_parser .add_argument ('-r' , action = 'store' , help = '<filename> output file' , metavar = '' )
5657
5758 pyhackrf_transfer_parser = subparsers .add_parser (
58- 'transfer' , help = 'Send and receive signals using HackRF. Input/output files consist of complex64 quadrature samples.' , usage = 'python_hackrf transfer [-h] [-d] [-r] [-t] [-f] [-i] [-o] [-m] [-a] [-p] [-l] [-g] [-x] [-s] [-N] [-R] -[b] [-H]'
59+ 'transfer' , help = 'Send and receive signals using HackRF. Input/output files consist of complex64 quadrature samples.' , usage = 'python_hackrf transfer [-h] [-d] [-r] [-t] [-f] [-i] [-o] [-m] [-a] [-p] [-l] [-g] [-x] [-s] [-N] [-R] -[b] [-H]' ,
5960 )
6061 pyhackrf_transfer_parser .add_argument ('-d' , action = 'store' , help = 'serial number of desired HackRF' , metavar = '' )
6162 pyhackrf_transfer_parser .add_argument ('-r' , action = 'store' , help = '<filename> receive data into file (use "-" for stdout)' , metavar = '' )
@@ -79,13 +80,13 @@ def main():
7980 parser .print_help ()
8081 sys .exit (0 )
8182
82- args , unparsed_args = parser .parse_known_args ()
83+ args , _ = parser .parse_known_args ()
8384
8485 if args .command == 'info' :
8586 if args .serial_numbers :
8687 pyhackrf_info .pyhackrf_serial_numbers_list_info ()
87- else :
88- pyhackrf_info .pyhackrf_info ()
88+ return
89+ pyhackrf_info .pyhackrf_info ()
8990
9091 elif args .command == 'operacake' :
9192 if sum ([args .list , args .mode is not None , args .f is not None , args .t is not None , args .gpio_test , (args .a is not None or args .b is not None )]) == 1 :
@@ -96,80 +97,63 @@ def main():
9697 )
9798 return
9899
99- elif args .mode in ('manual' , 'frequency' , 'time' ):
100- address = 0
101- if args .address is not None :
102- try :
103- address = int (args .address )
104- except Exception :
105- pass
100+ if args .mode in {'manual' , 'frequency' , 'time' }:
101+ try :
102+ address = int (args .address )
103+ except Exception :
104+ address = 0
105+
106106 pyhackrf_operacake .pyhackrf_set_operacake_mode (
107107 address ,
108108 args .mode ,
109- serial_number = args .d
109+ serial_number = args .d ,
110110 )
111111 return
112112
113- elif args .f is not None :
113+ if args .f is not None :
114114 str_freq_ranges = args .f .split (',' )
115115 freq_ranges = []
116116 for freq_range in str_freq_ranges :
117- freq_range = freq_range .split (':' )
118- port , freq_min , freq_max = None , None , None
119117 try :
120- port = freq_range [0 ]
118+ port , freq_min , freq_max = freq_range .split (':' )
119+ freq_min , freq_max = int (freq_min ), int (freq_max )
120+ if port in pyhackrf .operacake_ports .keys ():
121+ freq_ranges .extend ([port , freq_min , freq_max ])
121122 except Exception :
122123 pass
123- try :
124- freq_min = int (freq_range [1 ])
125- except Exception :
126- pass
127- try :
128- freq_max = int (freq_range [2 ])
129- except Exception :
130- pass
131-
132- if freq_min is not None and freq_max is not None and port in ('A1' , 'A2' , 'A3' , 'A4' , 'B1' , 'B2' , 'B3' , 'B4' ):
133- freq_ranges .extend ([port , freq_min , freq_max ])
134124
135125 pyhackrf_operacake .pyhackrf_set_operacake_freq_ranges (
136126 freq_ranges ,
137- serial_number = args .d
127+ serial_number = args .d ,
138128 )
139129 return
140130
141- elif args .t is not None :
131+ if args .t is not None :
142132 str_dwell_times = args .f .split (',' )
143133 dwell_times = []
144134 for dwell_time in str_dwell_times :
145- dwell_time = dwell_time .split (':' )
146- dwell , port = None
147- if len (dwell_time ) == 2 :
148- try :
149- port = dwell_time [0 ]
150- except Exception :
151- pass
152- try :
153- dwell = int (dwell_time [1 ])
154- except Exception :
155- pass
156- else :
157- port = dwell_time [0 ]
158- try :
135+ try :
136+ dwell_split = dwell_time .split (':' )
137+
138+ if len (dwell_split ) == 2 :
139+ port , dwell = dwell_split
140+ dwell = int (dwell )
141+ else :
142+ port = dwell_split [0 ]
159143 dwell = int (args .w )
160- except Exception :
161- pass
162144
163- if dwell is not None and port in ('A1' , 'A2' , 'A3' , 'A4' , 'B1' , 'B2' , 'B3' , 'B4' ):
164- dwell_times .extend ([dwell , port ])
145+ if port in pyhackrf .operacake_ports .keys ():
146+ dwell_times .extend ([dwell , port ])
147+ except Exception :
148+ pass
165149
166150 pyhackrf_operacake .pyhackrf_set_operacake_dwell_times (
167151 dwell_times ,
168- serial_number = args .d
152+ serial_number = args .d ,
169153 )
170154 return
171155
172- elif (args .a is not None or args .b is not None ):
156+ if (args .a is not None or args .b is not None ):
173157 address , port_a , port_b = 0 , 'A1' , 'B1'
174158 if args .a is not None :
175159 port_a = args .a
@@ -193,7 +177,7 @@ def main():
193177 else :
194178 port_b = 'B1'
195179
196- if port_a in ( 'A1' , 'A2' , 'A3' , 'A4' , 'B1' , 'B2' , 'B3' , 'B4' ) and port_b in ( 'A1' , 'A2' , 'A3' , 'A4' , 'B1' , 'B2' , 'B3' , 'B4' ):
180+ if port_a in pyhackrf . operacake_ports . keys ( ) and port_b in pyhackrf . operacake_ports . keys ( ):
197181 if port_a [0 ] != port_b [0 ]:
198182 pyhackrf_operacake .pyhackrf_set_operacake_ports (
199183 address ,
@@ -219,25 +203,15 @@ def main():
219203 )
220204 return
221205
222- print ('Argument error' )
223- print (pyhackrf_sweep_parser .usage )
224-
225206 elif args .command == 'sweep' :
226207 str_frequencies = args .f .split (',' )
227208 frequencies = []
228209 for frequency_range in str_frequencies :
229- frequency_range = frequency_range .split (':' )
230- freq_min , freq_max = None , None
231210 try :
232- freq_min = int (frequency_range [0 ])
233- except Exception :
234- pass
235- try :
236- freq_max = int (frequency_range [1 ])
211+ freq_min , freq_max = map (int , frequency_range .split (':' ))
212+ frequencies .extend ([freq_min , freq_max ])
237213 except Exception :
238214 pass
239- if freq_min is not None and freq_max is not None :
240- frequencies .extend ([freq_min , freq_max ])
241215
242216 pyhackrf_sweep .pyhackrf_sweep (
243217 frequencies = frequencies ,
0 commit comments