Skip to content

Commit 1c2fde9

Browse files
authored
add config_board_with_bytes method which is not recommended to use (#692)
Signed-off-by: Andrey Parfenov <a1994ndrey@gmail.com>
1 parent dd90d0f commit 1c2fde9

File tree

18 files changed

+187
-1
lines changed

18 files changed

+187
-1
lines changed

cpp_package/src/board_shim.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,15 @@ std::string BoardShim::config_board (std::string config)
244244
return resp;
245245
}
246246

247+
void BoardShim::config_board_with_bytes (const char *bytes, int len)
248+
{
249+
int res = ::config_board_with_bytes (bytes, len, board_id, serialized_params.c_str ());
250+
if (res != (int)BrainFlowExitCodes::STATUS_OK)
251+
{
252+
throw BrainFlowException ("failed to config board with bytes", res);
253+
}
254+
}
255+
247256
void BoardShim::insert_marker (double value, int preset)
248257
{
249258
int res = ::insert_marker (value, preset, board_id, serialized_params.c_str ());

cpp_package/src/inc/board_shim.h

+2
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ class BoardShim
263263
BrainFlowArray<double, 2> get_board_data (int num_datapoints, int preset);
264264
/// send string to a board, use it carefully and only if you understand what you are doing
265265
std::string config_board (std::string config);
266+
/// send raw bytes to a board, not implemented for majority of devices, not recommended to use
267+
void config_board_with_bytes (const char *bytes, int len);
266268
/// insert marker in data stream
267269
void insert_marker (double value, int preset = (int)BrainFlowPresets::DEFAULT_PRESET);
268270
};

csharp_package/brainflow/brainflow/board_controller_library.cs

