Skip to main content

Crate mendi

Crate mendi 

Source
Expand description

§mendi

Async Rust library for streaming fNIRS neurofeedback data from Mendi headbands over Bluetooth Low Energy.

§Hardware

The Mendi headband is a consumer fNIRS (functional near-infrared spectroscopy) device that measures blood oxygenation in the prefrontal cortex. It communicates over BLE using protobuf-encoded messages on six GATT characteristics:

UUIDCharacteristicData
0xABB1FrameIMU + temperature + 3 optical channels (IR/red/ambient)
0xABB2SensorOptical sensor register read/write
0xABB3IMUIMU register read/write
0xABB4ADCBattery voltage, charging, USB status
0xABB5DiagnosticsSelf-test results at power-on
0xABB6CalibrationLED current offsets, auto-cal, low-power mode

§Quick start

use mendi::prelude::*;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let client = MendiClient::new(MendiClientConfig::default());
    let (mut rx, handle) = client.connect().await?;

    while let Some(event) = rx.recv().await {
        match event {
            MendiEvent::Frame(f) => {
                println!("temp={:.1}°C  IR: L={} R={} P={}",
                    f.temperature, f.ir_left, f.ir_right, f.ir_pulse);
            }
            MendiEvent::Battery(b) => {
                println!("Battery: {:.2}V charging={}", b.voltage(), b.charging);
            }
            MendiEvent::Disconnected => break,
            _ => {}
        }
    }
    Ok(())
}

§Module overview

ModulePurpose
mendi_clientBLE scanning, connecting, and the mendi_client::MendiHandle API
typesAll event and data types produced by the client
protocolGATT UUIDs and BLE constants
parseProtobuf decoders for BLE notification payloads
wireGenerated protobuf types from device_v4.proto

§Protobuf code generation

The wire module contains Rust types generated from proto/device_v4.proto using prost. By default, pre-generated code is bundled in src/wire_generated.rs so you do not need protoc installed.

To regenerate from the .proto file (e.g. after modifying it), enable the regenerate-proto feature:

cargo build --features regenerate-proto

This requires the protoc compiler to be available on your PATH. After regenerating, copy the output to src/wire_generated.rs to update the bundled version:

cp target/debug/build/mendi-*/out/mendi.rs src/wire_generated.rs

§Feature flags

FeatureDescription
simulateSimulated + mock devices for testing without hardware
tuiTerminal UI with real-time charts (implies simulate)
regenerate-protoRebuild protobuf types from .proto (requires protoc)

Modules§

mendi_client
BLE client for the Mendi neurofeedback headband.
parse
Protobuf decoders for Mendi BLE notification payloads.
prelude
Convenience re-exports.
protocol
GATT UUIDs and constants for the Mendi headband BLE protocol (V4).
types
Data types for events emitted by the Mendi BLE client.
wire
Generated protobuf types from the Mendi V4 wire protocol.