Skip to content

Commit 21a41d9

Browse files
committed
add get_board_sampling_rate method
Signed-off-by: Andrey Parfenov <a1994ndrey@gmail.com>
1 parent 7c53fc6 commit 21a41d9

30 files changed

Lines changed: 409 additions & 15 deletions

File tree

cpp_package/src/board_shim.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,18 @@ int BoardShim::get_board_id ()
282282
return master_board_id;
283283
}
284284

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+
285297
//////////////////////////////////////////
286298
///////////// data desc methods //////////
287299
//////////////////////////////////////////

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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ public static class BoardControllerLibrary64
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);
@@ -249,6 +251,8 @@ public static class BoardControllerLibrary32
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);
@@ -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 ())

csharp_package/brainflow/brainflow/board_shim.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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>

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ int get_current_board_data (int num_samples, int preset, double[] data_buf, int[
5656
int java_set_jnienv (JNIEnv java_jnienv);
5757

5858
int get_sampling_rate (int board_id, int preset, int[] sampling_rate);
59+
int get_board_sampling_rate (int preset, int[] sampling_rate, int board_id, String params);
5960

6061
int get_battery_channel (int board_id, int preset, int[] battery_channel);
6162

@@ -1359,6 +1360,28 @@ public int get_board_id ()
13591360
return master_board_id;
13601361
}
13611362

1363+
/**
1364+
* get actual sampling rate for this prepared board session
1365+
*/
1366+
public int get_board_sampling_rate (BrainFlowPresets preset) throws BrainFlowError
1367+
{
1368+
int[] res = new int[1];
1369+
int ec = instance.get_board_sampling_rate (preset.get_code (), res, board_id, input_json);
1370+
if (ec != BrainFlowExitCode.STATUS_OK.get_code ())
1371+
{
1372+
throw new BrainFlowError ("Error in get_board_sampling_rate", ec);
1373+
}
1374+
return res[0];
1375+
}
1376+
1377+
/**
1378+
* get actual sampling rate for this prepared board session
1379+
*/
1380+
public int get_board_sampling_rate () throws BrainFlowError
1381+
{
1382+
return get_board_sampling_rate (BrainFlowPresets.DEFAULT_PRESET);
1383+
}
1384+
13621385
/**
13631386
* add streamer
13641387
*/

julia_package/brainflow/src/board_shim.jl

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ end
136136
@brainflow_rethrow function get_eeg_names(board_id::BoardIdType, preset::PresetType=Integer(DEFAULT_PRESET))
137137
names_string = Vector{Cuchar}(undef, 4096)
138138
len = Vector{Cint}(undef, 1)
139-
ccall((:get_eeg_names, BOARD_CONTROLLER_INTERFACE), Cint, (Cint, Cint, Ptr{UInt8}, Ptr{Cint}), Int32(board_id), Int32(preset), names_string, len)
139+
ccall((:get_eeg_names, BOARD_CONTROLLER_INTERFACE), Cint, (Cint, Cint, Ptr{UInt8}, Ptr{Cint}, Cint),
140+
Int32(board_id), Int32(preset), names_string, len, length(names_string))
140141
sub_string = String(names_string)[1:len[1]]
141142
value = split(sub_string, ',')
142143
return value
@@ -145,7 +146,8 @@ end
145146
@brainflow_rethrow function get_board_descr(board_id::BoardIdType, preset::PresetType=Integer(DEFAULT_PRESET))
146147
names_string = Vector{Cuchar}(undef, 16000)
147148
len = Vector{Cint}(undef, 1)
148-
ccall((:get_board_descr, BOARD_CONTROLLER_INTERFACE), Cint, (Cint, Cint, Ptr{UInt8}, Ptr{Cint}), Int32(board_id), Int32(preset), names_string, len)
149+
ccall((:get_board_descr, BOARD_CONTROLLER_INTERFACE), Cint, (Cint, Cint, Ptr{UInt8}, Ptr{Cint}, Cint),
150+
Int32(board_id), Int32(preset), names_string, len, length(names_string))
149151
sub_string = String(names_string)[1:len[1]]
150152
value = JSON.parse(sub_string)
151153
return value
@@ -154,7 +156,8 @@ end
154156
@brainflow_rethrow function get_device_name(board_id::BoardIdType, preset::PresetType=Integer(DEFAULT_PRESET))
155157
names_string = Vector{Cuchar}(undef, 4096)
156158
len = Vector{Cint}(undef, 1)
157-
ccall((:get_device_name, BOARD_CONTROLLER_INTERFACE), Cint, (Cint, Cint, Ptr{UInt8}, Ptr{Cint}), Int32(board_id), Int32(preset), names_string, len)
159+
ccall((:get_device_name, BOARD_CONTROLLER_INTERFACE), Cint, (Cint, Cint, Ptr{UInt8}, Ptr{Cint}, Cint),
160+
Int32(board_id), Int32(preset), names_string, len, length(names_string))
158161
sub_string = String(names_string)[1:len[1]]
159162
return sub_string
160163
end
@@ -239,6 +242,15 @@ end
239242
BoardShim(id::BoardIds, params::BrainFlowInputParams) = BoardShim(Integer(id), params)
240243
BoardShim(id) = BoardShim(id, BrainFlowInputParams())
241244

245+
@brainflow_rethrow function get_board_sampling_rate(board_shim::BoardShim, preset::PresetType=Integer(DEFAULT_PRESET))
246+
val = Vector{Cint}(undef, 1)
247+
ccall((:get_board_sampling_rate, BOARD_CONTROLLER_INTERFACE), Cint,
248+
(Cint, Ptr{Cint}, Cint, Ptr{UInt8}), Int32(preset), val, board_shim.board_id,
249+
board_shim.input_json)
250+
value = val[1]
251+
return value
252+
end
253+
242254
@brainflow_rethrow function prepare_session(board_shim::BoardShim)
243255
ccall((:prepare_session, BOARD_CONTROLLER_INTERFACE), Cint, (Cint, Ptr{UInt8}), board_shim.board_id, board_shim.input_json)
244256
end
@@ -291,8 +303,8 @@ end
291303
@brainflow_rethrow function config_board(config::String, board_shim::BoardShim)
292304
resp_string = Vector{Cuchar}(undef, 4096)
293305
len = Vector{Cint}(undef, 1)
294-
ccall((:config_board, BOARD_CONTROLLER_INTERFACE), Cint, (Ptr{UInt8}, Ptr{UInt8}, Ptr{Cint}, Cint, Ptr{UInt8}),
295-
config, resp_string, len, board_shim.board_id, board_shim.input_json)
306+
ccall((:config_board, BOARD_CONTROLLER_INTERFACE), Cint, (Ptr{UInt8}, Ptr{UInt8}, Ptr{Cint}, Cint, Cint, Ptr{UInt8}),
307+
config, resp_string, len, length(resp_string), board_shim.board_id, board_shim.input_json)
296308
sub_string = String(resp_string)[1:len[1]]
297309
return sub_string
298310
end
@@ -335,4 +347,4 @@ end
335347
num_samples, Int32(preset), val, data_size, board_shim.board_id, board_shim.input_json)
336348
value = transpose(reshape(val[1:data_size[1] * num_rows], (data_size[1], num_rows)))
337349
return value
338-
end
350+
end