+17
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ public static class BoardControllerLibrary64
143143
[DllImport ("BoardController", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
144144
public static extern int config_board (string config, byte[] response, int[] len, int board_id, string input_json);
145145
[DllImport ("BoardController", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
146+
public static extern int config_board_with_bytes (byte[] bytes, int len, int board_id, string input_json);
147+
[DllImport ("BoardController", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
146148
public static extern int set_log_file_board_controller (string log_file);
147149
[DllImport ("BoardController", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
148150
public static extern int get_sampling_rate (int board_id, int preset, int[] sampling_rate);
@@ -231,6 +233,8 @@ public static class BoardControllerLibrary32
231233
[DllImport ("BoardController32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
232234
public static extern int config_board (string config, byte[] response, int[] len, int board_id, string input_json);
233235
[DllImport ("BoardController32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
236+
public static extern int config_board_with_bytes (byte[] bytes, int len, int board_id, string input_json);
237+
[DllImport ("BoardController32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
234238
public static extern int set_log_file_board_controller (string log_file);
235239
[DllImport ("BoardController32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
236240
public static extern int get_sampling_rate (int board_id, int preset, int[] sampling_rate);
@@ -456,6 +460,19 @@ public static int config_board (string config, byte[] str, int[] len, int board_
456460
return (int)BrainFlowExitCodes.GENERAL_ERROR;
457461
}
458462

463+
public static int config_board_with_bytes (byte[] bytes, int len, int board_id, string input_json)
464+
{
465+
switch (PlatformHelper.get_library_environment ())
466+
{
467+
case LibraryEnvironment.x64:
468+
return BoardControllerLibrary64.config_board_with_bytes (bytes, len, board_id, input_json);
469+
case LibraryEnvironment.x86:
470+
return BoardControllerLibrary32.config_board_with_bytes (bytes, len, board_id, input_json);
471+
}
472+
473+
return (int)BrainFlowExitCodes.GENERAL_ERROR;
474+
}
475+
459476
public static int add_streamer (string streamer, int preset, int board_id, string input_json)
460477
{
461478
switch (PlatformHelper.get_library_environment ())

csharp_package/brainflow/brainflow/board_shim.cs

+13
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,19 @@ public string config_board (string config)
725725
string response = System.Text.Encoding.UTF8.GetString (str, 0, len[0]);
726726
return response;
727727
}
728+
729+
/// <summary>
730+
/// send raw bytes to device, dont use it
731+
/// </summary>
732+
/// <param name="bytes"></param>
733+
public void config_board_with_bytes (byte[] bytes)
734+
{
735+
int res = BoardControllerLibrary.config_board_with_bytes (bytes, bytes.Length, board_id, input_json);
736+
if (res != (int)BrainFlowExitCodes.STATUS_OK)
737+
{
738+
throw new BrainFlowError (res);
739+
}
740+
}
728741

729742
/// <summary>
730743
/// add streamer

java_package/brainflow/src/main/java/brainflow/BoardShim.java

+14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ private interface DllInterface extends Library
2525

2626
int config_board (String config, byte[] names, int[] len, int board_id, String params);
2727

28+
int config_board_with_bytes (byte[] bytes, int len, int board_id, String params);
29+
2830
int add_streamer (String streamer, int preset, int board_id, String params);
2931

3032
int delete_streamer (String streamer, int preset, int board_id, String params);
@@ -1417,6 +1419,18 @@ public String config_board (String config) throws BrainFlowError
14171419
return resp;
14181420
}
14191421

1422+
/**
1423+
* send string to a board, dont use it
1424+
*/
1425+
public void config_board_with_bytes (byte[] bytes) throws BrainFlowError
1426+
{
1427+
int ec = instance.config_board_with_bytes (bytes, bytes.length, board_id, input_json);
1428+
if (ec != BrainFlowExitCode.STATUS_OK.get_code ())
1429+
{
1430+
throw new BrainFlowError ("Error in config_board", ec);
1431+
}
1432+
}
1433+
14201434
/**
14211435
* start streaming thread, store data in internal ringbuffer and stream them
14221436
* from brainflow at the same time

julia_package/brainflow/src/board_shim.jl

+5
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,11 @@ end
287287
return sub_string
288288
end
289289

290+
@brainflow_rethrow function config_board_with_bytes(bytes::Vector{Cuchar}, len::Integer, board_shim::BoardShim)
291+
ccall((:config_board_with_bytes, BOARD_CONTROLLER_INTERFACE), Cint, (Ptr{UInt8}, Cint, Cint, Ptr{UInt8}),
292+
bytes, len, board_shim.board_id, board_shim.input_json)
293+
end
294+
290295
@brainflow_rethrow function get_board_data(num_samples::Integer, board_shim::BoardShim, preset::PresetType=Integer(DEFAULT_PRESET))
291296
data_size = get_board_data_count(board_shim, preset)
292297
if num_samples < 0

matlab_package/brainflow/BoardShim.m

+8
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,14 @@ function prepare_session(obj)
385385
[exit_code, tmp, response] = calllib(lib_name, task_name, config, blanks(4096), 4096, obj.board_id, obj.input_params_json);
386386
BoardShim.check_ec(exit_code, task_name);
387387
end
388+
389+
function config_board_with_bytes(obj, bytes)
390+
% send bytes to the board, do not use it
391+
task_name = 'config_board_with_bytes';
392+
lib_name = BoardShim.load_lib();
393+
exit_code = calllib(lib_name, task_name, bytes, size(bytes, 2), obj.board_id, obj.input_params_json);
394+
BoardShim.check_ec(exit_code, task_name);
395+
end
388396

389397
function add_streamer(obj, streamer, preset)
390398
% add streamer

nodejs_package/brainflow/board_shim.ts

+11
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class BoardControllerDLL extends BoardControllerFunctions
6868
this.prepareSession = this.lib.func(CLike.prepare_session);
6969
this.startStream = this.lib.func(CLike.start_stream);
7070
this.configBoard = this.lib.func(CLike.config_board);
71+
this.configBoardWithBytes = this.lib.func(CLike.config_board_with_bytes);
7172
this.getBoardDataCount = this.lib.func(CLike.get_board_data_count);
7273
this.getBoardData = this.lib.func(CLike.get_board_data);
7374
this.getCurrentBoardData = this.lib.func(CLike.get_current_board_data);
@@ -285,6 +286,16 @@ export class BoardShim
285286
return out[0].substring(0, len[0]);
286287
}
287288

289+
public configBoardWithBytes(config: string, len: number): void
290+
{
291+
const res = BoardControllerDLL.getInstance().configBoardWithBytes(
292+
config, len, this.boardId, this.inputJson);
293+
if (res !== BrainFlowExitCodes.STATUS_OK)
294+
{
295+
throw new BrainFlowError (res, 'Could not config board with bytes');
296+
}
297+
}
298+
288299
public getBoardDataCount(preset = BrainFlowPresets.DEFAULT_PRESET): number
289300
{
290301
const dataSize = [0];

nodejs_package/brainflow/functions.types.ts

+8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export enum BoardControllerCLikeFunctions {
3333
'int add_streamer (const char *streamer, int preset, int board_id, const char *json_brainflow_input_params)',
3434
config_board =
3535
'int config_board (const char *config, _Inout_ char *response, _Inout_ int *resp_len, int board_id, const char *json_brainflow_input_params)',
36+
config_board_with_bytes =
37+
'int config_board_with_bytes (const char *bytes, int len, int board_id, const char *json_brainflow_input_params)',
3638
delete_streamer =
3739
'int delete_streamer (const char *streamer, int preset, int board_id, const char *json_brainflow_input_params)',
3840
insert_marker =
@@ -139,6 +141,12 @@ export class BoardControllerFunctions
139141
boardId: BoardIds,
140142
inputJson: string,
141143
) => BrainFlowExitCodes;
144+
configBoardWithBytes!: (
145+
config: string,
146+
len: number,
147+
boardId: BoardIds,
148+
inputJson: string,
149+
) => BrainFlowExitCodes;
142150
getBoardDataCount!: (
143151
preset: BrainFlowPresets,
144152
dataSize: number[],

python_package/brainflow/board_shim.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,15 @@ def __init__(self):
313313
ctypes.c_char_p
314314
]
315315

316+
self.config_board_with_bytes = self.lib.config_board_with_bytes
317+
self.config_board_with_bytes.restype = ctypes.c_int
318+
self.config_board_with_bytes.argtypes = [
319+
ndpointer(ctypes.c_ubyte),
320+
ctypes.c_int,
321+
ctypes.c_int,
322+
ctypes.c_char_p
323+
]
324+
316325
self.get_sampling_rate = self.lib.get_sampling_rate
317326
self.get_sampling_rate.restype = ctypes.c_int
318327
self.get_sampling_rate.argtypes = [
@@ -1360,7 +1369,7 @@ def get_board_data(self, num_samples=None, preset: int = BrainFlowPresets.DEFAUL
13601369

13611370
return data_arr.reshape(package_length, data_size)
13621371

1363-
def config_board(self, config) -> None:
1372+
def config_board(self, config) -> str:
13641373
"""Use this method carefully and only if you understand what you are doing, do NOT use it to start or stop streaming
13651374
13661375
:param config: string to send to a board
@@ -1380,3 +1389,13 @@ def config_board(self, config) -> None:
13801389
if res != BrainFlowExitCodes.STATUS_OK.value:
13811390
raise BrainFlowError('unable to config board', res)
13821391
return string.tobytes().decode('utf-8')[0:string_len[0]]
1392+
1393+
def config_board_with_bytes(self, bytes_to_send) -> None:
1394+
"""Use this method carefully and only if you understand what you are doing
1395+
1396+
:param bytes_to_send: bytes to send
1397+
:type config: ndarray astype(numpy.ubyte)
1398+
"""
1399+
res = BoardControllerDLL.get_instance().config_board_with_bytes(bytes_to_send, len(bytes_to_send), self.board_id, self.input_json)
1400+
if res != BrainFlowExitCodes.STATUS_OK.value:
1401+
raise BrainFlowError('unable to config board', res)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import numpy
2+
3+
from brainflow.board_shim import BoardShim, BrainFlowInputParams, BoardIds
4+
5+
6+
def main():
7+
BoardShim.enable_dev_board_logger()
8+
9+
params = BrainFlowInputParams()
10+
board = BoardShim(BoardIds.SYNTHETIC_BOARD, params)
11+
board.prepare_session()
12+
config = numpy.zeros(4).astype(numpy.ubyte)
13+
config[1] = 3
14+
board.config_board_with_bytes(config)
15+
board.release_session()
16+
17+
18+
if __name__ == "__main__":
19+
main()

rust_package/brainflow/src/board_shim.rs

+13
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,19 @@ impl BoardShim {
240240
.to_string())
241241
}
242242

243+
/// Use this method carefully and only if you understand what you are doing.
244+
pub fn config_board_with_bytes(&self, bytes: Vec<i8>) -> Result<()> {
245+
let res = unsafe {
246+
board_controller::config_board_with_bytes(
247+
bytes.as_ptr(),
248+
bytes.len() as c_int,
249+
self.board_id as c_int,
250+
self.json_brainflow_input_params.as_ptr(),
251+
)
252+
};
253+
Ok(check_brainflow_exit_code(res)?)
254+
}
255+
243256
/// Insert Marker to Data Stream.
244257
pub fn insert_marker(&self, value: f64, preset: BrainFlowPresets) -> Result<()> {
245258
let res = unsafe {

rust_package/brainflow/src/ffi/board_controller.rs

+8
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,14 @@ extern "C" {
258258
json_brainflow_input_params: *const ::std::os::raw::c_char,
259259
) -> ::std::os::raw::c_int;
260260
}
261+
extern "C" {
262+
pub fn config_board_with_bytes(
263+
bytes: *const ::std::os::raw::c_char,
264+
len: ::std::os::raw::c_int,
265+
board_id: ::std::os::raw::c_int,
266+
json_brainflow_input_params: *const ::std::os::raw::c_char,
267+
) -> ::std::os::raw::c_int;
268+
}
261269
extern "C" {
262270
pub fn is_prepared(
263271
prepared: *mut ::std::os::raw::c_int,

src/board_controller/board_controller.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,25 @@ int config_board (const char *config, char *response, int *response_len, int boa
476476
return res;
477477
}
478478

479+
int config_board_with_bytes (
480+
const char *bytes, int len, int board_id, const char *json_brainflow_input_params)
481+
{
482+
std::lock_guard<std::mutex> lock (mutex);
483+
if ((bytes == NULL) || (len < 1))
484+
{
485+
return (int)BrainFlowExitCodes::INVALID_ARGUMENTS_ERROR;
486+
}
487+
488+
std::pair<int, struct BrainFlowInputParams> key;
489+
int res = check_board_session (board_id, json_brainflow_input_params, key, false);
490+
if (res != (int)BrainFlowExitCodes::STATUS_OK)
491+
{
492+
return res;
493+
}
494+
auto board_it = boards.find (key);
495+
return board_it->second->config_board_with_bytes (bytes, len);
496+
}
497+
479498
int add_streamer (
480499
const char *streamer, int preset, int board_id, const char *json_brainflow_input_params)
481500
{

src/board_controller/inc/board.h

+7
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ class Board
5454
virtual int release_session () = 0;
5555
virtual int config_board (std::string config, std::string &response) = 0;
5656

57+
// some devices may implement it but there is no requirement to have this method and we do not
58+
// recommend anybody to use it
59+
virtual int config_board_with_bytes (const char *bytes, int len)
60+
{
61+
return (int)BrainFlowExitCodes::UNSUPPORTED_BOARD_ERROR;
62+
}
63+
5764
int get_current_board_data (
5865
int num_samples, int preset, double *data_buf, int *returned_samples);
5966
int get_board_data_count (int preset, int *result);

src/board_controller/inc/board_controller.h

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ extern "C"
2424
double *data_buf, int board_id, const char *json_brainflow_input_params);
2525
SHARED_EXPORT int CALLING_CONVENTION config_board (const char *config, char *response,
2626
int *response_len, int board_id, const char *json_brainflow_input_params);
27+
SHARED_EXPORT int CALLING_CONVENTION config_board_with_bytes (
28+
const char *bytes, int len, int board_id, const char *json_brainflow_input_params);
2729
SHARED_EXPORT int CALLING_CONVENTION is_prepared (
2830
int *prepared, int board_id, const char *json_brainflow_input_params);
2931
SHARED_EXPORT int CALLING_CONVENTION insert_marker (

src/board_controller/inc/synthetic_board.h

+1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ class SyntheticBoard : public Board
2626
int stop_stream ();
2727
int release_session ();
2828
int config_board (std::string config, std::string &response);
29+
int config_board_with_bytes (const char *bytes, int len);
2930
};

src/board_controller/synthetic_board.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,14 @@ int SyntheticBoard::config_board (std::string config, std::string &response)
258258
response = "Config:" + config;
259259
return (int)BrainFlowExitCodes::STATUS_OK;
260260
}
261+
262+
// if you use this board as a reference, more likely you dont need to implement this method
263+
int SyntheticBoard::config_board_with_bytes (const char *bytes, int len)
264+
{
265+
safe_logger (spdlog::level::info, "config_board_with_bytes for len: {}", len);
266+
for (int i = 0; i < len; i++)
267+
{
268+
safe_logger (spdlog::level::trace, "byte: {} value: {}", i, (int)bytes[i]);
269+
}
270+
return (int)BrainFlowExitCodes::STATUS_OK;
271+
}

0 commit comments

Comments
 (0)