Skip to content

Commit 12daeaf

Browse files
committed
Restore Muse Anthena data parsing
1 parent 6c919e9 commit 12daeaf

6 files changed

Lines changed: 742 additions & 185 deletions

File tree

src/board_controller/brainflow_boards.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,23 +1164,35 @@ BrainFlowBoards::BrainFlowBoards()
11641164
{
11651165
{"name", "MuseAnthena"},
11661166
{"sampling_rate", 256},
1167-
{"timestamp_channel", 6},
1168-
{"marker_channel", 7},
1167+
{"timestamp_channel", 9},
1168+
{"marker_channel", 10},
11691169
{"package_num_channel", 0},
1170-
{"num_rows", 8},
1171-
{"eeg_channels", {1, 2, 3, 4}},
1172-
{"eeg_names", "TP9,AF7,AF8,TP10"},
1173-
{"other_channels", {5}}
1170+
{"num_rows", 11},
1171+
{"eeg_channels", {1, 2, 3, 4, 5, 6, 7, 8}},
1172+
{"eeg_names", "TP9,AF7,AF8,TP10,AUX1,AUX2,AUX3,AUX4"}
11741173
};
11751174
brainflow_boards_json["boards"]["67"]["auxiliary"] =
11761175
{
11771176
{"name", "MuseAnthenaAux"},
1177+
{"sampling_rate", 52},
1178+
{"timestamp_channel", 7},
1179+
{"marker_channel", 8},
1180+
{"package_num_channel", 0},
1181+
{"num_rows", 9},
1182+
{"accel_channels", {1, 2, 3}},
1183+
{"gyro_channels", {4, 5, 6}}
1184+
};
1185+
brainflow_boards_json["boards"]["67"]["ancillary"] =
1186+
{
1187+
{"name", "MuseAnthenaAnc"},
11781188
{"sampling_rate", 64},
1179-
{"timestamp_channel", 4},
1180-
{"marker_channel", 5},
1189+
{"timestamp_channel", 21},
1190+
{"marker_channel", 22},
11811191
{"package_num_channel", 0},
1182-
{"num_rows", 6},
1192+
{"num_rows", 23},
11831193
{"ppg_channels", {1, 2, 3}},
1194+
{"optical_channels", {4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}},
1195+
{"battery_channel", 20}
11841196
};
11851197
}
11861198

src/board_controller/muse/inc/muse_anthena.h

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,83 @@
11
#pragma once
22

3+
#include <array>
4+
#include <cstddef>
5+
#include <cstdint>
36
#include <condition_variable>
47
#include <mutex>
8+
#include <string>
59
#include <utility>
610
#include <vector>
711

812
#include "ble_lib_board.h"
13+
#include "muse_anthena_types.h"
914

1015

