Skip to content

Commit

Permalink
Simplify RTL-SDR driver for bug #558 (#1905)
Browse files Browse the repository at this point in the history
Co-authored-by: Ilia Platone <info@iliaplatone.com>
  • Loading branch information
iliaplatone and Ilia Platone committed Jun 7, 2023
1 parent 021b839 commit 8a817a3
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 58 deletions.
100 changes: 42 additions & 58 deletions drivers/receiver/indi_rtlsdr.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
indi_rtlsdr - a software defined radio driver for INDI
Copyright (C) 2017 Ilia Platone - Jasem Mutlaq
Collaborators:
Expand Down Expand Up @@ -49,31 +49,45 @@ static pthread_mutex_t condMutex = PTHREAD_MUTEX_INITIALIZER;

void RTLSDR::Callback()
{
b_read = 0;
to_read = getSampleRate() * IntegrationRequest * getBPS() / 8;
setBufferSize(to_read);

int len = min(MAX_FRAME_SIZE, to_read);
int olen = 0;
unsigned char *buf = (unsigned char *)malloc(len);
LOG_INFO("Integration started...");
b_read = 0;
n_read = 0;
setBufferSize(getSampleRate() * IntegrationRequest * getBPS() / 8);
to_read = getBufferSize();
buffer = (unsigned char *)realloc(buffer, min(MAX_FRAME_SIZE, getBufferSize()));
if((getSensorConnection() & CONNECTION_TCP) == 0)
rtlsdr_reset_buffer(rtl_dev);
else
tcflush(PortFD, TCOFLUSH);
tcflush(PortFD, TCIFLUSH);
setIntegrationTime(IntegrationRequest);
InIntegration = true;
continuum = getBuffer();
gettimeofday(&IntStart, nullptr);
while (InIntegration)
{
if((getSensorConnection() & CONNECTION_TCP) == 0)
rtlsdr_read_sync(rtl_dev, buf, len, &olen);
else
olen = read(PortFD, buf, len);
if(olen < 0 )
AbortIntegration();
else
if (to_read > 0)
{
buffer = buf;
n_read = olen;
grabData();
if((getSensorConnection() & CONNECTION_TCP) == 0)
rtlsdr_read_sync(rtl_dev, continuum + b_read, min(MAX_FRAME_SIZE, to_read), &n_read);
else
n_read = read(PortFD, continuum + b_read, min(MAX_FRAME_SIZE, to_read));

if (n_read > 0) {
b_read += n_read;
to_read -= n_read;
}
} else {
if(!streamPredicate)
{
InIntegration = false;
IntegrationComplete();
}
else
{
StartIntegration(1.0 / Streamer->getTargetFPS());
Streamer->newFrame(getBuffer(), getBufferSize());
}
LOG_INFO("Download complete.");
}
}
}
Expand Down Expand Up @@ -119,6 +133,7 @@ RTLSDR::RTLSDR(int32_t index)
// We set the Receiver capabilities
uint32_t cap = SENSOR_CAN_ABORT | SENSOR_HAS_STREAMING | SENSOR_HAS_DSP;
SetReceiverCapability(cap);
buffer = (unsigned char*)malloc(1);
}

bool RTLSDR::Connect()
Expand All @@ -132,6 +147,7 @@ bool RTLSDR::Connect()
return false;
}
}

