bit operation¶
Bit operations for parsing header and payload information. Currently for use in streamDaq module.
- class mio.bit_operation.BufferFormatter¶
Class for bit/byte operations and converting the input buffer into header and data numpy arrays.
- static _reverse_bits_in_array(arr: ndarray) ndarray¶
Reverse the bit order in each 32-bit element of a numpy array. Check out the input/output in binary format to see the bit orders are reversed.
- Parameters:
arr (np.ndarray) – The input array.
- Returns:
The array with bits in each element reversed.
- Return type:
np.ndarray
Examples
>>> _reverse_bits_in_array(np.array([0b11000011101010000100000000000000], dtype=np.uint32)) array([136643], dtype=uint32) #0b00000000000000100001010111000011 in binary format >>> _reverse_bits_in_array(np.array([0b00000000000001111000000000000111], dtype=np.uint32)) array([3758219264], dtype=uint32) #0b11100000000000011110000000000000 in binary format
- static _reverse_byte_order_in_array(arr: ndarray) ndarray¶
Reverse the byte (8-bit) order of each 32-bit element in a numpy array. Check out the input/output in hexadecimal format. The order of each byte (2-digits in hex) in each 32-bit element is reversed.
- Parameters:
arr (np.ndarray) – The input array.
- Returns:
The array with byte order in each element reversed.
- Return type:
np.ndarray
Examples
>>> out = _reverse_byte_order_in_array(np.array([0x12345678, 0x11002200], dtype=np.uint32)) >>> np.vectorize(lambda x: f"0x{x:08X}")(out) array(['0x78563412', '0x00220011'], dtype='<U10')
- classmethod bytebuffer_to_ndarrays(buffer: bytes, header_length_words: int, preamble_length_words: int, reverse_header_bits: bool, reverse_header_bytes: bool, reverse_payload_bits: bool, reverse_payload_bytes: bool) Tuple[ndarray, ndarray]¶
Format the buffer and return the header (uint32) and payload (uint8) arrays. The bit/byte order can be optionally reversed.
The buffer is a concat of a uint32 header array and a uint8 payload array in bytes format. The inconsistency in element size arises from how headers are formatted in the MCU. Each sensor data is uint8, while metadata (header) uses uint32 because it needs larger size.
Bit/byte reversing is required because we handle all of this as a uint32 array in the MCU. This adds complexity to doing the bit/byte ordering on the MCU so we convert it here.
- Parameters:
buffer (bytes) – The binary buffer to format.
header_length_words (int) – Length of the header in words.
preamble_length_words (int) – Length of the preamble in words.
reverse_header_bits (bool) – Reverse bits in the header if True. Default is True.
reverse_header_bytes (bool) – Reverse byte order in the header if True. Default is True.
reverse_payload_bits (bool) – Reverse bits in the body if True. Default is True.
reverse_payload_bytes (bool) – Reverse byte order in the body if True. Default is True.
- Returns:
A tuple containing the processed header as a numpy array of uint32 and the processed payload as a numpy array of uint8.
- Return type:
Examples
Inputs like:
buf=b'\x12\x34\x56\x78\x11\x11\x11\x11\x00\x11\x22\x33\x44\x55\x66\x77', header_length_words=1, preamble_length_words=1, reverse_header_bits=False, reverse_header_bytes=False, reverse_payload_bits=False, reverse_payload_bytes=False,
Will return the following header array (uint32) and payload array (uint8):
np.array([0x11111111], dtype=np.uint32), np.array([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77], dtype=np.uint8)
Note that this truncates the preamble from the header. The first word
0x12345678is truncated becausepreamble_length_words=1.