diff options
-rw-r--r-- | gr-blocks/grc/blocks_repack_bits_bb.xml | 22 | ||||
-rw-r--r-- | gr-blocks/include/gnuradio/blocks/repack_bits_bb.h | 12 | ||||
-rw-r--r-- | gr-blocks/lib/repack_bits_bb_impl.cc | 117 | ||||
-rw-r--r-- | gr-blocks/lib/repack_bits_bb_impl.h | 7 | ||||
-rwxr-xr-x | gr-blocks/python/blocks/qa_repack_bits_bb.py | 14 |
5 files changed, 116 insertions, 56 deletions
diff --git a/gr-blocks/grc/blocks_repack_bits_bb.xml b/gr-blocks/grc/blocks_repack_bits_bb.xml index 4ad5ec631b..47aa73b02c 100644 --- a/gr-blocks/grc/blocks_repack_bits_bb.xml +++ b/gr-blocks/grc/blocks_repack_bits_bb.xml @@ -2,25 +2,29 @@ <name>Repack Bits</name> <key>blocks_repack_bits_bb</key> <import>from gnuradio import blocks</import> - <make>blocks.repack_bits_bb($k, $l, $len_tag_key, $align_output)</make> + <make>blocks.repack_bits_bb($k, $l, $len_tag_key, $align_output, $swap)</make> + <param> <name>Bits per input byte</name> <key>k</key> <value>1</value> <type>int</type> </param> + <param> <name>Bits per output byte</name> <key>l</key> <value>8</value> <type>int</type> </param> + <param> <name>Length Tag Key</name> <key>len_tag_key</key> <value>""</value> <type>string</type> </param> + <param> <name>Packet Alignment</name> <key>align_output</key> @@ -35,6 +39,22 @@ <key>False</key> </option> </param> + + <param> + <name>Swap Bits</name> + <key>swap</key> + <value>False</value> + <type>enum</type> + <option> + <name>Yes</name> + <key>True</key> + </option> + <option> + <name>No</name> + <key>False</key> + </option> + </param> + <sink> <name>in</name> <type>byte</type> diff --git a/gr-blocks/include/gnuradio/blocks/repack_bits_bb.h b/gr-blocks/include/gnuradio/blocks/repack_bits_bb.h index c594966e6e..425cfa0855 100644 --- a/gr-blocks/include/gnuradio/blocks/repack_bits_bb.h +++ b/gr-blocks/include/gnuradio/blocks/repack_bits_bb.h @@ -1,6 +1,6 @@ /* -*- c++ -*- */ /* - * Copyright 2012 Free Software Foundation, Inc. + * Copyright 2012,2014 Free Software Foundation, Inc. * * This file is part of GNU Radio * @@ -25,6 +25,7 @@ #include <gnuradio/blocks/api.h> #include <gnuradio/tagged_stream_block.h> +#include <gnuradio/endianness.h> namespace gr { namespace blocks { @@ -50,17 +51,18 @@ namespace gr { typedef boost::shared_ptr<repack_bits_bb> sptr; /*! - * \param k Number of relevant bits on the input stream - * \param l Number of relevant bits on the output stream + * \param k Number of relevant bits on the input stream. + * \param l Number of relevant bits on the output stream. * \param len_tag_key If not empty, this is the key for the length tag. * \param align_output If len_tag_key is given, this controls if the input * or the output is aligned. + * \param swap Swaps the output bits */ - static sptr make(int k, int l=8, const std::string &len_tag_key="", bool align_output=false); + static sptr make(int k, int l=8, const std::string &len_tag_key="", + bool align_output=false, bool swap=false); }; } // namespace blocks } // namespace gr #endif /* INCLUDED_BLOCKS_REPACK_BITS_BB_H */ - diff --git a/gr-blocks/lib/repack_bits_bb_impl.cc b/gr-blocks/lib/repack_bits_bb_impl.cc index af83187e92..acf133e83a 100644 --- a/gr-blocks/lib/repack_bits_bb_impl.cc +++ b/gr-blocks/lib/repack_bits_bb_impl.cc @@ -1,6 +1,6 @@ /* -*- c++ -*- */ /* - * Copyright 2012 Free Software Foundation, Inc. + * Copyright 2012,2014 Free Software Foundation, Inc. * * This file is part of GNU Radio * @@ -31,26 +31,30 @@ namespace gr { namespace blocks { repack_bits_bb::sptr - repack_bits_bb::make(int k, int l, const std::string &len_tag_key, bool align_output) + repack_bits_bb::make(int k, int l, const std::string &len_tag_key, + bool align_output, bool swap) { - return gnuradio::get_initial_sptr (new repack_bits_bb_impl(k, l, len_tag_key, align_output)); + return gnuradio::get_initial_sptr + (new repack_bits_bb_impl(k, l, len_tag_key, + align_output, swap)); } - repack_bits_bb_impl::repack_bits_bb_impl(int k, int l, const std::string &len_tag_key, bool align_output) + repack_bits_bb_impl::repack_bits_bb_impl(int k, int l, + const std::string &len_tag_key, + bool align_output, bool swap) : tagged_stream_block("repack_bits_bb", - io_signature::make(1, 1, sizeof (char)), - io_signature::make(1, 1, sizeof (char)), - len_tag_key), - d_k(k), d_l(l), - d_packet_mode(!len_tag_key.empty()), - d_in_index(0), d_out_index(0), - d_align_output(align_output) + io_signature::make(1, 1, sizeof (char)), + io_signature::make(1, 1, sizeof (char)), + len_tag_key), + d_k(k), d_l(l), d_packet_mode(!len_tag_key.empty()), + d_in_index(0), d_out_index(0), d_align_output(align_output), + d_swap(swap) { - if (d_k > 8 || d_k < 1 || d_l > 8 || d_l < 1) { + if(d_k > 8 || d_k < 1 || d_l > 8 || d_l < 1) { throw std::invalid_argument("k and l must be in [1, 8]"); } - set_relative_rate((double) d_k / d_l); + set_relative_rate(static_cast<double>(d_k) / static_cast<double>(d_l)); } repack_bits_bb_impl::~repack_bits_bb_impl() @@ -61,7 +65,7 @@ namespace gr { repack_bits_bb_impl::calculate_output_stream_length(const gr_vector_int &ninput_items) { int n_out_bytes_required = (ninput_items[0] * d_k) / d_l; - if ((ninput_items[0] * d_k) % d_l && (!d_packet_mode || (d_packet_mode && !d_align_output))) { + if((ninput_items[0] * d_k) % d_l && (!d_packet_mode || (d_packet_mode && !d_align_output))) { n_out_bytes_required++; } @@ -69,50 +73,72 @@ namespace gr { } int - repack_bits_bb_impl::work (int noutput_items, - gr_vector_int &ninput_items, - gr_vector_const_void_star &input_items, - gr_vector_void_star &output_items) + repack_bits_bb_impl::work(int noutput_items, + gr_vector_int &ninput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) { - const unsigned char *in = (const unsigned char *) input_items[0]; - unsigned char *out = (unsigned char *) output_items[0]; + const unsigned char *in = (const unsigned char *)input_items[0]; + unsigned char *out = (unsigned char *)output_items[0]; int bytes_to_write = noutput_items; - if (d_packet_mode) { // noutput_items could be larger than necessary + if(d_packet_mode) { // noutput_items could be larger than necessary int bytes_to_read = ninput_items[0]; bytes_to_write = bytes_to_read * d_k / d_l; - if (!d_align_output && (((bytes_to_read * d_k) % d_l) != 0)) { + if(!d_align_output && (((bytes_to_read * d_k) % d_l) != 0)) { bytes_to_write++; } } int n_read = 0; int n_written = 0; - while(n_written < bytes_to_write && n_read < ninput_items[0]) { - if (d_out_index == 0) { // Starting a fresh byte - out[n_written] = 0; - } - out[n_written] |= ((in[n_read] >> d_in_index) & 0x01) << d_out_index; - - d_in_index = (d_in_index + 1) % d_k; - d_out_index = (d_out_index + 1) % d_l; - if (d_in_index == 0) { - n_read++; - d_in_index = 0; - } - if (d_out_index == 0) { - n_written++; - d_out_index = 0; - } + if(!d_swap) { + while(n_written < bytes_to_write && n_read < ninput_items[0]) { + if(d_out_index == 0) { // Starting a fresh byte + out[n_written] = 0; + } + out[n_written] |= ((in[n_read] >> d_in_index) & 0x01) << d_out_index; + + d_in_index = (d_in_index + 1) % d_k; + d_out_index = (d_out_index + 1) % d_l; + if(d_in_index == 0) { + n_read++; + d_in_index = 0; + } + if(d_out_index == 0) { + n_written++; + d_out_index = 0; + } + } + } + else { + while(n_written < bytes_to_write && n_read < ninput_items[0]) { + if(d_out_index == 0) { // Starting a fresh byte + out[n_written] = 0; + } + out[n_written] |= ((in[n_read] >> d_in_index) & 0x01) << (d_l-1-d_out_index); + + d_in_index = (d_in_index + 1) % d_k; + d_out_index = (d_out_index + 1) % d_l; + if(d_in_index == 0) { + n_read++; + d_in_index = 0; + } + if(d_out_index == 0) { + n_written++; + d_out_index = 0; + } + } } - if (d_packet_mode) { - if (d_out_index) { - n_written++; - d_out_index = 0; - } - } else { - consume_each(n_read); + if(d_packet_mode) { + if(d_out_index) { + n_written++; + d_out_index = 0; + } + } + else { + consume_each(n_read); } return n_written; @@ -120,4 +146,3 @@ namespace gr { } /* namespace blocks */ } /* namespace gr */ - diff --git a/gr-blocks/lib/repack_bits_bb_impl.h b/gr-blocks/lib/repack_bits_bb_impl.h index ffb8349d35..3e7d9e6e45 100644 --- a/gr-blocks/lib/repack_bits_bb_impl.h +++ b/gr-blocks/lib/repack_bits_bb_impl.h @@ -1,6 +1,6 @@ /* -*- c++ -*- */ /* - * Copyright 2012 Free Software Foundation, Inc. + * Copyright 2012,2014 Free Software Foundation, Inc. * * This file is part of GNU Radio * @@ -37,12 +37,14 @@ namespace gr { int d_in_index; // Current bit of input byte int d_out_index; // Current bit of output byte bool d_align_output; //! true if the output shall be aligned, false if the input shall be aligned + bool d_swap; protected: int calculate_output_stream_length(const gr_vector_int &ninput_items); public: - repack_bits_bb_impl(int k, int l, const std::string &len_tag_key, bool align_output); + repack_bits_bb_impl(int k, int l, const std::string &len_tag_key, + bool align_output, bool swap=false); ~repack_bits_bb_impl(); int work(int noutput_items, @@ -55,4 +57,3 @@ namespace gr { } // namespace gr #endif /* INCLUDED_BLOCKS_REPACK_BITS_BB_IMPL_H */ - diff --git a/gr-blocks/python/blocks/qa_repack_bits_bb.py b/gr-blocks/python/blocks/qa_repack_bits_bb.py index d9bbfe4fac..ad3f7728b1 100755 --- a/gr-blocks/python/blocks/qa_repack_bits_bb.py +++ b/gr-blocks/python/blocks/qa_repack_bits_bb.py @@ -121,6 +121,18 @@ class qa_repack_bits_bb (gr_unittest.TestCase): self.assertEqual(pmt.symbol_to_string(out_tag.key), tag_name) self.assertEqual(pmt.to_long(out_tag.value), len(expected_data)) + def test_006_msb0 (self): + """ 8 -> 3 """ + src_data = 200*(0b11110100, 0b11111111, 0b00111111) + expected_data = 200*((0b001,) + (0b011,) + (0b111,)*5 + (0b100,)) + k = 8 + l = 3 + src = blocks.vector_source_b(src_data, False, 1) + repack = blocks.repack_bits_bb(k, l, swap=True) + sink = blocks.vector_sink_b() + self.tb.connect(src, repack, sink) + self.tb.run () + self.assertEqual(sink.data(), expected_data) + if __name__ == '__main__': gr_unittest.run(qa_repack_bits_bb, "qa_repack_bits_bb.xml") - |