Statistics
| Branch: | Tag: | Revision:

root / gr-noaa / lib / noaa_hrpt_deframer.cc @ 79cf7ef6

History | View | Annotate | Download (3 kB)

1 ce9a41e6 Johnathan Corgan
/* -*- c++ -*- */
2 ce9a41e6 Johnathan Corgan
/*
3 ce9a41e6 Johnathan Corgan
 * Copyright 2009 Free Software Foundation, Inc.
4 ce9a41e6 Johnathan Corgan
 * 
5 ce9a41e6 Johnathan Corgan
 * This file is part of GNU Radio
6 ce9a41e6 Johnathan Corgan
 * 
7 ce9a41e6 Johnathan Corgan
 * GNU Radio is free software; you can redistribute it and/or modify
8 ce9a41e6 Johnathan Corgan
 * it under the terms of the GNU General Public License as published by
9 ce9a41e6 Johnathan Corgan
 * the Free Software Foundation; either version 3, or (at your option)
10 ce9a41e6 Johnathan Corgan
 * any later version.
11 ce9a41e6 Johnathan Corgan
 * 
12 ce9a41e6 Johnathan Corgan
 * GNU Radio is distributed in the hope that it will be useful,
13 ce9a41e6 Johnathan Corgan
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ce9a41e6 Johnathan Corgan
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ce9a41e6 Johnathan Corgan
 * GNU General Public License for more details.
16 ce9a41e6 Johnathan Corgan
 * 
17 ce9a41e6 Johnathan Corgan
 * You should have received a copy of the GNU General Public License
18 ce9a41e6 Johnathan Corgan
 * along with GNU Radio; see the file COPYING.  If not, write to
19 ce9a41e6 Johnathan Corgan
 * the Free Software Foundation, Inc., 51 Franklin Street,
20 ce9a41e6 Johnathan Corgan
 * Boston, MA 02110-1301, USA.
21 ce9a41e6 Johnathan Corgan
 */
22 ce9a41e6 Johnathan Corgan
23 ce9a41e6 Johnathan Corgan
#ifdef HAVE_CONFIG_H
24 ce9a41e6 Johnathan Corgan
#include "config.h"
25 ce9a41e6 Johnathan Corgan
#endif
26 ce9a41e6 Johnathan Corgan
27 ce9a41e6 Johnathan Corgan
#include <noaa_hrpt_deframer.h>
28 ce9a41e6 Johnathan Corgan
#include <gr_io_signature.h>
29 4e0c8a37 Johnathan Corgan
#include <noaa_hrpt.h>
30 2628e9cd Johnathan Corgan
#include <cstring>
31 7b210507 Philip Balister
#include <cstdio>
32 ce9a41e6 Johnathan Corgan
33 ce9a41e6 Johnathan Corgan
#define ST_IDLE   0
34 ce9a41e6 Johnathan Corgan
#define ST_SYNCED 1
35 ce9a41e6 Johnathan Corgan
36 ce9a41e6 Johnathan Corgan
noaa_hrpt_deframer_sptr
37 ce9a41e6 Johnathan Corgan
noaa_make_hrpt_deframer()
38 ce9a41e6 Johnathan Corgan
{
39 ce9a41e6 Johnathan Corgan
  return gnuradio::get_initial_sptr(new noaa_hrpt_deframer());
40 ce9a41e6 Johnathan Corgan
}
41 ce9a41e6 Johnathan Corgan
42 ce9a41e6 Johnathan Corgan
noaa_hrpt_deframer::noaa_hrpt_deframer()
43 2628e9cd Johnathan Corgan
  : gr_block("noaa_hrpt_deframer",
44 2628e9cd Johnathan Corgan
             gr_make_io_signature(1, 1, sizeof(char)),
45 2628e9cd Johnathan Corgan
             gr_make_io_signature(1, 1, sizeof(short)))
46 ce9a41e6 Johnathan Corgan
{
47 2628e9cd Johnathan Corgan
  set_output_multiple(6); // room for writing full sync when received
48 1530b615 Johnathan Corgan
  d_mid_bit = true;
49 1530b615 Johnathan Corgan
  d_last_bit = 0;
50 ce9a41e6 Johnathan Corgan
  enter_idle();
51 ce9a41e6 Johnathan Corgan
}
52 ce9a41e6 Johnathan Corgan
53 ce9a41e6 Johnathan Corgan
void
54 ce9a41e6 Johnathan Corgan
noaa_hrpt_deframer::enter_idle()
55 ce9a41e6 Johnathan Corgan
{
56 ce9a41e6 Johnathan Corgan
  d_state = ST_IDLE;
57 ce9a41e6 Johnathan Corgan
}
58 ce9a41e6 Johnathan Corgan
59 ce9a41e6 Johnathan Corgan
void
60 ce9a41e6 Johnathan Corgan
noaa_hrpt_deframer::enter_synced()
61 ce9a41e6 Johnathan Corgan
{
62 ce9a41e6 Johnathan Corgan
  d_state = ST_SYNCED;
63 2628e9cd Johnathan Corgan
  d_bit_count = HRPT_BITS_PER_WORD;
64 2628e9cd Johnathan Corgan
  d_word_count = HRPT_MINOR_FRAME_WORDS-HRPT_SYNC_WORDS;
65 2628e9cd Johnathan Corgan
  d_word = 0;
66 ce9a41e6 Johnathan Corgan
}
67 ce9a41e6 Johnathan Corgan
68 ce9a41e6 Johnathan Corgan
int
69 2628e9cd Johnathan Corgan
noaa_hrpt_deframer::general_work(int noutput_items,
70 2628e9cd Johnathan Corgan
                                 gr_vector_int &ninput_items,
71 2628e9cd Johnathan Corgan
                                 gr_vector_const_void_star &input_items,
72 2628e9cd Johnathan Corgan
                                 gr_vector_void_star &output_items)
