Skip to content

Commit 008e524

Browse files
committed
WIP: adding support for multiple emotibits
Signed-off-by: Andrey Parfenov <a1994ndrey@gmail.com>
1 parent 134d538 commit 008e524

File tree

4 files changed

+92
-5
lines changed

4 files changed

+92
-5
lines changed

docs/SupportedBoards.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,7 @@ To create such board you need to specify the following board ID and fields of Br
12481248

12491249
- :code:`BoardIds.EMOTIBIT_BOARD`
12501250
- *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.
1251+
- *optional:* :code:`serial_number`, recommended you if have multiple boards in the same network.
12511252

12521253
Initialization Example:
12531254

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import argparse
2+
import time
3+
4+
from brainflow.board_shim import BoardShim, BrainFlowInputParams, BoardIds, BrainFlowPresets
5+
from brainflow.data_filter import DataFilter
6+
7+
8+
def main():
9+
BoardShim.enable_dev_board_logger()
10+
11+
parser = argparse.ArgumentParser()
12+
parser.add_argument('--id1', type=str, help='id for first emotibit', required=True)
13+
parser.add_argument('--id2', type=str, help='id for second emotibit', required=True)
14+
args = parser.parse_args()
15+
16+
params1 = BrainFlowInputParams()
17+
params1.serial_number = args.id1
18+
params2 = BrainFlowInputParams()
19+
params2.serial_number = args.id2
20+
board_id = BoardIds.EMOTIBIT_BOARD.value
21+
22+
presets = BoardShim.get_board_presets(board_id)
23+
print (presets)
24+
25+
# Init both boards
26+
board1 = BoardShim(board_id, params1)
27+
board2 = BoardShim(board_id, params2)
28+
board1.prepare_session()
29+
board2.prepare_session()
30+
31+
# Start streaming for both
32+
board1.start_stream()
33+
board2.start_stream()
34+
time.sleep(10)
35+
36+
# Get data from both
37+
data_default1 = board1.get_board_data(preset=BrainFlowPresets.DEFAULT_PRESET)
38+
data_aux1 = board1.get_board_data(preset=BrainFlowPresets.AUXILIARY_PRESET)
39+
data_anc1 = board1.get_board_data(preset=BrainFlowPresets.ANCILLARY_PRESET)
40+
data_default2 = board2.get_board_data(preset=BrainFlowPresets.DEFAULT_PRESET)
41+
data_aux2 = board2.get_board_data(preset=BrainFlowPresets.AUXILIARY_PRESET)
42+
data_anc2 = board1.get_board_data(preset=BrainFlowPresets.ANCILLARY_PRESET)
43+
44+
# Stop streaming for both
45+
board1.stop_stream()
46+
board2.stop_stream()
47+
48+
# Release both boards
49+
board1.release_session()
50+
board2.release_session()
51+
52+
# Write data from both
53+
DataFilter.write_file(data_default1, 'default1.csv', 'w')
54+
DataFilter.write_file(data_aux1, 'aux1.csv', 'w')
55+
DataFilter.write_file(data_anc1, 'anc1.csv', 'w')
56+
DataFilter.write_file(data_default2, 'default2.csv', 'w')
57+
DataFilter.write_file(data_aux2, 'aux2.csv', 'w')
58+
DataFilter.write_file(data_anc2, 'anc2.csv', 'w')
59+
60+
61+
if __name__ == "__main__":
62+
main()

src/board_controller/emotibit/emotibit.cpp

+27-5
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,13 @@ std::vector<std::string> Emotibit::split_string (const std::string &package, cha
493493

494494
bool Emotibit::get_header (
495495
const std::string &package_string, int *package_num, int *data_len, std::string &type_tag)
496+
{
497+
std::string serial_number = "";
498+
return get_header (package_string, package_num, data_len, type_tag, serial_number);
499+
}
500+
501+
bool Emotibit::get_header (const std::string &package_string, int *package_num, int *data_len,
502+
std::string &type_tag, std::string &serial_number)
496503
{
497504
std::vector<std::string> package = split_string (package_string, PAYLOAD_DELIMITER);
498505
if (package.size () >= HEADER_LENGTH)
@@ -518,6 +525,13 @@ bool Emotibit::get_header (
518525
if (package.at (3) != "")
519526
{
520527
type_tag = package.at (3);
528+
if (type_tag == HELLO_HOST)
529+
{
530+
if (package.size () > 9)
531+
{
532+
serial_number = package.at (9);
533+
}
534+
}
521535
}
522536
else
523537
{
@@ -630,13 +644,21 @@ int Emotibit::create_adv_connection ()
630644
int package_num = 0;
631645
int data_len = 0;
632646
std::string type_tag = "";
633-
if (get_header (recv_package, &package_num, &data_len, type_tag))
647+
std::string serial_number = "";
648+
if (get_header (
649+
recv_package, &package_num, &data_len, type_tag, serial_number))
634650
{
635651
safe_logger (spdlog::level::info, "received {} package", type_tag);
636-
if ((type_tag == HELLO_HOST) || (type_tag == PONG))
652+
if (type_tag == HELLO_HOST)
637653
{
638-
found = true;
639-
ip_address = emotibit_ip;
654+
safe_logger (
655+
spdlog::level::info, "Found emotibit: {}", serial_number);
656+
if (params.serial_number.empty () ||
657+
(params.serial_number == serial_number))
658+
{
659+
found = true;
660+
ip_address = emotibit_ip;
661+
}
640662
}
641663
}
642664
else
@@ -795,7 +817,7 @@ int Emotibit::wait_for_connection ()
795817
}
796818
else
797819
{
798-
int max_attempts = 15;
820+
int max_attempts = 20;
799821
for (int i = 0; i < max_attempts; i++)
800822
{
801823
safe_logger (spdlog::level::trace, "waiting for accept {}/{}", i, max_attempts);

src/board_controller/emotibit/inc/emotibit.h

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class Emotibit : public Board
4545
std::vector<std::string> split_string (const std::string &package, char delim);
4646
bool get_header (
4747
const std::string &package_string, int *package_num, int *data_len, std::string &type_tag);
48+
bool get_header (const std::string &package_string, int *package_num, int *data_len,
49+
std::string &type_tag, std::string &serial_number);
4850
std::vector<std::string> get_payload (const std::string &package_string, int data_len);
4951

5052
int create_adv_connection ();

0 commit comments

Comments
 (0)