Merge branch 'master' into udp
[debian/gnuradio] / gr-noaa / lib / noaa_hrpt_decoder.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2009 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
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <noaa_hrpt_decoder.h>
28 #include <noaa_hrpt.h>
29 #include <gr_io_signature.h>
30 #include <cstdio>
31
32 static const char *hrpt_ids[] = {
33   "000000",
34   "NOAA11",
35   "000002",
36   "NOAA16",
37   "000004",
38   "000005",
39   "000006",
40   "NOAA15",
41   "000008",
42   "NOAA12",
43   "000010",
44   "NOAA17",
45   "000012",
46   "NOAA18",
47   "000014",
48   "NOAA19"
49 };
50
51 noaa_hrpt_decoder_sptr
52 noaa_make_hrpt_decoder(bool verbose, bool output_files)
53 {
54   return gnuradio::get_initial_sptr(new noaa_hrpt_decoder(verbose, output_files));
55 }
56
57 noaa_hrpt_decoder::noaa_hrpt_decoder(bool verbose, bool output_files)
58   : gr_sync_block("noaa_hrpt_decoder",
59                   gr_make_io_signature(1, 1, sizeof(short)),
60                   gr_make_io_signature(0, 0, 0)),
61     d_verbose(verbose),
62     d_output_files(output_files),
63     d_word_num(0),
64     d_frames_seen(0),
65     d_current_mfnum(0),
66     d_expected_mfnum(0),
67     d_seq_errs(0),
68     d_address(0),
69     d_day_of_year(0),
70     d_milliseconds(0),
71     d_last_time(0)
72 {
73   // Start of capture processing here
74 }
75
76 int
77 noaa_hrpt_decoder::work(int noutput_items,
78                         gr_vector_const_void_star &input_items,
79                         gr_vector_void_star &output_items)
80 {
81   const unsigned short *in = (const unsigned short*)input_items[0];
82
83   int i = 0;
84   while (i < noutput_items) {
85     d_current_word = in[i++] & 0x3FF;
86     d_word_num++;
87
88     // Per HRPT word processing here
89
90     switch (d_word_num) {
91     case 7:
92       process_mfnum();
93       process_address();
94       break;
95
96     case 9:
97       process_day_of_year();
98       break;
99
100     case 10:
101       process_milli1();
102       break;
103
104     case 11:
105       process_milli2();
106       break;
107
108     case 12:
109       process_milli3();
110       break;
111
112     default:
113       break;
114     }
115
116     if (d_word_num == HRPT_MINOR_FRAME_WORDS) {
117
118       // End of minor frame processing here
119       d_frames_seen++;
120       d_word_num = 0;
121       fprintf(stderr, "\n");
122     }
123   }
124
125   return i;
126 }
127
128 void
129 noaa_hrpt_decoder::process_mfnum()
130 {
131   d_current_mfnum = (d_current_word & 0x180) >> 7;
132
133   if (d_verbose)
134     fprintf(stderr, "MF:");
135
136   if (d_current_mfnum != d_expected_mfnum && d_frames_seen > 0) {
137     d_seq_errs++;
138
139     if (d_verbose)
140       fprintf(stderr, "*");
141   }
142   else
143     if (d_verbose)
144       fprintf(stderr, " ");
145   
146   if (d_verbose)
147     fprintf(stderr, "%i  ", d_current_mfnum);
148   d_expected_mfnum = (d_current_mfnum == 3) ? 1 : d_current_mfnum+1;
149 }
150
151 void
152 noaa_hrpt_decoder::process_address()
153 {
154   d_address = ((d_current_word & 0x078) >> 3) & 0x000F;
155
156   if (d_verbose)
157     fprintf(stderr, "SA: %s  ", hrpt_ids[d_address]);
158 }
159
160 void
161 noaa_hrpt_decoder::process_day_of_year()
162 {
163   d_day_of_year = d_current_word >> 1;
164
165   if (d_verbose)
166     fprintf(stderr, "DOY: %3i  ", d_day_of_year);
167 }
168
169 void
170 noaa_hrpt_decoder::process_milli1()
171 {
172   d_milliseconds = (d_current_word & 0x7F) << 20;
173 }
174
175 void
176 noaa_hrpt_decoder::process_milli2()
177 {
178   d_milliseconds |= (d_current_word << 10);
179 }
180
181 void
182 noaa_hrpt_decoder::process_milli3()
183 {
184   d_milliseconds |= d_current_word;
185   int delta = d_milliseconds - d_last_time;
186   d_last_time = d_milliseconds;
187
188   if (d_verbose)
189     fprintf(stderr, "MS: %8i  DT: %8i", d_milliseconds, delta);
190 }
191
192 noaa_hrpt_decoder::~noaa_hrpt_decoder()
193 {
194   // End of capture processing here
195
196   if (d_verbose) {
197     fprintf(stderr, "Frames seen:     %10i\n", d_frames_seen);
198     fprintf(stderr, "Sequence errors: %10i\n", d_seq_errs);
199   }
200 }