better filter bandwidths
[debian/gnuradio] / gr-qtgui / src / lib / qt_examples.cc
1 #include <stdio.h>
2 #include <unistd.h>
3
4 #include <iostream>
5 #include <fstream>
6
7 #include <qapplication.h>
8 #include <omnithread.h>
9 #include <vector>
10
11 #include <fftdisplay.h>
12
13 #warning Must make this threadsafe
14 static bool g_exit_flag = false;
15
16 void read_function(void* ptr){
17
18         fft_display* fftDisplay = (fft_display*)ptr;
19
20         std::vector<gr_complex> fftData(fftDisplay->get_fft_bin_size());
21         for(unsigned int number = 0; number < fftData.size(); number++){
22                 fftData[number] = gr_complex(static_cast<float>(number), static_cast<float>(number));
23         }
24
25         float* readBuffer = new float[fftDisplay->get_fft_bin_size()*2];
26         int amntRead = 0;
27
28         fftDisplay->set_data(fftData);
29
30         while(!g_exit_flag){
31                 // Read in the data here
32                 sched_yield();
33                 std::cin.read((char*)readBuffer, fftDisplay->get_fft_bin_size()*sizeof(gr_complex));
34                 amntRead = std::cin.gcount();
35
36                 if(amntRead != static_cast<int>(fftDisplay->get_fft_bin_size()*sizeof(gr_complex))){
37                         fprintf(stderr, "Invalid Read Amount from stdin - closing program\n");
38                         qApp->quit();
39                         g_exit_flag = true;
40                 }
41                 else{
42                         for(unsigned int number = 0; number < fftData.size(); number++){
43                                 fftData[number] = gr_complex(readBuffer[2*number], readBuffer[(2*number)+1]);
44                         }
45
46                         fftDisplay->set_data(fftData);
47
48                         fftDisplay->update_display();
49
50                         qApp->wakeUpGuiThread();
51                 }
52         }       
53
54         delete[] readBuffer;
55 }
56
57 int main (int argc, char* argv[]){
58         extern char* optarg;
59         extern int optind, optopt;
60         float start_frequency = 0.0;
61         float stop_frequency = 4000.0;
62         int c;
63         unsigned int fft_bin_size = 1024;
64
65         while ((c = getopt(argc, argv, "s:p:b:")) != -1){
66                 switch(c){
67                 case 's': start_frequency = strtod(optarg, NULL); break;
68                 case 'p': stop_frequency = strtod(optarg, NULL); break;
69                 case 'b': fft_bin_size = (unsigned int)(atoi(optarg)); break;
70                 case ':': /* -s or -p w/o operand */
71                 fprintf(stderr, "Option -%c requires an arguement\n", optopt); break;
72                 case '?': fprintf(stderr, "Unrecognized option: -%c\n", optopt);
73                           fprintf(stderr, "Valid Arguements\n -s <start frequency>\n -p <stop frequency> -b <fft_bin_size> - number of fft samples per display\n");
74                           exit(-1); break;
75                 }
76         }
77
78         // Verify the stop frequency is greater than the stop frequency
79         if(stop_frequency < start_frequency){
80                 fprintf(stderr, "Stop Frequency (%0.0f Hz) was less than the Start Frequency (%0.0f Hz)\n", stop_frequency, start_frequency);
81                 exit(-1);
82         }
83
84         // Create the QApplication - this MUST be done before ANY QObjects are created
85         QApplication* qApp = new QApplication(argc, argv);
86
87         fft_display* fftDisplay = new fft_display(fft_bin_size); // No Parent Specified
88
89         // Resize the Display
90         fftDisplay->resize(640,240);
91
92         // Set the start and stop frequency
93         fftDisplay->set_start_frequency(start_frequency);
94         fftDisplay->set_stop_frequency(stop_frequency);
95
96         g_exit_flag = false;
97         omni_thread* thread_ptr = new omni_thread(&read_function, (void*)fftDisplay);
98         
99         // Set up the thread to read the data from stdin
100         thread_ptr->start();
101         sched_yield();
102
103         // Make the closing of the last window call the quit()
104         QObject::connect(qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(quit()));
105
106         // finally, refresh the plot
107         fftDisplay->update_display();
108         fftDisplay->show();
109
110         // Start the Event Thread
111         qApp->exec();
112
113         // No QObjects should be deleted once the event thread exits...
114         g_exit_flag = true;
115
116         delete fftDisplay;
117
118         // Destroy the Event Thread
119         delete qApp;
120         return 0;
121 }
122