From a70b964dd304078d88b13c19a91530864dad0409 Mon Sep 17 00:00:00 2001 From: Arturs Artamonovs Date: Mon, 13 Mar 2023 22:42:11 +0000 Subject: Fixed API for AirSpyHF class. --- airspyhf/airspyhf.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++------ airspyhf_rx.py | 2 +- test_class.py | 41 ++++++++++++++++++++++++++++++++- 3 files changed, 99 insertions(+), 9 deletions(-) diff --git a/airspyhf/airspyhf.py b/airspyhf/airspyhf.py index 4314c76..562d8e5 100644 --- a/airspyhf/airspyhf.py +++ b/airspyhf/airspyhf.py @@ -4,31 +4,47 @@ from ctypes import * class AirSpyHF: dev_p = airspyhf_device_t_p(None) sample_rates = [] + initalized = False def __init__(self,): - self.dev_p = None + #self.dev_p = airspyhf_device_t_p(None) + pass - def open(self, device_index:int=None, serialnumber:int=None): + def open(self, device_index=None, serialnumber=None): if serialnumber is not None: - ret = libairspyhf.airspyhf_open_sn(self.dev_p, serialnumber) + print("open by serial number") + ret = libairspyhf.airspyhf_open_sn(self.dev_p, 0x3b52ab5dada12535) if ret != 0: print("Cant open device by serial number") return -1 + self.initalized = True elif device_index is not None: + print("open by index") ndev = libairspyhf.airspyhf_list_devices(None, 0) if ndev < device_index+1: print("Device index higher then device num") return -1 + print(ndev) serial = c_uint64(0) libairspyhf.airspyhf_list_devices(byref(serial), device_index + 1) + print("try to open by serial ",hex(serial.value)) + ret = libairspyhf.airspyhf_open_sn(self.dev_p, serial.value) + if ret != 0: + print("Cant open device by index") + return -1 + self.initalized = True return 0 def get_samplerates(self): + if not self.initalized: + return [] nsrates = c_uint32(0) + print("get sample rate number") ret = libairspyhf.airspyhf_get_samplerates(self.dev_p, byref(nsrates), c_uint32(0)) if ret != 0: print("Cant get number of avaliable sample rates") return [] - supported_samplerates = (c_uint32 * nsrates)(0) + print("supported sample rates values") + supported_samplerates = (c_uint32 * nsrates.value)(0) ret = libairspyhf.airspyhf_get_samplerates(self.dev_p, supported_samplerates, nsrates) if ret != 0: print("Cant get avaliable sample rate list") @@ -37,6 +53,8 @@ class AirSpyHF: return self.sample_rates def set_samplerate(self, samplerate:int): + if not self.initalized: + return -1 if self.sample_rates == []: self.get_samplerates() if samplerate not in self.sample_rates: @@ -51,25 +69,58 @@ class AirSpyHF: return 0 def set_hf_agc(self,flag): + if not self.initalized: + return -1 ret = libairspyhf.airspyhf_set_hf_agc(self.dev_p, flag) return ret def set_hf_agc_threshold(self,flag): + if not self.initalized: + return -1 ret = libairspyhf.airspyhf_set_hf_agc_threshold(self.dev_p, flag) return ret def set_hf_att(self, value): - ret = libairspyhf.airspyhf_set_hf_att(dev_p, value) + if not self.initalized: + return -1 + ret = libairspyhf.airspyhf_set_hf_att(self.dev_p, value) return ret - + def set_hf_lna(self,flag): - ret = libairspyhf.airspyhf_set_hf_lna(dev_p, 1) + if not self.initalized: + return -1 + ret = libairspyhf.airspyhf_set_hf_lna(self.dev_p, flag) return ret + def start(self, read_samples): + if not self.initalized: + return -1 + ret = libairspyhf.airspyhf_start(self.dev_p, airspyhf_sample_block_cb_fn(read_samples), None) + if ret != 0: + print(f"airspyhf_start ret={ret}") + + def is_streaming(self): + if not self.initalized: + return -1 + return libairspyhf.airspyhf_is_streaming(self.dev_p) + + def stop(self): + if not self.initalized: + return -1 + ret = libairspyhf.airspyhf_stop(self.dev_p) + if ret != 0: + print(f"airspyhf_stop ret={ret}") + return -1 + return 0 + def close(self): + if not self.initalized: + return -1 ret = libairspyhf.close(self.dev_p) if ret != 0: print("Cant close device") + return -1 + return 0 diff --git a/airspyhf_rx.py b/airspyhf_rx.py index a3b5634..2ae35f7 100755 --- a/airspyhf_rx.py +++ b/airspyhf_rx.py @@ -54,7 +54,7 @@ ret = libairspyhf.airspyhf_get_samplerates(dev_p,byref(nsrates),c_uint32(0)) print("ret %d"%ret) print("sample rates %d"% nsrates.value) -supported_samplerates = (c_uint32*4)(0) +supported_samplerates = (c_uint32*nsrates.value)(0) ret = libairspyhf.airspyhf_get_samplerates(dev_p,supported_samplerates,nsrates) print("ret %d"%ret) print("Sample rate list:") diff --git a/test_class.py b/test_class.py index 35a6fd3..b8eea64 100755 --- a/test_class.py +++ b/test_class.py @@ -8,6 +8,45 @@ import argparse airspy = AirSpyHF() -airspy.open(device_index=0) +if -1 == airspy.open(device_index=0): + print("Couldnt open device") + sys.exit(1) +print("1") +airspy.set_samplerate(192000) +print("1") +airspy.set_hf_agc(1) +print("1") +airspy.set_hf_agc_threshold(0) +print("1") +airspy.set_hf_lna(1) +print("1") +sample_count = 0 +def read_samples(transfer): + global sample_count + global wave_file + print("Python call back") + t = transfer.contents + bytes_to_write = t.sample_count * 4 * 2 + rx_buffer = t.samples + #print(f"{bytes_to_write} bytes receieved") + sample_count += t.sample_count + #for i in range(0,t.sample_count): + #d_re = t.samples[i].re + #d_im = t.samples[i].im + #data = struct.pack("