Skip to content

Commit 1d41b7b

Browse files
committed
Add new function
1 parent 4a76414 commit 1d41b7b

9 files changed

+515
-205
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
11
Hello everyone, This is my first github project. It is a virtual oscilloscope to display the wave of hackrf one by hackrf API. It designed by Qt and it didn't use QChart class.
2+
3+
4+
![Alt text](./doc/picture/mainview.jpg "mainview")
5+
6+
Next work:
7+
<1> To adjust time offest and time scale in stop status.
8+
<2> To add exact time and voltage label.
9+
<3> To development pre view function.
10+
<4> To add measure function, vpp, frequncy and others.
11+
<5> To add drag function.
12+

doc/picture/mainview.jpg

81 KB
Loading

hackrf_scope.pro.user

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 4.11.1, 2023-02-22T20:07:31. -->
3+
<!-- Written by QtCreator 4.11.1, 2023-03-09T23:30:47. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>

mainwindow.cpp

+77-33
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,59 @@
66
#include <QTime>
77
#include <cmath>
88

9+
#include <pthread.h>
10+
911
//Debug switch
10-
#define _DEBUG_REC
12+
//#define _DEBUG_REC
1113

1214

1315
#define BUFFER_SIZE 10000000
1416
static int16_t rx_data_bufferI[BUFFER_SIZE];
1517
static int16_t rx_data_bufferQ[BUFFER_SIZE];
1618
static int16_t magnitude[BUFFER_SIZE];
17-
size_t datalength=0;
18-
scope_ch_data_t ch_data={
19-
.ch_count=3,
20-
.ch1_data=rx_data_bufferI,
21-
.ch2_data=rx_data_bufferQ,
22-
.ch3_data=magnitude,
23-
.ch4_data=NULL,
24-
.buffer_size=BUFFER_SIZE,
25-
.update_index=0,
26-
.pre_index=0,
27-
.update_count=0
19+
20+
scope_ch_data_t ch_data = {
21+
.ch_mask = CH1_MASK | CH2_MASK | CH3_MASK,
22+
.ch1_data = rx_data_bufferI,
23+
.ch2_data = rx_data_bufferQ,
24+
.ch3_data = magnitude,
25+
.ch4_data = NULL,
26+
.buffer_size = BUFFER_SIZE,
27+
.head = 0,
28+
.tail = 0,
29+
.valid_length = 0,
30+
.overflow_flag = false
2831
};
2932

33+
pthread_mutex_t mutex_lock;
34+
3035
static int rx_callback(hackrf_transfer* transfer) {
31-
#ifdef _DEBUG_REC
32-
qDebug()<<QTime::currentTime().msecsSinceStartOfDay()<<transfer->valid_length;
33-
#endif
36+
if(ch_data.overflow_flag)
37+
return -1;
38+
3439
if(transfer->valid_length)
3540
{
36-
ch_data.pre_index = ch_data.update_index;
41+
pthread_mutex_lock(&mutex_lock);
42+
int16_t I,Q;
3743
for(int i = 0; i < transfer->valid_length; i += 2){
38-
*(rx_data_bufferI + ch_data.update_index) = (int) *((int8_t*)transfer->buffer + i);
39-
*(rx_data_bufferQ + ch_data.update_index) = (int) *((int8_t*)transfer->buffer + i + 1);
40-
*(magnitude + ch_data.update_index) = sqrt(pow(*(rx_data_bufferI + ch_data.update_index), 2)
41-
+ pow(*(rx_data_bufferQ + ch_data.update_index), 2));
42-
ch_data.update_index++;
43-
if(ch_data.update_index == ch_data.buffer_size)
44-
ch_data.update_index = 0;
44+
I = *((int8_t*)transfer->buffer + i);
45+
Q = *((int8_t*)transfer->buffer + i + 1);
46+
*(rx_data_bufferI + ch_data.tail) = I;
47+
*(rx_data_bufferQ + ch_data.tail) = Q;
48+
*(magnitude + ch_data.tail) = sqrt(pow(I, 2) + pow(Q, 2));
49+
ch_data.tail++;
50+
if(ch_data.tail == ch_data.buffer_size)
51+
ch_data.tail = 0;
52+
if(ch_data.tail == ch_data.head) //Receive overflow
53+
{
54+
ch_data.overflow_flag = true;
55+
qDebug() << "Receive data flow!";
56+
}
4557
}
46-
datalength+=transfer->valid_length;
47-
ch_data.update_count += (transfer->valid_length / 2);
58+
ch_data.valid_length = ch_data.tail > ch_data.head ?
59+
ch_data.tail - ch_data.head:
60+
ch_data.tail + ch_data.buffer_size - ch_data.head;
61+
pthread_mutex_unlock(&mutex_lock);
4862
}
4963
return 0;
5064
}
@@ -60,36 +74,47 @@ MainWindow::MainWindow(QWidget *parent) :
6074
int result = HACKRF_SUCCESS;
6175
result = hackrf_init_t();
6276
if (result != HACKRF_SUCCESS) {
77+
device_run = false;
6378
QMessageBox::warning(this,tr("ERROR"),tr("Error:%1 Code:%2").arg(hackrf_error_name((hackrf_error)result)).arg(result));
6479
}
80+
else
81+
{
82+
device_run = true;
83+
}
6584

6685
for(int i = 0; i <= 40; i += 8)
6786
{
6887
ui->LNA_CB->addItem(tr("%1").arg(i));
6988
}
89+
ui->LNA_CB->setCurrentIndex(1);
7090

7191
for(int i = 0; i <= 62; i += 2)
7292
{
7393
ui->VGA_CB->addItem(tr("%1").arg(i));
7494
}
95+
ui->VGA_CB->setCurrentIndex(10);
7596

7697
datarx_timer=new QTimer(this);
7798
connect(datarx_timer,&QTimer::timeout,this,&MainWindow::datarx_slot);
7899
datarx_timer->start(33);
100+
101+
pthread_mutex_init(&mutex_lock, NULL);
79102
}
80103

