emotiv 0.0.13

Async Rust client and TUI for Emotiv EEG headsets via the Cortex API WebSocket (JSON-RPC 2.0)
Documentation
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
//! Raw BLE/USB device streaming CLI.
//!
//! Direct connection to Emotiv headsets via BLE or USB, bypassing the Cortex API.
//!
//! # Usage
//!
//! ```bash
//! # Discover devices
//! cargo run --bin emotiv-raw --features raw -- --list
//!
//! # Connect to a specific device
//! cargo run --bin emotiv-raw --features raw -- --connect "<BLE_ID_OR_SERIAL>"
//!
//! # Connect to first discovered device
//! cargo run --bin emotiv-raw --features raw
//!
//! # Decode audit mode (live decode sanity scoring)
//! cargo run --bin emotiv-raw --features raw -- --decode-audit
//! ```

use anyhow::Result;
use log::{error, info};
use std::collections::VecDeque;
use std::io::{self, BufRead};

#[cfg(feature = "raw")]
use emotiv::raw;

#[tokio::main]
async fn main() -> Result<()> {
    env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();

    #[cfg(not(feature = "raw"))]
    {
        eprintln!("Error: raw mode requires the `raw` feature.");
        eprintln!("  cargo run --bin emotiv-raw --features raw");
        std::process::exit(1);
    }

    let args: Vec<String> = std::env::args().collect();
    let debug_view = args.contains(&"--debug-view".to_string());
    let decode_audit = args.contains(&"--decode-audit".to_string());

    // Handle --list flag
    if args.contains(&"--list".to_string()) {
        #[cfg(feature = "raw")]
        list_devices().await?;
        return Ok(());
    }

    // Get device address from --connect flag or use first available
    let device_address = args
        .windows(2)
        .find_map(|w| {
            if w[0] == "--connect" {
                Some(w[1].clone())
            } else {
                None
            }
        });

    #[cfg(feature = "raw")]
    {
        match device_address {
            Some(addr) => connect_to_device(&addr, debug_view, decode_audit).await?,
            None => connect_to_first_device(debug_view, decode_audit).await?,
        }
    }

    Ok(())
}

#[cfg(feature = "raw")]
async fn list_devices() -> Result<()> {
    info!("Discovering Emotiv devices...\n");
    let devices = raw::discover_devices().await?;

    if devices.is_empty() {
        info!("No devices found.");
        return Ok(());
    }

    println!("┌─────────────────────────────────────────────────────────────────────┐");
    println!("│ Found {} device(s):", devices.len());
    println!("├─────────────────────────────────────────────────────────────────────┤");

    for (i, device) in devices.iter().enumerate() {
        println!(
            "│ #{} - {} ({})",
            i + 1,
            device.model,
            device.transport
        );
        println!("│     Name:     {}", device.name);
        println!("│     BLE ID:   {}", device.ble_id);
        println!(
            "│     BLE MAC:  {}",
            device.ble_mac.as_deref().unwrap_or("(unavailable)")
        );
        println!("│     Address:  {}", device.address);
        println!("│     Serial:   {}", device.serial);
        println!("│     Battery:  {}%", device.battery_percent);
        println!("│     EEG Ch:   {}", device.model.channel_count());
    }
    println!("└─────────────────────────────────────────────────────────────────────┘");
    println!("\nUsage:");
    println!("  cargo run --bin emotiv-raw --features raw -- --connect \"<BLE_ID_OR_SERIAL>\"");

    Ok(())
}

#[cfg(feature = "raw")]
async fn connect_to_first_device(debug_view: bool, decode_audit: bool) -> Result<()> {
    info!("Discovering Emotiv devices...");
    let devices = raw::discover_devices().await?;

    if devices.is_empty() {
        error!("No devices found!");
        return Ok(());
    }

    let device = select_best_device(&devices)
        .ok_or_else(|| anyhow::anyhow!("No suitable BLE device candidate found"))?;
    info!(
        "Connecting to {} ({}) [{} / {}] - {}",
        device.model.name(),
        device.serial,
        device.name,
        device.ble_mac.as_deref().unwrap_or("no-mac"),
        device.transport
    );

    stream_device(device.clone(), debug_view, decode_audit).await?;
    Ok(())
}

