@@ -21,11 +21,12 @@ static void brainalive_adapter_1_on_scan_found (
2121static 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;
0 commit comments