1
1
#include < algorithm>
2
+ #include < regex>
2
3
3
4
#include " custom_cast.h"
4
5
#include " muse.h"
@@ -50,25 +51,25 @@ void peripheral_on_gyro (simpleble_uuid_t service, simpleble_uuid_t characterist
50
51
void peripheral_on_ppg0 (simpleble_uuid_t service, simpleble_uuid_t characteristic, uint8_t *data,
51
52
size_t size, void *board)
52
53
{
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 );
56
55
}
57
56
58
57
void peripheral_on_ppg1 (simpleble_uuid_t service, simpleble_uuid_t characteristic, uint8_t *data,
59
58
size_t size, void *board)
60
59
{
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 );
64
61
}
65
62
66
63
void peripheral_on_ppg2 (simpleble_uuid_t service, simpleble_uuid_t characteristic, uint8_t *data,
67
64
size_t size, void *board)
68
65
{
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);
72
73
}
73
74
74
75
@@ -80,9 +81,9 @@ Muse::Muse (int board_id, struct BrainFlowInputParams params) : BLELibBoard (boa
80
81
is_streaming = false ;
81
82
current_accel_pos = 0 ;
82
83
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 = " " ;
86
87
}
87
88
88
89
Muse::~Muse ()
@@ -184,6 +185,20 @@ int Muse::prepare_session ()
184
185
service.uuid , service.characteristics [j]);
185
186
control_characteristics_found = true ;
186
187
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
+ }
187
202
}
188
203
if (strcmp (service.characteristics [j].value , MUSE_GATT_ATTR_TP9) == 0 )
189
204
{
@@ -349,9 +364,7 @@ int Muse::prepare_session ()
349
364
new_eeg_data.resize (4 ); // 4 eeg channels total
350
365
current_gyro_pos = 0 ;
351
366
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));
355
368
for (int i = 0 ; i < 12 ; i++)
356
369
{
357
370
current_buf[i].resize (buffer_size);
@@ -362,6 +375,10 @@ int Muse::prepare_session ()
362
375
initialized = true ;
363
376
}
364
377
378
+ if (res == (int )BrainFlowExitCodes::STATUS_OK)
379
+ {
380
+ res = config_board (" v1" );
381
+ }
365
382
if (res == (int )BrainFlowExitCodes::STATUS_OK)
366
383
{
367
384
res = config_board (" p21" );
@@ -458,9 +475,7 @@ int Muse::release_session ()
458
475
new_eeg_data.clear ();
459
476
current_gyro_pos = 0 ;
460
477
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));
464
479
465
480
return (int )BrainFlowExitCodes::STATUS_OK;
466
481
}
@@ -662,7 +677,7 @@ void Muse::peripheral_on_gyro (
662
677
}
663
678
664
679
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)
666
681
{
667
682
std::lock_guard<std::mutex> lock (callback_lock);
668
683
if (size != 20 )
@@ -678,9 +693,39 @@ void Muse::peripheral_on_ppg (simpleble_uuid_t service, simpleble_uuid_t charact
678
693
double ppg_val = (double )cast_24bit_to_int32 ((unsigned char *)&data[2 + i * 3 ]);
679
694
for (int j = 0 ; j < 2 ; j++)
680
695
{
681
- int pos = (*ppg_pos + i * 2 + j) % 12 ;
696
+ int pos = (current_ppg_pos[ppg_num] + i * 2 + j) % 12 ;
682
697
current_buf[pos][ppg_channels[ppg_num]] = ppg_val;
683
698
}
684
699
}
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
+ }
686
731
}
0 commit comments