Skip to content

Commit

Permalink
Refactor unsigned sample reading into read_samples_cu8()
Browse files Browse the repository at this point in the history
Instead of having rtl_fm and rtl_sdr convert CS8 to CU8, add the
read_samples_cu8() convenience function. This reads from SoapySDR using
CS8 format, and adds 127 to convert to CU8, the actual native format - a
redundant but necessary conversion, could be improved after
pothosware/SoapyRTLSDR#15 if SoapyRTLSDR adds
native CU8 support (remove the loop in read_samples_cu8()).
  • Loading branch information
rxseger committed Jul 16, 2016
1 parent b594a3d commit eed1b84
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 30 deletions.
28 changes: 28 additions & 0 deletions src/convenience/convenience.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,4 +389,32 @@ int verbose_device_search(char *s)
return -1;
}

int read_samples_cu8(SoapySDRDevice *dev, SoapySDRStream *stream, uint8_t *buf, int len)
{
void *buffs[] = {buf};
int flags = 0;
long long timeNs;
long timeoutNs = 100000;
int bytes_read, r;

r = SoapySDRDevice_readStream(dev, stream, buffs, len, &flags, &timeNs, timeoutNs);

//fprintf(stderr, "readStream ret=%d, flags=%d, timeNs=%lld\n", r, flags, timeNs);
if (r >= 0) {
// r is number of elements read, elements=complex pairs of 8-bits, so buffer length in bytes is twice
bytes_read = r * 2;

// Convert CS8 to CU8, back to RTL-SDR native format!
// TODO: see https://github.com/pothosware/SoapyRTLSDR/issues/15
// TODO: or use "direct buffer access API"?
// TODO: or2, remove +127 here and -127 in rtlsdr_callback! back and forth too many times (-127 in SoapyRTLSDR)
for(int i = 0; i < bytes_read; ++i) {
buf[i] += 127;
}
}

return bytes_read;
}


// vim: tabstop=8:softtabstop=8:shiftwidth=8:noexpandtab
11 changes: 11 additions & 0 deletions src/convenience/convenience.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,15 @@ int verbose_reset_buffer(SoapySDRDevice *dev);

int verbose_device_search(char *s);

/*!
* Read samples as Complex Unsigned 8-bit (CU8) pairs
*
* \param dev the device handle
* \param stream the stream handle
* \param buf buffer to read into
* \param len maximum length of buffer
* \return number of bytes read, or negative if an error
*/
int read_samples_cu8(SoapySDRDevice *dev, SoapySDRStream *stream, uint8_t *buf, int len);

#endif /*__CONVENIENCE_H*/
22 changes: 4 additions & 18 deletions src/rtl_fm.c
Original file line number Diff line number Diff line change
Expand Up @@ -912,26 +912,12 @@ static void *dongle_thread_fn(void *arg)
int r = 0;
do
{
void *buffs[] = {buf};
int flags = 0;
long long timeNs;
long timeoutNs = 100000;
r = SoapySDRDevice_readStream(s->dev, s->stream, buffs, MAXIMUM_BUF_LENGTH, &flags, &timeNs, timeoutNs);
//fprintf(stderr, "ret=%d, flags=%d, timeNs=%lld\n", r, flags, timeNs);
r = read_samples_cu8(s->dev, s->stream, buf, MAXIMUM_BUF_LENGTH);

if (r >= 0) {
// r is number of elements read, elements=complex pairs of 8-bits, so buffer length in bytes is twice
s->buf_len = r * 2;
for(int i = 0; i < s->buf_len; ++i) {
//fprintf(stderr, "%.4x ", s->buf16[i]);
// Convert CS8 to CU8, back to RTL-SDR native format!
// TODO: see https://github.com/pothosware/SoapyRTLSDR/issues/15
// TODO: or use "direct buffer access API"?
// TODO: or2, remove +127 here and -127 in rtlsdr_callback! back and forth too many times (-127 in SoapyRTLSDR)
buf[i] += 127;
}
//fprintf(stderr, "\n");
s->buf_len = r;
rtlsdr_callback(buf, s->buf_len, s);
}
rtlsdr_callback(buf, s->buf_len, s);
} while(r > 0);

//rtlsdr_read_async(s->dev, rtlsdr_callback, s, 0, s->buf_len);
Expand Down
14 changes: 2 additions & 12 deletions src/rtl_sdr.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,22 +227,12 @@ int main(int argc, char **argv)
exit(1);
}
while (!do_exit) {
void *buffs[] = {buffer};
int flags = 0;
long long timeNs = 0;
r = SoapySDRDevice_readStream(dev, stream, buffs, out_block_size, &flags, &timeNs, 100000);
r = read_samples_cu8(dev, stream, buffer, out_block_size);
if (r < 0) {
fprintf(stderr, "WARNING: sync read failed.\n");
break;
} else {
// TODO: get native CU8, see https://github.com/pothosware/SoapyRTLSDR/issues/15 also in fm
for(int i = 0; i < r; ++i) {
buffer[i] += 127;
}
}
n_read = r;
// TODO: convert CS8 to CU8 (+127)
fprintf(stderr, "n_read=%d\n", n_read);

if ((bytes_to_read > 0) && (bytes_to_read < (uint32_t)n_read)) {
n_read = bytes_to_read;
Expand All @@ -254,7 +244,7 @@ int main(int argc, char **argv)
break;
}

// TODO: hmm.. n_read 8192, but out_block_size (16 * 16384) is much larger
// TODO: hmm.. n_read 8192, but out_block_size (16 * 16384) is much larger TODO: loop? or accept 8192? rtl_fm ok with it
/*
if ((uint32_t)n_read < out_block_size) {
fprintf(stderr, "Short read, samples lost, exiting! (%d < %d)\n", n_read, out_block_size);
Expand Down

0 comments on commit eed1b84

Please sign in to comment.