1
0
mirror of https://github.com/JvanKatwijk/qt-dab.git synced 2025-10-06 08:12:40 +02:00
Files
SDR-DAB_Qt-DAB/devices/hackrf-handler/hackrf-handler.cpp

214 lines
5.7 KiB
C++
Raw Normal View History

2018-05-15 21:57:28 +02:00
#
/*
* Copyright (C) 2014 .. 2017
* Jan van Katwijk (J.vanKatwijk@gmail.com)
* Lazy Chair Computing
*
* This file is part of Qt-DAB
*
* Qt-DAB is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation version 2 of the License.
*
* Qt-DAB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Qt-DAB if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <QThread>
#include <QSettings>
#include <QHBoxLayout>
#include <QLabel>
#include "hackrf-handler.h"
#define DEFAULT_GAIN 30
hackrfHandler::hackrfHandler (QSettings *s) {
int err;
int res;
hackrfSettings = s;
this -> myFrame = new QFrame (NULL);
setupUi (this -> myFrame);
this -> myFrame -> show ();
this -> inputRate = Khz (2048);
_I_Buffer = new RingBuffer<std::complex<float>>(1024 * 1024);
vfoFrequency = Khz (220000);
//
// See if there are settings from previous incarnations
hackrfSettings -> beginGroup ("hackrfSettings");
lnagainSlider -> setValue (
hackrfSettings -> value ("hack_lnaGain", DEFAULT_GAIN). toInt ());
vgagainSlider -> setValue (
hackrfSettings -> value ("hack_vgaGain", DEFAULT_GAIN). toInt ());
hackrfSettings -> endGroup ();
//
res = hackrf_init ();
if (res != HACKRF_SUCCESS) {
fprintf (stderr, "Problem with hackrf_init %d\n", res);
delete myFrame;
throw (21);
}
res = hackrf_open (&theDevice);
if (res != HACKRF_SUCCESS) {
fprintf (stderr, "Problem with hackrf_open %d\n", res);
delete myFrame;
throw (22);
}
res = hackrf_set_sample_rate (theDevice, 2048000.0);
if (res != HACKRF_SUCCESS) {
fprintf (stderr, "Problem with hackrf_set_samplerate %d\n", res);
delete myFrame;
throw (23);
}
res = hackrf_set_baseband_filter_bandwidth (theDevice, 2000000);
if (res != HACKRF_SUCCESS) {
fprintf (stderr, "Problem with hackrf_set_bw %d\n", res);
delete myFrame;
throw (24);
}
res = hackrf_set_freq (theDevice, 220000000);
if (res != HACKRF_SUCCESS) {
fprintf (stderr, "Problem with hackrf_set_freq %d\n", res);
delete myFrame;
throw (25);
}
setLNAGain (lnagainSlider -> value ());
setVGAGain (vgagainSlider -> value ());
// and be prepared for future changes in the settings
connect (lnagainSlider, SIGNAL (valueChanged (int)),
this, SLOT (setLNAGain (int)));
connect (vgagainSlider, SIGNAL (valueChanged (int)),
this, SLOT (setVGAGain (int)));
fprintf (stderr, "we passed the opening sequence\n");
running. store (false);
}
hackrfHandler::~hackrfHandler (void) {
stopReader ();
delete myFrame;
if (_I_Buffer != NULL)
delete _I_Buffer;
}
//
void hackrfHandler::setVFOFrequency (int32_t newFrequency) {
int res;
res = hackrf_set_freq (theDevice, newFrequency);
if (res != HACKRF_SUCCESS) {
fprintf (stderr, "Problem with hackrf_set_freq %d\n", res);
return;
}
vfoFrequency = newFrequency;
}
int32_t hackrfHandler::getVFOFrequency (void) {
return vfoFrequency;
}
void hackrfHandler::setLNAGain (int newGain) {
int res;
if ((newGain <= 60) && (newGain >= 0)) {
res = hackrf_set_lna_gain (theDevice, newGain);
if (res != HACKRF_SUCCESS) {
fprintf (stderr, "Problem with hackrf_lna_gain %d\n", res);
return;
}
lnagainDisplay -> display (newGain);
}
}
void hackrfHandler::setVGAGain (int newGain) {
int res;
if ((newGain <= 62) && (newGain >= 0)) {
res = hackrf_set_lna_gain (theDevice, newGain);
if (res != HACKRF_SUCCESS) {
fprintf (stderr, "Problem with hackrf_lna_gain %d\n", res);
return;
}
vgagainDisplay -> display (newGain);
}
}
static std::complex<float>buffer [32 * 32768];
static
int callback (hackrf_transfer *transfer) {
hackrfHandler *ctx = static_cast <hackrfHandler *>(transfer -> rx_ctx);
int i;
//std::complex<float> buffer [transfer -> buffer_length / 2];
uint8_t *p = transfer -> buffer;
RingBuffer<std::complex<float> > * q = ctx -> _I_Buffer;
for (i = 0; i < transfer -> valid_length / 2; i ++) {
float re = ((int16_t)((int8_t)(p [2 * i]))) / 128.0;
float im = ((int16_t)((int8_t)(p [2 * i + 1]))) / 128.0;
buffer [i] = std::complex<float> (re, im);
}
q -> putDataIntoBuffer (buffer, transfer -> valid_length / 2);
return 0;
}
bool hackrfHandler::restartReader (void) {
int res;
fprintf (stderr, " we gaan starten\n");
// if (hackrf_is_streaming (theDevice))
// return true;
res = hackrf_start_rx (theDevice, callback, this);
if (res != HACKRF_SUCCESS) {
fprintf (stderr, "Problem with hackrf_start_rx %d\n", res);
return false;
}
running. store (hackrf_is_streaming (theDevice));
fprintf (stderr, "starting device was %d\n", running. load ());
return running. load ();
}
void hackrfHandler::stopReader (void) {
int res;
if (!running. load ())
return;
res = hackrf_stop_rx (theDevice);
if (res != HACKRF_SUCCESS) {
fprintf (stderr, "Problem with hackrf_stop_rx %d\n", res);
return;
}
running. store (false);
}
//
// The brave old getSamples. For the hackrf, we get
// size still in I/Q pairs
int32_t hackrfHandler::getSamples (std::complex<float> *V, int32_t size) {
return _I_Buffer -> getDataFromBuffer (V, size);
}
int32_t hackrfHandler::Samples (void) {
return _I_Buffer -> GetRingBufferReadAvailable ();
}
void hackrfHandler::resetBuffer (void) {
_I_Buffer -> FlushRingBuffer ();
}
int16_t hackrfHandler::bitDepth (void) {
return 8;
}