aboutsummaryrefslogtreecommitdiffstats
path: root/airspyhf/airspyhf.py
blob: 562d8e5738622aafb57ecc515e460d78f9d70bb0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from .libairspyhf import *
from ctypes import *

class AirSpyHF:
    dev_p = airspyhf_device_t_p(None)
    sample_rates = []
    initalized = False
    def __init__(self,):
        #self.dev_p =  airspyhf_device_t_p(None)
        pass

    def open(self, device_index=None, serialnumber=None):
        if serialnumber is not None:
            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 []
        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")
            return []
        self.sample_rates = list(supported_samplerates)
        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:
            print(f"Unknown sample rate. Avaliable samplerate {self.sample_rates}")
            return -1

        ret = libairspyhf.airspyhf_set_samplerate(self.dev_p, samplerate)
        if ret != 0:
            print("Cannot set samplerate")
            return -1

        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):
        if not self.initalized:
            return -1
        ret = libairspyhf.airspyhf_set_hf_att(self.dev_p, value)
        return ret

    def set_hf_lna(self,flag):
        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