1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//! Ganglion board — 4-channel EEG via native Bluetooth LE.
//!
//! Uses the [`btleplug`] crate to communicate directly with the Ganglion's
//! BLE radio. No USB dongle or proprietary BGLIB library required.
//!
//! ## BLE characteristics
//! - **Write**: `2d30c083-f39f-4ce6-923f-3484ea480596`
//! - **Notify**: `2d30c082-f39f-4ce6-923f-3484ea480596`
//! - **Software revision**: `00002a28-0000-1000-8000-00805f9b34fb` (firmware version)
#[cfg(feature = "ble")]
mod impl_ {
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;
use btleplug::api::{Central, Manager as _, Peripheral as _, ScanFilter, WriteType, Characteristic};
use btleplug::platform::{Adapter, Manager, Peripheral};
use futures::StreamExt;
use crate::board::Board;
use crate::electrode::ElectrodeLayout;
use crate::error::{OpenBciError, Result};
use crate::packet::{decode_ganglion, GanglionState};
use crate::sample::{Sample, StreamHandle};
// ── BLE UUIDs ─────────────────────────────────────────────────────────────
const WRITE_CHAR_UUID: &str = "2d30c083-f39f-4ce6-923f-3484ea480596";
const NOTIFY_CHAR_UUID: &str = "2d30c082-f39f-4ce6-923f-3484ea480596";
const SW_REVISION_UUID: &str = "00002a28-0000-1000-8000-00805f9b34fb";
/// Compare a BLE characteristic's UUID against a UUID string (case-insensitive).
fn uuid_eq(c: &Characteristic, uuid_str: &str) -> bool {
c.uuid.to_string().eq_ignore_ascii_case(uuid_str)
}
// ─────────────────────────────────────────────────────────────────────────
/// How to identify the Ganglion during BLE scanning.
#[derive(Debug, Clone, Default)]
pub struct GanglionFilter {
/// If set, only connect to this exact hardware address.
pub mac_address: Option<String>,
/// If set, only match peripherals whose name contains this substring
/// (case-insensitive). Defaults to matching "Ganglion" or "Simblee".
pub device_name: Option<String>,
}
/// Which firmware version the Ganglion is running.
#[derive(Debug, Clone, Default)]
pub enum GanglionFirmware {
/// Read the software revision characteristic to determine the version.
#[default]
Auto,
V2,
V3,
}
/// Configuration for [`GanglionBoard`].
#[derive(Debug, Clone)]
pub struct GanglionConfig {
/// How long to scan for the device before giving up.
pub scan_timeout: Duration,
pub firmware: GanglionFirmware,
pub filter: GanglionFilter,
}
impl Default for GanglionConfig {
fn default() -> Self {
Self {
scan_timeout: Duration::from_secs(10),
firmware: GanglionFirmware::Auto,
filter: GanglionFilter::default(),
}
}
}
// ─────────────────────────────────────────────────────────────────────────
/// Ganglion 4-channel BLE board.
///
/// # Example
/// ```rust,no_run
/// use openbci::board::ganglion::{GanglionBoard, GanglionConfig};
/// use openbci::board::Board;
/// use openbci::electrode::ElectrodeLayout;
///
/// let mut board = GanglionBoard::new(GanglionConfig::default())
/// .with_electrode_layout(ElectrodeLayout::from_labels(&["Fp1","Fp2","C3","C4"]));
///
/// board.prepare().unwrap();
/// let stream = board.start_stream().unwrap();
/// for sample in stream.into_iter().take(200) {
/// println!("{:?}", sample.eeg);
/// }
/// board.release().unwrap();
/// ```
pub struct GanglionBoard {
config: GanglionConfig,
electrode_layout: ElectrodeLayout,
runtime: tokio::runtime::Runtime,
peripheral: Option<Peripheral>,
write_char: Option<Characteristic>,
notify_char: Option<Characteristic>,
firmware: u8,
streaming: bool,
keep_alive: Arc<AtomicBool>,
}
impl GanglionBoard {
/// Create a new Ganglion BLE board driver.
///
/// A multi-threaded Tokio runtime is created internally to drive the
/// `btleplug` async BLE stack. The runtime is owned by this struct and
/// lives until [`release`](crate::board::Board::release) is called.
pub fn new(config: GanglionConfig) -> Self {
let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
.enable_all()
.build()
.expect("Failed to create tokio runtime");
Self {
config,
electrode_layout: ElectrodeLayout::new(4),
runtime,
peripheral: None,
write_char: None,
notify_char: None,
firmware: 3,
streaming: false,
keep_alive: Arc::new(AtomicBool::new(false)),
}
}
/// Builder: attach an electrode layout describing the 4 channel sites.
pub fn with_electrode_layout(mut self, layout: ElectrodeLayout) -> Self {
self.electrode_layout = layout;
self
}
/// Scan the BLE adapter for a Ganglion peripheral matching `filter`.
///
/// Polls the adapter's peripheral list every 200 ms until a matching
/// device is found or `timeout` elapses. Returns `None` on timeout.
async fn scan_for_ganglion(
adapter: &Adapter,
filter: &GanglionFilter,
timeout: Duration,
) -> Option<Peripheral> {
adapter.start_scan(ScanFilter::default()).await.ok()?;
let deadline = tokio::time::Instant::now() + timeout;
loop {
if tokio::time::Instant::now() > deadline { break; }
if let Ok(peripherals) = adapter.peripherals().await {
for p in peripherals {
if let Ok(Some(props)) = p.properties().await {
let matched = if let Some(ref mac) = filter.mac_address {
p.address().to_string().eq_ignore_ascii_case(mac)
} else if let Some(ref name) = props.local_name {
let lc = name.to_lowercase();
if let Some(ref target) = filter.device_name {
lc.contains(&target.to_lowercase())
} else {
lc.contains("ganglion") || lc.contains("simblee")
}
} else {
false
};
if matched {
adapter.stop_scan().await.ok();
return Some(p);
}
}
}
}
tokio::time::sleep(Duration::from_millis(200)).await;
}
adapter.stop_scan().await.ok();
None
}
/// Write `data` to the Ganglion's BLE write characteristic
/// (`WriteType::WithoutResponse`) using the internal Tokio runtime.
fn write_ble(&self, data: &[u8]) -> Result<()> {
let p = self.peripheral.as_ref().ok_or(OpenBciError::BoardNotPrepared)?;
let ch = self.write_char.as_ref().ok_or(OpenBciError::BoardNotPrepared)?;
let data = data.to_vec();
let p_clone = p.clone();
let ch_clone = ch.clone();
self.runtime.block_on(async move {
p_clone.write(&ch_clone, &data, WriteType::WithoutResponse).await
})?;
Ok(())
}
}
// ─── Board trait ──────────────────────────────────────────────────────────
impl Board for GanglionBoard {
fn prepare(&mut self) -> Result<()> {
if self.peripheral.is_some() { return Ok(()); }
let timeout = self.config.scan_timeout;
let filter = self.config.filter.clone();
let (_adapter, peripheral) = self.runtime.block_on(async {
let manager = Manager::new().await?;
let adapters = manager.adapters().await?;
let adapter = adapters.into_iter().next().ok_or_else(|| {
OpenBciError::BoardNotReady("No BLE adapter found".into())
})?;
let peripheral = Self::scan_for_ganglion(&adapter, &filter, timeout).await
.ok_or_else(|| OpenBciError::BoardNotReady("Ganglion not found".into()))?;
Ok::<_, OpenBciError>((adapter, peripheral))
})?;
// Connect with retries
let mut ok = false;
for attempt in 1..=3 {
if self.runtime.block_on(peripheral.connect()).is_ok() { ok = true; break; }
log::warn!("BLE connect attempt {}/3 failed", attempt);
std::thread::sleep(Duration::from_secs(1));
}
if !ok {
return Err(OpenBciError::BoardNotReady("Could not connect to Ganglion".into()));
}
self.runtime.block_on(peripheral.discover_services())?;
let chars = peripheral.characteristics();
let write_char = chars.iter().find(|c| uuid_eq(c, WRITE_CHAR_UUID)).cloned()
.ok_or_else(|| OpenBciError::BoardNotReady("Write char not found".into()))?;
let notify_char = chars.iter().find(|c| uuid_eq(c, NOTIFY_CHAR_UUID)).cloned()
.ok_or_else(|| OpenBciError::BoardNotReady("Notify char not found".into()))?;
// Firmware autodetect
let firmware = match self.config.firmware {
GanglionFirmware::V2 => 2,
GanglionFirmware::V3 => 3,
GanglionFirmware::Auto => {
if let Some(sw_char) = chars.iter().find(|c| uuid_eq(c, SW_REVISION_UUID)) {
let data = self.runtime
.block_on(peripheral.read(sw_char))
.unwrap_or_default();
if data.first().copied() == Some(b'3') { 3 } else { 2 }
} else {
3
}
}
};
log::info!("Ganglion firmware: {}", firmware);
self.runtime.block_on(peripheral.subscribe(¬ify_char))?;
self.firmware = firmware;
self.write_char = Some(write_char);
self.notify_char = Some(notify_char);
self.peripheral = Some(peripheral);
Ok(())
}
fn start_stream(&mut self) -> Result<StreamHandle> {
if self.streaming { return Err(OpenBciError::AlreadyStreaming); }
self.write_ble(b"b")?;
let peripheral = self.peripheral.clone()
.ok_or(OpenBciError::BoardNotPrepared)?;
let firmware = self.firmware;
let (sample_tx, sample_rx) = std::sync::mpsc::sync_channel::<Sample>(512);
let (stop_tx, stop_rx) = std::sync::mpsc::sync_channel::<()>(1);
let keep_alive = self.keep_alive.clone();
keep_alive.store(true, Ordering::Release);
let tx = sample_tx.clone();
self.runtime.spawn(async move {
let mut notif_stream = match peripheral.notifications().await {
Ok(s) => s,
Err(e) => { log::error!("notifications() failed: {}", e); return; }
};
let mut state = GanglionState::default();
loop {
if stop_rx.try_recv().is_ok() || !keep_alive.load(Ordering::Acquire) { break; }
match tokio::time::timeout(
Duration::from_millis(50),
notif_stream.next(),
).await {
Ok(Some(n)) => {
for s in decode_ganglion(&n.value, &mut state, firmware) {
if tx.send(s).is_err() { return; }
}
}
Ok(None) => break,
Err(_) => continue, // timeout → loop back and check stop
}
}
});
self.streaming = true;
Ok(StreamHandle { receiver: sample_rx, stop_tx: Some(stop_tx) })
}
fn stop_stream(&mut self) -> Result<()> {
if !self.streaming { return Err(OpenBciError::NotStreaming); }
self.keep_alive.store(false, Ordering::Release);
let _ = self.write_ble(b"s");
self.streaming = false;
Ok(())
}
fn release(&mut self) -> Result<()> {
if self.streaming { let _ = self.stop_stream(); }
if let Some(ref p) = self.peripheral {
let p = p.clone();
let _ = self.runtime.block_on(p.disconnect());
}
self.peripheral = None;
self.write_char = None;
self.notify_char = None;
Ok(())
}
fn send_command(&mut self, cmd: &str) -> Result<String> {
self.write_ble(cmd.as_bytes())?;
Ok(String::new())
}
fn electrode_layout(&self) -> &ElectrodeLayout { &self.electrode_layout }
fn set_electrode_layout(&mut self, l: ElectrodeLayout) { self.electrode_layout = l; }
fn channel_count(&self) -> usize { 4 }
fn sampling_rate(&self) -> u32 { 200 }
}
}
// ─────────────────────────────────────────────────────────────────────────────
// Public re-exports
// ─────────────────────────────────────────────────────────────────────────────
#[cfg(feature = "ble")]
pub use impl_::{GanglionBoard, GanglionConfig, GanglionFirmware, GanglionFilter};
#[cfg(not(feature = "ble"))]
compile_error!(
"GanglionBoard requires the `ble` feature. \
Enable it in Cargo.toml, or use GanglionWifiBoard instead."
);