SDDC_Driver
Loading...
Searching...
No Matches
RadioHandler.h
1/*
2 * This file is part of SDDC_Driver.
3 *
4 * Copyright (C) 2020 - Oscar Steila
5 * Copyright (C) 2020 - Howard Su
6 * Copyright (C) 2025 - RenardSpark
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#ifndef _H_RADIOHANDLER
23#define _H_RADIOHANDLER
24
25#include <array>
26#include <vector>
27#include <stdio.h>
28#include <stdlib.h>
29#include <math.h>
30#include <stdint.h>
31
32#include "config.h"
33#include "FX3Class.h"
34#include "radio/RadioHardware.h"
35#include "fft_mt_r2iq/fft_mt_r2iq.h"
36#include "dsp/ringbuffer.h"
37
38using namespace std;
39
40class RadioHardware;
41
42enum {
43 RESULT_OK,
44 RESULT_BIG_STEP,
45 RESULT_TOO_HIGH,
46 RESULT_TOO_LOW,
47 RESULT_NOT_POSSIBLE
48};
49
52
54public:
57 sddc_err_t Init(SDDC::DeviceItem dev_index);
58 sddc_err_t AttachReal(void (*callback)(void* context, const int16_t*, uint32_t), void* context = nullptr);
59 sddc_err_t AttachIQ(void (*callback)(void* context, const sddc_complex_t*, uint32_t), void* context = nullptr);
60 sddc_err_t Start(bool convert_r2iq);
61 sddc_err_t Stop();
62 sddc_err_t Pause();
63 sddc_err_t Resume();
64
65 // --- r2iq --- //
66 sddc_err_t SetDecimation(uint8_t decimate);
67
68 // ----- RF mode ----- //
69 sddc_rf_mode_t GetBestRFMode(uint64_t freq);
70 sddc_rf_mode_t GetRFMode();
71 sddc_err_t SetRFMode(sddc_rf_mode_t mode);
72
73 // --- ADC --- //
74 array<float, 2> GetADCSampleRateLimits();
75 uint32_t GetADCSampleRate();
76 sddc_err_t SetADCSampleRate(uint32_t samplefreq);
77
78 // --- Tuner --- //
79 uint32_t GetCenterFrequency();
80 sddc_err_t SetCenterFrequency(uint32_t freq);
81
82 // --- RF/IF adjustments --- //
83 vector<float> GetRFGainSteps(sddc_rf_mode_t mode = NOMODE);
84 array<float, 2> GetRFGainRange(sddc_rf_mode_t mode = NOMODE);
85 float GetRFGain();
86 sddc_err_t SetRFGain(float new_att);
87
88 vector<float> GetIFGainSteps(sddc_rf_mode_t mode = NOMODE);
89 array<float, 2> GetIFGainRange(sddc_rf_mode_t mode = NOMODE);
90 float GetIFGain();
91 sddc_err_t SetIFGain(float new_gain);
92
93 // --- Misc --- //
94 bool GetBiasT_HF ();
95 sddc_err_t SetBiasT_HF (bool new_state);
96 bool GetBiasT_VHF();
97 sddc_err_t SetBiasT_VHF(bool new_state);
98 bool GetDither();
99 sddc_err_t SetDither(bool new_state);
100 bool GetPGA();
101 sddc_err_t SetPGA(bool new_state);
102 bool GetRand();
103 sddc_err_t SetRand(bool new_state);
104
105 // --- GPIOs --- //
106 sddc_err_t SetLED (sddc_leds_t led, bool on);
107 // ----- //
108
109 float getRealSamplesPerSecond() const { return real_samples_per_second; }
110 float getIQSamplesPerSecond() const { return iq_samples_per_second; }
111
113 RadioModel getHardwareModel() { return devModel; }
114 const char *getHardwareName() { return hardware->GetName(); }
115 uint16_t GetHardwareFirmware() { return devFirmware; }
116 // --- //
117
118
119 void EnableDebug(void (*dbgprintFX3)(const char* fmt, ...), bool (*getconsolein)(char* buf, int maxlen))
120 {
121 this->DbgPrintFX3 = dbgprintFX3;
122 this->GetConsoleIn = getconsolein;
123 };
124
125 bool ReadDebugTrace(uint8_t* pdata, uint8_t len) { return fx3->ReadDebugTrace(pdata, len); }
126
127 // --- Static functions --- //
128 static size_t GetDeviceListLength();
129 static vector<SDDC::DeviceItem> GetDeviceList();
130
131protected:
132 fx3class *fx3;
133
134private:
135 void CaculateStats();
136 void OnDataPacket();
137
138 void (*callbackReal)(void* context, const int16_t *data, uint32_t length);
139 void *callbackRealContext;
140 void (*callbackIQ)(void* context, const sddc_complex_t *data, uint32_t length);
141 void *callbackIQContext;
142
143 void (*DbgPrintFX3)(const char* fmt, ...);
144 bool (*GetConsoleIn)(char* buf, int maxlen);
145
146 bool streamRunning = false;
147
148 RadioModel devModel;
149 uint16_t devFirmware;
150
151 // transfer variables
152 ringbuffer<int16_t> real_buffer;
153 ringbuffer<float> iq_buffer;
154
155 // threads
156 std::thread show_stats_thread;
157 std::thread submit_thread;
158
159 // --- Stats --- //
160 uint32_t count_real_samples = 0;
161 uint32_t count_iq_samples = 0;
162 float real_samples_per_second = 0;
163 float iq_samples_per_second = 0;
164
165 RadioHardware* hardware;
166 std::mutex fc_mutex;
167 float fc;
169 fft_mt_r2iq* r2iqCntrl;
170 bool r2iqEnabled = false;
171};
172
173#endif
Definition RadioHandler.h:53
sddc_err_t AttachIQ(void(*callback)(void *context, const sddc_complex_t *, uint32_t), void *context=nullptr)
Define the function to call when an IQ sample is available.
Definition RadioHandler.cpp:216
uint32_t GetCenterFrequency()
Get the frequency currently tuned in the SDR.
Definition RadioHandler.cpp:575
sddc_err_t Init(SDDC::DeviceItem dev_index)
Initialize the RadioHandler instance.
Definition RadioHandler.cpp:104
sddc_rf_mode_t GetBestRFMode(uint64_t freq)
Return the best RF mode to use for the frequency given.
Definition RadioHandler.cpp:725
sddc_err_t Resume()
Resume data transmission from the SDR.
Definition RadioHandler.cpp:344
RadioModel getHardwareModel()
— Hardware infos — //
Definition RadioHandler.h:113
sddc_rf_mode_t GetRFMode()
Return the current RF mode.
Definition RadioHandler.cpp:731
RadioHandler()
Create a new Radio Handler.
Definition RadioHandler.cpp:83
sddc_err_t Stop()
Stop the SDR data stream and processing functions.
Definition RadioHandler.cpp:296
sddc_err_t Start(bool convert_r2iq)
Start the SDR data stream and processing functions.
Definition RadioHandler.cpp:258
sddc_err_t AttachReal(void(*callback)(void *context, const int16_t *, uint32_t), void *context=nullptr)
Define the function to call when a real sample is available.
Definition RadioHandler.cpp:196
sddc_err_t SetCenterFrequency(uint32_t freq)
Set the frequency tuned in the SDR.
Definition RadioHandler.cpp:597
array< float, 2 > GetADCSampleRateLimits()
Get the limits of the ADC of your SDR.
Definition RadioHandler.cpp:738
sddc_err_t Pause()
Pause the data transmission from the SDR.
Definition RadioHandler.cpp:329
sddc_err_t SetDecimation(uint8_t decimate)
Set the decimation factor of the FFT.
Definition RadioHandler.cpp:242
Definition RadioHardware.h:33
Definition fft_mt_r2iq.h:27
Definition FX3Class.h:24
Definition ringbuffer.h:18
Definition types_cpp.h:9
Definition pf_mixer.h:206