import { initEegWasm, band_powers, AthenaWasmDecoder } from "@elata-biosciences/eeg-web";
import type { HeadbandFrameV1 } from "@elata-biosciences/eeg-web";
import { BleTransport } from "@elata-biosciences/eeg-web-ble";
// Step 1: Initialize WASM
await initEegWasm();
// Step 2: Create transport with Athena support
const transport = new BleTransport({
sourceName: "my-app",
deviceOptions: {
athenaDecoderFactory: () => new AthenaWasmDecoder(),
logger: (msg) => console.debug("[BLE]", msg),
},
});
// Step 3: Handle status changes
transport.onStatus = (status) => {
const el = document.getElementById("status");
if (el) el.textContent = status.state;
};
// Step 4: Process incoming frames
transport.onFrame = (frame: HeadbandFrameV1) => {
const { eeg } = frame;
// Process each channel
for (let ch = 0; ch < eeg.channelCount; ch++) {
const channelSamples = eeg.samples.map(row => row[ch]);
const powers = band_powers(new Float64Array(channelSamples), eeg.sampleRateHz);
console.log(`${eeg.channelNames[ch]}: α=${powers.alpha.toFixed(2)} β=${powers.beta.toFixed(2)}`);
}
};
// Step 5: Connect and stream
try {
await transport.connect(); // opens Bluetooth picker
await transport.start(); // begins streaming
} catch (err) {
console.error("Connection failed:", err);
}