| 
									
										
										
										
											2018-02-07 19:56:39 +01:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							| 
									
										
										
										
											2019-04-11 06:57:41 +02:00
										 |  |  | // Copyright (C) 2018 Edouard Griffiths, F4EXB                                   //
 | 
					
						
							| 
									
										
										
										
											2018-02-07 19:56:39 +01:00
										 |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // This program 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 as version 3 of the License, or                  //
 | 
					
						
							| 
									
										
										
										
											2019-04-11 06:57:41 +02:00
										 |  |  | // (at your option) any later version.                                           //
 | 
					
						
							| 
									
										
										
										
											2018-02-07 19:56:39 +01:00
										 |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // This program 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 V3 for more details.                               //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU General Public License             //
 | 
					
						
							|  |  |  | // along with this program. If not, see <http://www.gnu.org/licenses/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <QtGlobal>
 | 
					
						
							| 
									
										
										
										
											2018-02-24 10:29:27 +01:00
										 |  |  | #include <algorithm>
 | 
					
						
							| 
									
										
										
										
											2020-07-12 00:35:37 +02:00
										 |  |  | #include "perseusworker.h"
 | 
					
						
							| 
									
										
										
										
											2018-02-07 19:56:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 00:35:37 +02:00
										 |  |  | PerseusWorker *PerseusWorker::m_this = 0; | 
					
						
							| 
									
										
										
										
											2018-02-08 18:37:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 00:35:37 +02:00
										 |  |  | PerseusWorker::PerseusWorker(perseus_descr* dev, SampleSinkFifo* sampleFifo, QObject* parent) : | 
					
						
							|  |  |  |     QObject(parent), | 
					
						
							| 
									
										
										
										
											2018-02-07 23:44:20 +01:00
										 |  |  |     m_running(false), | 
					
						
							|  |  |  |     m_dev(dev), | 
					
						
							|  |  |  |     m_convertBuffer(PERSEUS_NBSAMPLES), | 
					
						
							|  |  |  |     m_sampleFifo(sampleFifo), | 
					
						
							| 
									
										
										
										
											2020-06-21 11:46:08 +02:00
										 |  |  |     m_log2Decim(0), | 
					
						
							|  |  |  |     m_iqOrder(true) | 
					
						
							| 
									
										
										
										
											2018-02-07 23:44:20 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     m_this = this; | 
					
						
							| 
									
										
										
										
											2018-02-24 10:29:27 +01:00
										 |  |  |     std::fill(m_buf, m_buf + 2*PERSEUS_NBSAMPLES, 0); | 
					
						
							| 
									
										
										
										
											2018-02-07 23:44:20 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 00:35:37 +02:00
										 |  |  | PerseusWorker::~PerseusWorker() | 
					
						
							| 
									
										
										
										
											2018-02-07 23:44:20 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     stopWork(); | 
					
						
							|  |  |  |     m_this = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 00:35:37 +02:00
										 |  |  | void PerseusWorker::startWork() | 
					
						
							| 
									
										
										
										
											2018-02-07 23:44:20 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-12 00:35:37 +02:00
										 |  |  |     qDebug("PerseusWorker::startWork"); | 
					
						
							| 
									
										
										
										
											2018-02-07 23:44:20 +01:00
										 |  |  | 	int rc = perseus_start_async_input(m_dev, PERSEUS_BLOCKSIZE, rx_callback, 0); | 
					
						
							| 
									
										
										
										
											2018-02-07 19:56:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 00:35:37 +02:00
										 |  |  | 	if (rc < 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  | 		qCritical("PerseusWorker::run: failed to start Perseus Rx: %s", perseus_errorstr()); | 
					
						
							|  |  |  |         m_running = false; | 
					
						
							| 
									
										
										
										
											2018-02-07 19:56:39 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2020-07-12 00:35:37 +02:00
										 |  |  | 	    qDebug("PerseusWorker::run: start Perseus Rx"); | 
					
						
							|  |  |  |         m_running = true; | 
					
						
							| 
									
										
										
										
											2018-02-07 19:56:39 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-07-12 00:35:37 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-02-07 19:56:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 00:35:37 +02:00
										 |  |  | void PerseusWorker::stopWork() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     qDebug("PerseusWorker::stopWork"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int rc = perseus_stop_async_input(m_dev); | 
					
						
							| 
									
										
										
										
											2018-02-07 19:56:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (rc < 0) { | 
					
						
							| 
									
										
										
										
											2020-07-12 00:35:37 +02:00
										 |  |  | 		qCritical("PerseusWorker::run: failed to stop Perseus Rx: %s", perseus_errorstr()); | 
					
						
							| 
									
										
										
										
											2018-02-07 19:56:39 +01:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2020-07-12 00:35:37 +02:00
										 |  |  | 		qDebug("PerseusWorker::run: stopped Perseus Rx"); | 
					
						
							| 
									
										
										
										
											2018-02-07 19:56:39 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 00:35:37 +02:00
										 |  |  |     m_running = false; | 
					
						
							| 
									
										
										
										
											2018-02-07 19:56:39 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 00:35:37 +02:00
										 |  |  | void PerseusWorker::setLog2Decimation(unsigned int log2_decim) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_log2Decim = log2_decim; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PerseusWorker::callbackIQ(const uint8_t* buf, qint32 len) | 
					
						
							| 
									
										
										
										
											2018-02-07 19:56:39 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-02-07 23:44:20 +01:00
										 |  |  |     SampleVector::iterator it = m_convertBuffer.begin(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (m_log2Decim) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     case 0: | 
					
						
							| 
									
										
										
										
											2020-06-21 11:46:08 +02:00
										 |  |  |         m_decimators32IQ.decimate1(&it, (TripleByteLE<qint32>*) buf, len); | 
					
						
							| 
									
										
										
										
											2018-02-07 23:44:20 +01:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case 1: | 
					
						
							| 
									
										
										
										
											2020-06-21 11:46:08 +02:00
										 |  |  |         m_decimators64IQ.decimate2_cen(&it, (TripleByteLE<qint64>*) buf, len); | 
					
						
							| 
									
										
										
										
											2018-02-07 23:44:20 +01:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case 2: | 
					
						
							| 
									
										
										
										
											2020-06-21 11:46:08 +02:00
										 |  |  |         m_decimators64IQ.decimate4_cen(&it, (TripleByteLE<qint64>*) buf, len); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_sampleFifo->write(m_convertBuffer.begin(), it); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 00:35:37 +02:00
										 |  |  | void PerseusWorker::callbackQI(const uint8_t* buf, qint32 len) | 
					
						
							| 
									
										
										
										
											2020-06-21 11:46:08 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     SampleVector::iterator it = m_convertBuffer.begin(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (m_log2Decim) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     case 0: | 
					
						
							|  |  |  |         m_decimators32QI.decimate1(&it, (TripleByteLE<qint32>*) buf, len); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case 1: | 
					
						
							|  |  |  |         m_decimators64QI.decimate2_cen(&it, (TripleByteLE<qint64>*) buf, len); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case 2: | 
					
						
							|  |  |  |         m_decimators64QI.decimate4_cen(&it, (TripleByteLE<qint64>*) buf, len); | 
					
						
							| 
									
										
										
										
											2018-02-07 23:44:20 +01:00
										 |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-07 19:56:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 23:44:20 +01:00
										 |  |  |     m_sampleFifo->write(m_convertBuffer.begin(), it); | 
					
						
							| 
									
										
										
										
											2018-02-07 19:56:39 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 00:35:37 +02:00
										 |  |  | int PerseusWorker::rx_callback(void *buf, int buf_size, void *extra) | 
					
						
							| 
									
										
										
										
											2018-02-07 19:56:39 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-11-13 08:51:14 +01:00
										 |  |  |     (void) extra; | 
					
						
							| 
									
										
										
										
											2018-02-07 19:56:39 +01:00
										 |  |  | 	qint32 nbIAndQ = buf_size / 3; // 3 bytes per I or Q
 | 
					
						
							| 
									
										
										
										
											2020-06-21 11:46:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (m_this->m_iqOrder) { | 
					
						
							|  |  |  |     	m_this->callbackIQ((uint8_t*) buf, nbIAndQ); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         m_this->callbackQI((uint8_t*) buf, nbIAndQ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2018-02-07 19:56:39 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 |