Skip to content

Commit f47833b

Browse files
authored
add get_board_sampling_rate method and overall cleanup (#805)
* add get_bord_sampling_rate() method, which tracks the current sampling rate of the device, instead of the static value Signed-off-by: Andrey Parfenov <a1994ndrey@gmail.com>
1 parent ee7d056 commit f47833b

75 files changed

Lines changed: 857 additions & 332 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cpp_package/src/board_shim.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ std::string BoardShim::config_board (std::string config)
235235
int response_len = 0;
236236
char response[8192];
237237
int res = ::config_board (
238-
config.c_str (), response, &response_len, board_id, serialized_params.c_str ());
238+
config.c_str (), response, &response_len, sizeof (response), board_id,
239+
serialized_params.c_str ());
239240
if (res != (int)BrainFlowExitCodes::STATUS_OK)
240241
{
241242
throw BrainFlowException ("failed to config board", res);
@@ -281,6 +282,18 @@ int BoardShim::get_board_id ()
281282
return master_board_id;
282283
}
283284

285+
int BoardShim::get_board_sampling_rate (int preset)
286+
{
287+
int sampling_rate = -1;
288+
int res =
289+
::get_board_sampling_rate (preset, &sampling_rate, board_id, serialized_params.c_str ());
290+
if (res != (int)BrainFlowExitCodes::STATUS_OK)
291+
{
292+
throw BrainFlowException ("failed to get board sampling rate", res);
293+
}
294+
return sampling_rate;
295+
}
296+
284297
//////////////////////////////////////////
285298
///////////// data desc methods //////////
286299
//////////////////////////////////////////
@@ -289,7 +302,8 @@ json BoardShim::get_board_descr (int board_id, int preset)
289302
{
290303
char board_descr_str[16000];
291304
int string_len = 0;
292-
int res = ::get_board_descr (board_id, preset, board_descr_str, &string_len);
305+
int res =
306+
::get_board_descr (board_id, preset, board_descr_str, &string_len, sizeof (board_descr_str));
293307
if (res != (int)BrainFlowExitCodes::STATUS_OK)
294308
{
295309
throw BrainFlowException ("failed to get board info", res);
@@ -368,7 +382,7 @@ std::vector<std::string> BoardShim::get_eeg_names (int board_id, int preset)
368382
{
369383
char eeg_names[4096];
370384
int string_len = 0;
371-
int res = ::get_eeg_names (board_id, preset, eeg_names, &string_len);
385+
int res = ::get_eeg_names (board_id, preset, eeg_names, &string_len, sizeof (eeg_names));
372386
if (res != (int)BrainFlowExitCodes::STATUS_OK)
373387
{
374388
throw BrainFlowException ("failed to get board info", res);
@@ -401,7 +415,7 @@ std::string BoardShim::get_device_name (int board_id, int preset)
401415
{
402416
char name[4096];
403417
int string_len = 0;
404-
int res = ::get_device_name (board_id, preset, name, &string_len);
418+
int res = ::get_device_name (board_id, preset, name, &string_len, sizeof (name));
405419
if (res != (int)BrainFlowExitCodes::STATUS_OK)
406420
{
407421
throw BrainFlowException ("failed to get board info", res);
@@ -602,4 +616,4 @@ std::string BoardShim::get_version ()
602616
std::string verion_str (version, string_len);
603617

604618
return verion_str;
605-
}
619+
}

cpp_package/src/inc/board_shim.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ class BoardShim
255255
int num_samples, int preset = (int)BrainFlowPresets::DEFAULT_PRESET);
256256
/// Get board id, for some boards can be different than provided (playback, streaming)
257257
int get_board_id ();
258+
/// get actual sampling rate for prepared board session
259+
int get_board_sampling_rate (int preset = (int)BrainFlowPresets::DEFAULT_PRESET);
258260
/// get number of packages in ringbuffer
259261
int get_board_data_count (int preset = (int)BrainFlowPresets::DEFAULT_PRESET);
260262
/// get all collected data and flush it from internal buffer

csharp_package/brainflow/brainflow/board_controller_library.cs

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,16 @@ public static class BoardControllerLibrary64
151151
[DllImport ("BoardController", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
152152
public static extern int log_message_board_controller (int log_level, string message);
153153
[DllImport ("BoardController", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
154-
public static extern int config_board (string config, byte[] response, int[] len, int board_id, string input_json);
154+
public static extern int config_board (string config, byte[] response, int[] len, int max_len, int board_id, string input_json);
155155
[DllImport ("BoardController", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
156156
public static extern int config_board_with_bytes (byte[] bytes, int len, int board_id, string input_json);
157157
[DllImport ("BoardController", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
158158
public static extern int set_log_file_board_controller (string log_file);
159159
[DllImport ("BoardController", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
160160
public static extern int get_sampling_rate (int board_id, int preset, int[] sampling_rate);
161161
[DllImport ("BoardController", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
162+
public static extern int get_board_sampling_rate (int preset, int[] sampling_rate, int board_id, string input_json);
163+
[DllImport ("BoardController", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
162164
public static extern int get_timestamp_channel (int board_id, int preset, int[] timestamp_channel);
163165
[DllImport ("BoardController", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
164166
public static extern int get_marker_channel (int board_id, int preset, int[] marker_channel);
@@ -195,19 +197,19 @@ public static class BoardControllerLibrary64
195197
[DllImport ("BoardController", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
196198
public static extern int is_prepared (int[] prepared, int board_id, string input_json);
197199
[DllImport ("BoardController", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
198-
public static extern int get_eeg_names (int board_id, int preset, byte[] eeg_names, int[] len);
200+
public static extern int get_eeg_names (int board_id, int preset, byte[] eeg_names, int[] len, int max_len);
199201
[DllImport ("BoardController", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
200202
public static extern int get_resistance_channels (int board_id, int preset, int[] channels, int[] len);
201203
[DllImport ("BoardController", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
202204
public static extern int get_magnetometer_channels (int board_id, int preset, int[] channels, int[] len);
203205
[DllImport ("BoardController", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
204206
public static extern int get_exg_channels (int board_id, int preset, int[] channels, int[] len);
205207
[DllImport ("BoardController", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
206-
public static extern int get_device_name (int board_id, int preset, byte[] name, int[] len);
208+
public static extern int get_device_name (int board_id, int preset, byte[] name, int[] len, int max_len);
207209
[DllImport ("BoardController", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
208210
public static extern int insert_marker (double value, int preset, int board_id, string input_json);
209211
[DllImport ("BoardController", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
210-
public static extern int get_board_descr (int board_id, int preset, byte[] board_descr, int[] len);
212+
public static extern int get_board_descr (int board_id, int preset, byte[] board_descr, int[] len, int max_len);
211213
[DllImport ("BoardController", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
212214
public static extern int release_all_sessions ();
213215
[DllImport ("BoardController", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
@@ -241,14 +243,16 @@ public static class BoardControllerLibrary32
241243
[DllImport ("BoardController32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
242244
public static extern int log_message_board_controller (int log_level, string message);
243245
[DllImport ("BoardController32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
244-
public static extern int config_board (string config, byte[] response, int[] len, int board_id, string input_json);
246+
public static extern int config_board (string config, byte[] response, int[] len, int max_len, int board_id, string input_json);
245247
[DllImport ("BoardController32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
246248
public static extern int config_board_with_bytes (byte[] bytes, int len, int board_id, string input_json);
247249
[DllImport ("BoardController32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
248250
public static extern int set_log_file_board_controller (string log_file);
249251
[DllImport ("BoardController32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
250252
public static extern int get_sampling_rate (int board_id, int preset, int[] sampling_rate);
251253
[DllImport ("BoardController32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
254+
public static extern int get_board_sampling_rate (int preset, int[] sampling_rate, int board_id, string input_json);
255+
[DllImport ("BoardController32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
252256
public static extern int get_timestamp_channel (int board_id, int preset, int[] timestamp_channel);
253257
[DllImport ("BoardController32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
254258
public static extern int get_marker_channel (int board_id, int preset, int[] marker_channel);
@@ -285,17 +289,17 @@ public static class BoardControllerLibrary32
285289
[DllImport ("BoardController32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
286290
public static extern int is_prepared (int[] prepared, int board_id, string input_json);
287291
[DllImport ("BoardController32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
288-
public static extern int get_eeg_names (int board_id, int preset, byte[] eeg_names, int[] len);
292+
public static extern int get_eeg_names (int board_id, int preset, byte[] eeg_names, int[] len, int max_len);
289293
[DllImport ("BoardController32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
290294
public static extern int get_resistance_channels (int board_id, int preset, int[] channels, int[] len);
291295
[DllImport ("BoardController32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
292296
public static extern int get_exg_channels (int board_id, int preset, int[] channels, int[] len);
293297
[DllImport ("BoardController32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
294-
public static extern int get_device_name (int board_id, int preset, byte[] name, int[] len);
298+
public static extern int get_device_name (int board_id, int preset, byte[] name, int[] len, int max_len);
295299
[DllImport ("BoardController32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
296300
public static extern int insert_marker (double value, int preset, int board_id, string input_json);
297301
[DllImport ("BoardController32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
298-
public static extern int get_board_descr (int board_id, int preset, byte[] board_descr, int[] len);
302+
public static extern int get_board_descr (int board_id, int preset, byte[] board_descr, int[] len, int max_len);
299303
[DllImport ("BoardController32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
300304
public static extern int release_all_sessions ();
301305
[DllImport ("BoardController32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
@@ -457,14 +461,14 @@ public static int insert_marker (double value, int preset, int board_id, string
457461
}
458462

459463

460-
public static int config_board (string config, byte[] str, int[] len, int board_id, string input_json)
464+
public static int config_board (string config, byte[] str, int[] len, int max_len, int board_id, string input_json)
461465
{
462466
switch (PlatformHelper.get_library_environment ())
463467
{
464468
case LibraryEnvironment.x64:
465-
return BoardControllerLibrary64.config_board (config, str, len, board_id, input_json);
469+
return BoardControllerLibrary64.config_board (config, str, len, max_len, board_id, input_json);
466470
case LibraryEnvironment.x86:
467-
return BoardControllerLibrary32.config_board (config, str, len, board_id, input_json);
471+
return BoardControllerLibrary32.config_board (config, str, len, max_len, board_id, input_json);
468472
}
469473

470474
return (int)BrainFlowExitCodes.GENERAL_ERROR;
@@ -535,6 +539,19 @@ public static int get_sampling_rate (int board_id, int preset, int[] sampling_ra
535539
return (int)BrainFlowExitCodes.GENERAL_ERROR;
536540
}
537541

542+
public static int get_board_sampling_rate (int preset, int[] sampling_rate, int board_id, string input_json)
543+
{
544+
switch (PlatformHelper.get_library_environment ())
545+
{
546+
case LibraryEnvironment.x64:
547+
return BoardControllerLibrary64.get_board_sampling_rate (preset, sampling_rate, board_id, input_json);
548+
case LibraryEnvironment.x86:
549+
return BoardControllerLibrary32.get_board_sampling_rate (preset, sampling_rate, board_id, input_json);
550+
}
551+
552+
return (int)BrainFlowExitCodes.GENERAL_ERROR;
553+
}
554+
538555
public static int get_package_num_channel (int board_id, int preset, int[] package_num)
539556
{
540557
switch (PlatformHelper.get_library_environment ())
@@ -600,14 +617,14 @@ public static int get_marker_channel (int board_id, int preset, int[] marker_cha
600617
return (int)BrainFlowExitCodes.GENERAL_ERROR;
601618
}
602619

603-
public static int get_eeg_names (int board_id, int preset, byte[] names, int[] len)
620+
public static int get_eeg_names (int board_id, int preset, byte[] names, int[] len, int max_len)
604621
{
605622
switch (PlatformHelper.get_library_environment ())
606623
{
607624
case LibraryEnvironment.x64:
608-
return BoardControllerLibrary64.get_eeg_names (board_id, preset, names, len);
625+
return BoardControllerLibrary64.get_eeg_names (board_id, preset, names, len, max_len);
609626
case LibraryEnvironment.x86:
610-
return BoardControllerLibrary32.get_eeg_names (board_id, preset, names, len);
627+
return BoardControllerLibrary32.get_eeg_names (board_id, preset, names, len, max_len);
611628
}
612629

613630
return (int)BrainFlowExitCodes.GENERAL_ERROR;
@@ -626,27 +643,27 @@ public static int get_board_presets (int board_id, int[] names, int[] len)
626643
return (int)BrainFlowExitCodes.GENERAL_ERROR;
627644
}
628645

629-
public static int get_board_descr (int board_id, int preset, byte[] descr, int[] len)
646+
public static int get_board_descr (int board_id, int preset, byte[] descr, int[] len, int max_len)
630647
{
631648
switch (PlatformHelper.get_library_environment ())
632649
{
633650
case LibraryEnvironment.x64:
634-
return BoardControllerLibrary64.get_board_descr (board_id, preset, descr, len);
651+
return BoardControllerLibrary64.get_board_descr (board_id, preset, descr, len, max_len);
635652
case LibraryEnvironment.x86:
636-
return BoardControllerLibrary32.get_board_descr (board_id, preset, descr, len);
653+
return BoardControllerLibrary32.get_board_descr (board_id, preset, descr, len, max_len);
637654
}
638655

639656
return (int)BrainFlowExitCodes.GENERAL_ERROR;
640657
}
641658

642-
public static int get_device_name (int board_id, int preset, byte[] name, int[] len)
659+
public static int get_device_name (int board_id, int preset, byte[] name, int[] len, int max_len)
643660
{
644661
switch (PlatformHelper.get_library_environment ())
645662
{
646663
case LibraryEnvironment.x64:
647-
return BoardControllerLibrary64.get_device_name (board_id, preset, name, len);
664+
return BoardControllerLibrary64.get_device_name (board_id, preset, name, len, max_len);
648665
case LibraryEnvironment.x86:
649-
return BoardControllerLibrary32.get_device_name (board_id, preset, name, len);
666+
return BoardControllerLibrary32.get_device_name (board_id, preset, name, len, max_len);
650667
}
651668

652669
return (int)BrainFlowExitCodes.GENERAL_ERROR;

csharp_package/brainflow/brainflow/board_shim.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public static string[] get_eeg_names (int board_id, int preset = (int)BrainFlowP
179179
{
180180
int[] len = new int[1];
181181
byte[] str = new byte[4096];
182-
int res = BoardControllerLibrary.get_eeg_names (board_id, preset, str, len);
182+
int res = BoardControllerLibrary.get_eeg_names (board_id, preset, str, len, str.Length);
183183
if (res != (int)BrainFlowExitCodes.STATUS_OK)
184184
{
185185
throw new BrainFlowError (res);
@@ -222,7 +222,7 @@ public static T get_board_descr<T> (int board_id, int preset = (int)BrainFlowPre
222222
{
223223
int[] len = new int[1];
224224
byte[] str = new byte[16000];
225-
int res = BoardControllerLibrary.get_board_descr (board_id, preset, str, len);
225+
int res = BoardControllerLibrary.get_board_descr (board_id, preset, str, len, str.Length);
226226
if (res != (int)BrainFlowExitCodes.STATUS_OK)
227227
{
228228
throw new BrainFlowError (res);
@@ -246,7 +246,7 @@ public static string get_device_name (int board_id, int preset = (int)BrainFlowP
246246
{
247247
int[] len = new int[1];
248248
byte[] str = new byte[4096];
249-
int res = BoardControllerLibrary.get_device_name (board_id, preset, str, len);
249+
int res = BoardControllerLibrary.get_device_name (board_id, preset, str, len, str.Length);
250250
if (res != (int)BrainFlowExitCodes.STATUS_OK)
251251
{
252252
throw new BrainFlowError (res);
@@ -717,7 +717,7 @@ public string config_board (string config)
717717
{
718718
int[] len = new int[1];
719719
byte[] str = new byte[4096];
720-
int res = BoardControllerLibrary.config_board (config, str, len, board_id, input_json);
720+
int res = BoardControllerLibrary.config_board (config, str, len, str.Length, board_id, input_json);
721721
if (res != (int)BrainFlowExitCodes.STATUS_OK)
722722
{
723723
throw new BrainFlowError (res);
@@ -839,6 +839,22 @@ public int get_board_id ()
839839
return master_board;
840840
}
841841

842+
/// <summary>
843+
/// get actual sampling rate for this prepared board session
844+
/// </summary>
845+
/// <param name="preset">preset for device</param>
846+
/// <returns>sampling rate</returns>
847+
public int get_board_sampling_rate (int preset = (int)BrainFlowPresets.DEFAULT_PRESET)
848+
{
849+
int[] val = new int[1];
850+
int res = BoardControllerLibrary.get_board_sampling_rate (preset, val, board_id, input_json);
851+
if (res != (int)BrainFlowExitCodes.STATUS_OK)
852+
{
853+
throw new BrainFlowError (res);
854+
}
855+
return val[0];
856+
}
857+
842858
///<summary>
843859
/// Get input params
844860
///</summary>

0 commit comments

Comments
 (0)