optoKummenberg

Loading...
Searching...
No Matches
Functions
optoKummenberg.tools.parsing_tools Namespace Reference

## Functions
| parse_error (int error_code)
| encode (command_id, register_id, data=None)
| encode_vector (register, index, vector)
| decode (bytes data)

## Function Documentation

## ◆ decode()

optoKummenberg.tools.parsing_tools.decode | ( | bytes | data | ) |

    Given bytestring, will unpack/destuff values, following stuffing method described in Notes.

    Parameters
    ----------
    command_id
        Identifier corresponding to command
    register_id
        Identifier corresponding to register. See register['id'] field.

    Raises
    ------
    none

    Returns
    -------
    result: bytearray
        Destuffed bytestring, following protocol above.

    Notes
    -----
    Given a FRAME_BOUNDARY of 0x7e and ESCAPE_BYTE of 0x7d, every appearance of the bytes 0x7e or 0x7d in the data
    (so in a role other than the delimiter or the escape byte)  is replaced by the sequence 0x7d 0x5e or 0x7d 0x5d,
    respectively. This follows ESCAPE_MASK = 1 << 5, or 0x20, thus the byte is XOR'd with the mask and prepended by 0x7d

## ◆ encode()

optoKummenberg.tools.parsing_tools.encode | ( |  | command_id,
|  |  | register_id,
|  |  | data = None )

    Given command_id, register_id and payload, will determine proper bytestring to transmit.
    Uses the following format:

    +----------+---------------------+---------+-----------+------------+---------------+----------+
    | Frame    | Reserved            | Command | Data size | Data       | CRC           | Frame    |
    | Boundary | slave address: 0x00 |         |           | payload    | if applicable | Boundary |
    +==========+=====================+=========+===========+============+===============+==========+
    | 1 byte   | 1 byte              | 1 byte  | 1 byte    | 0-50 bytes | 2 bytes       | 1 byte   |
    +----------+---------------------+---------+-----------+------------+---------------+----------+

    Parameters
    ----------
    command_id
    Identifier corresponding to command
    register_id
    Identifier corresponding to register. See register['id'] field.
    data
    Float or int or bytes, depending on the type of register

    Raises
    ------
    none

    Returns
    -------
    b_message: bytearray
    Stuffed and escaped bytestring, following protocol above.

    Notes
    -----
    Given a FRAME_BOUNDARY of 0x7e and ESCAPE_BYTE of 0x7d, every appearance of the bytes 0x7e or 0x7d in the data
    (so in a role other than the delimiter or the escape byte)  is replaced by the sequence 0x7d 0x5e or 0x7d 0x5d,
    respectively. This follows ESCAPE_MASK = 1 << 5, or 0x20, thus the byte is XOR'd with the mask and prepended by 0x7d

## ◆ encode_vector()

optoKummenberg.tools.parsing_tools.encode_vector | ( |  | register,
|  |  | index,
|  |  | vector )

    Encodes the given register vector to bytes.

    Parameters
    ----------
    register : dict or int
        Register dictionary corresponding to desired registers.{'id': registers number, 'type': data type, ...}
        Further, a raw registers id can be passed, in which case no validation will occur (COULD CAUSE ERRORS).
    index : int
        The location to store the given vector.
    value : varies, can also be list of values
        The type and range of value is dependent on the registers being addressed, see 'type' element above.
    cmd_delay : float
        How long to wait for response in secs. By default is set to registers.CMD_DELAY

    Returns
    -------
    str
        byte string to transmit.

## ◆ parse_error()

optoKummenberg.tools.parsing_tools.parse_error | ( | int | error_code | ) |

        Given error number, returns error string.

        Parameters
        ----------
        error_code : int
            Error code hex number, will convert if string.

        Examples
        --------
        Return error information from _board

        Returns
        -------
        code: str
            Error code string.

        >>> parse_error(0x0001)
        'COMMAND_LENGTH_MISMATCH'
        >>> parse_error('1')
        'COMMAND_LENGTH_MISMATCH'