Skip to content

Commit 702dd06

Browse files
authored
cleanup ppg for muse and add status check (#400)
* fix ppg data for muse devices
1 parent 769c2a5 commit 702dd06

File tree

4 files changed

+79
-58
lines changed

4 files changed

+79
-58
lines changed

src/board_controller/muse/inc/muse.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Muse : public BLELibBoard
1616
bool initialized;
1717
bool is_streaming;
1818
std::mutex m;
19+
std::mutex callback_lock;
1920
std::condition_variable cv;
2021
std::vector<std::pair<simpleble_uuid_t, simpleble_uuid_t>> notified_characteristics;
2122
std::pair<simpleble_uuid_t, simpleble_uuid_t> control_characteristics;
@@ -24,8 +25,9 @@ class Muse : public BLELibBoard
2425
double last_timestamp;
2526
int current_accel_pos;
2627
int current_gyro_pos;
27-
28-
std::mutex callback_lock;
28+
int current_ppg_pos[3];
29+
std::string fw_version;
30+
std::string status_string;
2931

3032
public:
3133
Muse (int board_id, struct BrainFlowInputParams params);
@@ -42,13 +44,11 @@ class Muse : public BLELibBoard
4244
void peripheral_on_eeg (simpleble_uuid_t service, simpleble_uuid_t characteristic,
4345
uint8_t *data, size_t size, size_t channel_num);
4446
void peripheral_on_ppg (simpleble_uuid_t service, simpleble_uuid_t characteristic,
45-
uint8_t *data, size_t size, size_t ppg_num, int *ppg_pos);
47+
uint8_t *data, size_t size, size_t ppg_num);
4648
void peripheral_on_accel (
4749
simpleble_uuid_t service, simpleble_uuid_t characteristic, uint8_t *data, size_t size);
4850
void peripheral_on_gyro (
4951
simpleble_uuid_t service, simpleble_uuid_t characteristic, uint8_t *data, size_t size);
50-
51-
int current_ppg_pos0;
52-
int current_ppg_pos1;
53-
int current_ppg_pos2;
52+
void peripheral_on_status (
53+
simpleble_uuid_t service, simpleble_uuid_t characteristic, uint8_t *data, size_t size);
5454
};

src/board_controller/muse/muse.cpp

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <algorithm>
2+
#include <regex>
23

