Skip to content

Commit 6ab2141

Browse files
committed
cleaned up the code
1 parent d8529d2 commit 6ab2141

File tree

11 files changed

+219
-178
lines changed

11 files changed

+219
-178
lines changed

android/libbladerf/__init__.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import os
22
import shutil
3+
from typing import Any
34

4-
import sh
5-
from pythonforandroid.archs import Arch
6-
from pythonforandroid.logger import shprint
7-
from pythonforandroid.recipe import NDKRecipe, Recipe
8-
from pythonforandroid.util import current_directory
5+
import sh # type: ignore
6+
from pythonforandroid.archs import Arch # type: ignore
7+
from pythonforandroid.logger import shprint # type: ignore
8+
from pythonforandroid.recipe import NDKRecipe, Recipe # type: ignore
9+
from pythonforandroid.util import current_directory # type: ignore
910

1011

11-
class LibbladerfRecipe(NDKRecipe):
12+
class LibbladerfRecipe(NDKRecipe): # type: ignore
1213

1314
url = 'git+https://github.com/Nuand/bladeRF.git'
1415
version = 'f81b82aa8e75ee8866d1aede2a09191d92399829'
@@ -56,11 +57,11 @@ def prebuild_arch(self, arch: Arch) -> None:
5657
'-DLIBBLADERF_BACKEND_CONFIG_DIR=' + os.path.join(self.get_build_dir(arch.arch), 'host', 'libraries', 'libbladeRF', 'src', 'backend'),
5758
'-DLIBBLADERF_ROOT_DIR=' + self.get_build_dir(arch.arch),
5859
'-P',
59-
os.path.join(self.get_recipe_dir(),'pre_install.cmake'),
60+
os.path.join(self.get_recipe_dir(), 'pre_install.cmake'),
6061
)
6162

62-
def get_recipe_env(self, arch: Arch) -> dict:
63-
env = super().get_recipe_env(arch)
63+
def get_recipe_env(self, arch: Arch) -> dict[str, Any]:
64+
env: dict[str, Any] = super().get_recipe_env(arch)
6465
env['LDFLAGS'] += f'-L{self.ctx.get_libs_dir(arch.arch)}'
6566

6667
return env
@@ -71,7 +72,7 @@ def get_jni_dir(self, arch: Arch) -> str:
7172
def get_lib_dir(self, arch: Arch) -> str:
7273
return os.path.join(self.get_build_dir(arch.arch), 'android', 'obj', 'local', arch.arch)
7374

74-
def build_arch(self, arch: Arch, *extra_args) -> None:
75+
def build_arch(self, arch: Arch, *extra_args: Any) -> None:
7576
env = self.get_recipe_env(arch)
7677

7778
shutil.copyfile(os.path.join(self.ctx.get_libs_dir(arch.arch), 'libusb1.0.so'), os.path.join(self.get_build_dir(arch.arch), 'android', 'jni', 'libusb1.0.so'))

android/libusb/__init__.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import os
22
import shutil
3+
from typing import Any
34

4-
import sh
5-
from pythonforandroid.archs import Arch
6-
from pythonforandroid.logger import shprint
7-
from pythonforandroid.recipe import NDKRecipe
8-
from pythonforandroid.util import current_directory
5+
import sh # type: ignore
6+
from pythonforandroid.archs import Arch # type: ignore
7+
from pythonforandroid.logger import shprint # type: ignore
8+
from pythonforandroid.recipe import NDKRecipe # type: ignore
9+
from pythonforandroid.util import current_directory # type: ignore
910

1011

11-
class LibusbRecipe(NDKRecipe):
12+
class LibusbRecipe(NDKRecipe): # type: ignore
1213

1314
url = 'https://github.com/libusb/libusb/archive/refs/tags/v{version}.tar.gz'
1415
generated_libraries = ('libusb-1.0.so', )
@@ -25,7 +26,7 @@ def get_jni_dir(self, arch: Arch) -> str:
2526
def get_lib_dir(self, arch: Arch) -> str:
2627
return os.path.join(self.get_build_dir(arch.arch), 'android', 'obj', 'local', arch.arch)
2728

