Skip to content

Commit 70e18d7

Browse files
authored
Added the the get gain command (#735)
* Added the the get gain command
1 parent b92a1c3 commit 70e18d7

File tree

2 files changed

+42
-29
lines changed

2 files changed

+42
-29
lines changed

src/board_controller/brainalive/brainalive.cpp

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ static void brainalive_adapter_1_on_scan_found (
2121
static void brainalive_read_notifications (simpleble_uuid_t service,
2222
simpleble_uuid_t characteristic, uint8_t *data, size_t size, void *board)
2323
{
24-
if (size == BrainAlive::brainalive_handshaking_packet_size)
24+
if ((size == BrainAlive::brainalive_handshaking_packet_size) && (data[0] == START_BYTE) &&
25+
(data[size - 1] == STOP_BYTE) && (data[2] == BrainAlive::brainalive_handshaking_command))
2526
{
26-
((BrainAlive *)(board))->setSoftwareGain (data[1]);
27-
((BrainAlive *)(board))->setHardwareGain (data[2]);
28-
((BrainAlive *)(board))->setReferenceVoltage (((data[3] << 8) | data[4]));
27+
((BrainAlive *)(board))->set_internal_gain (data[3]);
28+
((BrainAlive *)(board))->set_external_gain (data[4]);
29+
((BrainAlive *)(board))->set_ref_Voltage (((data[5] << 8) | data[6]));
2930
}
3031
else
3132
{
@@ -113,6 +114,7 @@ int BrainAlive::prepare_session ()
113114
{
114115
safe_logger (spdlog::level::info, "Connected to BrainAlive Device");
115116
res = (int)BrainFlowExitCodes::STATUS_OK;
117+
116118
break;
117119
}
118120
else
@@ -190,6 +192,11 @@ int BrainAlive::prepare_session ()
190192
if ((res == (int)BrainFlowExitCodes::STATUS_OK) && (control_characteristics_found))
191193
{
192194
initialized = true;
195+
res = config_board ("0a036007000d");
196+
if (res == (int)BrainFlowExitCodes::STATUS_OK)
197+
safe_logger (spdlog::level::debug, "Get config command send");
198+
else
199+
safe_logger (spdlog::level::debug, "Failed to send get config command");
193200
}
194201
else
195202
{
@@ -208,7 +215,7 @@ int BrainAlive::start_stream (int buffer_size, const char *streamer_params)
208215

209216
if (res == (int)BrainFlowExitCodes::STATUS_OK)
210217
{
211-
res = config_board ("0a8100000d");
218+
res = config_board ("0a038100000d");
212219
}
213220
if (res == (int)BrainFlowExitCodes::STATUS_OK)
214221
{
@@ -228,7 +235,7 @@ int BrainAlive::stop_stream ()
228235
int res = (int)BrainFlowExitCodes::STATUS_OK;
229236
if (is_streaming)
230237
{
231-
res = config_board ("0a4000000d");
238+
res = config_board ("0a034000000d");
232239
}
233240
else
234241
{
@@ -302,15 +309,20 @@ int BrainAlive::config_board (std::string config)
302309
{
303310
return (int)BrainFlowExitCodes::BOARD_NOT_CREATED_ERROR;
304311
}
305-
uint8_t command[5];
306-
command[0] = 0x0a;
307-
command[1] = 0x81; // it is hardcoded for now only
308-
command[2] = 0x00;
309-
command[3] = 0x00;
310-
command[4] = 0x0d;
311-
safe_logger (spdlog::level::trace, config[2]);
312+
// Calculate the number of bytes
313+
size_t num_bytes = config.length () / 2; // Each byte is represented by 2 hex characters
314+
std::vector<uint8_t> byte_array (num_bytes);
315+
// Convert hex string to byte array
316+
for (size_t i = 0; i < num_bytes; ++i)
317+
{
318+
std::string byte_string = config.substr (i * 2, 2); // Get 2 characters for each byte
319+
byte_array[i] =
320+
static_cast<unsigned char> (std::stoul (byte_string, nullptr, 16)); // Convert to byte
321+
}
322+
312323
if (simpleble_peripheral_write_command (brainalive_peripheral, write_characteristics.first,
313-
write_characteristics.second, command, sizeof (command)) != SIMPLEBLE_SUCCESS)
324+
write_characteristics.second, byte_array.data (),
325+
sizeof (byte_array)) != SIMPLEBLE_SUCCESS)
314326
{
315327
safe_logger (spdlog::level::err, "failed to send command {} to device", config.c_str ());
316328
return (int)BrainFlowExitCodes::BOARD_WRITE_ERROR;
@@ -394,8 +406,8 @@ void BrainAlive::read_data (simpleble_uuid_t service, simpleble_uuid_t character
394406
{
395407
package[eeg_channels[k]] =
396408
(float)(((data[j] << 16 | data[j + 1] << 8 | data[j + 2]) << 8) >> 8) *
397-
((((float)getReferenceVoltage () * 1000) /
398-
(float)(getSoftwareGain () * getHardwareGain () * FSR_Value)));
409+
((((float)get_ref_voltage () * 1000) /
410+
(float)(get_internal_gain () * get_external_gain () * FSR_Value)));
399411
}
400412

401413
for (int j = i + brainalive_axl_start_index, k = 0; j < i + brainalive_axl_end_index;

src/board_controller/brainalive/inc/brainalive.h

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class BrainAlive : public BLELibBoard
1010
{
1111

1212
private:
13-
int software_gain = 0;
14-
int hardware_gain = 0;
13+
int internal_gain = 0;
14+
int external_gain = 0;
1515
int reference_voltage = 0;
1616

1717
public:
@@ -28,32 +28,32 @@ class BrainAlive : public BLELibBoard
2828
void adapter_1_on_scan_found (simpleble_adapter_t adapter, simpleble_peripheral_t peripheral);
2929
void read_data (simpleble_uuid_t service, simpleble_uuid_t characteristic, uint8_t *data,
3030
size_t size, int channel_num);
31-
void setSoftwareGain (int gain)
31+
void set_internal_gain (int gain)
3232
{
33-
software_gain = gain;
33+
internal_gain = gain;
3434
}
3535

36-
void setHardwareGain (int gain)
36+
void set_external_gain (int gain)
3737
{
38-
hardware_gain = gain;
38+
external_gain = gain;
3939
}
4040

41-
void setReferenceVoltage (int voltage)
41+
void set_ref_Voltage (int voltage)
4242
{
4343
reference_voltage = voltage;
4444
}
4545

46-
int getSoftwareGain () const
46+
int get_internal_gain () const
4747
{
48-
return software_gain;
48+
return internal_gain;
4949
}
5050

51-
int getHardwareGain () const
51+
int get_external_gain () const
5252
{
53-
return hardware_gain;
53+
return external_gain;
5454
}
5555

56-
int getReferenceVoltage () const
56+
int get_ref_voltage () const
5757
{
5858
return reference_voltage;
5959
}
@@ -81,7 +81,8 @@ class BrainAlive : public BLELibBoard
8181
static constexpr int FSR_Value = 8388607;
8282
static constexpr int ba_brainflow_package_size = 17;
8383

84-
static constexpr int brainalive_handshaking_packet_size = 6;
84+
static constexpr int brainalive_handshaking_packet_size = 8;
85+
static constexpr int brainalive_handshaking_command = 7;
8586

8687

8788
protected:

0 commit comments

Comments
 (0)