openbci_interface package

openbci_interface.exception module

Defines exceptions commonly used in open_bci module.

class openbci_interface.exception.BCIException[source]

Bases: object

Base exception for OpenBCI Interface specific exception.

exception openbci_interface.exception.UnexpectedMessageFormat(message)[source]

Bases: openbci_interface.exception.BCIException, ValueError

Board returned message not in OpenBCI format

exception openbci_interface.exception.NotSupported(board_type)[source]

Bases: openbci_interface.exception.BCIException, NotImplementedError

Unsupported board type was requested

exception openbci_interface.exception.DeviceNotConnected[source]

Bases: openbci_interface.exception.BCIException, RuntimeError

Serial is working but board is not connected

exception openbci_interface.exception.SampleAcquisitionTimeout[source]

Bases: openbci_interface.exception.BCIException, RuntimeError

Sample acuisition timed out.

openbci_interface.core module

Implement Serial IO as defined in documentation

class openbci_interface.core.Common(serial)[source]

Bases: object

Stateless interface common to Cyton and Ganglion

Parameters:serial (Serial) –
read_message()[source]

Read until $$$ is found or timeout occurs.

Returns:Message received from the board. If timeout occurs, the returned string might not end with $$$.
Return type:bytes
reset_board()[source]

Reset the board state.

References

http://docs.openbci.com/Hardware/03-Cyton_Data_Format#cyton-data-format-startup http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-command-set-miscellaneous http://docs.openbci.com/OpenBCI%20Software/06-OpenBCI_Ganglion_SDK#openbci-ganglion-sdk-command-set-miscellaneous

query_sample_rate()[source]

Query the current sample rate. Message must be read separately.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-firmware-v300-new-commands-sample-rate http://docs.openbci.com/OpenBCI%20Software/06-OpenBCI_Ganglion_SDK#openbci-ganglion-sdk-firmware-v2xx-new-commands-sample-rate

set_sample_rate(sample_rate)[source]

Set the sample rate.

Note

The Cyton with USB Dongle cannot and will not stream data over 250SPS. Plug in the WiFi Shield to get speeds over 250SPS streaming. You may still write to an SD card though, the firmware will not send EEG data over the Bluetooth radios.

Parameters:sample_rate (bytes) – One of b'7', b'6', b'5', b'4', b'3', b'2', b'1', or b'0'.

Note

Only Ganglion supports b'7' (200 Hz).

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-firmware-v300-new-commands-sample-rate http://docs.openbci.com/OpenBCI%20Software/06-OpenBCI_Ganglion_SDK#openbci-ganglion-sdk-firmware-v2xx-new-commands-sample-rate

attach_wifi()[source]

Attach WiFi shield.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-firmware-v300-new-commands-wifi-shield-commands http://docs.openbci.com/OpenBCI%20Software/06-OpenBCI_Ganglion_SDK#openbci-ganglion-sdk-firmware-v2xx-new-commands-wifi-shield-commands

detach_wifi()[source]

Detach WiFi shield.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-firmware-v300-new-commands-wifi-shield-commands http://docs.openbci.com/OpenBCI%20Software/06-OpenBCI_Ganglion_SDK#openbci-ganglion-sdk-firmware-v2xx-new-commands-wifi-shield-commands

query_wifi_status()[source]

Query the status of WiFi shield. Message must be read separately.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-firmware-v300-new-commands-wifi-shield-commands http://docs.openbci.com/OpenBCI%20Software/06-OpenBCI_Ganglion_SDK#openbci-ganglion-sdk-firmware-v2xx-new-commands-wifi-shield-commands

reset_wifi()[source]

Perform a soft (power) reset of the WiFi shield.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-firmware-v300-new-commands-wifi-shield-commands http://docs.openbci.com/OpenBCI%20Software/06-OpenBCI_Ganglion_SDK#openbci-ganglion-sdk-firmware-v2xx-new-commands-wifi-shield-commands

