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
//! Data types for events emitted by the IDUN Guardian BLE client.
//!
//! All data from the Guardian earbud is delivered as [`GuardianEvent`] variants
//! through the [`tokio::sync::mpsc::Receiver`] returned by
//! [`GuardianClient::connect`](crate::guardian_client::GuardianClient::connect).
//!
//! # Event flow
//!
//! ```text
//! GuardianClient::connect()
//! → mpsc::Receiver<GuardianEvent>
//! → Connected(name)
//! → DeviceInfo { mac, fw, hw }
//! → Battery { level }
//! → Eeg { index, timestamp, raw_data, samples, decode_source } (every ~80ms)
//! → Accelerometer { index, timestamp, sample } (~52 Hz)
//! → Gyroscope { index, timestamp, sample } (~52 Hz)
//! → Impedance { ohms, kohms, timestamp } (when streaming)
//! → Disconnected
//! ```
/// A single EEG data packet from the Guardian earbud.
///
/// The Guardian is a single-earbud device with a **bipolar montage**: an
/// in-ear-canal electrode (signal) and an outer-ear electrode (reference),
/// both on the same earbud. This yields one EEG channel measuring the
/// voltage difference between the two electrodes. All EEG + IMU data is
/// multiplexed onto a single BLE characteristic.
///
/// Each packet contains up to 20 samples at 250 Hz (80 ms of data).
///
/// # Decoding
///
/// Decoded samples may come from:
/// - **Local decoder** (`local-decode` feature): experimental 12-bit packed format
/// - **Cloud decoder** ([`CloudDecoder`](crate::cloud::CloudDecoder)): IDUN Cloud WebSocket API
/// - **None**: raw bytes only (no decoding attempted)
///
/// Check [`decode_source`](EegReading::decode_source) to determine provenance.
///
/// # Example
///
/// ```rust
/// # use idun::types::{EegReading, DecodeSource};
/// let reading = EegReading {
/// index: 42,
/// timestamp: 1710000000000.0,
/// raw_data: vec![0xAA, 42, 1, 2, 3],
/// samples: Some(vec![0.5, -0.3, 1.2]),
/// decode_source: DecodeSource::Local,
/// };
/// assert_eq!(reading.samples.as_ref().unwrap().len(), 3);
/// ```
/// Indicates how the EEG samples were decoded.
///
/// # Decoding priority
///
/// When both `--decode` and `--cloud` are enabled, the CLI uses this priority:
/// 1. [`Local`](DecodeSource::Local) — experimental 12-bit decoder (instant, no network)
/// 2. [`Cloud`](DecodeSource::Cloud) — IDUN Cloud WebSocket API (authoritative, requires token)
/// 3. [`None`](DecodeSource::None) — raw packet passthrough (always available)
/// A single 3-axis inertial measurement sample.
///
/// Used for both accelerometer (units: g) and gyroscope (units: °/s) readings.
///
/// # Example
///
/// ```rust
/// # use idun::types::XyzSample;
/// let gravity = XyzSample { x: 0.0, y: 0.0, z: -1.0 };
/// assert_eq!(gravity.z, -1.0);
/// ```
/// Accelerometer reading from the Guardian earbud's IMU.
///
/// The Guardian's EEG+IMU characteristic multiplexes inertial data
/// alongside EEG. Accelerometer values are in **g** (1 g ≈ 9.81 m/s²).
///
/// Scale assumes ±2g range with 0.0000610352 g/LSB (common for LSM6DS3-class
/// MEMS IMUs).
///
/// # Example
///
/// ```rust
/// # use idun::types::{AccelerometerReading, XyzSample};
/// let reading = AccelerometerReading {
/// index: 10,
/// timestamp: 1000.0,
/// sample: XyzSample { x: 0.01, y: -0.02, z: -0.98 },
/// };
/// // z ≈ -1g indicates the earbud is roughly upright
/// assert!(reading.sample.z < -0.9);
/// ```
/// Gyroscope reading from the Guardian earbud's IMU.
///
/// Gyroscope values are in **degrees per second** (°/s).
/// Scale assumes ±245 dps range with 0.0074768 °/s/LSB.
///
/// # Example
///
/// ```rust
/// # use idun::types::{GyroscopeReading, XyzSample};
/// let reading = GyroscopeReading {
/// index: 10,
/// timestamp: 1000.0,
/// sample: XyzSample { x: 1.5, y: -0.3, z: 0.0 },
/// };
/// assert_eq!(reading.sample.x, 1.5);
/// ```
/// Impedance measurement from the Guardian earbud.
///
/// Impedance is measured in ohms and indicates electrode-skin contact quality.
/// Lower values indicate better contact. Typical ranges:
///
/// | Quality | Range |
/// |---|---|
/// | Excellent | < 5 kΩ |
/// | Good | 5–10 kΩ |
/// | Acceptable | 10–20 kΩ |
/// | Poor | > 20 kΩ |
///
/// # Example
///
/// ```rust
/// # use idun::types::ImpedanceReading;
/// let reading = ImpedanceReading {
/// impedance_ohms: 5000,
/// impedance_kohms: 5.0,
/// timestamp: 1710000000000.0,
/// };
/// assert_eq!(reading.impedance_kohms, reading.impedance_ohms as f64 / 1000.0);
/// ```
/// Battery level reading.
///
/// The Guardian reports battery as a percentage (0–100) via the standard
/// BLE Battery Level characteristic (UUID `0x2A19`). Polled every 60 seconds
/// and also read once at connection time.
///
/// # Example
///
/// ```rust
/// # use idun::types::BatteryReading;
/// let battery = BatteryReading { level: 87 };
/// assert!(battery.level <= 100);
/// ```
/// Device information retrieved from the Guardian earbud at connection time.
///
/// Read from standard BLE Device Information Service characteristics:
/// - MAC address: UUID `0x2A25` (Serial Number)
/// - Firmware: UUID `0x2A26` (Firmware Revision)
/// - Hardware: UUID `0x2A27` (Hardware Revision)
///
/// # Example
///
/// ```rust
/// # use idun::types::DeviceInfo;
/// let info = DeviceInfo {
/// mac_address: "AA-BB-CC-DD-EE-FF".into(),
/// firmware_version: "1.2.3".into(),
/// hardware_version: "3.0a".into(),
/// };
/// assert!(info.mac_address.contains('-'));
/// ```
/// All data events emitted by the Guardian BLE client.
///
/// Consumers receive these values through the [`mpsc::Receiver`](tokio::sync::mpsc::Receiver)
/// returned by [`GuardianClient::connect`](crate::guardian_client::GuardianClient::connect)
/// or [`GuardianClient::connect_to`](crate::guardian_client::GuardianClient::connect_to).
///
/// # Event order
///
/// After a successful connection, events arrive in this typical order:
/// 1. `Connected(device_name)` — always first
/// 2. `DeviceInfo { mac, firmware, hardware }` — immediately after
/// 3. `Battery { level }` — initial reading
/// 4. `Eeg` / `Accelerometer` / `Gyroscope` — streaming data (after `start_recording()`)
/// 5. `Impedance` — streaming data (after `start_impedance()`)
/// 6. `Disconnected` — BLE link lost
///
/// # Example
///
/// ```no_run
/// # use idun::types::GuardianEvent;
/// # async fn example(mut rx: tokio::sync::mpsc::Receiver<GuardianEvent>) {
/// while let Some(event) = rx.recv().await {
/// match event {
/// GuardianEvent::Eeg(r) => println!("EEG #{}", r.index),
/// GuardianEvent::Battery(b) => println!("Battery: {}%", b.level),
/// GuardianEvent::Disconnected => break,
/// _ => {}
/// }
/// }
/// # }
/// ```