matlab_package/brainflow/BoardShim.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,16 @@ function insert_marker(obj, value, preset)
505505
prepared = boolean(res.value);
506506
end
507507

508+
function sampling_rate = get_board_sampling_rate(obj, preset)
509+
% get actual sampling rate for prepared board session
510+
task_name = 'get_board_sampling_rate';
511+
lib_name = BoardShim.load_lib();
512+
res = libpointer('int32Ptr', 0);
513+
exit_code = calllib(lib_name, task_name, preset, res, obj.board_id, obj.input_params_json);
514+
BoardShim.check_ec(exit_code, task_name);
515+
sampling_rate = res.value;
516+
end
517+
508518
end
509519

510520
end

nodejs_package/brainflow/board_shim.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class BoardControllerDLL extends BoardControllerFunctions
7878
this.releaseSession = this.lib.func(CLike.release_session);
7979
this.stopStream = this.lib.func(CLike.stop_stream);
8080
this.getSamplingRate = this.lib.func(CLike.get_sampling_rate);
81+
this.getBoardSamplingRate = this.lib.func(CLike.get_board_sampling_rate);
8182
this.getPackageNumChannel = this.lib.func(CLike.get_package_num_channel);
8283
this.getTimestampChannel = this.lib.func(CLike.get_timestamp_channel);
8384
this.getMarkerChannel = this.lib.func(CLike.get_marker_channel);
@@ -309,6 +310,18 @@ export class BoardShim
309310
return dataSize[0];
310311
}
311312