enable_channel(channel)[source]

Turn on channel for sample acquisition

Parameters:channel (bytes) – One of !@#$%^&*QWERTYUI.

Note

Ganglion supports (the first) 4 channels, Cyton supports 8 channels, Cyton with Daisy module supports 16 channels.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-16-channel-commands-turn-channels-on http://docs.openbci.com/OpenBCI%20Software/06-OpenBCI_Ganglion_SDK#openbci-ganglion-sdk-command-set-turn-channels-on

disable_channel(channel)[source]

Turn off channel for sample acquisition

Parameters:channel (bytes) – One of 12345678qwertyui

Note

Ganglion supports (the first) 4 channels, Cyton supports 8 channels, Cyton with Daisy module supports 16 channels.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-16-channel-commands-turn-channels-off http://docs.openbci.com/OpenBCI%20Software/06-OpenBCI_Ganglion_SDK#openbci-ganglion-sdk-command-set-turn-channels-off

start_streaming()[source]

Start streaming data.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-command-set-stream-data-commands http://docs.openbci.com/OpenBCI%20Software/06-OpenBCI_Ganglion_SDK#openbci-ganglion-sdk-command-set-stream-data-commands

stop_streaming()[source]

Stop streaming data.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-command-set-stream-data-commands http://docs.openbci.com/OpenBCI%20Software/06-OpenBCI_Ganglion_SDK#openbci-ganglion-sdk-command-set-stream-data-commands

class openbci_interface.core.CytonBoard(serial)[source]

Bases: openbci_interface.core.Common

Stateless interface to Cyton

START_BYTE = 160
query_firmware_version()[source]

Query firmware version. Message must be read separately.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-firmware-v300-new-commands-get-version

query_board_mode()[source]

Query the current board mode. Message must be read separately.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-firmware-v300-new-commands-board-mode

set_board_mode(mode)[source]

Set board mode.

Parameters:mode (bytes) – b'0', b'1', b'2', b'3' or b'4'

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-firmware-v300-new-commands-board-mode

attach_daisy()[source]

Set the number of maximum chnnels to 16.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-16-channel-commands-select-maximum-channel-number

detach_daisy()[source]

Set the number of maximum channels to 8

Note

On reset, the OpenBCI Cyton board will sniff for the Daisy Module, and if it is present, it will default to 16 channel capability.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-16-channel-commands-select-maximum-channel-number

configure_channel(command)[source]

Configure channel.

Parameters:command (bytes) – 9-length byte string in the following format. x (CHANNEL, POWER_DOWN, GAIN_SET, INPUT_TYPE_SET, BIAS_SET, SRB2_SET, SRB1_SET) X. See the reference for the detail.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-command-set-channel-setting-commands

enable_timestamp()[source]

Enable timestamp

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-firmware-v200-new-commands-time-stamping

disable_timestamp()[source]

Disable timestamp

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-firmware-v200-new-commands-time-stamping

reset_channels()[source]

Set all channels to default configuration.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-command-set-default-channel-settings

query_default_settings()[source]

Query channel default configs. Message must be read separately.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-command-set-default-channel-settings http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-command-set-channel-setting-commands

wait_start_byte()[source]

Keep reading data until start byte is found.

References

http://docs.openbci.com/Hardware/03-Cyton_Data_Format#cyton-data-format-binary-format

read_packet()[source]

Read 32 byte packet.

References

http://docs.openbci.com/Hardware/03-Cyton_Data_Format#cyton-data-format-binary-format

openbci_interface.cyton module

Define interface to Cyton board

class openbci_interface.cyton.Cyton(serial, close_on_terminate=True)[source]

Bases: object

Interface to Cyton board.

Parameters:
  • serial (serial.Serial, str or dict) – Serial object used to communicate with board. If str object is passed, it is interpreted as port and a new Serial object is constructed. If dict object is passed, it is passed to constructor of serial.Serial class as keyword arguments.
  • close_on_terminate (bool) – If True, underlying serial connection is closed when terminate() is called.
