mirror of
https://github.com/bleackstorm/SteamFrame-FPV-Control-Center.git
synced 2026-12-03 07:31:57 +01:00
refactor(review): harden audio OnceLock/buffer, NaN-safe XR spatial anchor, and Copy VrxLinkStats
This commit is contained in:
1 parent
dfa142c2d1
commit
cf1197a33b
5 files changed
+78
-37
No files matched your search
@@ -42,7 +42,7 @@ const VRX_TIMEOUT_MS: u64 = 3000;
|
|||||||
const TOAST_DURATION_MS: u64 = 1500;
|
const TOAST_DURATION_MS: u64 = 1500;
|
||||||
|
|
||||||
/// Télémétrie temps réel de la liaison radio WFB-ng et du SoC Radxa Zero 3W (`$VRX,...`).
|
/// Télémétrie temps réel de la liaison radio WFB-ng et du SoC Radxa Zero 3W (`$VRX,...`).
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct VrxLinkStats {
|
pub struct VrxLinkStats {
|
||||||
pub temp_c: f32,
|
pub temp_c: f32,
|
||||||
pub rssi_a_dbm: i16,
|
pub rssi_a_dbm: i16,
|
||||||
@@ -421,7 +421,11 @@ impl G29BridgeReceiver {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let mut st = state.lock();
|
let mut st = state.lock();
|
||||||
st.vrx.temp_c = if temp_c.is_finite() { temp_c } else { -1.0 };
|
st.vrx.temp_c = if temp_c.is_finite() && (-40.0..=150.0).contains(&temp_c) {
|
||||||
|
temp_c
|
||||||
|
} else {
|
||||||
|
-1.0
|
||||||
|
};
|
||||||
st.vrx.rssi_a_dbm = rssi_a.clamp(-127, 0);
|
st.vrx.rssi_a_dbm = rssi_a.clamp(-127, 0);
|
||||||
st.vrx.rssi_b_dbm = rssi_b.clamp(-127, 0);
|
st.vrx.rssi_b_dbm = rssi_b.clamp(-127, 0);
|
||||||
st.vrx.fec_recovered = fec_rec;
|
st.vrx.fec_recovered = fec_rec;
|
||||||
|
|||||||
+19
-25
@@ -5,7 +5,7 @@ use parking_lot::Mutex;
|
|||||||
use std::f32::consts::TAU;
|
use std::f32::consts::TAU;
|
||||||
use std::sync::atomic::{AtomicBool, AtomicU8, Ordering};
|
use std::sync::atomic::{AtomicBool, AtomicU8, Ordering};
|
||||||
use std::sync::mpsc::{self, SyncSender};
|
use std::sync::mpsc::{self, SyncSender};
|
||||||
use std::sync::Arc;
|
use std::sync::{Arc, OnceLock};
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
@@ -123,7 +123,8 @@ pub fn synthesize_alert_pcm_s16le(kind: AudioAlertKind, volume_pct: u8) -> Vec<u
|
|||||||
.iter()
|
.iter()
|
||||||
.map(|seg| ((seg.duration_ms as u64 * AUDIO_SAMPLE_RATE as u64) / 1000) as usize)
|
.map(|seg| ((seg.duration_ms as u64 * AUDIO_SAMPLE_RATE as u64) / 1000) as usize)
|
||||||
.sum();
|
.sum();
|
||||||
let mut bytes = Vec::with_capacity(total_samples * 2);
|
let tail_silence = ((AUDIO_SAMPLE_RATE as usize) * 10) / 1000; // 10 ms anti-clic en fin de salve
|
||||||
|
let mut bytes = Vec::with_capacity((total_samples + tail_silence) * 2);
|
||||||
let fade_samples = ((AUDIO_SAMPLE_RATE as usize) * 5) / 1000; // 5 ms attack/release
|
let fade_samples = ((AUDIO_SAMPLE_RATE as usize) * 5) / 1000; // 5 ms attack/release
|
||||||
|
|
||||||
for seg in pattern {
|
for seg in pattern {
|
||||||
@@ -147,6 +148,7 @@ pub fn synthesize_alert_pcm_s16le(kind: AudioAlertKind, volume_pct: u8) -> Vec<u
|
|||||||
bytes.extend_from_slice(&sample.to_le_bytes());
|
bytes.extend_from_slice(&sample.to_le_bytes());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
bytes.resize(bytes.len() + tail_silence * 2, 0);
|
||||||
bytes
|
bytes
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -177,7 +179,7 @@ pub struct AudioAlertEngine {
|
|||||||
enabled: Arc<AtomicBool>,
|
enabled: Arc<AtomicBool>,
|
||||||
volume_pct: Arc<AtomicU8>,
|
volume_pct: Arc<AtomicU8>,
|
||||||
tracker: Mutex<AlertTracker>,
|
tracker: Mutex<AlertTracker>,
|
||||||
tx: Mutex<Option<SyncSender<(AudioAlertKind, u8)>>>,
|
tx: OnceLock<SyncSender<(AudioAlertKind, u8)>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AudioAlertEngine {
|
impl AudioAlertEngine {
|
||||||
@@ -186,7 +188,7 @@ impl AudioAlertEngine {
|
|||||||
enabled: Arc::new(AtomicBool::new(initial_enabled)),
|
enabled: Arc::new(AtomicBool::new(initial_enabled)),
|
||||||
volume_pct: Arc::new(AtomicU8::new(initial_volume_pct.min(100))),
|
volume_pct: Arc::new(AtomicU8::new(initial_volume_pct.min(100))),
|
||||||
tracker: Mutex::new(AlertTracker::default()),
|
tracker: Mutex::new(AlertTracker::default()),
|
||||||
tx: Mutex::new(None),
|
tx: OnceLock::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -223,10 +225,16 @@ impl AudioAlertEngine {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.ensure_worker_started();
|
let sender = self.tx.get_or_init(|| {
|
||||||
if let Some(ref sender) = *self.tx.lock() {
|
let (tx, rx) = mpsc::sync_channel::<(AudioAlertKind, u8)>(8);
|
||||||
let _ = sender.try_send((kind, vol));
|
let _ = thread::Builder::new()
|
||||||
}
|
.name("audio-alerts".to_string())
|
||||||
|
.spawn(move || {
|
||||||
|
audio_worker_loop(rx);
|
||||||
|
});
|
||||||
|
tx
|
||||||
|
});
|
||||||
|
let _ = sender.try_send((kind, vol));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Évalue les transitions d'armement et les seuils télémétriques (batterie LiPo + LQ < 50 %)
|
/// Évalue les transitions d'armement et les seuils télémétriques (batterie LiPo + LQ < 50 %)
|
||||||
@@ -272,22 +280,6 @@ impl AudioAlertEngine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ensure_worker_started(&self) {
|
|
||||||
let mut guard = self.tx.lock();
|
|
||||||
if guard.is_some() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let (tx, rx) = mpsc::sync_channel::<(AudioAlertKind, u8)>(8);
|
|
||||||
*guard = Some(tx);
|
|
||||||
drop(guard);
|
|
||||||
|
|
||||||
let _ = thread::Builder::new()
|
|
||||||
.name("audio-alerts".to_string())
|
|
||||||
.spawn(move || {
|
|
||||||
audio_worker_loop(rx);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn audio_worker_loop(rx: mpsc::Receiver<(AudioAlertKind, u8)>) {
|
fn audio_worker_loop(rx: mpsc::Receiver<(AudioAlertKind, u8)>) {
|
||||||
@@ -334,7 +326,9 @@ fn audio_worker_loop(rx: mpsc::Receiver<(AudioAlertKind, u8)>) {
|
|||||||
if pcm.is_empty() {
|
if pcm.is_empty() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let mut buf = gst::Buffer::with_size(pcm.len()).unwrap_or_else(|_| gst::Buffer::new());
|
let Ok(mut buf) = gst::Buffer::with_size(pcm.len()) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
if let Some(buf_ref) = buf.get_mut() {
|
if let Some(buf_ref) = buf.get_mut() {
|
||||||
if let Ok(mut map) = buf_ref.map_writable() {
|
if let Ok(mut map) = buf_ref.map_writable() {
|
||||||
map.as_mut_slice().copy_from_slice(&pcm);
|
map.as_mut_slice().copy_from_slice(&pcm);
|
||||||
|
|||||||
@@ -737,6 +737,9 @@ impl EngineControlHandle {
|
|||||||
(VrMenuPage::Advanced, 8) => {
|
(VrMenuPage::Advanced, 8) => {
|
||||||
if is_activate {
|
if is_activate {
|
||||||
let next = !self.shared.audio_alerts.is_enabled();
|
let next = !self.shared.audio_alerts.is_enabled();
|
||||||
|
if next && self.shared.audio_alerts.volume_pct() == 0 {
|
||||||
|
self.shared.audio_alerts.set_volume_pct(70);
|
||||||
|
}
|
||||||
self.set_audio_alerts_enabled(next);
|
self.set_audio_alerts_enabled(next);
|
||||||
if next {
|
if next {
|
||||||
self.trigger_audio_test_beep();
|
self.trigger_audio_test_beep();
|
||||||
|
|||||||
+1
-1
@@ -664,7 +664,7 @@ fn sync_controller_status(
|
|||||||
if active_src == ControlSource::SteamController {
|
if active_src == ControlSource::SteamController {
|
||||||
let hub_failsafe = !g29_snapshot.is_live() || g29_snapshot.failsafe;
|
let hub_failsafe = !g29_snapshot.is_live() || g29_snapshot.failsafe;
|
||||||
let mut hud = ctrl_state.to_hud_g29_state(hub_failsafe);
|
let mut hud = ctrl_state.to_hud_g29_state(hub_failsafe);
|
||||||
hud.vrx = g29_snapshot.vrx.clone();
|
hud.vrx = g29_snapshot.vrx;
|
||||||
hud
|
hud
|
||||||
} else {
|
} else {
|
||||||
g29_snapshot.clone()
|
g29_snapshot.clone()
|
||||||
|
|||||||
@@ -511,13 +511,21 @@ pub fn try_run_openxr_session(ctx: &mut EngineWorkerRefs<'_>) -> Result<()> {
|
|||||||
let recenter_now = ctx.shared.recenter_flag.swap(false, Ordering::AcqRel);
|
let recenter_now = ctx.shared.recenter_flag.swap(false, Ordering::AcqRel);
|
||||||
let head_pose_local = if orientation_valid {
|
let head_pose_local = if orientation_valid {
|
||||||
let q = location.pose.orientation;
|
let q = location.pose.orientation;
|
||||||
let quat = Quat::from_xyzw(q.x, q.y, q.z, q.w).normalize();
|
let quat = safe_normalize_quat(
|
||||||
|
Quat::from_xyzw(q.x, q.y, q.z, q.w),
|
||||||
|
Quat::IDENTITY,
|
||||||
|
);
|
||||||
let pos = if position_valid {
|
let pos = if position_valid {
|
||||||
Vec3::new(
|
let candidate = Vec3::new(
|
||||||
location.pose.position.x,
|
location.pose.position.x,
|
||||||
location.pose.position.y,
|
location.pose.position.y,
|
||||||
location.pose.position.z,
|
location.pose.position.z,
|
||||||
)
|
);
|
||||||
|
if candidate.is_finite() {
|
||||||
|
candidate
|
||||||
|
} else {
|
||||||
|
Vec3::ZERO
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Vec3::ZERO
|
Vec3::ZERO
|
||||||
};
|
};
|
||||||
@@ -1155,6 +1163,16 @@ fn resolve_projection(
|
|||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
fn _unused_check(_: OsdExtraOptions) {}
|
fn _unused_check(_: OsdExtraOptions) {}
|
||||||
|
|
||||||
|
/// Normalise un quaternion en protégeant contre les quaternions nuls ou non-finis (`NaN`/`Inf`).
|
||||||
|
#[inline]
|
||||||
|
fn safe_normalize_quat(q: Quat, fallback: Quat) -> Quat {
|
||||||
|
if q.is_finite() && q.length_squared() > 1e-8 {
|
||||||
|
q.normalize()
|
||||||
|
} else {
|
||||||
|
fallback
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Extrait uniquement la rotation horizontale (Yaw autour de +Y, pitch=0, roll=0)
|
/// Extrait uniquement la rotation horizontale (Yaw autour de +Y, pitch=0, roll=0)
|
||||||
/// pour maintenir l'horizon parfaitement de niveau en mode World-Locked (`LOCAL`).
|
/// pour maintenir l'horizon parfaitement de niveau en mode World-Locked (`LOCAL`).
|
||||||
pub(crate) fn yaw_only_quat(q: Quat) -> Quat {
|
pub(crate) fn yaw_only_quat(q: Quat) -> Quat {
|
||||||
@@ -1225,6 +1243,7 @@ pub(crate) struct SpatialAnchorState {
|
|||||||
last_mode: Option<VrLockMode>,
|
last_mode: Option<VrLockMode>,
|
||||||
world_anchor: Option<(Quat, Vec3)>,
|
world_anchor: Option<(Quat, Vec3)>,
|
||||||
smooth_pose: Option<(Quat, Vec3)>,
|
smooth_pose: Option<(Quat, Vec3)>,
|
||||||
|
pending_recenter: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SpatialAnchorState {
|
impl SpatialAnchorState {
|
||||||
@@ -1238,13 +1257,20 @@ impl SpatialAnchorState {
|
|||||||
) -> LayerFramePose {
|
) -> LayerFramePose {
|
||||||
let mode_changed = self.last_mode != Some(mode);
|
let mode_changed = self.last_mode != Some(mode);
|
||||||
self.last_mode = Some(mode);
|
self.last_mode = Some(mode);
|
||||||
|
if recenter_now {
|
||||||
|
self.pending_recenter = true;
|
||||||
|
}
|
||||||
|
|
||||||
match mode {
|
match mode {
|
||||||
VrLockMode::Head => LayerFramePose::VIEW_IDENTITY,
|
VrLockMode::Head => {
|
||||||
|
self.pending_recenter = false;
|
||||||
|
LayerFramePose::VIEW_IDENTITY
|
||||||
|
}
|
||||||
VrLockMode::World => {
|
VrLockMode::World => {
|
||||||
if let Some((head_ori, head_pos)) = head_pose_local {
|
if let Some((head_ori, head_pos)) = head_pose_local {
|
||||||
if mode_changed || recenter_now || self.world_anchor.is_none() {
|
if mode_changed || self.pending_recenter || self.world_anchor.is_none() {
|
||||||
self.world_anchor = Some((yaw_only_quat(head_ori), head_pos));
|
self.world_anchor = Some((yaw_only_quat(head_ori), head_pos));
|
||||||
|
self.pending_recenter = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some((ori, pos)) = self.world_anchor {
|
if let Some((ori, pos)) = self.world_anchor {
|
||||||
@@ -1259,14 +1285,28 @@ impl SpatialAnchorState {
|
|||||||
}
|
}
|
||||||
VrLockMode::Smooth => {
|
VrLockMode::Smooth => {
|
||||||
if let Some((head_ori, head_pos)) = head_pose_local {
|
if let Some((head_ori, head_pos)) = head_pose_local {
|
||||||
let s = smoothing.clamp(0.0, 1.0);
|
let s = if smoothing.is_finite() {
|
||||||
if mode_changed || recenter_now || self.smooth_pose.is_none() || s <= 0.001 {
|
smoothing.clamp(0.0, 1.0)
|
||||||
|
} else {
|
||||||
|
0.35
|
||||||
|
};
|
||||||
|
if mode_changed
|
||||||
|
|| self.pending_recenter
|
||||||
|
|| self.smooth_pose.is_none()
|
||||||
|
|| s <= 0.001
|
||||||
|
{
|
||||||
self.smooth_pose = Some((head_ori, head_pos));
|
self.smooth_pose = Some((head_ori, head_pos));
|
||||||
|
self.pending_recenter = false;
|
||||||
} else if let Some((cur_ori, cur_pos)) = self.smooth_pose {
|
} else if let Some((cur_ori, cur_pos)) = self.smooth_pose {
|
||||||
let dt = dt_secs.clamp(0.001, 0.1);
|
let dt = if dt_secs.is_finite() {
|
||||||
|
dt_secs.clamp(0.001, 0.1)
|
||||||
|
} else {
|
||||||
|
1.0 / 90.0
|
||||||
|
};
|
||||||
let tau = 0.02 + s * s * 0.48;
|
let tau = 0.02 + s * s * 0.48;
|
||||||
let alpha = (1.0 - (-dt / tau).exp()).clamp(0.0, 1.0);
|
let alpha = (1.0 - (-dt / tau).exp()).clamp(0.0, 1.0);
|
||||||
let next_ori = cur_ori.slerp(head_ori, alpha).normalize();
|
let next_ori =
|
||||||
|
safe_normalize_quat(cur_ori.slerp(head_ori, alpha), head_ori);
|
||||||
let next_pos = cur_pos.lerp(head_pos, alpha);
|
let next_pos = cur_pos.lerp(head_pos, alpha);
|
||||||
self.smooth_pose = Some((next_ori, next_pos));
|
self.smooth_pose = Some((next_ori, next_pos));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user