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
//! The [`Sample`] type and the [`StreamHandle`] streaming interface.
use ;
/// A single decoded data sample returned by any OpenBCI board.
///
/// All voltage values are in **microvolts (µV)**, accelerometer values in
/// **g**, and timestamps as **seconds since the UNIX epoch** (host clock).
///
/// The `eeg` vector length always equals the board's channel count:
/// 4 (Ganglion), 8 (Cyton), 16 (Cyton+Daisy), or 24 (Galea).
///
/// # Which aux fields are populated?
///
/// The Cyton's 33-byte packet includes 6 auxiliary bytes whose meaning
/// depends on the end byte:
///
/// | `end_byte` | `accel` | `analog` |
/// |---|---|---|
/// | `0xC0` | ✅ 3-axis accelerometer | — |
/// | `0xC1` | — | ✅ 3 analog pin readings |
/// | `0xC2`–`0xC6` | — | — |
///
/// The Ganglion always populates `accel`. Galea populates neither.
/// Return the current UNIX timestamp in seconds with sub-second precision.
///
/// Used internally to timestamp samples as they are decoded.
// ─────────────────────────────────────────────────────────────────────────────
/// Owned handle to a running data stream returned by [`crate::Board::start_stream`].
///
/// Internally this wraps an `mpsc` channel fed by a background reader thread.
/// The stream **stops automatically** when the handle is dropped (via the `Drop`
/// impl), so you never need to call anything to clean up.
///
/// # Usage patterns
///
/// **Blocking iterator** — simplest, processes every sample in order:
/// ```rust,no_run
/// # use openbci::sample::{Sample, StreamHandle};
/// # fn get_stream() -> StreamHandle { unimplemented!() }
/// let stream: StreamHandle = get_stream();
/// for sample in stream.into_iter().take(250) {
/// println!("{:?}", sample.eeg);
/// }
/// ```
///
/// **Non-blocking polling** — useful when you need to do other work between samples:
/// ```rust,no_run
/// # use openbci::sample::{Sample, StreamHandle};
/// # fn get_stream() -> StreamHandle { unimplemented!() }
/// let stream: StreamHandle = get_stream();
/// loop {
/// if let Some(s) = stream.try_recv() {
/// println!("{:?}", s.eeg);
/// }
/// // ... other work ...
/// }
/// ```
///
/// **Explicit stop** (before drop):
/// ```rust,no_run
/// # use openbci::sample::{Sample, StreamHandle};
/// # fn get_stream() -> StreamHandle { unimplemented!() }
/// let stream: StreamHandle = get_stream();
/// std::thread::sleep(std::time::Duration::from_secs(5));
/// stream.stop(); // sends the stop signal immediately; `drop` is a no-op after this
/// ```
/// Consuming iterator over a [`StreamHandle`].
///
/// Created by `stream_handle.into_iter()`. Blocks on each call to `next()`
/// until a sample arrives or the stream ends.
/// Iterator produced by [`StreamHandle::into_iter`].
;