From 85ab3f43e74a4ec48cd786297d54ec3c8532d864 Mon Sep 17 00:00:00 2001 From: cubee Date: Sun, 29 Dec 2024 09:38:49 +1100 Subject: [PATCH] osc-battery: xsoverlay parity! add average tracker and controller battery parameters. this should now be in parity with xsoverlay's parameters. gone back to 1-indexing trackers because it's more user-friendly, so other programs can standardise more easily. --- src/backend/osc.rs | 55 +++++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/src/backend/osc.rs b/src/backend/osc.rs index 5a25ca7..61eed6c 100644 --- a/src/backend/osc.rs +++ b/src/backend/osc.rs @@ -103,7 +103,10 @@ impl OscSender { if self.last_sent_battery.elapsed().as_millis() >= 10000 { self.last_sent_battery = Instant::now(); - let mut tracker_idx = 0; + let mut tracker_count: i8 = 0; + let mut controller_count: i8 = 0; + let mut tracker_total_bat = 0.0; + let mut controller_total_bat = 0.0; for device in &app.input_state.devices { let tracker_param; @@ -111,7 +114,7 @@ impl OscSender { // soc is the battery level (set to device status.charge) let level = device.soc.unwrap_or(-1.0); let parameter = match device.role { - TrackedDeviceRole::None => {""} + TrackedDeviceRole::None => {continue} TrackedDeviceRole::Hmd => { // legacy OVR Toolkit style (int) // as of 20 Nov 2024 OVR Toolkit uses int 0-100, but this may change in a future update. @@ -123,28 +126,44 @@ impl OscSender { "headset" } - TrackedDeviceRole::LeftHand => {"leftController"} - TrackedDeviceRole::RightHand => {"rightController"} + TrackedDeviceRole::LeftHand => { + controller_count += 1; + controller_total_bat += level; + "leftController" + } + TrackedDeviceRole::RightHand => { + controller_count += 1; + controller_total_bat += level; + "rightController" + } TrackedDeviceRole::Tracker => { - tracker_param = format!("tracker{tracker_idx}"); - tracker_idx += 1; + tracker_param = format!("tracker{tracker_count}"); + tracker_count += 1; + tracker_total_bat += level; tracker_param.as_str() } }; - // send battery parameters - if !parameter.is_empty() { - - self.send_message( - format!("/avatar/parameters/{parameter}Battery").into(), - vec![OscType::Float(level)], - )?; - self.send_message( - format!("/avatar/parameters/{parameter}Charging").into(), - vec![OscType::Bool(device.charging)], - )?; - } + // send device battery parameters + self.send_message( + format!("/avatar/parameters/{parameter}Battery").into(), + vec![OscType::Float(level)], + )?; + self.send_message( + format!("/avatar/parameters/{parameter}Charging").into(), + vec![OscType::Bool(device.charging)], + )?; } + + // send average controller and tracker battery parameters + self.send_message( + format!("/avatar/parameters/averageControllerBattery").into(), + vec![OscType::Float(controller_total_bat / controller_count as f32)], + )?; + self.send_message( + format!("/avatar/parameters/averageTrackerBattery").into(), + vec![OscType::Float(tracker_total_bat / tracker_count as f32)], + )?; } Ok(())