Morelia.packet package

Subpackages

Submodules

Morelia.packet.channel_mode module

This file contains types used for indicating the types of various channels read from aquisition devices. Currently, the only device Morelia supports that takes advantage of modal channels is the 8401HR.

class Morelia.packet.channel_mode.PrimaryChannelMode(*values)

Bases: Enum

Mode for a β€œprimary” channel (i.e. not TTL/AEXT)

BIOSENSOR = 2
EEG_EMG = 1
class Morelia.packet.channel_mode.SecondaryChannelMode(*values)

Bases: Enum

Mode for AEXT/TTL channels. Analog channels report a numeric value, as digital values can be high or low (i.e. bypasses the onboard ADC).

ANALOG = 1
DIGITAL = 2

Morelia.packet.control_packet module

class Morelia.packet.control_packet.ControlPacket(decode_from, raw_packet)

Bases: PodPacket

Class for parsing β€œStandard” POD packets from raw bytes. These are associated with controlling or configuring a device, hence the name These packets take the following form

_images/control_packet.png

The payload is normally several values that need to parsed into a tuple of integers. Oftentimes, this class is used a β€œfactory” of sorts. For any given device, you might partially apply the constructor with the decode_from argument, and then any time you need to make a packet, just call the curried constructor with the raw bytes of the packet you want to parse. For a concrete example of this, see the constructor of the Pod8206HR class.

Parameters:
  • decode_from (Union[CommandSet, Callable[[int, bytes], tuple]]) – Instructs the packet how to parse the payload from raw bytes to a Python object. If a CommandSet object is passed, the payload is decoded according to that. Otherwise, a function must be passed that takes a command number and bytes object to decode, and returns a tuple. For a simple example of how passing a decode_from function directly may be used, see the constructor of the Pod8206HR class.

  • raw_packet (bytes) – The raw bytes to be parsed as a control packet.

static decode_payload_from_cmd_set(cmds, cmd_number, payload)

Used for parsing packet payloads according to a CommandSet object. Do not use this function for payload access (it lacks the memoization that makes the payload property speedy), but for building bespoke decoding functions to pass to this class as decode_from. Generally, this function is used to create decoding functions that default to using a command set, but have the capability to handle edge-cases on a per-command basis. For an example of how this is used, see the constructor of the Pod8206HR class.

Parameters:
  • cmds (CommandSet) – The CommandSet to decode from.

  • cmd_number (int) – Command number of the packet that the payload corresponds to.

  • payload (bytes) – Raw bytes of the payload you wish to decode.

Return type:

tuple

Returns:

The parsed payload as a tuple.

property payload: tuple

Parses the packet payload in a lazy, memoized fashion.

Returns:

The parsed payload as a tuple.

Morelia.packet.conversion module

This file implements the following commutative diagram:

_images/conv_comm_diagram.png

In layperson’s terms, it is full of utility functions for converting between binary-coded decimal, ASCII, and decimal integers.

class Morelia.packet.conversion.Endianness(*values)

Bases: Enum

This enumeration is used to specify the endianness of a series of bytes passed into a function.

BIG = 1
LITTLE = 2
Morelia.packet.conversion.ascii_bytes_to_binary_bytes(msg_b, num_bytes, byteorder=Endianness.BIG, signed=False)

Converts a ASCII-encoded bytestring to an binary-coded decimal one.

Parameters:
  • msg_b (bytes) – ASCII-encoded bytestring to convert.

  • num_bytes (bytes) – Number of bytes that will be in returned bytes object. Must be at least enough to hold expected output.

  • byteorder (Endianness) – Endianness of bytes.

  • signed (bool) – True if msg_b is signed, false if unsigned. Defaults to False.

Return type:

bytes

Returns:

msg_b as binary-coded decimal bytes.

Morelia.packet.conversion.ascii_bytes_to_int(msg_b, signed=False)

msg_b contain a series of ascii-encoded hexadecimal digits that encode an integer. If signed=True, then this integer is interpreted as a negative two’s complement number, and the negative number that the bits of the integer encoded in the hexadecimal digits represent is returned. If signed=True but the integer encoded in msg_b is not negative, then the β€œunderlying” two’s complement number is ignored and the integer encoded in the hex is returned.

Parameters:
  • msg_b (bytes) – Bytes message to be converted to an integer. The bytes must be ascii-encoded or the conversion will fail.

  • signed (bool) – True if the message is signed, false if unsigned. Defaults to False.

Return type:

int

Returns:

Integer result from the ASCII-encoded byte conversion.

Morelia.packet.conversion.ascii_bytes_to_int_split(msg, msb_index, lsb_index)