73 ce9a41e6 Johnathan Corgan
{
74 2628e9cd Johnathan Corgan
  int ninputs = ninput_items[0];
75 ce9a41e6 Johnathan Corgan
  const char *in = (const char *)input_items[0];
76 2628e9cd Johnathan Corgan
  unsigned short *out = (unsigned short *)output_items[0];
77 ce9a41e6 Johnathan Corgan
78 2628e9cd Johnathan Corgan
  int i = 0, j = 0;
79 2628e9cd Johnathan Corgan
  while (i < ninputs && j < noutput_items) {
80 ce9a41e6 Johnathan Corgan
    char bit = in[i++];
81 1530b615 Johnathan Corgan
    char diff = bit^d_last_bit;
82 1530b615 Johnathan Corgan
    d_last_bit = bit;
83 1530b615 Johnathan Corgan
84 1530b615 Johnathan Corgan
    // Wait for transition if not synced, otherwise, alternate bits
85 1530b615 Johnathan Corgan
    if (d_mid_bit && (diff | (d_state == ST_SYNCED))) {
86 1530b615 Johnathan Corgan
      switch (d_state) {
87 1530b615 Johnathan Corgan
      case ST_IDLE:
88 1530b615 Johnathan Corgan
        d_shifter = (d_shifter << 1) | bit; // MSB transmitted first
89 1530b615 Johnathan Corgan
        
90 1530b615 Johnathan Corgan
        if ((d_shifter & 0x0FFFFFFFFFFFFFFFLL) == HRPT_MINOR_FRAME_SYNC) {
91 4e0c8a37 Johnathan Corgan
          out[j++] = HRPT_SYNC1;
92 4e0c8a37 Johnathan Corgan
          out[j++] = HRPT_SYNC2;
93 4e0c8a37 Johnathan Corgan
          out[j++] = HRPT_SYNC3;
94 4e0c8a37 Johnathan Corgan
          out[j++] = HRPT_SYNC4;
95 4e0c8a37 Johnathan Corgan
          out[j++] = HRPT_SYNC5;
96 4e0c8a37 Johnathan Corgan
          out[j++] = HRPT_SYNC6;
97 1530b615 Johnathan Corgan
          enter_synced();
98 2628e9cd Johnathan Corgan
        }
99 1530b615 Johnathan Corgan
        break;
100 1530b615 Johnathan Corgan
        
101 1530b615 Johnathan Corgan
      case ST_SYNCED:
102 1530b615 Johnathan Corgan
        d_word = (d_word << 1) | bit; // MSB transmitted first
103 1530b615 Johnathan Corgan
        if (--d_bit_count == 0) {
104 1530b615 Johnathan Corgan
          out[j++] = d_word;
105 1530b615 Johnathan Corgan
          d_word = 0;
106 1530b615 Johnathan Corgan
          d_bit_count = HRPT_BITS_PER_WORD;
107 1530b615 Johnathan Corgan
          if (--d_word_count == 0) {
108 1530b615 Johnathan Corgan
            enter_idle();
109 1530b615 Johnathan Corgan
          }
110 1530b615 Johnathan Corgan
        }
111 1530b615 Johnathan Corgan
        break;
112 1530b615 Johnathan Corgan
        
113 1530b615 Johnathan Corgan
      default:
114 1530b615 Johnathan Corgan
        throw std::runtime_error("noaa_hrpt_deframer: bad state\n");
115 ce9a41e6 Johnathan Corgan
      }
116 ce9a41e6 Johnathan Corgan
117 1530b615 Johnathan Corgan
      d_mid_bit = false;
118 1530b615 Johnathan Corgan
    }
119 1530b615 Johnathan Corgan
    else {
120 1530b615 Johnathan Corgan
      d_mid_bit = true;
121 ce9a41e6 Johnathan Corgan
    }
122 ce9a41e6 Johnathan Corgan
  }
123 ce9a41e6 Johnathan Corgan
124 2628e9cd Johnathan Corgan
  consume_each(i);
125 2628e9cd Johnathan Corgan
  return j;
126 ce9a41e6 Johnathan Corgan
}