36bed316ac625582073650cf6bbf037ca5312a19
[debian/gnuradio] / gr-msdd6000 / src / msdd_rs_source_simple.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <msdd_rs_source_simple.h>
26 #include <gr_io_signature.h>
27 #include <string.h>
28
29
30 msdd_rs_source_simple_sptr
31 msdd_rs_make_source_simple ( const char *src, unsigned short port_src) 
32 {
33   return msdd_rs_source_simple_sptr (new msdd_rs_source_simple ( src, port_src)); 
34 }
35
36
37 msdd_rs_source_simple::msdd_rs_source_simple (
38                     const char *src, 
39                     unsigned short port_src) 
40                 : gr_sync_block("MSDD_RS_SOURCE_SIMPLE",
41                                 gr_make_io_signature (0,0,0),
42                                 gr_make_io_signature (1, 1, sizeof (short))),
43                   rcv(new MSDD6000_RS((char*) src)), d_lastseq(0)
44 {
45 }
46
47 msdd_rs_source_simple::~msdd_rs_source_simple ()
48 {
49 }
50
51
52 int
53 msdd_rs_source_simple::work (int noutput_items,
54                          gr_vector_const_void_star &input_items,
55                          gr_vector_void_star &output_items)
56 {
57         
58 #define BUF_LEN (366*sizeof(short)*2 + 6)
59
60         float* out1 =(float*) output_items[0];
61
62         char buffer[BUF_LEN];
63         /* Read a buffer out -- looking at UDP payload at this point.*/
64         rcv->read( &buffer[0], BUF_LEN );
65         
66         int seq = *((int*) &buffer[2]);
67         char type = buffer[0];
68         //printf("Sequence %d\n",seq);
69         
70         // FIXME get rid of these magic 366's!
71         if(d_lastseq == -366)
72         {
73         if (type != 0){
74             /* Received control packet -- parse and update locally stored parameters */
75             printf("Parsing control Packet\n");
76             rcv->parse_control(&buffer[0], seq);       
77         }
78         else{   
79             // not started case
80             if(seq == 0){
81                 d_lastseq = 0;
82             } 
83             else 
84             {
85                 // THROW AWAY SAMPLES WE ARE NOT STARTED YET!
86                 return 0;
87             }
88         }
89         } 
90         // Started case
91         else 
92         {
93         if (type != 0){
94                         /* Received control packet -- parse and update locally stored parameters */
95             printf("Parsing control Packet\n");
96             rcv->parse_control(&buffer[0], seq);       
97         }
98             
99         else {
100             int samples_missed = seq - d_lastseq - 366;
101             if(samples_missed > 0)
102             {
103                 printf("dropped %d samples.\n", samples_missed);
104             }
105             d_lastseq = seq;
106         }
107         }
108         
109         if(noutput_items< 366*2){
110                 printf("NOT ENOUGH SPACE IN OUTPUT BUFFER!!! >:-(\n");
111                 }
112         
113         memcpy(&out1[0], &buffer[6], BUF_LEN - 6);
114         
115 //      for(int i = 0; i < 366*2; i++){
116 //              out1[i] = (float)  (*((short*) &buffer[6+2*i]) );
117 //      }
118         
119         return 366*2;
120 }
121
122 //bool msdd_rs_source_simple::set_decim_rate(unsigned int rate)
123 //{
124 //      // FIXME seems buggy.  How about a floor or ceil?
125 //        rcv->set_decim((int) log2(rate));
126 //      return true;
127 //}
128
129 bool msdd_rs_source_simple::set_rx_freq(double freq)
130 {
131         long new_fc = (long)freq;
132         rcv->set_fc( new_fc/1000000, new_fc%1000000);
133         return true;
134 }
135
136
137 bool msdd_rs_source_simple::set_ddc_gain(double gain)
138 {
139         if(gain < 0 || gain > 7){ // only 3 bits available.
140                 printf("GAIN IS OUTSIDE ACCEPTABLE RANGE!\n");
141                 return false;
142         }
143         //decimation gain
144         rcv->set_ddc_gain((int)gain);
145         return true;
146 }
147
148 // Set desired sample rate of MSDD6000 -- Note bounds checking is 
149 // done by the module and it will return the value actually used in the hardware.
150 bool msdd_rs_source_simple::set_ddc_samp_rate(double rate)
151 {
152         rcv->set_ddc_samp_rate((float) rate);
153         return true;            
154 }
155
156 // Set desired input BW of MSDD6000 -- Note bounds checking is 
157 // done by the module and it will return the value actually used in the hardware.
158 bool msdd_rs_source_simple::set_ddc_bw(double bw)
159 {
160         rcv->set_ddc_bw((float) bw);
161         return true;            
162 }
163
164 bool msdd_rs_source_simple::set_rf_atten(double rf_atten)
165 {
166         rcv->set_rf_attn((int) rf_atten);
167         return true;
168 }
169
170 bool msdd_rs_source_simple::start()
171 {
172         rcv->start();
173     rcv->stop_data();
174         return true;
175 }
176
177 bool msdd_rs_source_simple::stop()
178 {
179         rcv->stop();
180         return true;
181 }
182
183 int msdd_rs_source_simple::start_data()
184 {
185         return rcv->start_data();
186 }
187
188 int msdd_rs_source_simple::stop_data()
189 {
190         return rcv->stop_data();
191 }
192
193 /* Query functions */
194 long msdd_rs_source_simple::pull_adc_freq(){
195         return 102400000;
196 }
197
198 /* Request the current ddc sample rate */
199 float msdd_rs_source_simple::pull_ddc_samp_rate(){
200         return(rcv->pull_ddc_samp_rate());
201 }
202
203 /* Request the current ddc bandwidth */
204 float msdd_rs_source_simple::pull_ddc_bw(){
205         return(rcv->pull_ddc_bw());
206         
207 }
208
209 /* Request the current rx freq */
210 float msdd_rs_source_simple::pull_rx_freq(){
211         return(rcv->pull_rx_freq());
212 }
213
214 /* Request current ddc gain */
215 int msdd_rs_source_simple::pull_ddc_gain(){
216         return(rcv->pull_ddc_gain());
217 }
218
219 /* Request current RF attenuation */
220 int msdd_rs_source_simple::pull_rf_atten(){
221         return(rcv->pull_rf_atten());
222 }
223
224 std::vector<int> msdd_rs_source_simple::gain_range(){
225         static std::vector<int> r;
226         r.push_back(0);
227         r.push_back(12);
228         return r;
229 }
230
231 std::vector<float> msdd_rs_source_simple::freq_range(){
232         std::vector<float> r;
233         r.push_back(30.0*1000*1000);
234         r.push_back(6.0*1000*1000*1000);
235         return r;
236 }