Skip to content

Commit

Permalink
WIP: adding support for multiple emotibits
Browse files Browse the repository at this point in the history
Signed-off-by: Andrey Parfenov <[email protected]>
  • Loading branch information
Andrey1994 committed Nov 23, 2024
1 parent 134d538 commit 008e524
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/SupportedBoards.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,7 @@ To create such board you need to specify the following board ID and fields of Br

- :code:`BoardIds.EMOTIBIT_BOARD`
- *optional:* :code:`ip_address`, you can provide *broadcast* ip address of the network with EmotiBit device, e.g. 192.168.178.255. If not provided BrainFlow will try to autodiscover the network and it may take a little longer.
- *optional:* :code:`serial_number`, recommended you if have multiple boards in the same network.

Initialization Example:

Expand Down
62 changes: 62 additions & 0 deletions python_package/examples/tests/two_emotibits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import argparse
import time

from brainflow.board_shim import BoardShim, BrainFlowInputParams, BoardIds, BrainFlowPresets
from brainflow.data_filter import DataFilter


def main():
BoardShim.enable_dev_board_logger()

parser = argparse.ArgumentParser()
parser.add_argument('--id1', type=str, help='id for first emotibit', required=True)
parser.add_argument('--id2', type=str, help='id for second emotibit', required=True)
args = parser.parse_args()

params1 = BrainFlowInputParams()
params1.serial_number = args.id1
params2 = BrainFlowInputParams()
params2.serial_number = args.id2
board_id = BoardIds.EMOTIBIT_BOARD.value

presets = BoardShim.get_board_presets(board_id)
print (presets)

# Init both boards
board1 = BoardShim(board_id, params1)
board2 = BoardShim(board_id, params2)
board1.prepare_session()
board2.prepare_session()

# Start streaming for both
board1.start_stream()
board2.start_stream()
time.sleep(10)

# Get data from both
data_default1 = board1.get_board_data(preset=BrainFlowPresets.DEFAULT_PRESET)
data_aux1 = board1.get_board_data(preset=BrainFlowPresets.AUXILIARY_PRESET)
data_anc1 = board1.get_board_data(preset=BrainFlowPresets.ANCILLARY_PRESET)
data_default2 = board2.get_board_data(preset=BrainFlowPresets.DEFAULT_PRESET)
data_aux2 = board2.get_board_data(preset=BrainFlowPresets.AUXILIARY_PRESET)
data_anc2 = board1.get_board_data(preset=BrainFlowPresets.ANCILLARY_PRESET)

# Stop streaming for both
board1.stop_stream()
board2.stop_stream()

# Release both boards
board1.release_session()
board2.release_session()

# Write data from both
DataFilter.write_file(data_default1, 'default1.csv', 'w')
DataFilter.write_file(data_aux1, 'aux1.csv', 'w')
DataFilter.write_file(data_anc1, 'anc1.csv', 'w')
DataFilter.write_file(data_default2, 'default2.csv', 'w')
DataFilter.write_file(data_aux2, 'aux2.csv', 'w')
DataFilter.write_file(data_anc2, 'anc2.csv', 'w')


if __name__ == "__main__":
main()
32 changes: 27 additions & 5 deletions src/board_controller/emotibit/emotibit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,13 @@ std::vector<std::string> Emotibit::split_string (const std::string &package, cha

bool Emotibit::get_header (
const std::string &package_string, int *package_num, int *data_len, std::string &type_tag)
{
std::string serial_number = "";
return get_header (package_string, package_num, data_len, type_tag, serial_number);
}

bool Emotibit::get_header (const std::string &package_string, int *package_num, int *data_len,
std::string &type_tag, std::string &serial_number)
{
std::vector<std::string> package = split_string (package_string, PAYLOAD_DELIMITER);
if (package.size () >= HEADER_LENGTH)
Expand All @@ -518,6 +525,13 @@ bool Emotibit::get_header (
if (package.at (3) != "")
{
type_tag = package.at (3);
if (type_tag == HELLO_HOST)
{
if (package.size () > 9)
{
serial_number = package.at (9);
}
}
}
else
{
Expand Down Expand Up @@ -630,13 +644,21 @@ int Emotibit::create_adv_connection ()
int package_num = 0;
int data_len = 0;
std::string type_tag = "";
if (get_header (recv_package, &package_num, &data_len, type_tag))
std::string serial_number = "";
if (get_header (
recv_package, &package_num, &data_len, type_tag, serial_number))
{
safe_logger (spdlog::level::info, "received {} package", type_tag);
if ((type_tag == HELLO_HOST) || (type_tag == PONG))
if (type_tag == HELLO_HOST)
{
found = true;
ip_address = emotibit_ip;
safe_logger (
spdlog::level::info, "Found emotibit: {}", serial_number);
if (params.serial_number.empty () ||
(params.serial_number == serial_number))
{
found = true;
ip_address = emotibit_ip;
}
}
}
else
Expand Down Expand Up @@ -795,7 +817,7 @@ int Emotibit::wait_for_connection ()
}
else
{
int max_attempts = 15;
int max_attempts = 20;
for (int i = 0; i < max_attempts; i++)
{
safe_logger (spdlog::level::trace, "waiting for accept {}/{}", i, max_attempts);
Expand Down
2 changes: 2 additions & 0 deletions src/board_controller/emotibit/inc/emotibit.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class Emotibit : public Board
std::vector<std::string> split_string (const std::string &package, char delim);
bool get_header (
const std::string &package_string, int *package_num, int *data_len, std::string &type_tag);
bool get_header (const std::string &package_string, int *package_num, int *data_len,
std::string &type_tag, std::string &serial_number);
std::vector<std::string> get_payload (const std::string &package_string, int data_len);

int create_adv_connection ();
Expand Down

0 comments on commit 008e524

Please sign in to comment.