Variables:
  • num_aux (int) – The number of AUX channels. (3)
  • board_info (str) – The message returned by Cyton board when resetting the board. This variable is set when reset_board() method is called.
  • firmware_version (str) – Firmware version string. This variable is set when get_firmware_version() method is called.
  • board_mode (str) – Board mode string. This variable is set when either get_board_mode() or set_board_mode() is called.
  • sample_rate (int) – Sampling rate. This variable is set when either get_sample_rate() or set_sample_rate() is called.
  • streaming (bool) – True if streaming
  • wifi_attached (bool) – True if WiFi is attached via attach_wifi() method.
  • channel_configs (list) – List of ChannelConfig. For Daisy compatibility, this list always has 16 items. Use num_eeg() to get the number of valid channels.
  • daisy_attached (bool) – Set to True if Daisy module is detected in reset_board(), or when the module is re-attached with attach_daisy(). False when no Daisy module is detected of one is detached with detach_daisy().

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK http://docs.openbci.com/Hardware/03-Cyton_Data_Format

__enter__()[source]

Context manager for open/close serial connection automatically.

By utilizing context manager with with statement, board is initialized automatically, then at exit streaming is stopped and serial connection is closed (based on close_on_terminate value) automatically.

with Cyton(port) as board:
    # no need to call board.initialize()
    board.start_streaming()
    board.read_sample()
    # no need to call board.stop_streaming()

However when passing an already-opened Serial instance and setting close_on_terminate to False, context manager does not close the serial.

# Passing an instance with open connection
ser = serial.Serial(port=port, baudrate=baudrate, timeout=timeout)
with Cyton(ser, close_on_terminate=False) as board:
    pass
assert ser.is_open  # Connection is still open.
# Passing an instance with open connection
ser = serial.Serial(port=port, baudrate=baudrate, timeout=timeout)
with Cyton(ser, close_on_terminate=True) as board:
    pass
assert not ser.is_open  # Connection is closed.
__exit__(exc_type, exc_val, exc_tb)[source]

Context manager for open/close serial connection automatically.

See __enter__

num_aux = 3
cycle

Time (in sec) to take one sample acquisition over all channels

num_eeg

The number of EEG channels. 16 if Daisy is attached, otherwise 8

read_message()[source]

Read until $$$ is found or timeout occurs.

Returns:

Message received from the board.

Return type:

str

Raises:
reset_board()[source]

Reset the board state.

References

http://docs.openbci.com/Hardware/03-Cyton_Data_Format#cyton-data-format-startup

get_firmware_version()[source]

Get firmware version

Returns:Version string
Return type:str

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-firmware-v300-new-commands-get-version

get_board_mode()[source]

Get the current board mode.

Returns:One of default, debug, analog, digital, marker if the mode string is parsed successfully. Otherwise None.
Return type:str or None

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-firmware-v300-new-commands-board-mode

set_board_mode(mode)[source]

Set board mode.

Parameters:mode (str) – default, debug, analog, digital or marker

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-firmware-v300-new-commands-board-mode

attach_daisy()[source]

Attach Daisy.

After successful attach, daisy_attached is set to True.

Note

On reset, the OpenBCI Cyton board will default to 16 channel if Daisy module is present. So this method is only useful for re-attaching Daisy.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-16-channel-commands-select-maximum-channel-number

detach_daisy()[source]

Detach Daisy.

After this method is called, daisy_attached is set to False.

Note

On reset, the OpenBCI Cyton board will sniff for the Daisy Module, and if it is present, it will default to 16 channel capability.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-16-channel-commands-select-maximum-channel-number

get_sample_rate()[source]

Get the current sample rate.

Returns:Sample rate on successful parse, else None.
Return type:int or None

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-firmware-v300-new-commands-sample-rate

set_sample_rate(sample_rate)[source]

Set the sample rate.

