|
1 | 1 | #pragma once |
2 | 2 |
|
| 3 | +#include <array> |
| 4 | +#include <cstddef> |
| 5 | +#include <cstdint> |
3 | 6 | #include <condition_variable> |
4 | 7 | #include <mutex> |
| 8 | +#include <string> |
5 | 9 | #include <utility> |
6 | 10 | #include <vector> |
7 | 11 |
|
8 | 12 | #include "ble_lib_board.h" |
| 13 | +#include "muse_anthena_types.h" |
9 | 14 |
|
10 | 15 |
|
11 | 16 | class MuseAnthena : public BLELibBoard |
12 | 17 | { |
13 | 18 |
|
14 | 19 | 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 | + |
15 | 37 | volatile simpleble_adapter_t muse_adapter; |
16 | 38 | volatile simpleble_peripheral_t muse_peripheral; |
17 | 39 | bool initialized; |
18 | 40 | bool is_streaming; |
19 | 41 | std::mutex m; |
| 42 | + std::mutex callback_lock; |
20 | 43 | std::condition_variable cv; |
21 | 44 | std::vector<std::pair<simpleble_uuid_t, simpleble_uuid_t>> notified_characteristics; |
22 | 45 | 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; |
26 | 52 |
|
| 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 (); |
27 | 58 | 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); |
31 | 67 |
|
32 | 68 | public: |
33 | 69 | MuseAnthena (int board_id, struct BrainFlowInputParams params); |
34 | | - ~MuseAnthena (); |
| 70 | + ~MuseAnthena () override; |
35 | 71 |
|
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; |
41 | 77 | int config_board (std::string config); |
42 | 78 |
|
43 | 79 | 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, |
47 | 81 | simpleble_uuid_t characteristic, const uint8_t *data, size_t size); |
48 | 82 | void peripheral_on_status (simpleble_peripheral_t peripheral, simpleble_uuid_t service, |
49 | 83 | simpleble_uuid_t characteristic, const uint8_t *data, size_t size); |
|
0 commit comments