Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions lighthouse-client/examples/direction_events.rs
Original file line number Diff line number Diff line change
@@ -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<TokioWebSocket>) -> 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
}
5 changes: 4 additions & 1 deletion lighthouse-protocol/src/input/input_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ impl InputEvent {

/// Parses the input event as an arbitrary direction.
pub fn direction(&self) -> Option<Direction> {
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.
Expand Down
17 changes: 17 additions & 0 deletions lighthouse-protocol/src/input/orientation_event.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use serde::{Deserialize, Serialize};

use crate::{Direction, Vec2};

use super::EventSource;

/// A device orientation event.
Expand All @@ -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<f64>,
}

impl OrientationEvent {
/// The approximate direction (outside of a small deadzone) for a phone tilted against a flat surface.
pub fn direction(&self) -> Option<Direction> {
let Some(beta) = self.beta else { return None };
let Some(gamma) = self.gamma else { return None };

let deadzone_radius: f64 = 10.0;
if beta.abs().max(gamma.abs()) < deadzone_radius {
return None;
}

Direction::approximate_from(Vec2::new(gamma, beta))
}
}