313+
public getBoardSamplingRate(preset = BrainFlowPresets.DEFAULT_PRESET): number
314+
{
315+
const samplingRate = [0];
316+
const res = BoardControllerDLL.getInstance().getBoardSamplingRate(
317+
preset, samplingRate, this.boardId, this.inputJson);
318+
if (res !== BrainFlowExitCodes.STATUS_OK)
319+
{
320+
throw new BrainFlowError (res, 'Could not get board sampling rate');
321+
}
322+
return samplingRate[0];
323+
}
324+
312325
public getBoardData(numSamples?: number, preset = BrainFlowPresets.DEFAULT_PRESET): number[][]
313326
{
314327
let dataSize = this.getBoardDataCount(preset);

nodejs_package/brainflow/functions.types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ export enum BoardControllerCLikeFunctions {
5151
get_num_rows = 'int get_num_rows (int board_id, int preset, _Inout_ int *num_rows)',
5252
get_sampling_rate =
5353
'int get_sampling_rate (int board_id, int preset, _Inout_ int *sampling_rate)',
54+
get_board_sampling_rate =
55+
'int get_board_sampling_rate (int preset, _Inout_ int *sampling_rate, int board_id, const char *json_brainflow_input_params)',
5456
get_battery_channel = 'int get_battery_channel (int board_id, int preset, _Inout_ int *value)',
5557
get_package_num_channel =
5658
'int get_package_num_channel (int board_id, int preset, _Inout_ int *value)',
@@ -182,6 +184,12 @@ export class BoardControllerFunctions
182184
boardId: BoardIds, preset: BrainFlowPresets, numRows: number[]) => BrainFlowExitCodes;
183185
getSamplingRate!: (
184186
boardId: BoardIds, preset: BrainFlowPresets, samplingRate: number[]) => BrainFlowExitCodes;
187+
getBoardSamplingRate!: (
188+
preset: BrainFlowPresets,
189+
samplingRate: number[],
190+
boardId: BoardIds,
191+
inputJson: string,
192+
) => BrainFlowExitCodes;
185193
getEegChannels!: (
186194
boardId: BoardIds,
187195
preset: BrainFlowPresets,

python_package/brainflow/board_shim.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,15 @@ def __init__(self):
348348
ndpointer(ctypes.c_int32)
349349
]
350350

351+
self.get_board_sampling_rate = self.lib.get_board_sampling_rate
352+
self.get_board_sampling_rate.restype = ctypes.c_int
353+
self.get_board_sampling_rate.argtypes = [
354+
ctypes.c_int,
355+
ndpointer(ctypes.c_int32),
356+
ctypes.c_int,
357+
ctypes.c_char_p
358+
]
359+
351360
self.get_battery_channel = self.lib.get_battery_channel
352361
self.get_battery_channel.restype = ctypes.c_int
353362
self.get_battery_channel.argtypes = [
@@ -1343,6 +1352,22 @@ def get_board_id(self) -> int:
13431352

13441353
return self._master_board_id
13451354

1355+
def get_board_sampling_rate(self, preset: int = BrainFlowPresets.DEFAULT_PRESET) -> int:
1356+
"""Get actual sampling rate for this prepared board session.
1357+
1358+
:param preset: preset
1359+
:type preset: int
1360+
:return: sampling rate
1361+
:rtype: int
1362+
"""
1363+
1364+
sampling_rate = numpy.zeros(1).astype(numpy.int32)
1365+
res = BoardControllerDLL.get_instance().get_board_sampling_rate(
1366+
preset, sampling_rate, self.board_id, self.input_json)
1367+
if res != BrainFlowExitCodes.STATUS_OK.value:
1368+
raise BrainFlowError('unable to get sampling rate for this board session', res)
1369+
return sampling_rate[0]
1370+
13461371
def insert_marker(self, value: float, preset: int = BrainFlowPresets.DEFAULT_PRESET) -> None:
13471372
"""Insert Marker to Data Stream
13481373

0 commit comments

Comments
 (0)