Note

The Cyton with USB Dongle cannot and will not stream data over 250SPS. Plug in the WiFi Shield to get speeds over 250SPS streaming. You may still write to an SD card though, the firmware will not send EEG data over the Bluetooth radios.

Parameters:sample_rate (int) – One of 250, 500, 1000, 2000, 4000, 8000 or 1600.
Returns:Sample rate on successful parse, else None.
Return type:int or None
Raises:ValueError – When the provided sample_rate is invalid.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-firmware-v300-new-commands-sample-rate

attach_wifi()[source]

Attach WiFi shield.

After successful attachment, wifi_attached is set to True.

Raises:RuntimeError – When failed to attach WiFi shield.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-firmware-v300-new-commands-wifi-shield-commands

detach_wifi()[source]

Detach WiFi shield.

After successful detachment, wifi_attached is set to False.

Returns:
Return type:None
Raises:RuntimeError – When failed to detach Wifi shield.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-firmware-v300-new-commands-wifi-shield-commands

get_wifi_status()[source]

Get the status of WiFi shield.

wifi_attached is updated based on the result.

Returns:Message received from the board.
Return type:str

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-firmware-v300-new-commands-wifi-shield-commands

reset_wifi()[source]

Perform a soft (power) reset of the WiFi shield.

Returns:Message received from the board.
Return type:str

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-firmware-v300-new-commands-wifi-shield-commands

enable_channel(channel)[source]

Turn on channel for sample acquisition

Parameters:channel (int) – value must be between 1 - 16, inclusive.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-16-channel-commands-turn-channels-on

disable_channel(channel)[source]

Turn off channel for sample acquisition

Parameters:channel (int) – value must be between 1 - 8, inclusive.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-16-channel-commands-turn-channels-off

configure_channel(channel, power_down='ON', gain=24, input_type='NORMAL', bias=1, srb2=1, srb1=0)[source]

Configure channel.

Parameters:
  • channel (int) – Channel to configure. [1, 8] for Cyton, [1, 16] for Daisy.
  • power_down (str or int) – POWER_DOWN value. ON or OFF if string. 0 (==ON) or 1 (==OFF) if integer.
  • gain (int) – GAIN_SET value. One of 1, 2, 4, 6, 8, 12, 24.
  • input_type (str or int) – INPUT_TYPE_SET value. One of NORMAL (corresponding integer value: 0), SHORTED (1), BIAS_MEAS (2), MVDD (3), TEMP (4), TESTSIG (5), BIAS_DRP (6), or BIAS_DRN (7).
  • bias (int) – BIAS_SET value. 0 for remove, 1 for include.
  • srb2 (int) – SRB2_SET value. 0 for disconnect, 1 for connect.
  • srb1 (int) – SRB1_SET value. 0 for disconnect, 1 for connect.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-command-set-channel-setting-commands

start_streaming()[source]

Start streaming data.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-command-set-stream-data-commands

stop_streaming()[source]

Stop streaming data.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-command-set-stream-data-commands

enable_timestamp()[source]

Enable timestamp

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-firmware-v200-new-commands-time-stamping

Note

Timestamp parsing is not supported yet.

Todo

Implement timestamp parsing.

disable_timestamp()[source]

Disable timestamp

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-firmware-v200-new-commands-time-stamping

reset_channels()[source]

Set all channels to default configuration.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-command-set-default-channel-settings

get_default_settings()[source]

Get channel default configuration string.

Returns:Parameters compatible with configure_channel() method.
Return type:dict

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-command-set-default-channel-settings http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-command-set-channel-setting-commands

read_sample()[source]

Read one sample from channels.

Returns:
{
  "eeg": [<channel1>, ..., <channelN>],
  "aux": [<channel1>, ..., <channel3>],
  "raw_eeg": [<channel1>, ..., <channelN>],
  "raw_aux": [<channel1>, ..., <channel3>],
  "packet_id": int,
  "timestamp": float,
  "valid": bool
}
Return type:dict

