brainvision 0.0.1

Rust library and TUI for Brain Products BrainVision RDA EEG streams over TCP/IP
Documentation
//! BrainVision RDA protocol types and constants.

/// Default BrainVision Recorder RDA port (16-bit data stream).
pub const RDA_PORT_I16: u16 = 51244;
/// Alternative BrainVision Recorder RDA port (32-bit float stream).
pub const RDA_PORT_F32: u16 = 51234;

/// Message envelope header length: 16-byte GUID + 4-byte size.
pub const ENVELOPE_LEN: usize = 20;

/// RDA message GUID: Start/header.
pub const GUID_START: [u8; 16] = [
    0x45, 0x5C, 0xA4, 0xBD, 0xC5, 0x79, 0x4D, 0x08, 0xB3, 0x9E, 0x72, 0xA8, 0x89, 0xDA, 0x1D, 0x8E,
];
/// RDA message GUID: Data block with int16 samples.
pub const GUID_DATA16: [u8; 16] = [
    0x7A, 0x3A, 0x56, 0x0D, 0x30, 0x86, 0x4D, 0x2A, 0xA4, 0x14, 0x4A, 0xB9, 0xD5, 0x9A, 0xD5, 0x9F,
];
/// RDA message GUID: Data block with float32 samples.
pub const GUID_DATA32: [u8; 16] = [
    0x7A, 0x3A, 0x56, 0x0D, 0x30, 0x86, 0x4D, 0x2A, 0xA4, 0x14, 0x4A, 0xB9, 0xD5, 0x9A, 0xD5, 0xA0,
];
/// RDA message GUID: Stop marker.
pub const GUID_STOP: [u8; 16] = [
    0x49, 0xC0, 0xF5, 0xD1, 0xD6, 0x24, 0x4D, 0x2A, 0x9B, 0x63, 0x28, 0x6E, 0xE0, 0xE6, 0x7A, 0xA0,
];

/// Marker in an RDA data block.
#[derive(Debug, Clone, PartialEq)]
pub struct Marker {
    pub position: u32,
    pub points: u32,
    pub channel: i32,
    pub kind: String,
    pub description: String,
}

/// Stream metadata from the RDA Start message.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct HeaderInfo {
    pub channel_count: u32,
    pub sampling_interval_us: f64,
    pub resolutions_uv: Vec<f64>,
    pub channel_names: Vec<String>,
}

impl HeaderInfo {
    /// Sampling rate in Hz.
    pub fn sampling_rate_hz(&self) -> f64 {
        if self.sampling_interval_us <= 0.0 {
            0.0
        } else {
            1_000_000.0 / self.sampling_interval_us
        }
    }
}

/// One decoded data block.
#[derive(Debug, Clone, PartialEq)]
pub struct DataBlock {
    pub block: u32,
    pub points: u32,
    /// Interleaved samples converted to µV.
    pub samples_uv: Vec<f64>,
    pub markers: Vec<Marker>,
}

/// Parsed RDA message.
#[derive(Debug, Clone, PartialEq)]
pub enum RdaMessage {
    Start(HeaderInfo),
    Data16(DataBlock),
    Data32(DataBlock),
    Stop,
}