34
#include "custom_cast.h"
45
#include "muse.h"
@@ -50,25 +51,25 @@ void peripheral_on_gyro (simpleble_uuid_t service, simpleble_uuid_t characterist
5051
void peripheral_on_ppg0 (simpleble_uuid_t service, simpleble_uuid_t characteristic, uint8_t *data,
5152
size_t size, void *board)
5253
{
53-
Muse *muse_board = (Muse *)board;
54-
muse_board->peripheral_on_ppg (
55-
service, characteristic, data, size, 0, &muse_board->current_ppg_pos0);
54+
((Muse *)(board))->peripheral_on_ppg (service, characteristic, data, size, 0);
5655
}
5756

5857
void peripheral_on_ppg1 (simpleble_uuid_t service, simpleble_uuid_t characteristic, uint8_t *data,
5958
size_t size, void *board)
6059
{
61-
Muse *muse_board = (Muse *)board;
62-
muse_board->peripheral_on_ppg (
63-
service, characteristic, data, size, 1, &muse_board->current_ppg_pos1);
60+
((Muse *)(board))->peripheral_on_ppg (service, characteristic, data, size, 1);
6461
}
6562

6663
void peripheral_on_ppg2 (simpleble_uuid_t service, simpleble_uuid_t characteristic, uint8_t *data,
6764
size_t size, void *board)
6865
{
69-
Muse *muse_board = (Muse *)board;
70-
muse_board->peripheral_on_ppg (
71-
service, characteristic, data, size, 2, &muse_board->current_ppg_pos2);
66+
((Muse *)(board))->peripheral_on_ppg (service, characteristic, data, size, 2);
67+
}
68+
69+
void peripheral_on_status (simpleble_uuid_t service, simpleble_uuid_t characteristic, uint8_t *data,
70+
size_t size, void *board)
71+
{
72+
((Muse *)(board))->peripheral_on_status (service, characteristic, data, size);
7273
}
7374

7475

@@ -80,9 +81,9 @@ Muse::Muse (int board_id, struct BrainFlowInputParams params) : BLELibBoard (boa
8081
is_streaming = false;
8182
current_accel_pos = 0;
8283
current_gyro_pos = 0;
83-
current_ppg_pos0 = 0;
84-
current_ppg_pos1 = 0;
85-
current_ppg_pos2 = 0;
84+
memset (current_ppg_pos, 0, sizeof (current_ppg_pos));
85+
fw_version = "";
86+
status_string = "";
8687
}
8788

8889
Muse::~Muse ()
@@ -184,6 +185,20 @@ int Muse::prepare_session ()
184185
service.uuid, service.characteristics[j]);
185186
control_characteristics_found = true;
186187
safe_logger (spdlog::level::info, "found control characteristic");
188+
if (simpleble_peripheral_notify (muse_peripheral, service.uuid,
189+
service.characteristics[j], ::peripheral_on_status,
190+
(void *)this) == SIMPLEBLE_SUCCESS)
191+
{
192+
notified_characteristics.push_back (
193+
std::pair<simpleble_uuid_t, simpleble_uuid_t> (
194+
service.uuid, service.characteristics[j]));
195+
}
196+
else
197+
{
198+
safe_logger (spdlog::level::err, "Failed to notify for {} {}",
199+
service.uuid.value, service.characteristics[j].value);
200+
res = (int)BrainFlowExitCodes::GENERAL_ERROR;
201+
}
187202
}
188203
if (strcmp (service.characteristics[j].value, MUSE_GATT_ATTR_TP9) == 0)
189204
{
@@ -349,9 +364,7 @@ int Muse::prepare_session ()
349364
new_eeg_data.resize (4); // 4 eeg channels total
350365
current_gyro_pos = 0;
351366
current_accel_pos = 0;
352-
current_ppg_pos0 = 0;
353-
current_ppg_pos1 = 0;
354-
current_ppg_pos2 = 0;
367+
memset (current_ppg_pos, 0, sizeof (current_ppg_pos));
355368
for (int i = 0; i < 12; i++)
356369
{
357370
current_buf[i].resize (buffer_size);
@@ -362,6 +375,10 @@ int Muse::prepare_session ()
362375
initialized = true;
363376
}
364377

378+
if (res == (int)BrainFlowExitCodes::STATUS_OK)
379+
{
380+
res = config_board ("v1");
381+
}
365382
if (res == (int)BrainFlowExitCodes::STATUS_OK)
366383
{
367384
res = config_board ("p21");
@@ -458,9 +475,7 @@ int Muse::release_session ()
458475
new_eeg_data.clear ();
459476
current_gyro_pos = 0;
460477
current_accel_pos = 0;
461-
current_ppg_pos0 = 0;
462-
current_ppg_pos1 = 0;
463-
current_ppg_pos2 = 0;
478+
memset (current_ppg_pos, 0, sizeof (current_ppg_pos));
464479

465480
return (int)BrainFlowExitCodes::STATUS_OK;
466481
}
@@ -662,7 +677,7 @@ void Muse::peripheral_on_gyro (
662677
}
663678

664679
void Muse::peripheral_on_ppg (simpleble_uuid_t service, simpleble_uuid_t characteristic,
665-
uint8_t *data, size_t size, size_t ppg_num, int *ppg_pos)
680+
uint8_t *data, size_t size, size_t ppg_num)
666681
{
667682
std::lock_guard<std::mutex> lock (callback_lock);
668683
if (size != 20)
@@ -678,9 +693,39 @@ void Muse::peripheral_on_ppg (simpleble_uuid_t service, simpleble_uuid_t charact
678693
double ppg_val = (double)cast_24bit_to_int32 ((unsigned char *)&data[2 + i * 3]);
679694
for (int j = 0; j < 2; j++)
680695
{
681-
int pos = (*ppg_pos + i * 2 + j) % 12;
696+
int pos = (current_ppg_pos[ppg_num] + i * 2 + j) % 12;
682697
current_buf[pos][ppg_channels[ppg_num]] = ppg_val;
683698
}
684699
}
685-
*ppg_pos += 2;
700+
current_ppg_pos[ppg_num] += 2;
701+
}
702+
703+
void Muse::peripheral_on_status (
704+
simpleble_uuid_t service, simpleble_uuid_t characteristic, uint8_t *data, size_t size)
705+
{
706+
std::lock_guard<std::mutex> lock (callback_lock);
707+
if (size != 20)
708+
{
709+
safe_logger (spdlog::level::warn, "unknown size for status callback: {}", size);
710+
return;
711+
}
712+
713+
int len = (int)data[0]; // first byte is a len of non garbage data
714+
std::string incom_string (((char *)data) + 1, len);
715+
status_string += incom_string;
716+
safe_logger (spdlog::level::trace, "status string is {}", status_string);
717+
std::regex rgx ("fw\":\"([0-9]+\\.[0-9]+\\.[0-9]+)");
718+
std::smatch matches;
719+
if (std::regex_search (status_string, matches, rgx) == true)
720+
{
721+
if (matches.size () == 2)
722+
{
723+
fw_version = matches.str (1);
724+
safe_logger (spdlog::level::trace, "Determined fw version: {}", fw_version.c_str ());
725+
}
726+
else
727+
{
728+
safe_logger (spdlog::level::warn, "invalid number of groups found");
729+
}
730+
}
686731
}

