mirror of
https://github.com/bleackstorm/SteamFrame-FPV-Control-Center.git
synced 2026-12-03 07:31:57 +01:00
feat(unified): add 100% wired Radxa/G29 hub (HT, cockpit OSD, CRSF telemetry) & update README
This commit is contained in:
1 parent
f01bf1aeb3
commit
51a5d0c7ad
10 files changed
+1012
-26
No files matched your search
@@ -0,0 +1,491 @@
|
||||
use crate::engine::EngineControlHandle;
|
||||
use crate::telemetry_osd::{TelemetryReceiver, TelemetryState};
|
||||
use anyhow::Result;
|
||||
use parking_lot::Mutex;
|
||||
use std::net::UdpSocket;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
const G29_TIMEOUT_MS: u64 = 1500;
|
||||
const TOAST_DURATION_MS: u64 = 1500;
|
||||
|
||||
/// État temps réel reçu depuis le volant G29 (RP2350 -> ESP32-C3 -> UDP)
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct G29State {
|
||||
pub connected: bool,
|
||||
pub failsafe: bool,
|
||||
pub armed: bool,
|
||||
pub gear: i8,
|
||||
pub throttle_pct: u8,
|
||||
pub brake_pct: u8,
|
||||
pub ffb_raw: u8,
|
||||
pub ffb_pct: u8,
|
||||
pub vib_pct: u8,
|
||||
pub packets_received: u64,
|
||||
pub last_packet: Option<Instant>,
|
||||
pub toast_text: String,
|
||||
pub toast_until: Option<Instant>,
|
||||
}
|
||||
|
||||
impl Default for G29State {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
connected: false,
|
||||
failsafe: false,
|
||||
armed: false,
|
||||
gear: 0,
|
||||
throttle_pct: 0,
|
||||
brake_pct: 0,
|
||||
ffb_raw: 140,
|
||||
ffb_pct: 55,
|
||||
vib_pct: 50,
|
||||
packets_received: 0,
|
||||
last_packet: None,
|
||||
toast_text: String::new(),
|
||||
toast_until: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl G29State {
|
||||
pub fn is_live(&self) -> bool {
|
||||
self.connected
|
||||
&& self
|
||||
.last_packet
|
||||
.map(|t| t.elapsed() <= Duration::from_millis(G29_TIMEOUT_MS))
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
pub fn set_toast(&mut self, msg: impl Into<String>) {
|
||||
self.toast_text = msg.into();
|
||||
self.toast_until = Some(Instant::now() + Duration::from_millis(TOAST_DURATION_MS));
|
||||
}
|
||||
|
||||
pub fn active_toast(&self) -> Option<&str> {
|
||||
match self.toast_until {
|
||||
Some(until) if Instant::now() < until && !self.toast_text.is_empty() => {
|
||||
Some(&self.toast_text)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn gear_label(&self) -> &'static str {
|
||||
match self.gear {
|
||||
-1 => "R",
|
||||
0 => "N",
|
||||
1 => "1",
|
||||
2 => "2",
|
||||
3 => "3",
|
||||
_ => "?",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum G29ButtonEvent {
|
||||
Cross,
|
||||
Circle,
|
||||
Square,
|
||||
Triangle,
|
||||
Share,
|
||||
Options,
|
||||
Ps,
|
||||
L3,
|
||||
R3,
|
||||
DpadUp,
|
||||
DpadDown,
|
||||
DpadLeft,
|
||||
DpadRight,
|
||||
}
|
||||
|
||||
impl G29ButtonEvent {
|
||||
pub fn from_name(name: &str) -> Option<Self> {
|
||||
match name.trim() {
|
||||
"CROSS" => Some(Self::Cross),
|
||||
"CIRCLE" => Some(Self::Circle),
|
||||
"SQUARE" => Some(Self::Square),
|
||||
"TRIANGLE" => Some(Self::Triangle),
|
||||
"SHARE" => Some(Self::Share),
|
||||
"OPTIONS" => Some(Self::Options),
|
||||
"PS" => Some(Self::Ps),
|
||||
"L3" => Some(Self::L3),
|
||||
"R3" => Some(Self::R3),
|
||||
"DPAD_UP" => Some(Self::DpadUp),
|
||||
"DPAD_DOWN" => Some(Self::DpadDown),
|
||||
"DPAD_LEFT" => Some(Self::DpadLeft),
|
||||
"DPAD_RIGHT" => Some(Self::DpadRight),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Récepteur UDP non-bloquant pour la passerelle Wi-Fi G29
|
||||
pub struct G29BridgeReceiver {
|
||||
state: Arc<Mutex<G29State>>,
|
||||
stop_flag: Arc<AtomicBool>,
|
||||
worker: Option<thread::JoinHandle<()>>,
|
||||
}
|
||||
|
||||
impl G29BridgeReceiver {
|
||||
pub fn start(
|
||||
bind_ip: &str,
|
||||
port: u16,
|
||||
control: EngineControlHandle,
|
||||
telemetry: Option<Arc<Mutex<TelemetryState>>>,
|
||||
) -> Result<Self> {
|
||||
let addr = format!("{}:{}", bind_ip, port);
|
||||
let socket = UdpSocket::bind(&addr)?;
|
||||
socket.set_read_timeout(Some(Duration::from_millis(100)))?;
|
||||
log::info!("[G29 Bridge] En écoute UDP unifiée sur {}", addr);
|
||||
|
||||
let state = Arc::new(Mutex::new(G29State::default()));
|
||||
let state_clone = Arc::clone(&state);
|
||||
let stop_flag = Arc::new(AtomicBool::new(false));
|
||||
let stop_clone = Arc::clone(&stop_flag);
|
||||
|
||||
let worker = thread::Builder::new()
|
||||
.name("g29-udp-rx".into())
|
||||
.spawn(move || {
|
||||
let mut buf = [0u8; 512];
|
||||
while !stop_clone.load(Ordering::Acquire) {
|
||||
if let Ok((len, _src)) = socket.recv_from(&mut buf) {
|
||||
if let Ok(text) = std::str::from_utf8(&buf[..len]) {
|
||||
for line in text.lines() {
|
||||
Self::process_line(
|
||||
line,
|
||||
&state_clone,
|
||||
Some(&control),
|
||||
telemetry.as_ref(),
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let mut st = state_clone.lock();
|
||||
if st.connected && !st.is_live() {
|
||||
st.connected = false;
|
||||
log::warn!("[G29 Bridge] Perte de liaison avec le volant G29.");
|
||||
}
|
||||
}
|
||||
}
|
||||
log::info!("[G29 Bridge] Thread d'écoute UDP arrêté.");
|
||||
})?;
|
||||
|
||||
Ok(Self {
|
||||
state,
|
||||
stop_flag,
|
||||
worker: Some(worker),
|
||||
})
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn send_command(target_ip: &str, target_port: u16, cmd: &str) -> Result<()> {
|
||||
let socket = UdpSocket::bind("0.0.0.0:0")?;
|
||||
let target = format!("{}:{}", target_ip.trim(), target_port);
|
||||
let payload = if cmd.ends_with('\n') {
|
||||
cmd.to_string()
|
||||
} else {
|
||||
format!("{}\n", cmd)
|
||||
};
|
||||
socket.send_to(payload.as_bytes(), target)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn state(&self) -> Arc<Mutex<G29State>> {
|
||||
Arc::clone(&self.state)
|
||||
}
|
||||
|
||||
pub fn process_line(
|
||||
raw_line: &str,
|
||||
state: &Arc<Mutex<G29State>>,
|
||||
control: Option<&EngineControlHandle>,
|
||||
telemetry: Option<&Arc<Mutex<TelemetryState>>>,
|
||||
) {
|
||||
let line = raw_line.trim();
|
||||
if let Some(rest) = line.strip_prefix("$G29,") {
|
||||
Self::parse_status_fields(rest, state);
|
||||
} else if let Some(rest) = line.strip_prefix("$G29_EVT,") {
|
||||
Self::parse_button_event(rest, state, control);
|
||||
} else if let Some(hex) = line.strip_prefix("$CRSF,") {
|
||||
if let Some(telem) = telemetry {
|
||||
Self::parse_crsf_hex(hex, telem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_crsf_hex(hex: &str, telemetry: &Arc<Mutex<TelemetryState>>) {
|
||||
let bytes = hex.trim().as_bytes();
|
||||
if bytes.len() < 8 || bytes.len() % 2 != 0 || bytes.len() > 128 {
|
||||
return;
|
||||
}
|
||||
let mut raw = [0u8; 64];
|
||||
let mut out_len = 0;
|
||||
for chunk in bytes.chunks_exact(2) {
|
||||
let Some(hi) = Self::hex_nibble(chunk[0]) else {
|
||||
return;
|
||||
};
|
||||
let Some(lo) = Self::hex_nibble(chunk[1]) else {
|
||||
return;
|
||||
};
|
||||
raw[out_len] = (hi << 4) | lo;
|
||||
out_len += 1;
|
||||
}
|
||||
TelemetryReceiver::parse_datagram(&raw[..out_len], telemetry);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn hex_nibble(b: u8) -> Option<u8> {
|
||||
match b {
|
||||
b'0'..=b'9' => Some(b - b'0'),
|
||||
b'a'..=b'f' => Some(b - b'a' + 10),
|
||||
b'A'..=b'F' => Some(b - b'A' + 10),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_status_fields(fields: &str, state: &Arc<Mutex<G29State>>) {
|
||||
let mut st = state.lock();
|
||||
let had_packets = st.packets_received > 0;
|
||||
let prev_ffb_pct = st.ffb_pct;
|
||||
let prev_vib_pct = st.vib_pct;
|
||||
|
||||
for pair in fields.split(',') {
|
||||
let Some((k, v)) = pair.split_once('=') else {
|
||||
continue;
|
||||
};
|
||||
match k.trim() {
|
||||
"ARM" => {
|
||||
if let Ok(val) = v.trim().parse::<u8>() {
|
||||
st.armed = val != 0;
|
||||
}
|
||||
}
|
||||
"GEAR" => {
|
||||
if let Ok(val) = v.trim().parse::<i8>() {
|
||||
st.gear = val.clamp(-1, 3);
|
||||
}
|
||||
}
|
||||
"THROT" => {
|
||||
if let Ok(val) = v.trim().parse::<u8>() {
|
||||
st.throttle_pct = val.min(100);
|
||||
}
|
||||
}
|
||||
"BRAKE" => {
|
||||
if let Ok(val) = v.trim().parse::<u8>() {
|
||||
st.brake_pct = val.min(100);
|
||||
}
|
||||
}
|
||||
"FFB" => {
|
||||
if let Ok(val) = v.trim().parse::<u8>() {
|
||||
st.ffb_raw = val;
|
||||
st.ffb_pct = ((val as u16 * 100) / 255) as u8;
|
||||
}
|
||||
}
|
||||
"VIB" => {
|
||||
if let Ok(val) = v.trim().parse::<u8>() {
|
||||
st.vib_pct = val.min(100);
|
||||
}
|
||||
}
|
||||
"FAIL" => {
|
||||
if let Ok(val) = v.trim().parse::<u8>() {
|
||||
st.failsafe = val != 0;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
if !st.connected {
|
||||
log::info!("[G29 Bridge] Volant G29 connecté en Wi-Fi UDP.");
|
||||
}
|
||||
st.connected = true;
|
||||
st.packets_received = st.packets_received.wrapping_add(1);
|
||||
st.last_packet = Some(Instant::now());
|
||||
|
||||
if had_packets && st.ffb_pct != prev_ffb_pct {
|
||||
let pct = st.ffb_pct;
|
||||
st.set_toast(format!("FFB {}%", pct));
|
||||
} else if had_packets && st.vib_pct != prev_vib_pct {
|
||||
let pct = st.vib_pct;
|
||||
st.set_toast(format!("VIB {}%", pct));
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_button_event(
|
||||
fields: &str,
|
||||
state: &Arc<Mutex<G29State>>,
|
||||
control: Option<&EngineControlHandle>,
|
||||
) {
|
||||
let Some((_, btn_name)) = fields.split_once("BTN=") else {
|
||||
return;
|
||||
};
|
||||
let Some(btn) = G29ButtonEvent::from_name(btn_name) else {
|
||||
return;
|
||||
};
|
||||
|
||||
log::info!("[G29 Bridge] Bouton volant reçu : {:?}", btn);
|
||||
|
||||
let toast = match btn {
|
||||
G29ButtonEvent::Cross | G29ButtonEvent::Ps | G29ButtonEvent::R3 => {
|
||||
if let Some(ctrl) = control {
|
||||
ctrl.request_recenter();
|
||||
}
|
||||
Some("TARE HT OK".to_string())
|
||||
}
|
||||
G29ButtonEvent::Share | G29ButtonEvent::Circle => {
|
||||
if let Some(ctrl) = control {
|
||||
let now_rec = ctrl.toggle_dvr();
|
||||
Some(if now_rec {
|
||||
"DVR REC".to_string()
|
||||
} else {
|
||||
"DVR STOP".to_string()
|
||||
})
|
||||
} else {
|
||||
Some("DVR TOGGLE".to_string())
|
||||
}
|
||||
}
|
||||
G29ButtonEvent::Triangle => {
|
||||
if let Some(ctrl) = control {
|
||||
let now_on = ctrl.toggle_telemetry();
|
||||
Some(if now_on {
|
||||
"OSD ON".to_string()
|
||||
} else {
|
||||
"OSD OFF".to_string()
|
||||
})
|
||||
} else {
|
||||
Some("OSD TOGGLE".to_string())
|
||||
}
|
||||
}
|
||||
G29ButtonEvent::Square => {
|
||||
if let Some(ctrl) = control {
|
||||
let mode_lbl = ctrl.cycle_projection_mode();
|
||||
Some(format!("PROJ: {}", mode_lbl))
|
||||
} else {
|
||||
Some("PROJ CYCLE".to_string())
|
||||
}
|
||||
}
|
||||
G29ButtonEvent::DpadUp => {
|
||||
if let Some(ctrl) = control {
|
||||
ctrl.adjust_head_trim(0.0, 1.0);
|
||||
}
|
||||
Some("TILT +1".to_string())
|
||||
}
|
||||
G29ButtonEvent::DpadDown => {
|
||||
if let Some(ctrl) = control {
|
||||
ctrl.adjust_head_trim(0.0, -1.0);
|
||||
}
|
||||
Some("TILT -1".to_string())
|
||||
}
|
||||
G29ButtonEvent::DpadLeft => {
|
||||
if let Some(ctrl) = control {
|
||||
ctrl.adjust_head_trim(-1.0, 0.0);
|
||||
}
|
||||
Some("PAN -1".to_string())
|
||||
}
|
||||
G29ButtonEvent::DpadRight => {
|
||||
if let Some(ctrl) = control {
|
||||
ctrl.adjust_head_trim(1.0, 0.0);
|
||||
}
|
||||
Some("PAN +1".to_string())
|
||||
}
|
||||
G29ButtonEvent::Options | G29ButtonEvent::L3 => None,
|
||||
};
|
||||
|
||||
if let Some(msg) = toast {
|
||||
let mut st = state.lock();
|
||||
st.connected = true;
|
||||
st.last_packet = Some(Instant::now());
|
||||
st.set_toast(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for G29BridgeReceiver {
|
||||
fn drop(&mut self) {
|
||||
self.stop_flag.store(true, Ordering::Release);
|
||||
if let Some(handle) = self.worker.take() {
|
||||
let _ = handle.join();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_parse_g29_status_and_toast() {
|
||||
let state = Arc::new(Mutex::new(G29State::default()));
|
||||
G29BridgeReceiver::process_line(
|
||||
"$G29,ARM=1,GEAR=2,THROT=64,BRAKE=12,FFB=140,VIB=50,FAIL=0",
|
||||
&state,
|
||||
None,
|
||||
None,
|
||||
);
|
||||
{
|
||||
let st = state.lock();
|
||||
assert!(st.connected);
|
||||
assert!(st.armed);
|
||||
assert_eq!(st.gear, 2);
|
||||
assert_eq!(st.gear_label(), "2");
|
||||
assert_eq!(st.throttle_pct, 64);
|
||||
assert_eq!(st.brake_pct, 12);
|
||||
assert_eq!(st.ffb_raw, 140);
|
||||
assert_eq!(st.vib_pct, 50);
|
||||
assert!(!st.failsafe);
|
||||
}
|
||||
|
||||
G29BridgeReceiver::process_line(
|
||||
"$G29,ARM=1,GEAR=-1,THROT=0,BRAKE=80,FFB=200,VIB=50,FAIL=0",
|
||||
&state,
|
||||
None,
|
||||
None,
|
||||
);
|
||||
{
|
||||
let st = state.lock();
|
||||
assert_eq!(st.gear_label(), "R");
|
||||
assert_eq!(st.brake_pct, 80);
|
||||
assert_eq!(st.active_toast(), Some("FFB 78%"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_g29_button_event() {
|
||||
let state = Arc::new(Mutex::new(G29State::default()));
|
||||
G29BridgeReceiver::process_line("$G29_EVT,BTN=CROSS", &state, None, None);
|
||||
let st = state.lock();
|
||||
assert_eq!(st.active_toast(), Some("TARE HT OK"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_crsf_telemetry_hex() {
|
||||
// Trame CRSF Battery Sensor (0x08) : 16.8V (168 = 0x00A8), 12.5A (125 = 0x007D), 1200 mAh, 75%
|
||||
let mut frame = [0u8; 12];
|
||||
frame[0] = 0xEA;
|
||||
frame[1] = 10;
|
||||
frame[2] = 0x08;
|
||||
frame[3] = 0x00;
|
||||
frame[4] = 0xA8; // 16.8V
|
||||
frame[5] = 0x00;
|
||||
frame[6] = 0x7D; // 12.5A
|
||||
frame[7] = 0x00;
|
||||
frame[8] = 0x04;
|
||||
frame[9] = 0xB0; // 1200 mAh
|
||||
frame[10] = 75; // 75%
|
||||
frame[11] = crate::nomad_elrs::NomadHeadTracker::crc8_dvb_s2(&frame[2..11]);
|
||||
|
||||
let hex: String = frame.iter().map(|b| format!("{:02X}", b)).collect();
|
||||
let line = format!("$CRSF,{}", hex);
|
||||
|
||||
let g29_state = Arc::new(Mutex::new(G29State::default()));
|
||||
let telem_state = Arc::new(Mutex::new(TelemetryState::default()));
|
||||
G29BridgeReceiver::process_line(&line, &g29_state, None, Some(&telem_state));
|
||||
|
||||
let ts = telem_state.lock();
|
||||
assert_eq!(ts.packets_received, 1);
|
||||
assert!((ts.battery_voltage_v - 16.8).abs() < 0.01);
|
||||
assert!((ts.battery_current_a - 12.5).abs() < 0.01);
|
||||
assert_eq!(ts.battery_remaining_pct, 75);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user