Converts a specific bit range in an ASCII-encoded bytes object to an integer.

Parameters:
  • msg (bytes) – Bytes message holding binary information to be converted into an integer.

  • msb_index (int) – Integer position of the msb of desired bit range.

  • lsb_index (int) – Integer number of lsb to remove.

Return type:

int

Returns:

Integer result from the ASCII-encoded bytes message in a given bit range.

Morelia.packet.conversion.binary_bytes_to_ascii_bytes(msg, num_chars, byteorder=Endianness.BIG, signed=False)

Converts a binary-coded decimal bytestring to an ASCII-encoded one.

Parameters:
  • msg (bytes) – Binary-coded decimal bytestring to convert.

  • num_chars (int) – Number characters to be the length of the ASCII-encoded message.

  • byteorder (Endianness) – Endianness of bytes.

  • signed (bool) – True if msg is signed, false if unsigned. Defaults to False.

Return type:

bytes

Returns:

msg as ASCII-encoded bytes.

Morelia.packet.conversion.binary_bytes_to_int(msg, byteorder=Endianness.BIG, signed=False)

Converts binary-encoded bytes into an integer.

Parameters:
  • msg (bytes) – Bytes message holding binary information to be converted into an integer.

  • byteorder (Endianness) – Endianness of bytes.

  • signed (bool) – True if the message is signed, false if unsigned. Defaults to False.

Return type:

int

Returns:

Integer result from the binary-encoded bytes message.

Morelia.packet.conversion.binary_bytes_to_int_split(msg, msb_index, lsb_index, byteorder=Endianness.BIG, signed=False)

Converts a specific bit range in a binary-encoded bytes object to an integer.

Parameters:
  • msg (bytes) – Bytes message holding binary information to be converted into an integer.

  • msb_index (int) – Integer position of the msb of desired bit range.

  • lsb_index (int) – Integer number of lsb to remove.

  • byteorder (Endianness) – Endianness of bytes.

  • signed (bool) – True if the message is signed, false if unsigned. Defaults to False.

Return type:

int

Returns:

Integer result from the binary-encoded bytes message in a given bit range.

Morelia.packet.conversion.int_to_ascii_bytes(value, num_chars)

Converts an integer value into ASCII-encoded bytes.

First, it converts the integer value into a usable uppercase hexadecimal string. Then it converts the ASCII code for each character into bytes. Lastly, it ensures that the final message is the desired length.

Example: if value=2 and numBytes=4, the returned ASCII will show b’0002’, which is β€˜0x30 0x30 0x30 0x32’ in bytes. Uses the 2’s complement if the val is negative.

Parameters:
  • value (int) – Integer value to be converted into ASCII-encoded bytes.

  • num_chars (int) – Number characters to be the length of the ASCII-encoded message.

Return type:

bytes

Returns:

Bytes that are ASCII-encoded conversions of the value parameter.

Morelia.packet.conversion.int_to_binary_bytes(msg, num_bytes, byteorder=Endianness.BIG)

Converts an integer to a bytes object.

Parameters:
  • msg (int) – Integer to be converted

  • num_bytes (int) – Number of bytes that will be in returned bytes object. Must be at least enough to hold msg.

  • byteorder (Endianness) – Desired endianness of returned bytes.

Return type:

bytes

Returns:

Bytes object representing msg in binary-coded decimal.

Morelia.packet.conversion.neg_int_to_twos_complement(val, nbits)

Gets the two’s complement of the argument value (negative int).

Parameters:
  • val (int) – Negative integer to get the two’s complament representation of.

  • nbits (int) – Number of bits in val. This is necessary as the built-in int type does not track this

Return type:

int

Returns:

The two’s complament representation of val. This may seem wrong, but if you look at the returned number in binary, it should match val but as two’s complament.

Morelia.packet.conversion.twos_complement_to_neg_int(val, nbits)

Interpret the binary representation of val as a two’s complament number and return the value as a Python integer.

Parameters:
  • val (int) – Integer to interpret as two’s complament for conversion.

  • nbits (int) – Number of bits in val. This is necessary as the built-in int type does not track this

Return type:

int

Returns:

The value of val interpreted as a two’s complament number as a negative Python integer.

Morelia.packet.pod_packet module

class Morelia.packet.pod_packet.PodPacket(raw_packet, min_length=6)

Bases: object

The parent class of all POD protocol packets. Contains basic attributes common to all.

ETX: bytes = b'\x03'
STX: bytes = b'\x02'
property command_number: int
Returns:

Packet command number. Decoded lazily and memoized for speed.

property raw_packet: bytes
Returns:

Raw bytes of packet.

Module contents