Skip to main content

Crate emotiv

Crate emotiv 

Source
Expand description

§emotiv

Async Rust client for streaming EEG and BCI data from Emotiv headsets via the Cortex API WebSocket (JSON-RPC 2.0).

§Supported hardware

ModelEEG chBCINotes
EPOC X14Full EEG, motion, performance metrics, mental commands
EPOC+14Same protocol as EPOC X
Insight5Lightweight 5-channel headset
EPOC Flex32Research-grade flexible cap

§API credentials

To connect to a real headset you need a Client ID and Client Secret from the Emotiv developer portal:

  1. Install and log into the EMOTIV Launcher.
  2. Create a Cortex App at https://www.emotiv.com/my-account/cortex-apps/.
  3. Pass the credentials via CortexClientConfig or the EMOTIV_CLIENT_ID / EMOTIV_CLIENT_SECRET environment variables.

On the first connection the Cortex service will prompt you to approve the app inside the EMOTIV Launcher (one-time only).

§Quick start

use emotiv::prelude::*;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let config = CortexClientConfig {
        client_id: std::env::var("EMOTIV_CLIENT_ID")
            .expect("set EMOTIV_CLIENT_ID"),
        client_secret: std::env::var("EMOTIV_CLIENT_SECRET")
            .expect("set EMOTIV_CLIENT_SECRET"),
        ..Default::default()
    };
    let client = CortexClient::new(config);
    let (mut rx, handle) = client.connect().await?;

    while let Some(event) = rx.recv().await {
        match event {
            CortexEvent::SessionCreated(_) => {
                handle.subscribe(&["eeg", "mot", "met", "pow"]).await?;
            }
            CortexEvent::Eeg(data) => {
                println!("EEG: {:?}", &data.samples[..5.min(data.samples.len())]);
            }
            CortexEvent::Disconnected => break,
            _ => {}
        }
    }
    Ok(())
}

§Simulation mode (feature = simulate)

For testing without hardware or API keys, enable the simulate feature:

[dependencies]
emotiv = { version = "0.0.1", features = ["simulate"] }

Then use [simulator::spawn_simulator] to generate synthetic data through the same CortexEvent channel.

§Module overview

ModulePurpose
preludeOne-line glob import of the most commonly needed types
clientWebSocket connection, auth flow, and the CortexHandle command API
typesAll event and data types (CortexEvent, EegData, etc.)
protocolJSON-RPC request builders and Cortex API constants
[simulator]Signal simulator for offline testing (feature = simulate)

Modules§

client
Cortex WebSocket client for Emotiv headsets.
config
Configuration
error
Error Types
health
Connection Health Monitor
prelude
Convenience re-exports for downstream crates.
protocol
Cortex API JSON-RPC 2.0 protocol definitions and request builders.
reconnect
Resilient Client
retry
Retry Policies
types
All data types produced and consumed by the Emotiv Cortex client.