return true;
}
/**************************************************************************************
Expand Down Expand Up @@ -172,13 +188,13 @@ bool RTLSDR::initProperties()
// Must init parent properties first!
INDI::Receiver::initProperties();

setMinMaxStep("SENSOR_INTEGRATION", "SENSOR_INTEGRATION_VALUE", 0.001, 600, 0.001, false);
setMinMaxStep("RECEIVER_SETTINGS", "RECEIVER_FREQUENCY", 2.4e+7, 2.0e+9, 1, false);
setMinMaxStep("RECEIVER_SETTINGS", "RECEIVER_SAMPLERATE", 2.5e+5, 2.0e+6, 2.5e+5, false);
setMinMaxStep("RECEIVER_SETTINGS", "RECEIVER_GAIN", 0.0, 25.0, 0.1, false);
setMinMaxStep("RECEIVER_SETTINGS", "RECEIVER_BANDWIDTH", 2.5e+5, 2.0e+6, 2.5e+5, false);
setMinMaxStep("RECEIVER_SETTINGS", "RECEIVER_BITSPERSAMPLE", 16, 16, 0, false);
setMinMaxStep("RECEIVER_SETTINGS", "RECEIVER_ANTENNA", 1, 1, 0, false);
setMinMaxStep("SENSOR_INTEGRATION", "SENSOR_INTEGRATION_VALUE", 0.001, 600, 0.001);
setMinMaxStep("RECEIVER_SETTINGS", "RECEIVER_FREQUENCY", 2.4e+7, 2.0e+9, 1);
setMinMaxStep("RECEIVER_SETTINGS", "RECEIVER_SAMPLERATE", 2.5e+5, 2.0e+6, 2.5e+5);
setMinMaxStep("RECEIVER_SETTINGS", "RECEIVER_GAIN", 0.0, 25.0, 0.1);
setMinMaxStep("RECEIVER_SETTINGS", "RECEIVER_BANDWIDTH", 2.5e+5, 2.0e+6, 2.5e+5);
setMinMaxStep("RECEIVER_SETTINGS", "RECEIVER_BITSPERSAMPLE", 16, 16, 1);
setMinMaxStep("RECEIVER_SETTINGS", "RECEIVER_ANTENNA", 1, 1, 0, true);
setIntegrationFileExtension("fits");

// Add Debug, Simulator, and Configuration controls
Expand Down Expand Up @@ -314,15 +330,9 @@ bool RTLSDR::StartIntegration(double duration)
IntegrationRequest = static_cast<float>(duration);
AbortIntegration();

LOG_INFO("Integration started...");
// Run threads
std::thread(&RTLSDR::Callback, this).detach();
gettimeofday(&IntStart, nullptr);
InIntegration = true;
return true;

// We're done
return false;
}

/**************************************************************************************
Expand Down Expand Up @@ -388,32 +398,6 @@ void RTLSDR::TimerHit()
***************************************************************************************/
void RTLSDR::grabData()
{
if (InIntegration)
{
n_read = min(to_read, n_read);
continuum = getBuffer();
if (n_read > 0)
{
memcpy(continuum + b_read, buffer, n_read);
b_read += n_read;
to_read -= n_read;
}

if (to_read <= 0)
{
InIntegration = false;
if(!streamPredicate)
{
LOG_INFO("Download complete.");
IntegrationComplete();
}
else
{
StartIntegration(1.0 / Streamer->getTargetFPS());
Streamer->newFrame(getBuffer(), getBufferSize());
}
}
}
}

//Streamer API functions
Expand Down
11 changes: 11 additions & 0 deletions libs/indibase/indireceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,17 @@ void Receiver::setFrequency(double freq)
ReceiverSettingsN[Receiver::RECEIVER_FREQUENCY].value = freq;

IDSetNumber(&ReceiverSettingsNP, nullptr);

}

void Receiver::setBPS(int BPS)
{
BitsPerSample = BPS;

ReceiverSettingsN[Receiver::RECEIVER_BITSPERSAMPLE].value = BitsPerSample;

IDSetNumber(&ReceiverSettingsNP, nullptr);
SensorInterface::setBPS(BPS);
}

void Receiver::SetReceiverCapability(uint32_t cap)
Expand Down
7 changes: 7 additions & 0 deletions libs/indibase/indireceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ class Receiver : public virtual SensorInterface
*/
void setFrequency(double freq);

/**
* @brief setBPS Set the bits per sample.
* @param BPS bits per sample
*/
void setBPS(int BPS);

/**
* @brief getBandwidth Get requested integration bandwidth for the sensor in Hz.
* @return requested integration bandwidth for the sensor in Hz.
Expand Down Expand Up @@ -191,6 +197,7 @@ class Receiver : public virtual SensorInterface
INumber ReceiverSettingsN[7];

private:
int BitsPerSample;
double Frequency;
double SampleRate;
double Bandwidth;
Expand Down
1 change: 1 addition & 0 deletions libs/indibase/indisensorinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,7 @@ void SensorInterface::setBPS(int bps)
// DSP
if (HasDSP())
DSP->setSizes(1, new int[1] { getBufferSize() * 8 / BPS });

}


Expand Down

0 comments on commit 8a817a3

Please sign in to comment.