Skip to content

Commit

Permalink
Merge remote-tracking branch 'openbci/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey1994 committed Feb 1, 2020
2 parents e413c87 + 5926e46 commit cb15bb1
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 90 deletions.
11 changes: 11 additions & 0 deletions cpp-package/src/board_shim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ void BoardShim::prepare_session ()
}
}

bool BoardShim::is_prepared ()
{
int prepared = 0;
int res = ::is_prepared (&prepared, board_id, const_cast<char *> (serialized_params.c_str ()));
if (res != STATUS_OK)
{
throw BrainFlowException ("failed to check session", res);
}
return (bool)prepared;
}

void BoardShim::start_stream (int buffer_size, char *streamer_params)
{
int res = ::start_stream (
Expand Down
2 changes: 2 additions & 0 deletions cpp-package/src/inc/board_shim.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ class BoardShim
Range for multicast addresses is from "224.0.0.0" to "239.255.255.255"
*/
void start_stream (int buffer_size = 450000, char *streamer_params = NULL);
/// check if session is ready or not
bool is_prepared ();
/// stop streaming thread, doesnt release other resources
void stop_stream ();
/// release streaming session
Expand Down
12 changes: 12 additions & 0 deletions csharp-package/brainflow/brainflow/board_controller_library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ public static class BoardControllerLibrary64
public static extern int get_other_channels (int board_id, int[] channels, int[] len);
[DllImport ("BoardController.dll", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public static extern int get_temperature_channels (int board_id, int[] channels, int[] len);
[DllImport("BoardController.dll", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public static extern int is_prepared(int[] prepared, int board_id, string input_json);
}

public static class BoardControllerLibrary32
Expand Down Expand Up @@ -170,6 +172,8 @@ public static class BoardControllerLibrary32
public static extern int get_other_channels (int board_id, int[] channels, int[] len);
[DllImport ("BoardController32.dll", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public static extern int get_temperature_channels (int board_id, int[] channels, int[] len);
[DllImport("BoardController32.dll", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public static extern int is_prepared(int[] prepared, int board_id, string input_json);
}

public static class BoardControllerLibrary
Expand Down Expand Up @@ -222,6 +226,14 @@ public static int get_board_data_count (int[] result, int board_id, string input
return BoardControllerLibrary32.get_board_data_count (result, board_id, input_json);
}

public static int is_prepared(int[] result, int board_id, string input_json)
{
if (System.Environment.Is64BitProcess)
return BoardControllerLibrary64.is_prepared(result, board_id, input_json);
else
return BoardControllerLibrary32.is_prepared(result, board_id, input_json);
}

public static int get_board_data (int data_count, double[] data_buf, int board_id, string input_json)
{
if (System.Environment.Is64BitProcess)
Expand Down
15 changes: 15 additions & 0 deletions csharp-package/brainflow/brainflow/board_shim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,21 @@ public void release_session ()
}
}

/// <summary>
/// check session status
/// </summary>
/// <returns>session status</returns>
public bool is_prepared ()
{
int[] res = new int[1];
int ec = BoardControllerLibrary.is_prepared(res, board_id, input_json);
if (ec != (int)CustomExitCodes.STATUS_OK)
{
throw new BrainFlowException(ec);
}
return res[0] != 0;
}

/// <summary>
/// get number of packages in ringbuffer
/// </summary>
Expand Down
32 changes: 24 additions & 8 deletions java-package/brainflow/src/main/java/brainflow/BoardShim.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ public class BoardShim

private interface DllInterface extends Library
{
int prepare_session (int board_id, String port_name);
int prepare_session (int board_id, String params);

int config_board (String config, int board_id, String port_name);
int config_board (String config, int board_id, String params);

int start_stream (int buffer_size, String streamer_params, int board_id, String port_name);
int start_stream (int buffer_size, String streamer_params, int board_id, String params);

int stop_stream (int board_id, String port_name);
int stop_stream (int board_id, String params);

int release_session (int board_id, String port_name);
int release_session (int board_id, String params);

int get_current_board_data (int num_samples, double[] data_buf, int[] returned_samples, int board_id,
String port_name);
String params);

int get_board_data_count (int[] result, int board_id, String port_name);
int get_board_data_count (int[] result, int board_id, String params);

int get_board_data (int data_count, double[] data_buf, int board_id, String port_name);
int get_board_data (int data_count, double[] data_buf, int board_id, String params);

int set_log_level (int log_level);

Expand Down Expand Up @@ -73,6 +73,8 @@ int get_current_board_data (int num_samples, double[] data_buf, int[] returned_s
int get_other_channels (int board_id, int[] other_channels, int[] len);

int get_temperature_channels (int board_id, int[] temperature_channels, int[] len);

int is_prepared (int[] prepared, int board_id, String params);
}

private static DllInterface instance;
Expand Down Expand Up @@ -560,6 +562,20 @@ public int get_board_data_count () throws BrainFlowError
return res[0];
}

/**
* check session status
*/
public boolean is_prepared () throws BrainFlowError
{
int[] res = new int[1];
int ec = instance.is_prepared (res, board_id, input_json);
if (ec != ExitCode.STATUS_OK.get_code ())
{
throw new BrainFlowError ("Error in is_prepared", ec);
}
return res[0] != 0;
}

/**
* get latest collected data, can return less than "num_samples", doesnt flush
* it from ringbuffer
Expand Down
21 changes: 21 additions & 0 deletions python-package/brainflow/board_shim.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ def __init__ (self):
ctypes.c_char_p
]

self.is_prepared = self.lib.is_prepared
self.is_prepared.restype = ctypes.c_int
self.is_prepared.argtypes = [
ndpointer (ctypes.c_int32),
ctypes.c_int,
ctypes.c_char_p
]

self.start_stream = self.lib.start_stream
self.start_stream.restype = ctypes.c_int
self.start_stream.argtypes = [
Expand Down Expand Up @@ -774,6 +782,19 @@ def get_board_data_count (self):
raise BrainFlowError ('unable to obtain buffer size', res)
return data_size[0]

def is_prepared (self):
"""Check if session is ready or not
:return: session status
:rtype: bool
"""
prepared = numpy.zeros (1).astype (numpy.int32)

res = BoardControllerDLL.get_instance ().is_prepared (prepared, self.board_id, self.input_json)
if res != BrainflowExitCodes.STATUS_OK.value:
raise BrainFlowError ('unable to check session status', res)
return bool(prepared[0])

def get_board_data (self):
"""Get all board data and remove them from ringbuffer
Expand Down
120 changes: 53 additions & 67 deletions src/board_controller/board_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ std::mutex mutex;

std::pair<int, struct BrainFlowInputParams> get_key (
int board_id, struct BrainFlowInputParams params);
static int check_board_session (std::pair<int, struct BrainFlowInputParams> key);
static int check_board_session (int board_id, char *json_brainflow_input_params,
std::pair<int, struct BrainFlowInputParams> &key, bool log_error = true);
static int string_to_brainflow_input_params (
const char *json_brainflow_input_params, struct BrainFlowInputParams *params);

Expand Down Expand Up @@ -96,24 +97,38 @@ int prepare_session (int board_id, char *json_brainflow_input_params)
{
board = NULL;
}
boards[key] = board;
else
{
boards[key] = board;
}
return res;
}

int start_stream (
int buffer_size, char *streamer_params, int board_id, char *json_brainflow_input_params)
int is_prepared (int *prepared, int board_id, char *json_brainflow_input_params)
{
std::lock_guard<std::mutex> lock (mutex);

struct BrainFlowInputParams params;
int res = string_to_brainflow_input_params (json_brainflow_input_params, &params);
if (res != STATUS_OK)
std::pair<int, struct BrainFlowInputParams> key;
int res = check_board_session (board_id, json_brainflow_input_params, key, false);
if (res == STATUS_OK)
{
return res;
*prepared = 1;
}
if (res == BOARD_NOT_CREATED_ERROR)
{
*prepared = 0;
res = STATUS_OK;
}
return res;
}

std::pair<int, struct BrainFlowInputParams> key = get_key (board_id, params);
res = check_board_session (key);
int start_stream (
int buffer_size, char *streamer_params, int board_id, char *json_brainflow_input_params)
{
std::lock_guard<std::mutex> lock (mutex);

std::pair<int, struct BrainFlowInputParams> key;
int res = check_board_session (board_id, json_brainflow_input_params, key, false);
if (res != STATUS_OK)
{
return res;
Expand All @@ -126,15 +141,8 @@ int stop_stream (int board_id, char *json_brainflow_input_params)
{
std::lock_guard<std::mutex> lock (mutex);

struct BrainFlowInputParams params;
int res = string_to_brainflow_input_params (json_brainflow_input_params, &params);
if (res != STATUS_OK)
{
return res;
}

std::pair<int, struct BrainFlowInputParams> key = get_key (board_id, params);
res = check_board_session (key);
std::pair<int, struct BrainFlowInputParams> key;
int res = check_board_session (board_id, json_brainflow_input_params, key, false);
if (res != STATUS_OK)
{
return res;
Expand All @@ -147,15 +155,8 @@ int release_session (int board_id, char *json_brainflow_input_params)
{
std::lock_guard<std::mutex> lock (mutex);

struct BrainFlowInputParams params;
int res = string_to_brainflow_input_params (json_brainflow_input_params, &params);
if (res != STATUS_OK)
{
return res;
}

std::pair<int, struct BrainFlowInputParams> key = get_key (board_id, params);
res = check_board_session (key);
std::pair<int, struct BrainFlowInputParams> key;
int res = check_board_session (board_id, json_brainflow_input_params, key, false);
if (res != STATUS_OK)
{
return res;
Expand All @@ -171,15 +172,8 @@ int get_current_board_data (int num_samples, double *data_buf, int *returned_sam
{
std::lock_guard<std::mutex> lock (mutex);

struct BrainFlowInputParams params;
int res = string_to_brainflow_input_params (json_brainflow_input_params, &params);
if (res != STATUS_OK)
{
return res;
}

std::pair<int, struct BrainFlowInputParams> key = get_key (board_id, params);
res = check_board_session (key);
std::pair<int, struct BrainFlowInputParams> key;
int res = check_board_session (board_id, json_brainflow_input_params, key, false);
if (res != STATUS_OK)
{
return res;
Expand All @@ -192,15 +186,8 @@ int get_board_data_count (int *result, int board_id, char *json_brainflow_input_
{
std::lock_guard<std::mutex> lock (mutex);

struct BrainFlowInputParams params;
int res = string_to_brainflow_input_params (json_brainflow_input_params, &params);
if (res != STATUS_OK)
{
return res;
}

std::pair<int, struct BrainFlowInputParams> key = get_key (board_id, params);
res = check_board_session (key);
std::pair<int, struct BrainFlowInputParams> key;
int res = check_board_session (board_id, json_brainflow_input_params, key, false);
if (res != STATUS_OK)
{
return res;
Expand All @@ -214,15 +201,8 @@ int get_board_data (
{
std::lock_guard<std::mutex> lock (mutex);

struct BrainFlowInputParams params;
int res = string_to_brainflow_input_params (json_brainflow_input_params, &params);
if (res != STATUS_OK)
{
return res;
}

std::pair<int, struct BrainFlowInputParams> key = get_key (board_id, params);
res = check_board_session (key);
std::pair<int, struct BrainFlowInputParams> key;
int res = check_board_session (board_id, json_brainflow_input_params, key, false);
if (res != STATUS_OK)
{
return res;
Expand Down Expand Up @@ -273,15 +253,8 @@ int config_board (char *config, int board_id, char *json_brainflow_input_params)
{
std::lock_guard<std::mutex> lock (mutex);

struct BrainFlowInputParams params;
int res = string_to_brainflow_input_params (json_brainflow_input_params, &params);
if (res != STATUS_OK)
{
return res;
}

std::pair<int, struct BrainFlowInputParams> key = get_key (board_id, params);
res = check_board_session (key);
std::pair<int, struct BrainFlowInputParams> key;
int res = check_board_session (board_id, json_brainflow_input_params, key, false);
if (res != STATUS_OK)
{
return res;
Expand All @@ -301,12 +274,25 @@ std::pair<int, struct BrainFlowInputParams> get_key (
return key;
}

int check_board_session (std::pair<int, struct BrainFlowInputParams> key)
int check_board_session (int board_id, char *json_brainflow_input_params,
std::pair<int, struct BrainFlowInputParams> &key, bool log_error)
{
struct BrainFlowInputParams params;
int res = string_to_brainflow_input_params (json_brainflow_input_params, &params);
if (res != STATUS_OK)
{
return res;
}

key = get_key (board_id, params);

if (boards.find (key) == boards.end ())
{
Board::board_logger->error (
"Board with id {} and port provided config is not created", key.first);
if (log_error)
{
Board::board_logger->error (
"Board with id {} and port provided config is not created", key.first);
}
return BOARD_NOT_CREATED_ERROR;
}
return STATUS_OK;
Expand Down
2 changes: 2 additions & 0 deletions src/board_controller/inc/board_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ extern "C"
int data_count, double *data_buf, int board_id, char *json_brainflow_input_params);
SHARED_EXPORT int CALLING_CONVENTION config_board (
char *config, int board_id, char *json_brainflow_input_params);
SHARED_EXPORT int CALLING_CONVENTION is_prepared (
int *prepared, int board_id, char *json_brainflow_input_params);

// logging methods
SHARED_EXPORT int CALLING_CONVENTION set_log_level (int log_level);
Expand Down

0 comments on commit cb15bb1

Please sign in to comment.