cremalink.core.binary module
This module provides binary utility functions for handling data conversions, checksum calculations, and bitwise operations, often required for communication with hardware devices.
- cremalink.core.binary.b64_to_cmd_hex(b64_data: str) str[source]
Decodes a base64 string, extracts a command frame, and returns it as a hex string.
The function cleans the input base64 string, decodes it, and then uses the second byte of the decoded data as a length specifier to extract the relevant command frame.
- Parameters:
b64_data – The base64 encoded data string.
- Returns:
The extracted command frame as a hexadecimal string.
- cremalink.core.binary.crc16_ccitt(data: bytes) bytes[source]
Calculates the CRC-16 CCITT checksum for the given data.
This implementation uses a specific initial value (0x1D0F) and polynomial (0x1021).
- Parameters:
data – The input bytes to checksum.
- Returns:
A 2-byte sequence representing the calculated CRC in big-endian format.
- cremalink.core.binary.get_bit(byte_value: int, bit_index: int) bool[source]
Gets the value of a specific bit in a byte.
- Parameters:
byte_value – The integer representation of the byte.
bit_index – The index of the bit to retrieve (0-7).
- Returns:
True if the bit is 1, False if it is 0.
- Raises:
ValueError – If bit_index is not between 0 and 7.
- cremalink.core.binary.safe_byte_at(data: Iterable[int] | bytes, index: int) int | None[source]
Safely retrieves a byte from a sequence or iterable at a given index.
- Parameters:
data – The data to access, can be bytes or an iterable of integers.
index – The index of the byte to retrieve.
- Returns:
The byte as an integer if the index is valid, otherwise None.