* Add ranges * Initial Hugo setup * Work in progress * Added selection for local or remote site * Updated to 'light' theme * Changed to using Hextra Hugo theme * Changed to using Hextra Hugo theme * Changed to Hextra Hugo theme * Change to Hextra Hugo theme * Updated Hugo setup. * Updated Hugo setup. # Conflicts: # docs/releases/_index.md * Work in progress * Added new fonts Added new documentation * Latest documentation updates * Latest documentation updates # Conflicts: # docs/containers/array.md # docs/containers/array_view.md # docs/containers/array_wrapper.md # docs/containers/bip_buffer_spsc_atomic.md # docs/containers/bitset.md # docs/containers/indirect_vector.md # docs/containers/vector.md # docs/getting-started/compilers.md * Added bloom_filter markdown doc * Added more documentation Updated CSS for light and dark modes * Fixed some menus Added mode documentation files * Updated CSS rules Added badges to home page Added uniqur_ptr + pool tutorial * Fixed formatting on the home page markdown Modified light amd dark code formatting * Updated unique_ptr-with-pool * Added container and shared message tutorials * Updates to documentation * Added const_multimap * Updated source-formatting.md * Added initial raw text files form Web site editor * Innore coverage build directory * Exported raw text documentation files from the web site editor * Hugo updates * Added Hugo intalation and markdown descriptions * More addition to the documentation * Added closure.md and updates to delegate.md * Added format.md * Added documentation for etl::delegate_observable, etl::function, Base64 codec * Added io_port documentation * Added basic_format_spec * Added documentation for string_stream and string utilities. * Added more documentation Updated the documentation CSS * Added documentation for clocks, day, duration * Added more documentation for chrono classes Updated callouts * More chrono documentation * Completed chrono documentation * Maths functions documentation * Completed maths documentation * Completed maths documentation * Completed maths documentation * Completed maths documentation * Added multiple documentation files * Added iterator.md * Added debug_count.md and versions.md * Added debug_count.md and versions.md * Added more documentation * More documentation * Added some design pattern documentation Modified some of the layout files Modified the About documentation * Converted more documentation pages Modified the site CSS * Added more documentation Moced some documentation files to new directories * Added more documentation Tweaks to CSS * Added callback_timer_deferred_locked documentation * Added callback_timer_locked documentation * More documentation updates * More documentation updates * More documentation updates * New documentation files. Harmonised file name format * New documentation files. * Multiple document updates * Multiple document updates * Final conversion of web pages * Updates before PR * Updates before PR * Updates before PR # Conflicts: # docs/blog/_index.md * Final pre PR updates * Updates to message framework documentation * Renamed directory * Fix spelling * Added author and date to blog files Moved documentation files merged from development * Fixed 'Description' typo * Fix typos # Conflicts: # docs/IO/io_port.md # docs/containers/sets/const-multiset.md # docs/containers/sets/const-set.md # docs/maths/correlation.md # docs/maths/gamma.md * Renamed two files to lower case * Minor renaming * Added author and date * Updated callout on bresenham_line.md Added support for showing the ETL version on the documentation first page, by copying the version.txt file as a hugo asset. Updated the Python 'update_release.py' to copy 'version.txt' * Replace space in filename with hyphen. Added more information to hugo-commands.md * Replace space in filename with hyphen. Added more information to hugo-commands.md # Conflicts: # docs/getting-started/view-the-docs-locally/hugo-commands.md * Added a link to pseudo_moving_average.md * Updated title pages for groups * Fixed missing 404 for non-existent pages * Fixed coordinate variable names in the 'Calculating the intersection' example --------- Co-authored-by: Roland Reichwein <Roland.Reichwein@bmw.de> Co-authored-by: John Wellbelove <john.wellbelove@etlcpp.com> Co-authored-by: John Wellbelove <john.wellbelove@etlcpp.co.uk>
8.5 KiB
| title | weight |
|---|---|
| Manchester encoding and decoding | 1 |
{{< callout type="info">}}
Header: manchester.h
Support: 20.45.0
{{< /callout >}}
Efficient Manchester encoding and decoding of data. The Manchester code represents a data bit as a sequence of a 'high' and a 'low' values. In software this translates to a conversion from one to two bits, or in a practical situation, from n bytes to n*2 bytes.
See also
Features
- Normal and inverted Manchester encoding
- Support for multiple encoding chunk sizes: 8-bit, 16-bit and 32-bit
- Span-based operations or chunk-based operations
- Constexpr functions for compile-time encoding/decoding
- Validation of encoded data
- Chunked span I/O uses little-endian byte order for multi-byte chunks, independent of host platform endianness
Algorithm background
To encode the value 0b11001100 we must first duplicate all bits to create the value 0b1111000011110000. We then perform an XOR of this value with the constant 0b1010101010101010 (0xAAAA) to obtain the Manchester coded value of 0b1010010110100101. We have now replaced each 1 bit with the sequence 10 and each 0 bit with the sequence 01.
2. Bit duplication
Bit duplication is achieved with the following steps. This is also called binary interleaving. The example shows encoding of an 8-bit value.
| Step | High Byte | Low Byte | Operation |
|---|---|---|---|
| 0 | _ _ _ _ _ _ _ _ |
A B C D E F G H |
input value (i) |
| 1 | _ _ _ _ A B C D |
_ _ _ _ E F G H |
(i | (i << 4)) & 0x0F0F |
| 2 | _ _ A B _ _ C D |
_ _ E F _ _ G H |
(i | (i << 2)) & 0x3333 |
| 3 | _ A _ B _ C _ D |
_ E _ F _ G _ H |
(i | (i << 1)) & 0x5555 |
| 4 | A A B B C C D D |
E E F F G G H H |
(i | (i << 1)) |
This process can be easily extended to 16-bit or 32-bit values by adding additional steps to the bit duplication.
3. Manchester Decoding
Manchester decoding is done in a similar, but reversed way.
4. Error Detection
Error detection in Manchester coded data is done by comparing 2 neighboring bits. If they are equal, then there is an error in the encoded input data.
Comparing all 8 bit pairs in a 16-bit word is done as follows.
| Step | Binary Value | Operation | Description |
|---|---|---|---|
| 1 | 11011000 |
Original | First bit pair (lsb, 00) is invalid. Last bit pair is also invalid. Other bit pairs are valid |
| 2 | 01101100 |
Shift right by 1 | Shift the original value right by one bit |
| 3 | 10110100 |
XOR | XOR the original with the shifted value |
| 4 | 01010101 |
Mask with 0x55 | Apply mask to isolate bit pairs |
| 5 | 00010100 |
Result | If result is not equal to 0x55, there was an error in the input |
Analysis
Most traditional ways to Manchester encode data consist of a loop over all bits and a nested if-statement to check the value of the current bit. This approach does not scale well to increasing number of bits. The algorithm implemented here contains no conditional code and scales well. Doubling the number of processed bits per step (the chunk size) adds a single row to the bit duplication table. Because of the lack of loops and conditional code, this algorithm is likely to perform better than traditional ones on simple processors or when compiler optimization is disabled. On modern, powerful processors with caches and advanced optimization possibilities this algorithm may not show much benefit. In any case, the performance of the algorithm depends heavily on the processor type, compiler and compiler (optimization) settings.
Classes
Classes etl::manchester and etl::manchester_inverted contain static functions for encoding, decoding and validity checking. It is not necessary to instantiate objects of these classes.
etl::manchester
typedef manchester_base<private_manchester::manchester_type_normal> manchester;
Manchester encoder using normal encoding (no inversion).
etl::manchester_inverted
typedef manchester_base<private_manchester::manchester_type_inverted> manchester_inverted;
Manchester encoder using inverted encoding.
Encoding Functions
Encode single value
template <typename TDecoded>
static ETL_CONSTEXPR14 typename encoded<TDecoded>::type encode(TDecoded decoded)
Encodes a single value using Manchester encoding.
Parameters
decoded: The value to encode (uint8_t, uint16_t, or uint32_t)
Returns
The Manchester encoded value (twice the bit width of input)
Example
uint16_t encoded = etl::manchester::encode(0x55);
Encode range
template <typename TChunk = uint_least8_t>
static ETL_CONSTEXPR14 void encode(etl::span<const uint_least8_t> decoded,
etl::span<uint_least8_t> encoded)
Encodes a span of data using the specified chunk size.
Parameters
decoded: Source data to encode
encoded: Destination for encoded data (must be twice the size of decoded)
Template Parameters
TChunk: Chunk size for encoding (uint8_t, uint16_t or uint32_t)
Example
std::array<uint8_t, 4> data = {0x12, 0x34, 0x56, 0x78};
std::array<uint8_t, 8> encoded_data1{};
std::array<uint8_t, 8> encoded_data2{};
// Encode with TChunk == uint8_t
etl::manchester::encode(data, encoded_data1);
// Encode with TChunk == uint32_t
etl::manchester::encode<uint32_t>(data, encoded_data2);
Decoding Functions
Decode single value
template <typename TEncoded>
static ETL_CONSTEXPR14 typename decoded<TEncoded>::type decode(TEncoded encoded)
Description
Decodes a single Manchester encoded value.
Parameters
encoded: The encoded value to decode (uint16_t, uint32_t, or uint64_t)
Returns
The Manchester decoded value (half the bit width of input)
Example
uint8_t decoded = etl::manchester::decode(0x5A5A);
Decode range
template <typename TChunk =
typename private_manchester::encoded<uint_least8_t>::type>
static ETL_CONSTEXPR14
void decode(etl::span<const uint_least8_t> encoded,
etl::span<uint_least8_t> decoded)
Decodes a span of Manchester encoded data.
Parameters
encoded: Source data to decode
decoded: Destination for decoded data (must be half the size of encoded)
Template Parameters
TChunk: Chunk type for decoding (uint16_t, uint32_t, or uint64_t)
Example
std::array<uint8_t, 8> encoded = {/* ... */};
std::array<uint8_t, 4> decoded1 {};
std::array<uint8_t, 4> decoded2 {};
// Decode with TChunk == uint16_t
etl::manchester::decode(encoded, decoded1);
// Decode with TChunk == uint64_t
etl::manchester::decode<uint64_t>(encoded, decoded2);
Validation Functions
Single value
template <typename TChunk>
static ETL_CONSTEXPR14 bool is_valid(TChunk encoded)
Validates that a single value contains valid Manchester encoded data.
Parameters
encoded: The encoded value to validate
Returns
true if the value contains valid Manchester encoded data, false otherwise
Example
bool valid = etl::manchester::is_valid(0x5A5A);
Range
static
ETL_CONSTEXPR14
bool is_valid(etl::span<const uint_least8_t> encoded)
Validates that a range contains valid Manchester encoded data.
Parameters
encoded: The range of encoded data to validate
Returns
true if all data is valid Manchester encoding, false otherwise
Example
std::array<uint8_t, 8> encoded_data = {/* ... */};
bool valid = etl::manchester::is_valid(encoded_data);
Input/chunk types for encoding
uint8_t → uint16_t (if 8-bit types are supported)
uint16_t → uint32_t
uint32_t → uint64_t (if 64-bit types are supported)
Input/chunk types for decoding
uint16_t → uint8_t (if 8-bit types are supported)
uint32_t → uint16_t
uint64_t → uint32_t (if 64-bit types are supported)