Examples#
Both examples are in the SDK’s examples/ folder, with a CMake project that
builds them against the SDK. Set DEVICE_NAME to the name printed on the
back of your device first, then from the unpacked SDK:
$ cmake -S examples -B build
$ cmake --build build
On Windows the build copies bacore.dll next to the executables.
Connecting to a device#
Scans for devices, connects to one, reads its battery level and disconnects.
1// BrainAccess Core connection example: scans for devices, connects to one,
2// reads its battery level and disconnects. Set DEVICE_NAME to the name
3// printed on the back of your device.
4
5#include "bacore.h" // Include the core library
6#include "eeg_manager.h" // Include the EEG manager library
7#include <stdio.h> // Include a standard I/O library for printing
8#include <string.h> // Include string manipulation functions
9
10#ifdef _WIN32
11#include <windows.h> // Windows-specific sleep function
12#define sleep_ms(x) Sleep(x)
13#else
14#include <unistd.h> // POSIX (Linux/Unix) sleep function
15#define sleep_ms(x) usleep((x) * 1000) // usleep takes microseconds
16#endif
17
18#define DEVICE_NAME "BA MINI 018" // Name of the device to connect to
19ba_eeg_manager* manager1;
20
21// Callback function for handling disconnection events.
22// This function is called when the connection to the device is lost.
23// It prints a message indicating the disconnection event.
24void disconnect_callback(void* data)
25{
26 (void)data;
27 printf("Disconnected\n");
28}
29
30// Main function to run the Bluetooth EEG system application
31int main()
32{
33 ba_core_init(); // Initialize the core library
34 ba_ble_device* device_list = NULL; // Pointer to the list of Bluetooth devices
35 size_t device_list_size = 0; // Size of the device list
36 ba_error status_core = ba_core_scan(&device_list,
37 &device_list_size); // Scan for available Bluetooth devices
38
39 printf("Scan returned with status: %d | device count: %zu\n", status_core,
40 device_list_size); // Print scan status and device count
41 printf("Printing device list: \n");
42 for (size_t i = 0; i < device_list_size; ++i)
43 {
44 printf("device %zu name: %s MAC: %s \n", i, device_list[i].name,
45 device_list[i].mac_address); // Print each device's name and MAC address
46 }
47
48 manager1 = ba_eeg_manager_new(); // Create a new EEG manager instance
49 ba_callback_disconnect disconnect_cb = disconnect_callback; // Define the disconnect callback function
50 ba_eeg_manager_set_callback_disconnect(manager1, disconnect_cb,
51 NULL); // Set the disconnect callback for the EEG manager
52 ba_error status = BA_ERROR_OK; // Initialize the status variable
53
54 status = ba_eeg_manager_connect(manager1, DEVICE_NAME, NULL, NULL); // Connect to the EEG device
55 if (status != BA_ERROR_OK)
56 {
57 printf("Failed to connect with error: %d\n", status); // Print an error message if connection fails
58 ba_eeg_manager_free(manager1);
59 ba_core_close();
60 return status;
61 }
62
63 printf("Connected\n"); // Print a message indicating successful connection
64
65 ba_battery_info info;
66 if (ba_eeg_manager_read_battery_info(manager1, &info) == BA_ERROR_OK) // Get the battery information
67 {
68 printf("battery level: %d | is charger connected: %d | is charging: %d \n", info.level,
69 info.is_charger_connected, info.is_charging); // Print battery information
70 }
71
72 ba_eeg_manager_disconnect(manager1); // Disconnect from the EEG device
73 ba_eeg_manager_free(manager1);
74 ba_core_close(); // Close the core library
75 printf("Core closed.\n"); // Print a message indicating the core has been
76 // closed
77
78 return status; // Return the status code
79}
Reading EEG data#
Configures the electrode channels, streams for RUNTIME seconds and prints
every sample from the chunk callback. Set DEVICE_CHANNEL to the device’s
electrode count.
1// BrainAccess Core streaming example: connects to a device, configures its
2// electrode channels, streams EEG for RUNTIME seconds and prints every sample.
3// Set DEVICE_NAME and DEVICE_CHANNEL (electrode count) to match your device.
4
5#include "bacore.h" // Include the core library
6#include "eeg_manager.h" // Include the EEG manager library
7#include <stdio.h> // Include standard I/O library for printing
8#include <string.h> // Include string manipulation functions
9
10#ifdef _WIN32
11#include <windows.h> // Windows-specific sleep function
12#define sleep_ms(x) Sleep(x)
13#else
14#include <unistd.h> // POSIX (Linux/Unix) sleep function
15#define sleep_ms(x) usleep((x) * 1000) // usleep takes microseconds
16#endif
17
18#define DEVICE_NAME "BA HALO 002" // Name of the device to connect to
19#define DEVICE_CHANNEL 4 // Number of electrodes to stream
20#define SAMPLE_RATE BA_DATA_STREAM_RATE_250_HZ // Sample rate supported by every device
21#define RUNTIME 1 // Streaming time in seconds
22ba_eeg_manager* manager1;
23
24/**
25 * @brief Callback function to handle EEG data chunks.
26 *
27 * This function processes the EEG data received in chunks. It extracts the
28 * sample number and channel data, and prints the data for each sample.
29 *
30 * @param data Pointer to the array of data pointers for each channel.
31 * @param size Number of samples in the data chunk.
32 * @param user_data User-defined data (not used in this function).
33 */
34static void chunk_callback(const void* const* data, size_t size, void* user_data)
35{
36 (void)user_data;
37 // Get the data for the sample number and channels
38 const size_t* eeg_data_sample_number = (const size_t*)data[ba_eeg_manager_get_channel_index(manager1, BA_EEG_CHANNEL_ID_SAMPLE_NUMBER)];
39
40 for (size_t i = 0; i < size; ++i)
41 {
42 // Process the data directly without storing it in a structure
43 printf("[%zu]", eeg_data_sample_number[i]);
44 for (int j = 0; j < DEVICE_CHANNEL; ++j)
45 {
46 const double* eeg_data_channel = (const double*)data[ba_eeg_manager_get_channel_index(manager1, BA_EEG_CHANNEL_ID_ELECTRODE_MEASUREMENT + j)];
47 printf(" %f", eeg_data_channel[i]);
48 }
49 printf("\n");
50 }
51}
52
53/**
54 * @brief Callback function to handle disconnection events.
55 *
56 * This function is called when the EEG device is disconnected. It prints a
57 * message indicating that the device has been disconnected.
58 *
59 * @param data User-defined data (not used in this function).
60 */
61void disconnect_callback(void* data)
62{
63 (void)data;
64 printf("Disconnected\n");
65}
66
67// Main function to run the Bluetooth EEG system application
68int main()
69{
70 ba_core_init(); // Initialize the core library
71 ba_ble_device* device_list = NULL; // Pointer to the list of Bluetooth devices
72 size_t device_list_size = 0; // Size of the device list
73 ba_error status_core = ba_core_scan(&device_list,
74 &device_list_size); // Scan for available Bluetooth devices
75
76 printf("Scan returned with status: %d | device count: %zu\n", status_core,
77 device_list_size); // Print scan status and device count
78 printf("Printing device list: \n");
79 for (size_t i = 0; i < device_list_size; ++i)
80 {
81 printf("device %zu name: %s MAC: %s \n", i, device_list[i].name,
82 device_list[i].mac_address); // Print each device's name and MAC address
83 }
84
85 manager1 = ba_eeg_manager_new(); // Create a new EEG manager instance
86 ba_callback_disconnect disconnect_cb = disconnect_callback; // Define the disconnect callback function
87 ba_eeg_manager_set_callback_disconnect(manager1, disconnect_cb,
88 NULL); // Set the disconnect callback for the EEG manager
89 ba_error status = BA_ERROR_OK; // Initialize the status variable
90
91 status = ba_eeg_manager_connect(manager1, DEVICE_NAME, NULL, NULL); // Connect to the EEG device
92 if (status != BA_ERROR_OK)
93 {
94 printf("Failed to connect with error: %d\n", status); // Print an error message if connection fails
95 ba_eeg_manager_free(manager1);
96 ba_core_close();
97 return status;
98 }
99
100 printf("Connected\n"); // Print a message indicating successful connection
101
102 ba_eeg_manager_set_data_stream_rate(manager1, SAMPLE_RATE);
103
104 // Stream normal EEG. Pass BA_OUTPUT_MODE_SQUARE_WAVE instead to stream the
105 // device's internal square-wave test signal (firmware 3.7.0 or later).
106 ba_eeg_manager_set_output_mode(manager1, BA_OUTPUT_MODE_NORMAL);
107
108 // Enable and set the gain for all electrode channels
109 for (int i = 0; i < DEVICE_CHANNEL; ++i)
110 {
111 // Enable electrode contact channel
112 ba_eeg_manager_set_channel_enabled(manager1, BA_EEG_CHANNEL_ID_ELECTRODE_CONTACT + i, true);
113 // Enable electrode measurement channel
114 ba_eeg_manager_set_channel_enabled(manager1, BA_EEG_CHANNEL_ID_ELECTRODE_MEASUREMENT + i, true);
115 // Set gain for electrode measurement channel
116 ba_eeg_manager_set_channel_gain(manager1, BA_EEG_CHANNEL_ID_ELECTRODE_MEASUREMENT + i, BA_GAIN_MODE_X8);
117 }
118
119 // Enable sample number channel
120 ba_eeg_manager_set_channel_enabled(manager1, BA_EEG_CHANNEL_ID_SAMPLE_NUMBER, true);
121 // Enable streaming channel
122 ba_eeg_manager_set_channel_enabled(manager1, BA_EEG_CHANNEL_ID_STREAMING, true);
123
124 printf("streaming idx : %zu\n ", ba_eeg_manager_get_channel_index(manager1, BA_EEG_CHANNEL_ID_STREAMING));
125
126 // Load the configuration
127 ba_eeg_manager_load_config(manager1, NULL, NULL);
128
129 // Define the callback function for handling EEG data chunks
130 ba_callback_chunk my_callback = chunk_callback;
131
132 // Set the callback function for handling EEG data chunks
133 ba_eeg_manager_set_callback_chunk(manager1, my_callback, manager1);
134
135 // Start the EEG data streaming
136 printf("stream_start\n");
137 printf("start stream returned %u\n", ba_eeg_manager_start_stream(manager1, NULL, NULL));
138
139
140
141 // Stop the EEG data streaming after RUNTIME seconds
142 for (size_t i = 0; i < RUNTIME; i++)
143 {
144 printf("sec: %zu started\n", i);
145 sleep_ms(1000 * 1);
146 }
147 printf("Let's stop the stream.\n");
148 ba_eeg_manager_stop_stream(manager1, NULL, NULL);
149 printf("stream_stoped\n");
150
151 ba_eeg_manager_disconnect(manager1); // Disconnect from the EEG device
152 ba_eeg_manager_free(manager1);
153 ba_core_close(); // Close the core library
154 printf("Core closed.\n"); // Print a message indicating the core has been
155 // closed
156
157 return status; // Return the status code
158}