#[cfg(feature = "raw")]
async fn connect_to_device(address: &str, debug_view: bool, decode_audit: bool) -> Result<()> {
    let devices = raw::discover_devices().await?;
    let device = devices
        .iter()
        .find(|d| id_match(&d.address, address) || id_match(&d.serial, address))
        .ok_or_else(|| anyhow::anyhow!("Device not found: {}", address))?;

    info!(
        "Connecting to {} ({}) [{} / {}] - {}",
        device.model.name(),
        device.serial,
        device.name,
        device.ble_mac.as_deref().unwrap_or("no-mac"),
        device.transport
    );

    stream_device(device.clone(), debug_view, decode_audit).await?;
    Ok(())
}

#[cfg(feature = "raw")]
async fn stream_device(device: raw::DeviceInfo, debug_view: bool, decode_audit: bool) -> Result<()> {
    let (_, model_uv_max) = device.model.eeg_physical_range();
    let model_channel_count = device.model.channel_count();
    let (mut rx, handle) = raw::RawDevice::from_info(device).connect().await?;

    info!("✅ Connected! Streaming EEG data...");
    info!("Press Ctrl-C to stop.\n");

    let (line_tx, mut line_rx) = tokio::sync::mpsc::unbounded_channel::<String>();
    std::thread::spawn(move || {
        let stdin = io::stdin();
        for line in stdin.lock().lines() {
            if line.is_ok() {
                if line_tx.send(line.unwrap()).is_err() {
                    break;
                }
            }
        }
    });

    let mut packet_count = 0u64;
    let mut low_quality_warn_count = 0u64;
    let start_time = std::time::Instant::now();

    println!("┌────────────────────────────────────────────────────────────────────┐");
    println!("│ EEG Stream - Press Ctrl-C to quit                                  │");
    println!("├────────────────────────────────────────────────────────────────────┤");
    if debug_view {
        println!("│ DEBUG view enabled: showing rx/decode counters every 2s            │");
        println!("├────────────────────────────────────────────────────────────────────┤");
    }
    if decode_audit {
        println!("│ DECODE AUDIT enabled: continuity/clip/rms/correlation checks       │");
        println!("├────────────────────────────────────────────────────────────────────┤");
    }

    let mut debug_tick = tokio::time::interval(std::time::Duration::from_secs(2));
    let mut audit_tick = tokio::time::interval(std::time::Duration::from_secs(3));

    let mut prev_counter: Option<u32> = None;
    let mut counter_jumps = 0u64;
    let mut sample_count = 0u64;
    let mut clipped_count = 0u64;
    let mut abs_sum = 0.0f64;
    let mut sq_sum = 0.0f64;
    let audit_ch = 4usize.min(model_channel_count).max(1);
    let mut corr_hist: Vec<VecDeque<f64>> = (0..audit_ch)
        .map(|_| VecDeque::with_capacity(512))
        .collect();

    loop {
        tokio::select! {
            Some(_line) = line_rx.recv() => {
                // Check for quit command
                break;
            }
            _ = debug_tick.tick(), if debug_view => {
                let stats = handle.debug_stats().await;
                println!(
                    "│ DBG rx={} dec={} fail={} timeout={} last_uuid={} len={} key={} writes={} subs={}",
                    stats.received_notifications,
                    stats.decoded_packets,
                    stats.decrypt_failures,
                    stats.timeout_count,
                    stats.last_notify_uuid.as_deref().unwrap_or("-"),
                    stats.last_payload_len,
                    stats.active_serial_candidate.as_deref().unwrap_or("-"),
                    stats.start_command_writes,
                    stats.subscribed_characteristics.len(),
                );
                if !stats.subscribed_characteristics.is_empty() {
                    println!(
                        "│ DBG subs: {}",
                        stats.subscribed_characteristics.join(",")
                    );
                }
            }
            _ = audit_tick.tick(), if decode_audit => {
                let continuity = if packet_count == 0 {
                    0.0
                } else {
                    1.0 - (counter_jumps as f64 / packet_count as f64)
                };

                let clip_rate = if sample_count == 0 {
                    0.0
                } else {
                    clipped_count as f64 / sample_count as f64
                };

                let mean_abs = if sample_count == 0 {
                    0.0
                } else {
                    abs_sum / sample_count as f64
                };

                let rms = if sample_count == 0 {
                    0.0
                } else {
                    (sq_sum / sample_count as f64).sqrt()
                };

                let corr = average_pairwise_corr(&corr_hist).unwrap_or(0.0);

                let verdict = if continuity > 0.99 && clip_rate < 0.02 && rms < 300.0 && corr.abs() > 0.02 {
                    "GOOD"
                } else if continuity > 0.95 && clip_rate < 0.10 && rms < 1200.0 {
                    "WARN"
                } else {
                    "BAD"
                };

                println!(
                    "│ AUDIT {verdict:<4} cont={:.3} clip={:.1}% mean|uV|={:.1} rms={:.1} corr={:.3}",
                    continuity,
                    clip_rate * 100.0,
                    mean_abs,
                    rms,
                    corr,
                );
            }
            maybe_data = rx.recv() => {
                let Some(data) = maybe_data else {
                    break;
                };

                packet_count += 1;

                if let Some(prev) = prev_counter {
                    let expected = (prev + 1) & 0xFFFF;
                    if data.counter != expected {
                        counter_jumps += 1;
                    }
                }
                prev_counter = Some(data.counter);

                for (ch_idx, value) in data.eeg_uv.iter().enumerate() {
                    if value.abs() >= model_uv_max * 0.98 {
                        clipped_count += 1;
                    }
                    abs_sum += value.abs();
                    sq_sum += value * value;
                    sample_count += 1;

                    if ch_idx < audit_ch {
                        let hist = &mut corr_hist[ch_idx];
                        hist.push_back(*value);
                        while hist.len() > 512 {
                            hist.pop_front();
                        }
                    }
                }

                // Show every 16th packet (roughly 2x per second at 128 Hz)
                if packet_count % 16 == 0 {
                    let elapsed = start_time.elapsed().as_secs_f64();
                    let rate = packet_count as f64 / elapsed;

                    println!(
                        "│ Packet {:6} | Counter {:5} | Avg Rate: {:.1} Hz | Battery: {}%",
                        packet_count, data.counter, rate, data.battery_percent
                    );

                    // Show first 5 channels
                    let display_ch = 5.min(data.eeg_uv.len());
                    print!("│ CH[0..{}]: [", display_ch);
                    for (i, uv) in data.eeg_uv[..display_ch].iter().enumerate() {
                        print!(" {:8.1}µV", uv);
                        if i < display_ch - 1 {
                            print!(",");
                        }
                    }
                    println!(" ]");
                }

                // Check signal quality
                let cq_unknown = !data.contact_quality.is_empty()
                    && data.contact_quality.iter().all(|&v| v == 0);
                if data.signal_quality < 2 && !cq_unknown && packet_count % 64 == 0 {
                    low_quality_warn_count += 1;
                    eprintln!("⚠️  Low signal quality: {}", data.signal_quality);
                }
            }
            else => break,
        }
    }

    println!("├────────────────────────────────────────────────────────────────────┤");
    let elapsed = start_time.elapsed().as_secs_f64();
    let avg_rate = packet_count as f64 / elapsed;
    println!(
        "│ Streamed {} packets in {:.1}s ({:.1} Hz avg) | low-signal warns {}",
        packet_count, elapsed, avg_rate, low_quality_warn_count
    );
    println!("└────────────────────────────────────────────────────────────────────┘");

    Ok(())
}

