Skip to main content

Pure Python interface to Blackrock Neurotech Cerebus devices

Project description

PyPI version

pycbsdk

Pure Python package for communicating with Blackrock Cerebus devices

Quick Start

From a shell...

pip install pycbsdk

Then in python

from pycbsdk import cbsdk


params_obj = cbsdk.create_params()
nsp_obj = cbsdk.get_device(params_obj)  # NSPDevice instance. This will be the first argument to most API calls. 
err = cbsdk.connect(nsp_obj)  # Bind sockets, change device run state, and get device config.
config = cbsdk.get_config(nsp_obj)
print(config)

You may also try the provided test script with python -m pycbsdk.examples.print_rates or via the shortcut: pycbsdk_print_rates.

Introduction

pycbsdk is a pure Python package for communicating with a Blackrock Neurotech Cerebus device. It is loosely based on Blackrock's cbsdk, but shares no code nor is pycbsdk supported by Blackrock.

pycbsdk's API design is intended to mimic that of a C-library. Indeed, a primary goal of this library is to help prototype libraries in other languages. After all, Python is a poor choice to handle high throughput data without some compiled language underneath doing all the heavy lifting.

However, it's pretty useful as is! And so far it has been good-enough for some quick test scripts, and it even drops fewer packets than CereLink. So, please use it, and contribute! We are more than happy to see the API expand to support more features, or even to have an additional "pythonic" API.

Design

Upon initialization, the NSPDevice instance configures its sockets (but no connection yet), it allocates memory for its mirror of the device state, and it registers callbacks to monitor config state.

When the connection to the device is established, two threads are created and started:

  • CerebusDatagramThread
    • Retrieves datagrams using asyncio
    • Slices into generic packets
    • Casts packets into their native type
    • Enqueues packets for the PacketHandlerThread
  • PacketHandlerThread
    • Updates device state (e.g., mirrors device time)
    • Calls registered callbacks depending on the packet type.

connect() has startup_sequence=True by default. This will cause the SDK to attempt to put the device into a running state. Otherwise, it'll stay in its original run state.

After the connection is established, the client can use API functions to:

  • Get / Set config
    • set_config and set_channel_config do not do anything yet
    • set_channel_spk_config and set_channel_config_by_packet do things and are blocking.
    • get_config is non-blocking by default and will simply read the local mirror of the config. However, if force_refresh=True is passed as a kwarg, then this function will block and wait for a reply from the device. Use this sparingly.
  • Register a callback to receive data as soon as it appears on the handler thread.

This and more should appear in the documentation at some point in the future...

Limitations

  • This library takes exclusive control over the UDP socket on port 51002 and thus cannot be used with Central, nor any other instance of pycbsdk. You only get one instance of pycbsdk or Central per machine.
    • CereLink's cerebus.cbpy uses shared memory and therefore can work in parallel to Central or other cbpy instances.
  • The API is still very sparse and limited in functionality.
  • For now, Python still has the GIL. This means that despite using threading, if your callback functions are slow and hold up the PacketHandlerThread, this could hold up datagram retrieval and ultimately cause packets to be dropped.
    • Callbacks may enqueue the data for a longer-running multiprocessing process to handle.
    • Switch to No GIL Python as soon as it is available.
    • Use pycbsdk to prototype an application in a language that uses real parallelism.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycbsdk-0.1.3.tar.gz (38.8 kB view details)

Uploaded Source

Built Distribution

pycbsdk-0.1.3-py3-none-any.whl (48.8 kB view details)

Uploaded Python 3

File details

Details for the file pycbsdk-0.1.3.tar.gz.

File metadata

  • Download URL: pycbsdk-0.1.3.tar.gz
  • Upload date:
  • Size: 38.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for pycbsdk-0.1.3.tar.gz
Algorithm Hash digest
SHA256 c9d80a826c2a0bf346da0f1afcc4f99f4103f8739ccca517c53ffb06a49ed39e
MD5 7cc822ac1f15ac31b0a111ab62d86eb4
BLAKE2b-256 4e50092914e46587c78e57401f21e6805042168012661a2c29e81cd435fe1256

See more details on using hashes here.

File details

Details for the file pycbsdk-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: pycbsdk-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 48.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for pycbsdk-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4463d6dde19c5ad499f5519ce4885b856d25406e6c22b511b46d70a0f569612e
MD5 e86c1439b90970ee8c6ca77683a028f4
BLAKE2b-256 c0500f52fb5b390a3158bea491f500257c11a79c3b162bd4be007853b72b319f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page