6
6
#include < QTime>
7
7
#include < cmath>
8
8
9
+ #include < pthread.h>
10
+
9
11
// Debug switch
10
- #define _DEBUG_REC
12
+ // #define _DEBUG_REC
11
13
12
14
13
15
#define BUFFER_SIZE 10000000
14
16
static int16_t rx_data_bufferI[BUFFER_SIZE];
15
17
static int16_t rx_data_bufferQ[BUFFER_SIZE];
16
18
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
28
31
};
29
32
33
+ pthread_mutex_t mutex_lock;
34
+
30
35
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
+
34
39
if (transfer->valid_length )
35
40
{
36
- ch_data.pre_index = ch_data.update_index ;
41
+ pthread_mutex_lock (&mutex_lock);
42
+ int16_t I,Q;
37
43
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
+ }
45
57
}
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);
48
62
}
49
63
return 0 ;
50
64
}
@@ -60,36 +74,47 @@ MainWindow::MainWindow(QWidget *parent) :
60
74
int result = HACKRF_SUCCESS;
61
75
result = hackrf_init_t ();
62
76
if (result != HACKRF_SUCCESS) {
77
+ device_run = false ;
63
78
QMessageBox::warning (this ,tr (" ERROR" ),tr (" Error:%1 Code:%2" ).arg (hackrf_error_name ((hackrf_error)result)).arg (result));
64
79
}
80
+ else
81
+ {
82
+ device_run = true ;
83
+ }
65
84
66
85
for (int i = 0 ; i <= 40 ; i += 8 )
67
86
{
68
87
ui->LNA_CB ->addItem (tr (" %1" ).arg (i));
69
88
}
89
+ ui->LNA_CB ->setCurrentIndex (1 );
70
90
71
91
for (int i = 0 ; i <= 62 ; i += 2 )
72
92
{
73
93
ui->VGA_CB ->addItem (tr (" %1" ).arg (i));
74
94
}
95
+ ui->VGA_CB ->setCurrentIndex (10 );
75
96
76
97
datarx_timer=new QTimer (this );
77
98
connect (datarx_timer,&QTimer::timeout,this ,&MainWindow::datarx_slot);
78
99
datarx_timer->start (33 );
100
+
101
+ pthread_mutex_init (&mutex_lock, NULL );
79
102
}
80
103
81
104
MainWindow::~MainWindow ()
82
105
{
83
106
hackrf_close_t ();
84
107
delete ui;
108
+ pthread_mutex_destroy (&mutex_lock);
85
109
}
86
110
87
111
void MainWindow::datarx_slot ()
88
112
{
89
- if (datalength){
113
+ if (ch_data.valid_length ){
114
+ pthread_mutex_lock (&mutex_lock);
90
115
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) ;
93
118
}
94
119
}
95
120
@@ -158,7 +183,7 @@ int MainWindow::hackrf_init_t()
158
183
159
184
int MainWindow::hackrf_close_t ()
160
185
{
161
- if (device == NULL )
186
+ if (device_run == false )
162
187
{
163
188
qDebug ()<<" No device, exit!" ;
164
189
hackrf_exit ();
@@ -180,25 +205,44 @@ int MainWindow::hackrf_close_t()
180
205
}
181
206
hackrf_exit ();
182
207
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 ;
184
228
}
185
229
186
230
void MainWindow::on_doubleSpinBox_valueChanged (double arg1)
187
231
{
188
- if (device != NULL )
232
+ if (device_run )
189
233
hackrf_set_freq (device, static_cast <long long >(arg1*1000000 ));
190
234
}
191
235
192
236
void MainWindow::on_LNA_CB_currentTextChanged (const QString &arg1)
193
237
{
194
238
int lna = arg1.toInt ();
195
- if (device != NULL )
239
+ if (device_run )
196
240
hackrf_set_lna_gain (device, lna);
197
241
}
198
242
199
243
void MainWindow::on_VGA_CB_currentTextChanged (const QString &arg1)
200
244
{
201
245
int vga = arg1.toInt ();
202
- if (device != NULL )
246
+ if (device_run )
203
247
hackrf_set_vga_gain (device, vga);
204
248
}
0 commit comments