#[cfg(feature = "raw")]
fn average_pairwise_corr(ch_hist: &[VecDeque<f64>]) -> Option<f64> {
    if ch_hist.len() < 2 {
        return None;
    }

    let min_len = ch_hist.iter().map(|h| h.len()).min().unwrap_or(0);
    if min_len < 64 {
        return None;
    }

    let mut total = 0.0f64;
    let mut pairs = 0u32;
    for i in 0..ch_hist.len() {
        for j in (i + 1)..ch_hist.len() {
            if let Some(c) = pearson_tail(&ch_hist[i], &ch_hist[j], min_len.min(256)) {
                total += c;
                pairs += 1;
            }
        }
    }

    if pairs == 0 {
        None
    } else {
        Some(total / pairs as f64)
    }
}

#[cfg(feature = "raw")]
fn pearson_tail(a: &VecDeque<f64>, b: &VecDeque<f64>, n: usize) -> Option<f64> {
    if n == 0 || a.len() < n || b.len() < n {
        return None;
    }

    let a_start = a.len() - n;
    let b_start = b.len() - n;
    let mut sum_a = 0.0;
    let mut sum_b = 0.0;
    for k in 0..n {
        sum_a += a[a_start + k];
        sum_b += b[b_start + k];
    }
    let mean_a = sum_a / n as f64;
    let mean_b = sum_b / n as f64;

    let mut num = 0.0;
    let mut den_a = 0.0;
    let mut den_b = 0.0;
    for k in 0..n {
        let da = a[a_start + k] - mean_a;
        let db = b[b_start + k] - mean_b;
        num += da * db;
        den_a += da * da;
        den_b += db * db;
    }

    let den = (den_a * den_b).sqrt();
    if den < 1e-9 {
        None
    } else {
        Some((num / den).clamp(-1.0, 1.0))
    }
}