81104
MainWindow::~MainWindow()
82105
{
83106
hackrf_close_t();
84107
delete ui;
108+
pthread_mutex_destroy(&mutex_lock);
85109
}
86110

87111
void MainWindow::datarx_slot()
88112
{
89-
if(datalength){
113+
if(ch_data.valid_length){
114+
pthread_mutex_lock(&mutex_lock);
90115
emit rx_data_update(&ch_data);
91-
qDebug()<<QTime::currentTime().msecsSinceStartOfDay()<<datalength;
92-
datalength=0;
116+
clear_rxch_buff(&ch_data);
117+
pthread_mutex_unlock(&mutex_lock);
93118
}
94119
}
95120

@@ -158,7 +183,7 @@ int MainWindow::hackrf_init_t()
158183

159184
int MainWindow::hackrf_close_t()
160185
{
161-
if(device == NULL)
186+
if(device_run == false)
162187
{
163188
qDebug()<<"No device, exit!";
164189
hackrf_exit();
@@ -180,25 +205,44 @@ int MainWindow::hackrf_close_t()
180205
}
181206
hackrf_exit();
182207
fprintf(stderr, "hackrf_exit() done\n");
183-
return HACKRF_SUCCESS;
208+
return HACKRF_SUCCESS;
209+
}
210+
211+
int MainWindow::clear_rxch_buff(scope_ch_data_t *ch_buffer)
212+
{
213+
if(ch_buffer->overflow_flag)
214+
{
215+
ch_buffer->overflow_flag = false;
216+
ch_buffer->head = 0;
217+
ch_buffer->tail = 0;
218+
ch_buffer->valid_length = 0;
219+
return 1;
220+
}
221+
else if(ch_buffer->valid_length > 0)
222+
{
223+
ch_buffer->head = ch_buffer->tail;
224+
ch_buffer->valid_length = 0;
225+
return 0;
226+
}
227+
return -1;
184228
}
185229

186230
void MainWindow::on_doubleSpinBox_valueChanged(double arg1)
187231
{
188-
if(device != NULL)
232+
if(device_run)
189233
hackrf_set_freq(device, static_cast<long long>(arg1*1000000));
190234
}
191235

192236
void MainWindow::on_LNA_CB_currentTextChanged(const QString &arg1)
193237
{
194238
int lna = arg1.toInt();
195-
if(device != NULL)
239+
if(device_run)
196240
hackrf_set_lna_gain(device, lna);
197241
}
198242

199243
void MainWindow::on_VGA_CB_currentTextChanged(const QString &arg1)
200244
{
201245
int vga = arg1.toInt();
202-
if(device != NULL)
246+
if(device_run)
203247
hackrf_set_vga_gain(device, vga);
204248
}

mainwindow.h

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ private slots:
3333
int hackrf_init_t();
3434
int hackrf_close_t();
3535

36+
int clear_rxch_buff(scope_ch_data_t *ch_data);
37+
3638
private:
3739
Ui::MainWindow *ui;
3840

@@ -42,6 +44,8 @@ private slots:
4244

4345
QTimer *datarx_timer;
4446

47+
bool device_run;
48+
4549
signals:
4650
void rx_data_update(scope_ch_data_t*);
4751
};

0 commit comments

Comments
 (0)