28-
def build_arch(self, arch: Arch, *extra_args) -> None:
29+
def build_arch(self, arch: Arch, *extra_args: Any) -> None:
2930
env = self.get_recipe_env(arch)
3031
with current_directory(self.get_build_dir(arch.arch)):
3132
shprint(

android/python_bladerf/__init__.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
import os
2+
from typing import Any
23

3-
from pythonforandroid.archs import Arch
4-
from pythonforandroid.recipe import (
4+
from pythonforandroid.archs import Arch # type: ignore
5+
from pythonforandroid.recipe import ( # type: ignore
56
CythonRecipe,
67
Recipe,
78
)
89

910

10-
class PythonBladerfRecipe(CythonRecipe):
11+
class PythonBladerfRecipe(CythonRecipe): # type: ignore
1112
url = 'https://github.com/GvozdevLeonid/python_bladerf/releases/download/v.{version}/python_bladerf-{version}.tar.gz'
1213
depends = ('python3', 'setuptools', 'numpy', 'pyjnius', 'libbladerf')
1314
site_packages_name = 'python_bladerf'
1415
name = 'python_bladerf'
15-
version = '1.3.0'
16+
version = '1.3.1'
1617

17-
def get_recipe_env(self, arch: Arch) -> dict:
18-
env = super().get_recipe_env(arch)
18+
def get_recipe_env(self, arch: Arch) -> dict[str, Any]:
19+
env: dict[str, Any] = super().get_recipe_env(arch)
1920

2021
libbladerf_recipe = Recipe.get_recipe('libbladerf', arch)
2122

python_bladerf/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = '1.3.0'
1+
__version__ = '1.3.1'
22

33
from python_bladerf.pylibbladerf import pybladerf # noqa F401
44
from python_bladerf.pybladerf_tools import ( # noqa F401

python_bladerf/__main__.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ def main() -> None:
5959
pybladerf_transfer_parser.add_argument('-H', action='store_true', help='synchronize RX/TX to external trigger input')
6060
pybladerf_transfer_parser.add_argument('-o', action='store_true', help='oversample. If specified = Enable')
6161

62-
6362
if len(sys.argv) == 1:
6463
parser.print_help()
6564
sys.exit(0)
@@ -82,26 +81,26 @@ def main() -> None:
8281
pass
8382

8483
pybladerf_sweep.pybladerf_sweep(frequencies=frequencies,
85-
sample_rate=float(args.s) * 1e6,
86-
baseband_filter_bandwidth=float(args.b) * 1e6 if args.b is not None else None,
84+
sample_rate=int(float(args.s) * 1e6),
85+
baseband_filter_bandwidth=int(float(args.b) * 1e6) if args.b is not None else None,
8786
gain=int(args.g),
8887
bin_width=int(args.w),
8988
channel=int(args.c),
9089
oversample=args.o,
9190
antenna_enable=args.p,
92-
sweep_style=pybladerf.pybladerf_sweep_style.PYBLADERF_SWEEP_STYLE_LINEAR if args.s == 'L' else (pybladerf.pybladerf_sweep_style.PYBLADERF_SWEEP_STYLE_INTERLEAVED if args.s == 'I' else -1),
91+
sweep_style=pybladerf.pybladerf_sweep_style.PYBLADERF_SWEEP_STYLE_LINEAR if args.s == 'L' else (pybladerf.pybladerf_sweep_style.PYBLADERF_SWEEP_STYLE_INTERLEAVED if args.s == 'I' else -1), # type: ignore
9392
serial_number=args.d,
9493
binary_output=args.B,
95-
one_shot=args.__dict__.get('1'),
94+
one_shot=args.__dict__.get('1'), # type: ignore
9695
num_sweeps=int(args.N) if args.N is not None else None,
9796
filename=args.r,
9897
print_to_console=True)
9998

10099
elif args.command == 'transfer':
101100
pybladerf_transfer.pybladerf_transfer(
102101
frequency=int(args.freq_hz),
103-
sample_rate=int(args.s) * 1e6,
104-
baseband_filter_bandwidth=float(args.b) * 1e6 if args.b is not None else None,
102+
sample_rate=int(float(args.s) * 1e6),
103+
baseband_filter_bandwidth=int(float(args.b) * 1e6) if args.b is not None else None,
105104
gain=int(args.g),
106105
channel=int(args.c),
107106
oversample=args.o,

python_bladerf/pybladerf_tools/pybladerf_info.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,18 @@ def pybladerf_info(print_to_console: bool = True) -> str | None:
3232
if device_list.device_count > 0:
3333
for i in range(device_list.device_count):
3434
device = pybladerf.pybladerf_open_by_serial(device_list.serial_numbers[i])
35-
print_info += 'Found BladeRF:\n'
36-
print_info += f'Board: {device.pybladerf_get_board_name()} ({device.pybladerf_get_fpga_size()})\n'
37-
print_info += f'Instance: {device_list.instances[i]}\n'
38-
print_info += f'Serial number: {device_list.serial_numbers[i]}\n'
39-
print_info += f'USB Bus Address: {device_list.usb_buses[i]} {device_list.usb_addresses[i]}\n'
40-
print_info += f'Backend: {device_list.backends[i]}\n'
41-
print_info += f'USB Speed: {device.pybladerf_device_speed()}\n'
42-
print_info += f'FPGA Version: {device.pybladerf_fpga_version()}\n'
43-
print_info += f'VCTCXO DAC calibration: {hex(device.pybladerf_get_vctcxo_trim())}\n'
35+
if device:
36+
print_info += 'Found BladeRF:\n'
37+
print_info += f'Board: {device.pybladerf_get_board_name()} ({device.pybladerf_get_fpga_size()})\n'
38+
print_info += f'Instance: {device_list.instances[i]}\n'
39+
print_info += f'Serial number: {device_list.serial_numbers[i]}\n'
40+
print_info += f'USB Bus Address: {device_list.usb_buses[i]} {device_list.usb_addresses[i]}\n'
41+
print_info += f'Backend: {device_list.backends[i]}\n'
42+
print_info += f'USB Speed: {device.pybladerf_device_speed()}\n'
43+
print_info += f'FPGA Version: {device.pybladerf_fpga_version()}\n'
44+
print_info += f'VCTCXO DAC calibration: {hex(device.pybladerf_get_vctcxo_trim())}\n'
4445

45-
device.pybladerf_close()
46+
device.pybladerf_close()
4647
else:
4748
print_info += 'No BladeRF boards found.'
4849

@@ -54,7 +55,7 @@ def pybladerf_info(print_to_console: bool = True) -> str | None:
5455
return print_info
5556

5657

57-
def pybladerf_serial_numbers_list_info(print_to_console: bool = True) -> tuple[int, list] | None:
58+
def pybladerf_serial_numbers_list_info(print_to_console: bool = True) -> tuple[int, list[str]] | None:
5859
device_list = pybladerf.PyBladeRFDeviceList()
5960
device_count = device_list.device_count
6061
serial_numbers = device_list.serial_numbers

python_bladerf/pybladerf_tools/utils.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def __init__(self, dtype: type = np.complex64, use_thread: bool = False) -> None
5757

5858
if use_thread:
5959
self._run_available = True
60-
self._queue = Queue()
60+
self._queue = Queue() # type: ignore
6161
self._append_thread = Thread(target=self._append, daemon=True)
6262
self._append_thread.start()
6363

@@ -115,7 +115,7 @@ def _cleanup(self) -> None:
115115
except Exception as er:
116116
print(f'Exception during cleanup: {er}', file=sys.stderr)
117117

118-
if os.path.exists(filepath) and not self._save_file:
118+
if os.path.exists(filepath):
119119
os.remove(filepath)
120120

121121
def _register_cleanup(self) -> None:
@@ -139,11 +139,11 @@ def _append(self) -> None:
139139
self._not_empty.set()
140140

141141
if not self._run_available:
142-
break
142+
break # type: ignore
143143
else:
144144
time.sleep(.035)
145145

146-
def append(self, data: np.ndarray, chunk_size: int = 131072) -> None:
146+
def append(self, data: np.ndarray[Any, Any], chunk_size: int = 131072) -> None:
147147
if len(data) == 0:
148148
return
149149
if self._use_thread:
@@ -164,7 +164,7 @@ def append(self, data: np.ndarray, chunk_size: int = 131072) -> None:
164164
if not self._run_available:
165165
break
166166

167-
def get_all(self, use_memmap: bool = False, wait: bool = False, timeout: float | None = None) -> np.ndarray:
167+
def get_all(self, use_memmap: bool = False, wait: bool = False, timeout: float | None = None) -> np.ndarray[Any, Any]:
168168
with self._rlock:
169169

170170
if self._write_ptr == 0:
@@ -174,13 +174,13 @@ def get_all(self, use_memmap: bool = False, wait: bool = False, timeout: float |
174174

175175
if not use_memmap:
176176
self._reader.seek(0)
177-
result = np.frombuffer(self._reader.read(), dtype=self._dtype)
177+
result: np.ndarray[Any, Any] = np.frombuffer(self._reader.read(), dtype=self._dtype)
178178
self._reader.seek(self._read_ptr)
179179
return result
180180

181181
return np.memmap(self._temp_file, dtype=self._dtype)
182182

183-
def get_new(self, wait: bool = False, timeout: float | None = None) -> np.ndarray:
183+
def get_new(self, wait: bool = False, timeout: float | None = None) -> np.ndarray[Any, Any]:
184184
with self._rlock:
185185

186186
write_ptr = self._write_ptr
@@ -194,11 +194,11 @@ def get_new(self, wait: bool = False, timeout: float | None = None) -> np.ndarra
194194

195195
write_ptr = self._write_ptr
196196

197-
result = np.frombuffer(self._reader.read(write_ptr - self._read_ptr), dtype=self._dtype)
197+
result: np.ndarray[Any, Any] = np.frombuffer(self._reader.read(write_ptr - self._read_ptr), dtype=self._dtype)
198198
self._read_ptr = write_ptr
199199
return result
200200

201-
def get_chunk(self, num_elements: int, ring: bool = True, wait: bool = False, timeout: float | None = None) -> np.ndarray:
201+
def get_chunk(self, num_elements: int, ring: bool = True, wait: bool = False, timeout: float | None = None) -> np.ndarray[Any, Any]:
202202
with self._rlock:
203203

204204
if num_elements <= 0:
@@ -218,6 +218,8 @@ def get_chunk(self, num_elements: int, ring: bool = True, wait: bool = False, ti
218218
total_bytes = num_elements * self._dtype_size
219219
available_bytes = write_ptr - self._read_ptr
220220

221+
result: np.ndarray[Any, Any]
222+
221223
if available_bytes >= total_bytes:
222224
result = np.frombuffer(self._reader.read(total_bytes), dtype=self._dtype)
223225
self._read_ptr += total_bytes

python_bladerf/pylibbladerf/__android.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,51 +20,53 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
# SOFTWARE.
2222

23+
2324
from threading import Event
25+
from typing import Any
2426

2527
try:
26-
from jnius import (
28+
from jnius import ( # type: ignore
2729
autoclass,
2830
cast,
2931
)
3032
except ImportError:
31-
def autoclass(item):
33+
def autoclass(item: Any) -> None:
3234
raise RuntimeError('autoclass not available')
3335

34-
def cast(item):
36+
def cast(item: Any) -> None:
3537
raise RuntimeError('cast not available')
3638

3739
try:
38-
from android.broadcast import BroadcastReceiver
39-
from android.runnable import run_on_ui_thread
40+
from android.broadcast import BroadcastReceiver # type: ignore
41+
from android.runnable import run_on_ui_thread # type: ignore
4042
except ImportError:
41-
def BroadcastReceiver(item):
43+
def BroadcastReceiver(item: Any) -> None: # noqa N802
4244
raise RuntimeError('BroadcastReceiver not available')
4345

44-
def run_on_ui_thread(f):
46+
def run_on_ui_thread(f: Any) -> None:
4547
raise RuntimeError('run_on_ui_thread not available')
4648

4749

4850
class USBBroadcastReceiver:
49-
def __init__(self, events: dict) -> None:
51+
def __init__(self, events: dict[Any, Any]) -> None:
5052
self.events = events
5153

52-
@run_on_ui_thread
54+
@run_on_ui_thread # type: ignore
5355
def start(self) -> None:
5456
self.br = BroadcastReceiver(self.on_broadcast, actions=['libusb.android.USB_PERMISSION'])
5557
self.br.start()
5658

5759
def stop(self) -> None:
5860
self.br.stop()
5961

60-
def on_broadcast(self, context, intent) -> None:
62+
def on_broadcast(self, context: Any, intent: Any) -> None:
6163
action = intent.getAction()
62-
UsbManager = autoclass('android.hardware.usb.UsbManager')
64+
usb_manager = autoclass('android.hardware.usb.UsbManager')
6365
if action == 'libusb.android.USB_PERMISSION':
64-
usb_device = cast('android.hardware.usb.UsbDevice', intent.getParcelableExtra(UsbManager.EXTRA_DEVICE))
66+
usb_device = cast('android.hardware.usb.UsbDevice', intent.getParcelableExtra(usb_manager.EXTRA_DEVICE))
6567

6668
if usb_device is not None:
67-
granted = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, False)
69+
granted = intent.getBooleanExtra(usb_manager.EXTRA_PERMISSION_GRANTED, False)
6870
device_name = usb_device.getDeviceName()
6971

7072
if device_name in self.events:
@@ -77,8 +79,8 @@ def on_broadcast(self, context, intent) -> None:
7779
bladerf_usb_pids = (0x5246, 0x5250, 0x6066)
7880

7981

80-
def get_bladerf_device_list(num_devices: int | None = None) -> list:
81-
events = {}
82+
def get_bladerf_device_list(num_devices: int | None = None) -> list[tuple[int, int, str, str]]:
83+
events: dict[Any, Any] = {}
8284
bladerf_device_list = []
8385
usb_broadcast_receiver = USBBroadcastReceiver(events)
8486

@@ -113,7 +115,7 @@ def get_bladerf_device_list(num_devices: int | None = None) -> list:
113115
if num_devices is not None and idx + 1 == num_devices:
114116
break
115117

116-
if len(events):
118+
if events:
117119
usb_broadcast_receiver.start()
118120
for _, info in events.items():
119121
info['event'].wait()

0 commit comments

Comments
 (0)