diff --git a/src/board_controller/brainalive/brainalive.cpp b/src/board_controller/brainalive/brainalive.cpp index 70ccf6adc..fbd95e341 100644 --- a/src/board_controller/brainalive/brainalive.cpp +++ b/src/board_controller/brainalive/brainalive.cpp @@ -4,13 +4,9 @@ #include "timestamp.h" #include - // 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" @@ -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 { @@ -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 eeg_channels = board_descr["default"]["eeg_channels"]; + std::vector accel_channels = board_descr["default"]["accel_channels"]; + std::vector gyro_channels = board_descr["default"]["gyro_channels"]; + + package[board_descr["default"]["package_num_channel"].get ()] = + 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 ()] = + data[(brainalive_packet_index + 1) + i]; + package[board_descr["default"]["timestamp_channel"].get ()] = get_timestamp (); - push_package (&ba_data[0]); + push_package (&package[0]); } } else diff --git a/src/board_controller/brainalive/inc/brainalive.h b/src/board_controller/brainalive/inc/brainalive.h index f6dd825fb..b1800911f 100644 --- a/src/board_controller/brainalive/inc/brainalive.h +++ b/src/board_controller/brainalive/inc/brainalive.h @@ -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 (); @@ -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; @@ -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; diff --git a/src/board_controller/brainflow_boards.cpp b/src/board_controller/brainflow_boards.cpp index bfcee5c59..0057d82d8 100644 --- a/src/board_controller/brainflow_boards.cpp +++ b/src/board_controller/brainflow_boards.cpp @@ -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"] = {