Skip to content

Commit

Permalink
Solve the suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sarthak031 committed Jul 3, 2024
1 parent c9ae582 commit ca57ff1
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 28 deletions.
61 changes: 39 additions & 22 deletions src/board_controller/brainalive/brainalive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@
#include "timestamp.h"
#include <string.h>


// info about services and chars
#define START_BYTE 0x0A
#define STOP_BYTE 0x0D
#define BRAINALIVE_HANDSHAKING_PACKET_SIZE 6

int software_gain, hardware_gain, reffrence_volatage;

#define BRAINALIVE_WRITE_CHAR "0000fe41-8e22-4541-9d4c-21edae82ed19"
#define BRAINALIVE_NOTIFY_CHAR "0000fe42-8e22-4541-9d4c-21edae82ed19"
Expand All @@ -25,11 +21,11 @@ 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_HANDSHAKING_PACKET_SIZE)
if (size == BrainAlive::brainalive_handshaking_packet_size)
{
software_gain = data[1];
hardware_gain = data[2];
reffrence_volatage = (data[3] << 8) | data[4];
((BrainAlive *)(board))->setSoftwareGain (data[1]);
((BrainAlive *)(board))->setHardwareGain (data[2]);
((BrainAlive *)(board))->setReferenceVoltage (((data[3] << 8) | data[4]));
}
else
{
Expand Down Expand Up @@ -379,27 +375,48 @@ void BrainAlive::read_data (simpleble_uuid_t service, simpleble_uuid_t character
{
for (int i = 0; i < (int)size; i += brainalive_single_packet_size)
{
double ba_data[ba_brainflow_package_size] = {0};
int k = 0;
for (int j = i + brainalive_eeg_Start_index; j < i + brainalive_eeg_end_index;

int num_rows = board_descr["default"]["num_rows"];
double *package = new double[num_rows];
for (int i = 0; i < num_rows; i++)
{
package[i] = 0.0;
}
std::vector<int> eeg_channels = board_descr["default"]["eeg_channels"];
std::vector<int> accel_channels = board_descr["default"]["accel_channels"];
std::vector<int> gyro_channels = board_descr["default"]["gyro_channels"];

package[board_descr["default"]["package_num_channel"].get<int> ()] =
data[brainalive_packet_index + i];

for (int j = i + brainalive_eeg_Start_index, k = 0; j < i + brainalive_eeg_end_index;
j += 3, k++)
{
ba_data[k] = (float)(((data[j] << 16 | data[j + 1] << 8 | data[j + 2]) << 8) >> 8) *
((((float)reffrence_volatage * 1000) /
(float)(software_gain * hardware_gain * FSR_Value)));
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)));
}

for (int j = i + brainalive_axl_start_index, k = 0; j < i + brainalive_axl_end_index;
j += 2, k++)
{
package[accel_channels[k]] = (data[j] << 8) | data[j + 1];
if (package[accel_channels[k]] > 32767)
package[accel_channels[k]] = package[accel_channels[k]] - 65535;
}
for (int j = i + brainalive_axl_start_index; j < i + brainalive_axl_end_index;
for (int j = i + brainalive_gyro_start_index, k = 0; j < i + brainalive_gyro_end_index;
j += 2, k++)
{
ba_data[k] = (data[j] << 8) | data[j + 1];
if (ba_data[k] > 32767)
ba_data[k] = ba_data[k] - 65535;
package[gyro_channels[k]] = (data[j] << 8) | data[j + 1];
if (package[gyro_channels[k]] > 32767)
package[gyro_channels[k]] = package[gyro_channels[k]] - 65535;
}
ba_data[14] = data[brainalive_packet_index + i];
ba_data[15] = data[(brainalive_packet_index + 1) + i];
ba_data[16] = get_timestamp ();
package[board_descr["default"]["marker_channel"].get<int> ()] =
data[(brainalive_packet_index + 1) + i];
package[board_descr["default"]["timestamp_channel"].get<int> ()] = get_timestamp ();

push_package (&ba_data[0]);
push_package (&package[0]);
}
}
else
Expand Down
45 changes: 43 additions & 2 deletions src/board_controller/brainalive/inc/brainalive.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

class BrainAlive : public BLELibBoard
{

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

public:
BrainAlive (struct BrainFlowInputParams params);
~BrainAlive ();
Expand All @@ -22,6 +28,35 @@ 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)
{
software_gain = gain;
}

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

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

int getSoftwareGain () const
{
return software_gain;
}

int getHardwareGain () const
{
return hardware_gain;
}

int getReferenceVoltage () const
{
return reference_voltage;
}

// common constants
static constexpr int brainalive_packet_size = 220;
Expand All @@ -35,13 +70,19 @@ class BrainAlive : public BLELibBoard
static constexpr int brainalive_eeg_end_index =
(brainalive_eeg_Start_index + brainalive_eeg_data_szie);

static constexpr int brainalive_axl_gyro_data_size = 12;
static constexpr int brainalive_axl_data_size = 6;
static constexpr int brainalive_gyro_data_size = 6;
static constexpr int brainalive_axl_start_index = brainalive_eeg_end_index;
static constexpr int brainalive_axl_end_index =
brainalive_axl_start_index + brainalive_axl_gyro_data_size;
brainalive_axl_start_index + brainalive_axl_data_size;
static constexpr int brainalive_gyro_start_index = brainalive_axl_end_index;
static constexpr int brainalive_gyro_end_index =
brainalive_gyro_start_index + brainalive_gyro_data_size;
static constexpr int FSR_Value = 8388607;
static constexpr int ba_brainflow_package_size = 17;

static constexpr int brainalive_handshaking_packet_size = 6;


protected:
volatile simpleble_adapter_t brainalive_adapter;
Expand Down
8 changes: 4 additions & 4 deletions src/board_controller/brainflow_boards.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -710,14 +710,14 @@ BrainFlowBoards::BrainFlowBoards()
{
{"name", "BrainAlive"},
{"sampling_rate", 250},
{"package_num_channel", 14},
{"marker_channel", 15},
{"timestamp_channel", 16},
{"num_rows", 17},
{"eeg_channels", {0, 1, 2, 3, 4, 5, 6, 7}},
{"package_num_channel", 0},
{"eeg_channels", {1, 2, 3, 4, 5, 6, 7, 8}},
{"eeg_names", "F7,FT7,T7,CP5,CZ,C6,FC6,F4"},
{"accel_channels", {8, 9, 10}},
{"gyro_channels", {11, 12, 13}}
{"accel_channels", {9, 10, 11}},
{"gyro_channels", {12, 13, 14}}
};
brainflow_boards_json["boards"]["41"]["default"] =
{
Expand Down

0 comments on commit ca57ff1

Please sign in to comment.