37 __typeof__(a) _a = (a); \
38 __typeof__(b) _b = (b); \
42#define SUBFRAME_SIZE (16384)
43#define MIN_FRAME_SIZE (512)
44#define MAX_FRAME_SIZE (SUBFRAME_SIZE * 16)
45#define SPECTRUM_SIZE (256)
47static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
48static pthread_mutex_t condMutex = PTHREAD_MUTEX_INITIALIZER;
50void RTLSDR::Callback()
67 gettimeofday(&IntStart,
nullptr);
84 imaginary[x] =
buffer[x*2+1];
107 std::deque<std::unique_ptr<RTLSDR>> receivers;
111 size_t numofConnectedReceivers = rtlsdr_get_device_count();
112 if (numofConnectedReceivers == 0)
115 IDLog(
"No USB RTLSDR receivers detected. Power on?");
116 IDMessage(
nullptr,
"No USB RTLSDR receivers detected. Power on?");
121 for (
size_t i = 0; i < numofConnectedReceivers; i++)
123 receivers.push_back(std::unique_ptr<RTLSDR>(
new RTLSDR(i)));
137 receiverIndex = index;
146 buffer = (
unsigned char*)malloc(1);
153 int r = rtlsdr_open(&
rtl_dev,
static_cast<uint32_t
>(receiverIndex));
156 LOGF_ERROR(
"Failed to open rtlsdr device index %d.", receiverIndex);
176 pthread_mutex_lock(&condMutex);
178 terminateThread =
true;
179 pthread_cond_signal(&cv);
180 pthread_mutex_unlock(&condMutex);
181 LOG_INFO(
"RTL-SDR Receiver disconnected successfully!");
190 return "RTL-SDR Receiver";
201 setMinMaxStep(
"SENSOR_INTEGRATION",
"SENSOR_INTEGRATION_VALUE", 0.001, 600, 0.001);
202 setMinMaxStep(
"RECEIVER_SETTINGS",
"RECEIVER_FREQUENCY", 2.4e+7, 2.0e+9, 1);
203 setMinMaxStep(
"RECEIVER_SETTINGS",
"RECEIVER_SAMPLERATE", 2.5e+5, 2.0e+6, 2.5e+5);
204 setMinMaxStep(
"RECEIVER_SETTINGS",
"RECEIVER_GAIN", 0.0, 25.0, 0.1);
205 setMinMaxStep(
"RECEIVER_SETTINGS",
"RECEIVER_BANDWIDTH", 2.5e+5, 2.0e+6, 2.5e+5);
206 setMinMaxStep(
"RECEIVER_SETTINGS",
"RECEIVER_BITSPERSAMPLE", 16, 16, 1);
207 setMinMaxStep(
"RECEIVER_SETTINGS",
"RECEIVER_ANTENNA", 1, 1, 0,
true);
247 r |= rtlsdr_set_tuner_gain_mode(
rtl_dev, 1);
248 r |= rtlsdr_set_tuner_gain(
rtl_dev,
static_cast<int>(gain * 10));
249 r |= rtlsdr_set_center_freq(
rtl_dev,
static_cast<uint32_t
>(freq));
250 r |= rtlsdr_set_sample_rate(
rtl_dev,
static_cast<uint32_t
>(sr));
251 r |= rtlsdr_set_tuner_bandwidth(
rtl_dev,
static_cast<uint32_t
>(sr));
254 LOG_INFO(
"Issue(s) setting parameters.");
258 setGain(
static_cast<double>(rtlsdr_get_tuner_gain(
rtl_dev)) / 10.0);
265 sendTcpCommand(CMD_SET_FREQ,
static_cast<int>(freq));
266 sendTcpCommand(CMD_SET_SAMPLE_RATE,
static_cast<int>(sr));
267 sendTcpCommand(CMD_SET_TUNER_GAIN_MODE, 0);
268 sendTcpCommand(CMD_SET_GAIN,
static_cast<int>(gain * 10));
269 sendTcpCommand(CMD_SET_FREQ_COR, 0);
270 sendTcpCommand(CMD_SET_AGC_MODE, 0);
271 sendTcpCommand(CMD_SET_TUNER_GAIN_INDEX, 0);
281bool RTLSDR::sendTcpCommand(
int cmd,
int value)
283 unsigned char tosend[5];
284 tosend[0] =
static_cast<unsigned char>(
cmd);
285 tosend[1] = value & 0xff;
287 tosend[2] = value & 0xff;
289 tosend[3] = value & 0xff;
291 tosend[4] = value & 0xff;
293 tcflush(
PortFD, TCOFLUSH);
297 count = write(
PortFD, tosend, 5);
298 if (count < 0)
return false;
308 for(
int i = 0; i < n; i++)
310 if (!strcmp(names[i],
"RECEIVER_GAIN"))
314 else if (!strcmp(names[i],
"RECEIVER_FREQUENCY"))
318 else if (!strcmp(names[i],
"RECEIVER_SAMPLERATE"))
340 IntegrationRequest =
static_cast<float>(duration);
344 std::thread(&RTLSDR::Callback,
this).detach();
363float RTLSDR::CalcTimeLeft()
368 gettimeofday(&now,
nullptr);
370 timesince = (double)(now.tv_sec * 1000.0 + now.tv_usec / 1000) -
371 (double)(IntStart.tv_sec * 1000.0 + IntStart.tv_usec / 1000);
372 timesince = timesince / 1000;
374 timeleft = IntegrationRequest - timesince;
390 timeleft =
static_cast<long>(CalcTimeLeft());
394 LOG_INFO(
"Integration done, expecting data...");
417 pthread_mutex_lock(&condMutex);
420 pthread_mutex_unlock(&condMutex);
421 pthread_cond_signal(&cv);
428 pthread_mutex_lock(&condMutex);
430 pthread_mutex_unlock(&condMutex);
431 pthread_cond_signal(&cv);
442 LOG_ERROR(
"Failed to connect to rtl_tcp server.");
448 terminateThread =
false;
449 LOG_INFO(
"RTL-SDR Receiver connected successfully!");
void setDeviceName(const char *dev)
Set the device name.
void setDefaultPollingPeriod(uint32_t msec)
setDefaultPollingPeriod Change the default polling period to call TimerHit() function in the driver.
uint32_t getCurrentPollingPeriod() const
getCurrentPollingPeriod Return the current polling period.
void addAuxControls()
Add Debug, Simulation, and Configuration options to the driver.
int SetTimer(uint32_t ms)
Set a timer to call the function TimerHit after ms milliseconds.
void setGain(double gain)
setGain Set gain of Receiver device.
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
void setBandwidth(double bandwidth)
setBandwidth Set bandwidth of Receiver device.
void setBPS(int BPS)
setBPS Set the bits per sample.
virtual bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
virtual void setMinMaxStep(const char *property, const char *element, double min, double max, double step, bool sendToClient=true) override
setMinMaxStep for a number property element
void setFrequency(double freq)
setFrequency Set the frequency observed.
INumberVectorProperty ReceiverSettingsNP
double getSampleRate()
getSampleRate Get requested sample rate for the sensor in Hz.
double getFrequency()
getFrequency Get requested integration frequency for the sensor in Hz.
double getBandwidth()
getBandwidth Get requested integration bandwidth for the sensor in Hz.
void SetReceiverCapability(uint32_t cap)
SetReceiverCapability Set the Receiver capabilities. Al fields must be initialized.
void setSampleRate(double sr)
setSampleRate Set depth of Receiver device.
double getGain()
getGain Get requested integration gain for the sensor.
bool processNumber(const char *dev, const char *name, double values[], char *names[], int n)
uint8_t * getReal()
getBuffer Get real buffer of the stream of the Sensor device.
void setBufferSize(int nbuf, bool allocMem=true)
setBufferSize Set desired buffer size. The function will allocate memory accordingly....
void setSensorConnection(const uint8_t &value)
setSensorConnection Set Sensor connection mode. Child class should call this in the constructor befor...
void setIntegrationLeft(double duration)
setIntegrationLeft Update Integration time left. Inform the client of the new Integration time left v...
void setImaginary(uint8_t *buffer)
setBuffer Set imaginary frame buffer pointer.
uint8_t * getImaginary()
getBuffer Get imaginary buffer of the stream of the Sensor device.
std::unique_ptr< StreamManager > Streamer
void setIntegrationTime(double duration)
setIntegrationTime Set desired Sensor frame Integration duration for next Integration....
int PortFD
For Serial & TCP connections.
int getBufferSize() const
getContinuumBufferSize Get allocated continuum buffer size to hold the Sensor integrationd stream.
virtual bool IntegrationComplete()
Uploads target Device exposed buffer as FITS to the client. Derived classes should class this functio...
uint8_t getSensorConnection()
uint8_t * getBuffer()
getBuffer Get continuum buffer of the stream of the Sensor device.
void setReal(uint8_t *buffer)
setBuffer Set real frame buffer pointer.
int getBPS() const
getBPS Get Sensor depth (bits per sample).
void setIntegrationFileExtension(const char *ext)
setIntegrationExtension Set integration extension
bool AbortIntegration() override
Abort ongoing Integration.
bool StopStreaming() override
StopStreaming Stop live video streaming.
void TimerHit() override
Callback function to be called once SetTimer duration elapses.
bool Disconnect() override
Disconnect from device.
bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
void setupParams(float sr, float freq, float gain)
bool Connect() override
Connect to the device. INDI::DefaultDevice implementation connects to appropriate connection interfac...
const char * getDefaultName() override
bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
bool Handshake() override
perform handshake with device to check communication
bool StartIntegration(double duration) override
Start integration from the Sensor device.
bool StartStreaming() override
StartStreaming Start live video streaming.
bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
void IDLog(const char *fmt,...)
Implementations for common driver routines.
void IDSetNumber(const INumberVectorProperty *nvp, const char *fmt,...)
void IDMessage(const char *dev, const char *fmt,...)
int IUUpdateNumber(INumberVectorProperty *nvp, double values[], char *names[], int n)
Update all numbers in a number vector property.
#define LOG_ERROR(txt)
Shorter logging macros. In order to use these macros, the function (or method) "getDeviceName()" must...
#define LOGF_ERROR(fmt,...)