valid is True when received stop byte matches 0xC0. When valid is False, the sample acquisition was out of sync, and values are not reliable.

Note

This method will discard the message received from board before receiving start byte.

Note

The output format is subject to change.

Note

For AUX data, only 0xC0 stop byte is supported now.

Raises:openbci_interface.exception.SampleAcquisitionTimeout – If time out occurs while waiting for a start byte.

References

http://docs.openbci.com/Hardware/03-Cyton_Data_Format#cyton-data-format-binary-format

initialize(board_mode='default', sample_rate=250, channel_configs=None)[source]

Initialize board, channel, sample rate to default.

terminate()[source]

Stop streaming if necessary then close connection

get_config()[source]

Get the current board config to JSON

Returns:
board_mode (str or None):
Current board mode.
sample_rate (int or None):
Current sample rate.
channels (list of dict):
Configuration of each channel with the following keys.
enabled (bool):
Wheather channels is enabled.
parameters (dict):
Channel parameters.
power_down (str)

gain (int)

input_type (str)

bias (int)

srb2 (str)

srb1 (str)

Return type:dict
set_channel_configs(channel_configs)[source]

Configure channels

Parameters:channel_configs (list of dict) –
enabled (bool):
Wheather channels is enabled.
parameters (dict):
Channel parameters.
power_down (str)

gain (int)

input_type (str)

bias (int)

srb2 (str)

srb1 (str)

openbci_interface.channel_config module

Helper module for generating channel config command and caching values.

openbci_interface.channel_config.get_channel_config_command(channel, power_down, gain, input_type, bias, srb2, srb1)[source]

Get command string for the given parameters.

See Cyton.configure_channel

class openbci_interface.channel_config.ChannelConfig(channel, enabled=None, power_down=None, gain=None, input_type=None, bias=None, srb2=None, srb1=None)[source]

Bases: object

Class for holding channel configuration, set by Cyton.

You should not use this class directly. Instead, you can use instances of this class managed by Cyton object, in read-only manner.

Examples

>>> cyton = openbci_interface.cyton.Cyton(port)
>>> cyton.initialize()
>>> print(cyton.channel_config[0].power_down)
'ON'
>>> cyton.configure_channel(1, power_down='OFF', ...)
>>> print(cyton.channel_config[0].power_down)
'OFF'
Variables:
  • enabled (bool) – If corresponding channel is enabled True, if disabled, False. None if not known. (initial value)
  • power_down (str) – POWER_DOWN value. ON or OFF
  • gain (int) – GAIN_SET value. One of 1, 2, 4, 6, 8, 12, 24.
  • input_type (str) – INPUT_TYPE_SET value. One of NORMAL, SHORTED, BIAS_MEAS, MVDD, TEMP, TESTSIG, BIAS_DRP, or BIAS_DRN.
  • bias (str) – BIAS_SET value. 0 or 1.
  • srb2 (str) – SRB2_SET value. 0 or 1.
  • srb1 (str) – SRB1_SET value. 0 or 1.

References

http://docs.openbci.com/OpenBCI%20Software/04-OpenBCI_Cyton_SDK#openbci-cyton-sdk-command-set-channel-setting-commands

set_config(power_down, gain, input_type, bias, srb2, srb1)[source]

Used by Cyton board to set values.

See get_channel_config_command() for parameters.

openbci_interface.util module

Module to implement utility functions.

openbci_interface.util.list_devices(filter_regex='OpenBCI', timeout=2)[source]

List OpenBCI devices by querying COM ports.

Parameters:
  • filter_regex (str) – Regular expression applied to firmware information string, using re.search function. To get only Cyton boards, you can use ADS1299. To get only Ganglion, you can use Gangion.
  • timeout (float) – Read timeout
Yields:

str – Name of the device found.

openbci_interface.util.validate_message(message)[source]

Validate message received from serial.

Raises: