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