read_packets/
read_packets.rs

1use std::time::Instant;
2
3use cerebrust::{DataReader, DeviceConfig, PacketVariant};
4
5#[tokio::main]
6async fn main() {
7    // Using the default configurations
8    let config = DeviceConfig::default();
9    // Connect to the device (make sure the device is on and discoverable,
10    // and NOT in BLE mode)
11    let stream = config.connect().await.unwrap();
12    // Create a data reader
13    let mut data_reader = DataReader::new(stream);
14    // Poll data packets asynchronously
15    let timer = Instant::now();
16    while let Ok(packet) = data_reader.poll_next().await {
17        // Optionally parse the packet into a specific variant
18        // and handle it accordingly
19        match packet.try_into() {
20            Ok(PacketVariant::RawWave { .. }) => {}
21            Ok(PacketVariant::EegPower {
22                poor_signal,
23                eeg_power,
24                ..
25            }) => {
26                println!(
27                    "[{:.02?}s]: {poor_signal:?} | {eeg_power:?}",
28                    timer.elapsed().as_secs_f64()
29                );
30            }
31            Err(e) => {
32                eprintln!("Error parsing packet: {:?}", e);
33                continue;
34            }
35        }
36    }
37}