src/board_controller/muse/muse_bglib/inc/muse_bglib_helper.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ class MuseBGLibHelper
5454
json board_descr;
5555
int current_accel_pos;
5656
int current_gyro_pos;
57-
int current_ppg_pos0;
58-
int current_ppg_pos1;
59-
int current_ppg_pos2;
57+
int current_ppg_pos[3];
6058

6159
std::string preset;
6260

@@ -80,9 +78,7 @@ class MuseBGLibHelper
8078
board_descr = descr;
8179
current_accel_pos = 0;
8280
current_gyro_pos = 0;
83-
current_ppg_pos0 = 0;
84-
current_ppg_pos1 = 0;
85-
current_ppg_pos2 = 0;
81+
memset (current_ppg_pos, 0, sizeof (current_ppg_pos));
8682
}
8783

8884
virtual ~MuseBGLibHelper ()

src/board_controller/muse/muse_bglib/muse_bglib_helper.cpp

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ int MuseBGLibHelper::initialize (struct BrainFlowInputParams params)
3434
new_eeg_data.resize (4); // 4 eeg channels total
3535
current_accel_pos = 0;
3636
current_gyro_pos = 0;
37-
current_ppg_pos0 = 0;
38-
current_ppg_pos1 = 0;
39-
current_ppg_pos2 = 0;
37+
memset (current_ppg_pos, 0, sizeof (current_ppg_pos));
4038
for (int i = 0; i < 12; i++)
4139
{
4240
current_buf[i].resize (buffer_size);
@@ -186,9 +184,7 @@ int MuseBGLibHelper::release ()
186184
}
187185
current_buf.clear ();
188186
current_accel_pos = 0;
189-
current_ppg_pos0 = 0;
190-
current_ppg_pos1 = 0;
191-
current_ppg_pos2 = 0;
187+
memset (current_ppg_pos, 0, sizeof (current_ppg_pos));
192188
current_gyro_pos = 0;
193189
last_timestamp = -1.0;
194190

@@ -419,48 +415,32 @@ void MuseBGLibHelper::ble_evt_attclient_attribute_value (
419415
else if ((uuid == MUSE_GATT_ATTR_PPG0) || (uuid == MUSE_GATT_ATTR_PPG1) ||
420416
(uuid == MUSE_GATT_ATTR_PPG2))
421417
{
422-
int current_ppg_pos = 0;
423418
int ppg_chann_num = 0;
424419
if (uuid == MUSE_GATT_ATTR_PPG0)
425420
{
426421
ppg_chann_num = 0;
427-
current_ppg_pos = current_ppg_pos0;
428422
}
429423
if (uuid == MUSE_GATT_ATTR_PPG1)
430424
{
431425
ppg_chann_num = 1;
432-
current_ppg_pos = current_ppg_pos1;
433426
}
434427
if (uuid == MUSE_GATT_ATTR_PPG2)
435428
{
436429
ppg_chann_num = 2;
437-
current_ppg_pos = current_ppg_pos2;
438430
}
439431

440-
441432
std::vector<int> ppg_channels = board_descr["ppg_channels"];
442433
for (int i = 0; i < 6; i++)
443434
{
444435
double ppg_val =
445436
(double)cast_24bit_to_int32 ((unsigned char *)&msg->value.data[2 + i * 3]);
446437
for (int j = 0; j < 2; j++)
447438
{
448-
int pos = (current_ppg_pos + i * 2 + j) % 12;
439+
int pos = (current_ppg_pos[ppg_chann_num] + i * 2 + j) % 12;
449440
current_buf[pos][ppg_channels[ppg_chann_num]] = ppg_val;
450441
}
451442
}
452-
if (uuid == MUSE_GATT_ATTR_PPG0)
453-
{
454-
current_ppg_pos0 += 2;
455-
}
456-
if (uuid == MUSE_GATT_ATTR_PPG1)
457-
{
458-
current_ppg_pos1 += 2;
459-
}
460-
if (uuid == MUSE_GATT_ATTR_PPG2)
461-
{
462-
current_ppg_pos2 += 2;
463-
}
443+
current_ppg_pos[ppg_chann_num] += 2;
464444
}
465445
else
466446
{

0 commit comments

Comments
 (0)