1use std::thread::sleep;
8use std::time::Duration;
9
10use crate::channel::{Channel, NUM_CHANNELS};
11use crate::error::{Error, Result};
12use crate::reg::{self, RegIo};
13
14#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
16pub enum Window {
17 Ms1,
19 Ms100,
21 S1,
23}
24
25impl Window {
26 pub fn index(self) -> usize {
28 self as usize
29 }
30
31 pub fn from_index(i: usize) -> Result<Window> {
33 [Window::Ms1, Window::Ms100, Window::S1].get(i).copied().ok_or_else(|| Error::InvalidArgument(format!("window {i} (0..=2)")))
34 }
35
36 pub fn samples(self) -> u64 {
38 [100_000, 10_000_000, 100_000_000][self.index()]
39 }
40}
41
42#[derive(Clone, Copy, Debug, PartialEq)]
44pub struct WindowStats {
45 pub mean: f64,
47 pub rms: f64,
49 pub min: f64,
51 pub max: f64,
53}
54
55pub fn read_window<R: RegIo + ?Sized>(io: &mut R, window: Window, ch: Channel) -> Result<WindowStats> {
64 let base = reg::STAT_BASE + ((window.index() * 16 + ch.index()) * 0x20) as u16;
65 for _ in 0..20 {
66 let s0 = io.read_reg(reg::STAT_SEQ)?;
68 let v = io.read_regs(base, 5)?;
69 let s1 = io.read_reg(reg::STAT_SEQ)?;
70 if s0 & 1 != 0 || s0 != s1 {
71 continue;
72 }
73 let n = window.samples() as f64;
74 let sum = ((v[1] as u64) << 32 | v[0] as u64) as i64 as f64;
75 let sq = ((v[3] as u64) << 32 | v[2] as u64) as f64;
76 let mean = sum / n;
77 return Ok(WindowStats {
79 mean: mean / 4.0,
80 rms: (sq / n - mean * mean).max(0.0).sqrt() / 4.0,
81 min: (v[4] as u16 as i16) as f64 / 4.0,
82 max: ((v[4] >> 16) as u16 as i16) as f64 / 4.0,
83 });
84 }
85 Err(Error::Device("statistics record changed during every read (register access slower than 1 ms: use AT+READ on the serial port)".into()))
86}
87
88#[derive(Clone, Debug)]
90pub struct RawCapture {
91 pub decimation_log2: u8,
93 pub samples: Vec<Vec<i16>>,
95}
96
97impl RawCapture {
98 pub fn channel(&self, ch: Channel) -> &[i16] {
100 &self.samples[ch.index()]
101 }
102
103 pub fn rate_hz(&self, sample_rate_hz: f64) -> f64 {
105 sample_rate_hz / (1u64 << self.decimation_log2) as f64
106 }
107}
108
109pub fn raw_capture<R: RegIo + ?Sized>(io: &mut R, decimation_log2: u8) -> Result<RawCapture> {
114 if decimation_log2 > 15 {
115 return Err(Error::InvalidArgument(format!("capture decimation 2^{decimation_log2} (0..=15)")));
116 }
117 io.write_regs(&[(reg::CAP_CTRL, (decimation_log2 as u32) << 4), (reg::CAP_CMD, 1)])?;
118 sleep(Duration::from_micros(30_000 + 1024 * (10u64 << decimation_log2) / 1000));
119 let mut busy = true;
120 for _ in 0..400 {
121 if io.read_reg(reg::BOARD)? >> 26 & 1 == 0 {
122 busy = false;
123 break;
124 }
125 sleep(Duration::from_millis(5));
126 }
127 if busy {
128 return Err(Error::Timeout("capture buffer still busy (data plane not running?)".into()));
129 }
130 let mut samples: Vec<Vec<i16>> = (0..NUM_CHANNELS).map(|_| Vec::with_capacity(1024)).collect();
131 for block in 0..16u16 {
133 let w = io.read_regs(reg::CAP_BASE + block * 0x800, 512)?;
134 for i in 0..64 {
135 for (c, col) in samples.iter_mut().enumerate() {
136 let v = (w[8 * i + c / 2] >> (16 * (c & 1))) as u16 as i16;
137 col.push(v >> 2);
138 }
139 }
140 }
141 Ok(RawCapture { decimation_log2, samples })
142}