1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//! Binary decoders for Guardian BLE notification payloads.
//!
//! The Guardian earbud sends EEG/IMU data on the vendor characteristic
//! [`EEG_IMU_CHARACTERISTIC`](crate::protocol::EEG_IMU_CHARACTERISTIC) (`fcc4`).
//! Each BLE notification is a binary packet:
//!
//! ```text
//! byte[0] : packet type / header tag
//! byte[1] : sequence index (0–255, wraps)
//! bytes[2..] : packed measurement samples
//! ```
//!
//! # Decoding strategies
//!
//! | Function | Feature | Description |
//! |---|---|---|
//! | [`parse_eeg_packet`] | always | Extract header (tag + index) and payload |
//! | [`parse_impedance`] | always | Decode impedance from LE unsigned int |
//! | [`try_decode_eeg_12bit`] | `local-decode` | Speculative 12-bit packed EEG decoder |
//! | [`try_decode_imu_i16le`] | `local-decode` | Decode 6×i16 LE as accel + gyro |
//! | [`parse_notification`] | `local-decode` | Heuristic classifier for EEG vs IMU |
//! | [`compute_rms`] | `local-decode` | RMS amplitude for signal quality |
//!
//! # ⚠️ Experimental decoders
//!
//! The Guardian's wire format is proprietary and undocumented. The local
//! decoders use heuristics and speculative formats. For authoritative decoding,
//! use the IDUN Cloud API via [`CloudDecoder`](crate::cloud::CloudDecoder).
use crateXyzSample;
/// Parsed EEG/IMU BLE packet header.
///
/// Extracted by [`parse_eeg_packet`] from the first 2 bytes of a notification.
///
/// # Example
///
/// ```rust
/// # use idun::parse::parse_eeg_packet;
/// let data = vec![0xAA, 42, 1, 2, 3, 4, 5];
/// let header = parse_eeg_packet(&data).unwrap();
/// assert_eq!(header.tag, 0xAA);
/// assert_eq!(header.index, 42);
/// assert_eq!(header.payload, vec![1, 2, 3, 4, 5]);
/// ```
/// Parsed content of a Guardian BLE notification.
///
/// Since EEG and IMU are multiplexed on one characteristic, parsing
/// a single notification may yield EEG data, IMU data, or both.
///
/// Requires the `local-decode` feature.
/// Parse the header from a raw Guardian EEG/IMU BLE notification.
///
/// Returns `None` if the packet is too short (< 2 bytes).
///
/// # Example
///
/// ```rust
/// # use idun::parse::parse_eeg_packet;
/// // Minimum valid packet: just header, no payload
/// let header = parse_eeg_packet(&[0x00, 0xFF]).unwrap();
/// assert_eq!(header.index, 255);
/// assert!(header.payload.is_empty());
///
/// // Too short
/// assert!(parse_eeg_packet(&[0x01]).is_none());
/// ```
/// Attempt to fully parse a Guardian BLE notification into typed data.
///
/// **EXPERIMENTAL**: The Guardian's wire format is proprietary. This parser
/// uses heuristics on the header tag byte and payload length to classify
/// packets as EEG or IMU data.
///
/// Returns a list of [`ParsedPacket`] variants extracted from the notification.
/// A single notification may produce multiple results if the format packs
/// both EEG and IMU into one packet.
///
/// # Heuristics
///
/// - Payloads ≥ 18 bytes: attempt 12-bit EEG decoding
/// - Payloads ≥ 12 bytes: attempt IMU extraction from last 12 bytes
/// - Otherwise: return [`ParsedPacket::Unknown`]
///
/// Requires the `local-decode` feature.
/// Parse an impedance BLE notification into ohms.
///
/// The earbud sends impedance as a little-endian unsigned integer
/// on characteristic [`IMPEDANCE_CHARACTERISTIC`](crate::protocol::IMPEDANCE_CHARACTERISTIC).
/// The payload length varies (1–4 bytes).
///
/// Returns `None` for empty payloads.
///
/// # Example
///
/// ```rust
/// # use idun::parse::parse_impedance;
/// assert_eq!(parse_impedance(&[0x88, 0x13, 0x00, 0x00]), Some(5000));
/// assert_eq!(parse_impedance(&[200]), Some(200));
/// assert!(parse_impedance(&[]).is_none());
/// ```
/// Attempt to decode EEG samples from the payload using a speculative
/// 12-bit packed unsigned format.
///
/// **EXPERIMENTAL**: Assumes 12-bit big-endian packed layout where every
/// 3 bytes encode 2 samples:
///
/// ```text
/// sample₀ = (byte[0] << 4) | (byte[1] >> 4)
/// sample₁ = ((byte[1] & 0x0F) << 8) | byte[2]
/// µV = 0.48828125 × (raw₁₂ − 2048)
/// ```
///
/// Mid-scale offset is 2048 (12-bit midpoint). Scale factor 0.48828125 µV/LSB
/// yields a range of approximately ±1000 µV.
///
/// Partial groups (< 3 remaining bytes) are silently ignored.
///
/// Requires the `local-decode` feature.
///
/// # Example
///
/// ```rust
/// # #[cfg(feature = "local-decode")]
/// # {
/// # use idun::parse::try_decode_eeg_12bit;
/// // Mid-scale for both samples → 0.0 µV
/// let samples = try_decode_eeg_12bit(&[0x80, 0x08, 0x00]);
/// assert_eq!(samples.len(), 2);
/// assert!((samples[0] - 0.0).abs() < 0.001);
/// # }
/// ```
/// Attempt to decode 6 × i16 LE values as accelerometer + gyroscope.
///
/// Expects at least 12 bytes arranged as:
///
/// ```text
/// [ax_lo, ax_hi, ay_lo, ay_hi, az_lo, az_hi,
/// gx_lo, gx_hi, gy_lo, gy_hi, gz_lo, gz_hi]
/// ```
///
/// Returns `(accelerometer, gyroscope)` as [`XyzSample`] pairs.
///
/// # Scale factors
///
/// Assumes common MEMS IMU configuration (e.g. LSM6DS3):
/// - **Accelerometer**: ±2g range → 0.0000610352 g/LSB (2/32768)
/// - **Gyroscope**: ±245 dps range → 0.0074768 °/s/LSB (245/32768)
///
/// Returns `None` if `data.len() < 12`.
///
/// Requires the `local-decode` feature.
///
/// # Example
///
/// ```rust
/// # #[cfg(feature = "local-decode")]
/// # {
/// # use idun::parse::try_decode_imu_i16le;
/// let data = [0u8; 12]; // all zeros
/// let (accel, gyro) = try_decode_imu_i16le(&data).unwrap();
/// assert_eq!(accel.x, 0.0);
/// assert_eq!(gyro.z, 0.0);
/// # }
/// ```
/// Compute the RMS (root mean square) amplitude of EEG samples.
///
/// Useful as a simple signal quality metric. Typical EEG RMS values:
///
/// | Condition | RMS range |
/// |---|---|
/// | Eyes closed (alpha) | 10–30 µV |
/// | Eyes open (beta) | 5–15 µV |
/// | Movement artifact | 50–500 µV |
/// | Blink artifact | 100–300 µV |
///
/// Returns `0.0` for empty input.
///
/// Requires the `local-decode` feature.
///
/// # Example
///
/// ```rust
/// # #[cfg(feature = "local-decode")]
/// # {
/// # use idun::parse::compute_rms;
/// assert!((compute_rms(&[10.0, 10.0, 10.0]) - 10.0).abs() < 0.001);
/// assert_eq!(compute_rms(&[]), 0.0);
/// # }
/// ```