1116
class MuseAnthena : public BLELibBoard
1217
{
1318

1419
protected:
20+
static const size_t PACKET_HEADER_SIZE = 14;
21+
static const size_t SUBPACKET_HEADER_SIZE = 5;
22+
23+
struct SensorConfig
24+
{
25+
SensorType type;
26+
int n_channels;
27+
int n_samples;
28+
double sampling_rate;
29+
size_t data_len;
30+
bool variable_length;
31+
32+
SensorConfig ();
33+
SensorConfig (SensorType type, int n_channels, int n_samples, double sampling_rate,
34+
size_t data_len, bool variable_length = false);
35+
};
36+
1537
volatile simpleble_adapter_t muse_adapter;
1638
volatile simpleble_peripheral_t muse_peripheral;
1739
bool initialized;
1840
bool is_streaming;
1941
std::mutex m;
42+
std::mutex callback_lock;
2043
std::condition_variable cv;
2144
std::vector<std::pair<simpleble_uuid_t, simpleble_uuid_t>> notified_characteristics;
2245
std::pair<simpleble_uuid_t, simpleble_uuid_t> control_characteristics;
23-
int last_sensor_packet_id;
24-
int last_status_packet_id;
25-
int last_traditional_packet_id;
46+
bool timestamp_initialized;
47+
uint32_t first_device_tick;
48+
double first_host_timestamp;
49+
double last_battery;
50+
std::string muse_preset;
51+
bool enable_low_latency;
2652

53+
static std::string trim_string (const std::string &value);
54+
static std::string to_lower (const std::string &value);
55+
static bool is_valid_muse_preset (const std::string &preset);
56+
static bool parse_bool_option (const std::string &value, bool &parsed);
57+
int parse_muse_options ();
2758
std::string bytes_to_string (const uint8_t *data, size_t size);
28-
29-
void parse_main_packet (const uint8_t *data, size_t start_pos, size_t size);
30-
void parse_sensor_packet (const uint8_t *data, size_t start_pos, size_t size);
59+
void handle_data_notification (const uint8_t *data, size_t size);
60+
void parse_sensor_payload (
61+
uint8_t tag, uint8_t sequence_num, uint32_t device_tick, const uint8_t *data, size_t size);
62+
bool get_sensor_config (uint8_t tag, SensorConfig &config);
63+
int get_optics_canonical_index (uint8_t tag, int channel);
64+
double average_present (const std::array<double, 16> &values,
65+
const std::array<bool, 16> &present, const int *indexes, int len);
66+
double get_sample_timestamp (uint32_t device_tick, int sample_index, double sampling_rate);
3167

3268
public:
3369
MuseAnthena (int board_id, struct BrainFlowInputParams params);
34-
~MuseAnthena ();
70+
~MuseAnthena () override;
3571

36-
int prepare_session ();
37-
int start_stream (int buffer_size, const char *streamer_params);
38-
int stop_stream ();
39-
int release_session ();
40-
int config_board (std::string config, std::string &response);
72+
int prepare_session () override;
73+
int start_stream (int buffer_size, const char *streamer_params) override;
74+
int stop_stream () override;
75+
int release_session () override;
76+
int config_board (std::string config, std::string &response) override;
4177
int config_board (std::string config);
4278

4379
void adapter_on_scan_found (simpleble_adapter_t adapter, simpleble_peripheral_t peripheral);
44-
void peripheral_on_main (simpleble_peripheral_t peripheral, simpleble_uuid_t service,
45-
simpleble_uuid_t characteristic, const uint8_t *data, size_t size);
46-
void peripheral_on_aux (simpleble_peripheral_t peripheral, simpleble_uuid_t service,
80+
void peripheral_on_data (simpleble_peripheral_t peripheral, simpleble_uuid_t service,
4781
simpleble_uuid_t characteristic, const uint8_t *data, size_t size);
4882
void peripheral_on_status (simpleble_peripheral_t peripheral, simpleble_uuid_t service,
4983
simpleble_uuid_t characteristic, const uint8_t *data, size_t size);

src/board_controller/muse/inc/muse_anthena_constants.h

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,12 @@
55
#define MUSE_ANTHENA_SERVICE_UUID 0xFE8D
66

77
#define MUSE_ANTHENA_GATT_ATTR_STREAM_TOGGLE "273e0001-4c4d-454d-96be-f03bac821358"
8-
#define MUSE_ANTHENA_GATT_EEG "273e0013-4c4d-454d-96be-f03bac821358"
9-
#define MUSE_ANTHENA_GATT_AUX "273e0014-4c4d-454d-96be-f03bac821358"
8+
#define MUSE_ANTHENA_GATT_DATA_1 "273e0013-4c4d-454d-96be-f03bac821358"
9+
#define MUSE_ANTHENA_GATT_DATA_2 "273e0014-4c4d-454d-96be-f03bac821358"
1010

11-
// info about packet type
12-
#define MUSE_S_ANTHENA_EEG_FOUR_PACKET_TYPE 0x11
13-
#define MUSE_S_ANTHENA_EEG_EIGHT_PACKET_TYPE 0x12
14-
#define MUSE_S_ANTHENA_OPTICS_FOUR_PACKET_TYPE 0x34
15-
#define MUSE_S_ANTHENA_OPTICS_EIGHT_PACKET_TYPE 0x35
16-
#define MUSE_S_ANTHENA_OPTICS_SIXTEEN_PACKET_TYPE 0x36
17-
#define MUSE_S_ANTHENA_SENSOR_PACKET_TYPE 0x47
18-
#define MUSE_S_ANTHENA_STATUS_PACKET_TYPE 0x53
19-
#define MUSE_S_ANTHENA_BATTERY_PACKET_TYPE 0x98
20-
21-
#define MUSE_S_ANTHENA_EEG_FOUR_PACKET_SIZE 32
22-
#define MUSE_S_ANTHENA_EEG_EIGHT_PACKET_SIZE 32
23-
#define MUSE_S_ANTHENA_MAIN_PACKET_SIZE MUSE_S_ANTHENA_EEG_FOUR_PACKET_SIZE
24-
#define MUSE_S_ANTHENA_TRADITIONAL_PACKET_SIZE MUSE_S_ANTHENA_EEG_EIGHT_PACKET_SIZE
25-
#define MUSE_S_ANTHENA_OPTICS_FOUR_PACKET_SIZE 34
26-
#define MUSE_S_ANTHENA_OPTICS_EIGHT_PACKET_SIZE 44
27-
#define MUSE_S_ANTHENA_OPTICS_SIXTEEN_PACKET_SIZE 44
28-
#define MUSE_S_ANTHENA_SENSOR_PACKET_SIZE 40
29-
#define MUSE_S_ANTHENA_STATUS_PACKET_SIZE 28
11+
// info for equations
12+
#define MUSE_ANTHENA_ACCELEROMETER_SCALE_FACTOR 0.0000610352
13+
#define MUSE_ANTHENA_GYRO_SCALE_FACTOR -0.0074768
14+
#define MUSE_ANTHENA_DEVICE_CLOCK_HZ 256000.0
15+
#define MUSE_ANTHENA_EEG_SCALE_FACTOR (1450.0 / 16383.0)
16+
#define MUSE_ANTHENA_OPTICS_SCALE_FACTOR (1.0 / 32768.0)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
4+
enum class SensorType
5+
{
6+
EEG,
7+
ACC_GYRO,
8+
OPTICS,
9+
BATTERY,
10+
UNKNOWN
11+
};

0 commit comments

Comments
 (0)