openbci_interface package¶
openbci_interface.exception module¶
Defines exceptions commonly used in open_bci module.
-
class
openbci_interface.exception.BCIException[source]¶ Bases:
objectBase exception for OpenBCI Interface specific exception.
-
exception
openbci_interface.exception.UnexpectedMessageFormat(message)[source]¶ Bases:
openbci_interface.exception.BCIException,ValueErrorBoard returned message not in OpenBCI format
-
exception
openbci_interface.exception.NotSupported(board_type)[source]¶ Bases:
openbci_interface.exception.BCIException,NotImplementedErrorUnsupported board type was requested
-
exception
openbci_interface.exception.DeviceNotConnected[source]¶ Bases:
openbci_interface.exception.BCIException,RuntimeErrorSerial is working but board is not connected
-
exception
openbci_interface.exception.SampleAcquisitionTimeout[source]¶ Bases:
openbci_interface.exception.BCIException,RuntimeErrorSample acuisition timed out.
openbci_interface.core module¶
Implement Serial IO as defined in documentation
-
class
openbci_interface.core.Common(serial)[source]¶ Bases:
objectStateless 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', orb'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 12345678qwertyuiNote
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
-
-
class
openbci_interface.core.CytonBoard(serial)[source]¶ Bases:
openbci_interface.core.CommonStateless interface to Cyton
-
START_BYTE= 160¶
-
query_firmware_version()[source]¶ Query firmware version. Message must be read separately.
References
-
query_board_mode()[source]¶ Query the current board mode. Message must be read separately.
References
-
set_board_mode(mode)[source]¶ Set board mode.
Parameters: mode (bytes) – b'0',b'1',b'2',b'3'orb'4'References
-
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
-
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
-
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:
objectInterface 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()orset_board_mode()is called. - sample_rate (int) – Sampling rate.
This variable is set when either
get_sample_rate()orset_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. Usenum_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 withattach_daisy(). False when no Daisy module is detected of one is detached withdetach_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
withstatement, board is initialized automatically, then at exit streaming is stopped and serial connection is closed (based onclose_on_terminatevalue) 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_terminateto 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: UnexpectedMessageFormat– The message received from the board does not end with$$$(which is likely due to timeout).DeviceNotConnected– Serial connection is working, but no board is avaialable.
-
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
-
get_board_mode()[source]¶ Get the current board mode.
Returns: One of default,debug,analog,digital,markerif the mode string is parsed successfully. Otherwise None.Return type: str or None References
-
set_board_mode(mode)[source]¶ Set board mode.
Parameters: mode (str) – default,debug,analog,digitalormarkerReferences
-
attach_daisy()[source]¶ Attach Daisy.
After successful attach,
daisy_attachedis 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
-
detach_daisy()[source]¶ Detach Daisy.
After this method is called,
daisy_attachedis 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
-
get_sample_rate()[source]¶ Get the current sample rate.
Returns: Sample rate on successful parse, else None. Return type: int or None References
-
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,8000or1600.Returns: Sample rate on successful parse, else None. Return type: int or None Raises: ValueError– When the providedsample_rateis invalid.References
-
attach_wifi()[source]¶ Attach WiFi shield.
After successful attachment,
wifi_attachedis set to True.Raises: RuntimeError– When failed to attach WiFi shield.References
-
detach_wifi()[source]¶ Detach WiFi shield.
After successful detachment,
wifi_attachedis set to False.Returns: Return type: None Raises: RuntimeError– When failed to detach Wifi shield.References
-
get_wifi_status()[source]¶ Get the status of WiFi shield.
wifi_attachedis updated based on the result.Returns: Message received from the board. Return type: str References
-
reset_wifi()[source]¶ Perform a soft (power) reset of the WiFi shield.
Returns: Message received from the board. Return type: str References
-
enable_channel(channel)[source]¶ Turn on channel for sample acquisition
Parameters: channel (int) – value must be between 1 - 16, inclusive. References
-
disable_channel(channel)[source]¶ Turn off channel for sample acquisition
Parameters: channel (int) – value must be between 1 - 8, inclusive. References
-
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_DOWNvalue.ONorOFFif string. 0 (==ON) or 1 (==OFF) if integer. - gain (int) –
GAIN_SETvalue. One of 1, 2, 4, 6, 8, 12, 24. - input_type (str or int) –
INPUT_TYPE_SETvalue. One ofNORMAL(corresponding integer value: 0),SHORTED(1),BIAS_MEAS(2),MVDD(3),TEMP(4),TESTSIG(5),BIAS_DRP(6), orBIAS_DRN(7). - bias (int) –
BIAS_SETvalue. 0 for remove, 1 for include. - srb2 (int) –
SRB2_SETvalue. 0 for disconnect, 1 for connect. - srb1 (int) –
SRB1_SETvalue. 0 for disconnect, 1 for connect.
References
-
enable_timestamp()[source]¶ Enable timestamp
References
Note
Timestamp parsing is not supported yet.
Todo
Implement timestamp parsing.
-
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 validis True when received stop byte matches 0xC0. Whenvalidis 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
0xC0stop 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.
-
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
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.
-
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:
objectClass 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_DOWNvalue.ONorOFF - gain (int) –
GAIN_SETvalue. One of 1, 2, 4, 6, 8, 12, 24. - input_type (str) –
INPUT_TYPE_SETvalue. One ofNORMAL,SHORTED,BIAS_MEAS,MVDD,TEMP,TESTSIG,BIAS_DRP, orBIAS_DRN. - bias (str) –
BIAS_SETvalue. 0 or 1. - srb2 (str) –
SRB2_SETvalue. 0 or 1. - srb1 (str) –
SRB1_SETvalue. 0 or 1.
References
-
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.searchfunction. To get only Cyton boards, you can useADS1299. To get only Ganglion, you can useGangion. - timeout (float) – Read timeout
Yields: str – Name of the device found.
- filter_regex (str) – Regular expression applied to firmware information string,
using
-
openbci_interface.util.validate_message(message)[source]¶ Validate message received from serial.
Raises: UnexpectedMessageFormat– Message does not end with$$$.DeviceNotConnected– Serial connection is working, but no board is avaialable.