Skip to main content

scdsu_core/
reader.rs

1//! Provides a background reader for reading [`DSUFrame`](crate::dsu::DSUFrame) data from devices.
2
3use std::sync::{Arc, atomic, mpsc};
4use std::thread;
5use std::time::{Duration, Instant};
6
7use crate::READ_ATOMIC_BOOL_ORDERING;
8use crate::devices::Device;
9use crate::dsu::DSUFrame;
10use crate::errors::DeviceError;
11
12/// Number of identical IMU frames before we consider the IMU frozen.
13/// At 100 Hz this is 1 second
14const FROZEN_DETECT_THRESHOLD: usize = 100;
15/// Retry interval for re-initializing the device when the IMU is frozen
16const REINIT_RETRY_INTERVAL: Duration = Duration::from_secs(1);
17/// Number of consecutive failed reads before assuming disconnect.
18/// At 100Hz this is ~1 second of no data.
19const DISCONNECT_THRESHOLD: usize = 100;
20
21/// Spawn a thread that reads from `device` and sends parsed frames over the returned channel.
22///
23/// Returns a [`JoinHandle`](std::thread::JoinHandle) and a mpsc Receiver for receiving frame data.
24pub fn spawn_reader(
25    running: Arc<atomic::AtomicBool>,
26    device: impl Device + std::marker::Send + 'static,
27) -> (std::thread::JoinHandle<()>, mpsc::Receiver<DSUFrame>) {
28    let (tx, rx) = mpsc::channel::<DSUFrame>();
29
30    let handle = thread::spawn(move || {
31        let mut frame_state = FrameState::new();
32
33        log::debug!("Reader thread started");
34
35        while running.load(READ_ATOMIC_BOOL_ORDERING) {
36            if !read_frame(&device, &mut frame_state, &tx) {
37                break;
38            }
39        }
40
41        log::debug!(
42            "Reader thread finished after {} frames",
43            frame_state.total_frames
44        );
45    });
46
47    (handle, rx)
48}
49
50struct FrameState {
51    pub frozen_count: usize,
52    pub total_frames: usize,
53    pub prev_frame: Option<DSUFrame>,
54    pub fail_count: usize,
55    pub last_init_attempt: Option<Instant>,
56}
57
58impl FrameState {
59    pub fn new() -> Self {
60        Self {
61            frozen_count: 0,
62            total_frames: 0,
63            prev_frame: None,
64            fail_count: 0,
65            last_init_attempt: None,
66        }
67    }
68}
69
70/// Read a frame, returning true if another should be read.
71fn read_frame<D>(device: &D, frame_state: &mut FrameState, tx: &mpsc::Sender<DSUFrame>) -> bool
72where
73    D: Device + std::marker::Send + 'static,
74{
75    match device.read_frame() {
76        Ok(frame) => {
77            frame_state.fail_count = 0;
78            frame_state.total_frames += 1;
79
80            // Check for frozen/stale IMU data
81            // This is observed behavior when Steam disables the IMU on Steam devices
82            let is_imu_frozen = frame_state
83                .prev_frame
84                .map(|prev| {
85                    frame.accel_x == prev.accel_x
86                        && frame.accel_y == prev.accel_y
87                        && frame.accel_z == prev.accel_z
88                        && frame.gyro_x == prev.gyro_x
89                        && frame.gyro_y == prev.gyro_y
90                        && frame.gyro_z == prev.gyro_z
91                })
92                .unwrap_or(false);
93
94            let mut frame_to_send = frame;
95
96            if is_imu_frozen {
97                frame_state.frozen_count += 1;
98
99                if frame_state.frozen_count == FROZEN_DETECT_THRESHOLD {
100                    log::warn!(
101                        "IMU data frozen ({} identical frames). Steam likely disabled the IMU.",
102                        frame_state.frozen_count
103                    );
104                }
105
106                // Periodically attempt to re-enable the IMU
107                if frame_state.frozen_count >= FROZEN_DETECT_THRESHOLD {
108                    let should_try = frame_state
109                        .last_init_attempt
110                        .map(|t| t.elapsed() >= REINIT_RETRY_INTERVAL)
111                        .unwrap_or(true);
112                    if should_try {
113                        frame_state.last_init_attempt = Some(Instant::now());
114                        if let Err(e) = device.initialize() {
115                            log::warn!("Failed to reinitialize device while IMU frozen: {e}");
116                        } else {
117                            log::info!("Reinitialized device while IMU was frozen.");
118                        }
119                    }
120                }
121
122                // Zero out motion data so clients don't drift on stale values
123                frame_to_send.accel_x = 0.0;
124                frame_to_send.accel_y = 0.0;
125                frame_to_send.accel_z = 0.0;
126                frame_to_send.gyro_x = 0.0;
127                frame_to_send.gyro_y = 0.0;
128                frame_to_send.gyro_z = 0.0;
129            } else {
130                frame_state.frozen_count = 0;
131                frame_state.last_init_attempt = None;
132            }
133
134            frame_state.prev_frame = Some(frame);
135
136            if tx.send(frame_to_send).is_err() {
137                log::debug!("Receiver has hung up, reader thread exiting");
138                return false;
139            }
140        }
141        Err(DeviceError::ShortRead(n, expected)) => {
142            log::trace!("Short read: {} bytes (expected {})", n, expected);
143            frame_state.fail_count += 1;
144        }
145        Err(DeviceError::InvalidReport(id)) => {
146            log::trace!("Ignoring invalid report (first byte: 0x{:02x})", id);
147            frame_state.fail_count = 0;
148        }
149        Err(e) => {
150            log::trace!("HID read error: {}", e);
151            frame_state.fail_count += 1;
152        }
153    }
154
155    if frame_state.fail_count >= DISCONNECT_THRESHOLD {
156        log::warn!(
157            "Controller appears disconnected ({} consecutive read failures). Exiting reader.",
158            frame_state.fail_count,
159        );
160        return false;
161    }
162
163    true
164}