Merged gr-msdd6000 portability fix to trunk (eb/msdd -r8940:9027)
[debian/gnuradio] / gr-msdd6000 / src / msdd_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_source_simple.h>
26 #include <gr_io_signature.h>
27
28
29 msdd_source_simple_sptr
30 msdd_make_source_simple ( const char *src, unsigned short port_src) 
31 {
32   return msdd_source_simple_sptr (new msdd_source_simple ( src, port_src)); 
33 }
34
35
36 msdd_source_simple::msdd_source_simple (
37                     const char *src, 
38                     unsigned short port_src) 
39                 : gr_sync_block("MSDD_SOURCE_SIMPLE",
40                                 gr_make_io_signature (0,0,0),
41                                 gr_make_io_signature (1, 1, sizeof (short))),
42                   rcv(new MSDD6000((char*) src)), d_lastseq(0)
43 {
44 }
45
46 msdd_source_simple::~msdd_source_simple ()
47 {
48 }
49
50
51 int
52 msdd_source_simple::work (int noutput_items,
53                          gr_vector_const_void_star &input_items,
54                          gr_vector_void_star &output_items)
55 {
56         
57 #define BUF_LEN (366*sizeof(short)*2 + 6)
58
59         float* out1 =(float*) output_items[0];
60
61         char buffer[BUF_LEN];
62         rcv->read( &buffer[0], BUF_LEN );
63
64         int seq = *((int*) &buffer[2]);
65         
66         // FIXME get rid of these magic 366's!
67         if(d_lastseq == -366){
68                 // not started case
69                 if(seq == 0){
70                         d_lastseq = 0;
71                         } else {
72                         // THROW AWAY SAMPLES WE ARE NOT STARTED YET!
73                         return 0;
74                         }
75                 
76                 } else {
77                 // started case
78                 int samples_missed = seq - d_lastseq - 366;
79                 if(samples_missed > 0){
80                         printf("dropped %d samples.\n", samples_missed);
81                         }
82                 d_lastseq = seq;
83                 }
84         
85         if(noutput_items< 366*2){
86                 printf("NOT ENOUGH SPACE IN OUTPUT BUFFER!!! >:-(\n");
87                 }
88         
89         memcpy(&out1[0], &buffer[6], BUF_LEN - 6);
90         
91 //      for(int i = 0; i < 366*2; i++){
92 //              out1[i] = (float)  (*((short*) &buffer[6+2*i]) );
93 //      }
94         
95         return 366*2;
96 }
97
98 bool msdd_source_simple::set_decim_rate(unsigned int rate)
99 {
100         // FIXME seems buggy.  How about a floor or ceil?
101         rcv->set_decim((int) log2(rate));
102         return true;
103 }
104
105
106 bool msdd_source_simple::set_rx_freq(int channel, double freq)
107 {
108         long new_fc = (long)freq;
109         rcv->set_fc( new_fc/1000000, new_fc%1000000);
110         return true;
111 }
112
113
114 bool msdd_source_simple::set_pga(int which, double gain)
115 {
116         if(gain < 0 || gain > 10){
117                 printf("GAIN IS OUTSIDE ACCEPTABLE RANGE!\n");
118                 return false;
119         }
120         // ok i lied this is not really a pga, its decimation gain
121         rcv->set_ddc_gain((int)gain);
122         return true;
123 }
124
125
126 bool msdd_source_simple::start()
127 {
128         rcv->start();
129         return true;
130 }
131
132
133 bool msdd_source_simple::stop()
134 {
135         rcv->stop();
136         return true;
137 }
138
139 long msdd_source_simple::adc_freq(){
140         return 102400000;
141 }
142
143 int msdd_source_simple::decim_rate(){
144         return 1 << rcv->d_decim;
145 }
146
147
148 std::vector<int> msdd_source_simple::gain_range(){
149         static std::vector<int> r;
150         r.push_back(0);
151         r.push_back(12);
152         return r;
153 }
154
155 std::vector<float> msdd_source_simple::freq_range(){
156         std::vector<float> r;
157         r.push_back(30.0*1000*1000);
158         r.push_back(6.0*1000*1000*1000);
159         return r;
160 }