From f764601d596643ecb98581a7f2ae07bb537616dc Mon Sep 17 00:00:00 2001 From: fwcd Date: Sun, 23 Mar 2025 03:16:32 +0100 Subject: [PATCH 1/3] Add support for parsing direction from orientation events --- lighthouse-protocol/src/input/input_event.rs | 5 ++++- .../src/input/orientation_event.rs | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lighthouse-protocol/src/input/input_event.rs b/lighthouse-protocol/src/input/input_event.rs index 3adf521..56cb805 100644 --- a/lighthouse-protocol/src/input/input_event.rs +++ b/lighthouse-protocol/src/input/input_event.rs @@ -34,7 +34,10 @@ impl InputEvent { /// Parses the input event as an arbitrary direction. pub fn direction(&self) -> Option { - self.left_direction().or_else(|| self.right_direction()) + match self { + InputEvent::Orientation(orientation) => orientation.direction(), + _ => self.left_direction().or_else(|| self.right_direction()), + } } /// The direction if the input event represents a WASD key, D-pad or left stick. diff --git a/lighthouse-protocol/src/input/orientation_event.rs b/lighthouse-protocol/src/input/orientation_event.rs index 8650a69..bf97e31 100644 --- a/lighthouse-protocol/src/input/orientation_event.rs +++ b/lighthouse-protocol/src/input/orientation_event.rs @@ -1,5 +1,7 @@ use serde::{Deserialize, Serialize}; +use crate::{Direction, Vec2}; + use super::EventSource; /// A device orientation event. @@ -17,3 +19,18 @@ pub struct OrientationEvent { /// The motion of the device around the y-axis (left to right motion), in degrees from -90 (inclusive) to 90 (exclusive). pub gamma: Option, } + +impl OrientationEvent { + /// The approximate direction (outside of a small deadzone) for a phone tilted against a flat surface. + pub fn direction(&self) -> Option { + let Some(beta) = self.beta else { return None }; + let Some(gamma) = self.gamma else { return None }; + + let deadzone_radius: f64 = 10.0; + if beta.max(gamma) < deadzone_radius { + return None; + } + + Direction::approximate_from(Vec2::new(gamma, beta)) + } +} From 63d4365eba7fbbff902305b7bcb1700486c5f147 Mon Sep 17 00:00:00 2001 From: fwcd Date: Sun, 23 Mar 2025 03:18:05 +0100 Subject: [PATCH 2/3] Add example for parsing directions --- .../examples/direction_events.rs | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 lighthouse-client/examples/direction_events.rs diff --git a/lighthouse-client/examples/direction_events.rs b/lighthouse-client/examples/direction_events.rs new file mode 100644 index 0000000..d38da0e --- /dev/null +++ b/lighthouse-client/examples/direction_events.rs @@ -0,0 +1,44 @@ +use clap::Parser; +use futures::StreamExt; +use lighthouse_client::{protocol::Authentication, Lighthouse, Result, TokioWebSocket, LIGHTHOUSE_URL}; +use tracing::info; + +async fn run(lh: Lighthouse) -> Result<()> { + info!("Connected to the Lighthouse server"); + + // Stream input events + let mut stream = lh.stream_input().await?; + while let Some(msg) = stream.next().await { + let event = msg?.payload; + if let Some(direction) = event.direction() { + info!("Input direction: {:?}", direction); + } + } + + Ok(()) +} + +#[derive(Parser)] +struct Args { + /// The username. + #[arg(short, long, env = "LIGHTHOUSE_USER")] + username: String, + /// The API token. + #[arg(short, long, env = "LIGHTHOUSE_TOKEN")] + token: String, + /// The server URL. + #[arg(long, env = "LIGHTHOUSE_URL", default_value = LIGHTHOUSE_URL)] + url: String, +} + +#[tokio::main(flavor = "current_thread")] +async fn main() -> Result<()> { + tracing_subscriber::fmt().init(); + _ = dotenvy::dotenv(); + + let args = Args::parse(); + let auth = Authentication::new(&args.username, &args.token); + let lh = Lighthouse::connect_with_tokio_to(&args.url, auth).await?; + + run(lh).await +} From 15a04dbdfbe28dd347f7e36dce85564718ae5097 Mon Sep 17 00:00:00 2001 From: fwcd Date: Sun, 23 Mar 2025 03:18:33 +0100 Subject: [PATCH 3/3] Add missing absolute value --- lighthouse-protocol/src/input/orientation_event.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lighthouse-protocol/src/input/orientation_event.rs b/lighthouse-protocol/src/input/orientation_event.rs index bf97e31..b335b1c 100644 --- a/lighthouse-protocol/src/input/orientation_event.rs +++ b/lighthouse-protocol/src/input/orientation_event.rs @@ -27,7 +27,7 @@ impl OrientationEvent { let Some(gamma) = self.gamma else { return None }; let deadzone_radius: f64 = 10.0; - if beta.max(gamma) < deadzone_radius { + if beta.abs().max(gamma.abs()) < deadzone_radius { return None; }