Skip to content

Commit

Permalink
Added the the get gain command (#735)
Browse files Browse the repository at this point in the history
* Added the the get gain command
  • Loading branch information
Sarthak031 authored Aug 2, 2024
1 parent b92a1c3 commit 70e18d7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
44 changes: 28 additions & 16 deletions src/board_controller/brainalive/brainalive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ static void brainalive_adapter_1_on_scan_found (
static void brainalive_read_notifications (simpleble_uuid_t service,
simpleble_uuid_t characteristic, uint8_t *data, size_t size, void *board)
{
if (size == BrainAlive::brainalive_handshaking_packet_size)
if ((size == BrainAlive::brainalive_handshaking_packet_size) && (data[0] == START_BYTE) &&
(data[size - 1] == STOP_BYTE) && (data[2] == BrainAlive::brainalive_handshaking_command))
{
((BrainAlive *)(board))->setSoftwareGain (data[1]);
((BrainAlive *)(board))->setHardwareGain (data[2]);
((BrainAlive *)(board))->setReferenceVoltage (((data[3] << 8) | data[4]));
((BrainAlive *)(board))->set_internal_gain (data[3]);
((BrainAlive *)(board))->set_external_gain (data[4]);
((BrainAlive *)(board))->set_ref_Voltage (((data[5] << 8) | data[6]));
}
else
{
Expand Down Expand Up @@ -113,6 +114,7 @@ int BrainAlive::prepare_session ()
{
safe_logger (spdlog::level::info, "Connected to BrainAlive Device");
res = (int)BrainFlowExitCodes::STATUS_OK;

break;
}
else
Expand Down Expand Up @@ -190,6 +192,11 @@ int BrainAlive::prepare_session ()
if ((res == (int)BrainFlowExitCodes::STATUS_OK) && (control_characteristics_found))
{
initialized = true;
res = config_board ("0a036007000d");
if (res == (int)BrainFlowExitCodes::STATUS_OK)
safe_logger (spdlog::level::debug, "Get config command send");
else
safe_logger (spdlog::level::debug, "Failed to send get config command");
}
else
{
Expand All @@ -208,7 +215,7 @@ int BrainAlive::start_stream (int buffer_size, const char *streamer_params)

if (res == (int)BrainFlowExitCodes::STATUS_OK)
{
res = config_board ("0a8100000d");
res = config_board ("0a038100000d");
}
if (res == (int)BrainFlowExitCodes::STATUS_OK)
{
Expand All @@ -228,7 +235,7 @@ int BrainAlive::stop_stream ()
int res = (int)BrainFlowExitCodes::STATUS_OK;
if (is_streaming)
{
res = config_board ("0a4000000d");
res = config_board ("0a034000000d");
}
else
{
Expand Down Expand Up @@ -302,15 +309,20 @@ int BrainAlive::config_board (std::string config)
{
return (int)BrainFlowExitCodes::BOARD_NOT_CREATED_ERROR;
}
uint8_t command[5];
command[0] = 0x0a;
command[1] = 0x81; // it is hardcoded for now only
command[2] = 0x00;
command[3] = 0x00;
command[4] = 0x0d;
safe_logger (spdlog::level::trace, config[2]);
// Calculate the number of bytes
size_t num_bytes = config.length () / 2; // Each byte is represented by 2 hex characters
std::vector<uint8_t> byte_array (num_bytes);
// Convert hex string to byte array
for (size_t i = 0; i < num_bytes; ++i)
{
std::string byte_string = config.substr (i * 2, 2); // Get 2 characters for each byte
byte_array[i] =
static_cast<unsigned char> (std::stoul (byte_string, nullptr, 16)); // Convert to byte
}

if (simpleble_peripheral_write_command (brainalive_peripheral, write_characteristics.first,
write_characteristics.second, command, sizeof (command)) != SIMPLEBLE_SUCCESS)
write_characteristics.second, byte_array.data (),
sizeof (byte_array)) != SIMPLEBLE_SUCCESS)
{
safe_logger (spdlog::level::err, "failed to send command {} to device", config.c_str ());
return (int)BrainFlowExitCodes::BOARD_WRITE_ERROR;
Expand Down Expand Up @@ -394,8 +406,8 @@ void BrainAlive::read_data (simpleble_uuid_t service, simpleble_uuid_t character
{
package[eeg_channels[k]] =
(float)(((data[j] << 16 | data[j + 1] << 8 | data[j + 2]) << 8) >> 8) *
((((float)getReferenceVoltage () * 1000) /
(float)(getSoftwareGain () * getHardwareGain () * FSR_Value)));
((((float)get_ref_voltage () * 1000) /
(float)(get_internal_gain () * get_external_gain () * FSR_Value)));
}

for (int j = i + brainalive_axl_start_index, k = 0; j < i + brainalive_axl_end_index;
Expand Down
27 changes: 14 additions & 13 deletions src/board_controller/brainalive/inc/brainalive.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class BrainAlive : public BLELibBoard
{

private:
int software_gain = 0;
int hardware_gain = 0;
int internal_gain = 0;
int external_gain = 0;
int reference_voltage = 0;

public:
Expand All @@ -28,32 +28,32 @@ class BrainAlive : public BLELibBoard
void adapter_1_on_scan_found (simpleble_adapter_t adapter, simpleble_peripheral_t peripheral);
void read_data (simpleble_uuid_t service, simpleble_uuid_t characteristic, uint8_t *data,
size_t size, int channel_num);
void setSoftwareGain (int gain)
void set_internal_gain (int gain)
{
software_gain = gain;
internal_gain = gain;
}

void setHardwareGain (int gain)
void set_external_gain (int gain)
{
hardware_gain = gain;
external_gain = gain;
}

void setReferenceVoltage (int voltage)
void set_ref_Voltage (int voltage)
{
reference_voltage = voltage;
}

int getSoftwareGain () const
int get_internal_gain () const
{
return software_gain;
return internal_gain;
}

int getHardwareGain () const
int get_external_gain () const
{
return hardware_gain;
return external_gain;
}

int getReferenceVoltage () const
int get_ref_voltage () const
{
return reference_voltage;
}
Expand Down Expand Up @@ -81,7 +81,8 @@ class BrainAlive : public BLELibBoard
static constexpr int FSR_Value = 8388607;
static constexpr int ba_brainflow_package_size = 17;

static constexpr int brainalive_handshaking_packet_size = 6;
static constexpr int brainalive_handshaking_packet_size = 8;
static constexpr int brainalive_handshaking_command = 7;


protected:
Expand Down

0 comments on commit 70e18d7

Please sign in to comment.