SDDC_Driver
Loading...
Searching...
No Matches
RadioHandler.h
1/*
2 * This file is part of SDDC_Driver.
3 *
4 * =====================
5 * MIT License
6 * =====================
7 *
8 * Copyright (C) 2020 - Oscar Steila
9 * Copyright (C) 2020 - Howard Su
10 * Copyright (C) 2025 - RenardSpark
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this software and associated documentation files (the "Software"), to deal
14 * in the Software without restriction, including without limitation the rights
15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 * copies of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
18
19 * The above copyright notice and this permission notice shall be included in all
20 * copies or substantial portions of the Software.
21
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 * SOFTWARE.
29 *
30 * SPDX-License-Identifier: MIT
31 */
32
33#ifndef _H_RADIOHANDLER
34#define _H_RADIOHANDLER
35
36#include <array>
37#include <vector>
38#include <stdio.h>
39#include <stdlib.h>
40#include <math.h>
41#include <stdint.h>
42
43#include "config.h"
44#include "FX3Class.h"
45#include "radio/RadioHardware.h"
46#include "fft_mt_r2iq/fft_mt_r2iq.h"
47#include "dsp/ringbuffer.h"
48
49class RadioHardware;
50
53
55public:
58 sddc_err_t Init(SDDC::DeviceItem dev_index);
59 sddc_err_t AttachReal(void (*callback)(void* context, const int16_t*, uint32_t), void* context = nullptr);
60 sddc_err_t AttachIQ(void (*callback)(void* context, const sddc_complex_t*, uint32_t), void* context = nullptr);
61 sddc_err_t Start(bool convert_r2iq);
62 sddc_err_t Stop();
63 sddc_err_t Pause();
64 sddc_err_t Resume();
65
66 // --- r2iq --- //
67 sddc_err_t SetDecimation(uint8_t decimate);
68
69 // ----- RF mode ----- //
70 sddc_rf_mode_t GetBestRFMode(uint64_t freq);
71 sddc_rf_mode_t GetRFMode();
72 sddc_err_t SetRFMode(sddc_rf_mode_t mode);
73
74 // --- ADC --- //
75 std::array<float, 2> GetADCSampleRateLimits();
76 uint32_t GetADCSampleRate();
77 sddc_err_t SetADCSampleRate(uint32_t samplefreq);
78
79 // --- Tuner --- //
80 uint32_t GetCenterFrequency();
81 sddc_err_t SetCenterFrequency(uint32_t freq);
82
83 // --- RF/IF adjustments --- //
84 std::vector<float> GetRFGainSteps(sddc_rf_mode_t mode = NOMODE);
85 std::array<float, 2> GetRFGainRange(sddc_rf_mode_t mode = NOMODE);
86 float GetRFGain();
87 sddc_err_t SetRFGain(float new_att);
88
89 std::vector<float> GetIFGainSteps(sddc_rf_mode_t mode = NOMODE);
90 std::array<float, 2> GetIFGainRange(sddc_rf_mode_t mode = NOMODE);
91 float GetIFGain();
92 sddc_err_t SetIFGain(float new_gain);
93
94 // --- Misc --- //
95 bool GetBiasT_HF ();
96 sddc_err_t SetBiasT_HF (bool new_state);
97 bool GetBiasT_VHF();
98 sddc_err_t SetBiasT_VHF(bool new_state);
99 bool GetDither();
100 sddc_err_t SetDither(bool new_state);
101 bool GetPGA();
102 sddc_err_t SetPGA(bool new_state);
103 bool GetRand();
104 sddc_err_t SetRand(bool new_state);
105
106 // --- GPIOs --- //
107 sddc_err_t SetLED (sddc_leds_t led, bool on);
108 // ----- //
109
110 float getRealSamplesPerSecond() const { return real_samples_per_second; }
111 float getIQSamplesPerSecond() const { return iq_samples_per_second; }
112
114 RadioModel getHardwareModel() { return devModel; }
115 const char *getHardwareName() { return hardware->GetName(); }
116 uint16_t GetHardwareFirmware() { return devFirmware; }
117 // --- //
118
119
120 void EnableDebug(void (*dbgprintFX3)(const char* fmt, ...), bool (*getconsolein)(char* buf, int maxlen))
121 {
122 this->DbgPrintFX3 = dbgprintFX3;
123 this->GetConsoleIn = getconsolein;
124 };
125
126 bool ReadDebugTrace(uint8_t* pdata, uint8_t len) { return fx3->ReadDebugTrace(pdata, len); }
127
128 // --- Static functions --- //
129 static size_t GetDeviceListLength();
130 static std::vector<SDDC::DeviceItem> GetDeviceList();
131
132protected:
133 fx3class *fx3;
134
135private:
136 void CaculateStats();
137 void OnDataPacket();
138
139 void (*callbackReal)(void* context, const int16_t *data, uint32_t length);
140 void *callbackRealContext;
141 void (*callbackIQ)(void* context, const sddc_complex_t *data, uint32_t length);
142 void *callbackIQContext;
143
144 void (*DbgPrintFX3)(const char* fmt, ...);
145 bool (*GetConsoleIn)(char* buf, int maxlen);
146
147 bool streamRunning = false;
148
149 RadioModel devModel;
150 uint16_t devFirmware;
151
152 // transfer variables
153 ringbuffer<int16_t> real_buffer;
154 ringbuffer<float> iq_buffer;
155
156 // threads
157 std::thread show_stats_thread;
158 std::thread submit_thread;
159
160 // --- Stats --- //
161 uint32_t count_real_samples = 0;
162 uint32_t count_iq_samples = 0;
163 float real_samples_per_second = 0;
164 float iq_samples_per_second = 0;
165
166 RadioHardware* hardware;
167 std::mutex fc_mutex;
168 float fc;
170 fft_mt_r2iq* r2iqCntrl;
171 bool r2iqEnabled = false;
172};
173
174#endif
Definition RadioHandler.h:54
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:227
uint32_t GetCenterFrequency()
Get the frequency currently tuned in the SDR.
Definition RadioHandler.cpp:584
sddc_err_t Init(SDDC::DeviceItem dev_index)
Initialize the RadioHandler instance.
Definition RadioHandler.cpp:115
sddc_rf_mode_t GetBestRFMode(uint64_t freq)
Return the best RF mode to use for the frequency given.
Definition RadioHandler.cpp:734
sddc_err_t Resume()
Resume data transmission from the SDR.
Definition RadioHandler.cpp:353
RadioModel getHardwareModel()
— Hardware infos — //
Definition RadioHandler.h:114
sddc_rf_mode_t GetRFMode()
Return the current RF mode.
Definition RadioHandler.cpp:740
RadioHandler()
Create a new Radio Handler.
Definition RadioHandler.cpp:94
sddc_err_t Stop()
Stop the SDR data stream and processing functions.
Definition RadioHandler.cpp:305
sddc_err_t Start(bool convert_r2iq)
Start the SDR data stream and processing functions.
Definition RadioHandler.cpp:269
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:207
sddc_err_t SetCenterFrequency(uint32_t freq)
Set the frequency tuned in the SDR.
Definition RadioHandler.cpp:606
std::array< float, 2 > GetADCSampleRateLimits()
Get the limits of the ADC of your SDR.
Definition RadioHandler.cpp:747
sddc_err_t Pause()
Pause the data transmission from the SDR.
Definition RadioHandler.cpp:338
sddc_err_t SetDecimation(uint8_t decimate)
Set the decimation factor of the FFT.
Definition RadioHandler.cpp:253
Definition RadioHardware.h:40
Definition fft_mt_r2iq.h:58
Definition FX3Class.h:54
Definition ringbuffer.h:49
Definition FX3Class.h:46
Definition pf_mixer.h:206