#[cfg(feature = "raw")]
fn id_match(candidate: &str, input: &str) -> bool {
    if candidate.eq_ignore_ascii_case(input) {
        return true;
    }
    let a = normalize_id(candidate);
    let b = normalize_id(input);
    !a.is_empty() && !b.is_empty() && (a == b || a.contains(&b) || b.contains(&a))
}

#[cfg(feature = "raw")]
fn normalize_id(s: &str) -> String {
    s.chars()
        .filter(|c| c.is_ascii_alphanumeric())
        .flat_map(|c| c.to_lowercase())
        .collect()
}

#[cfg(feature = "raw")]
fn select_best_device(devices: &[raw::DeviceInfo]) -> Option<&raw::DeviceInfo> {
    let likely: Vec<&raw::DeviceInfo> = devices.iter().filter(|d| is_likely_emotiv(d)).collect();
    if !likely.is_empty() {
        likely.into_iter().max_by_key(|d| device_score(d))
    } else {
        devices.iter().max_by_key(|d| device_score(d))
    }
}

#[cfg(feature = "raw")]
fn device_score(d: &raw::DeviceInfo) -> i32 {
    let mut score = 0;
    let name = d.name.to_ascii_lowercase();
    if name != "(unknown)" {
        score += 20;
    }
    if name.contains("emotiv")
        || name.contains("epoc")
        || name.contains("insight")
        || name.contains("flex")
        || name.contains("mn8")
        || name.contains("xtrodes")
    {
        score += 50;
    }
    if d.ble_mac.as_deref().is_some_and(|m| !is_zero_mac(m)) {
        score += 15;
    }
    if !normalize_id(&d.ble_id).is_empty() {
        score += 10;
    }
    if d.is_connected {
        score += 10;
    }
    score
}

#[cfg(feature = "raw")]
fn is_likely_emotiv(d: &raw::DeviceInfo) -> bool {
    let name = d.name.to_ascii_lowercase();
    if name.contains("emotiv")
        || name.contains("epoc")
        || name.contains("insight")
        || name.contains("flex")
        || name.contains("mn8")
        || name.contains("xtrodes")
    {
        return true;
    }

    // Exclude clearly generic unknown/autogenerated names from auto-connect.
    if name == "(unknown)" || name.starts_with("gb-") || name.starts_with("ble") {
        return false;
    }

    // Keep manually paired/connected devices as likely candidates.
    d.is_connected
}

#[cfg(feature = "raw")]
fn is_zero_mac(mac: &str) -> bool {
    let only_hex: String = mac
        .chars()
        .filter(|c| c.is_ascii_hexdigit())
        .collect::<String>()
        .to_lowercase();
    !only_hex.is_empty() && only_hex.chars().all(|c| c == '0')
}