Skip to content

Commit 31c78d7

Browse files
authored
Merge pull request #32 from ProjectLighthouseCAU/orientation-event-direction
Add `OrientationEvent::direction`
2 parents be85579 + 15a04db commit 31c78d7

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use clap::Parser;
2+
use futures::StreamExt;
3+
use lighthouse_client::{protocol::Authentication, Lighthouse, Result, TokioWebSocket, LIGHTHOUSE_URL};
4+
use tracing::info;
5+
6+
async fn run(lh: Lighthouse<TokioWebSocket>) -> Result<()> {
7+
info!("Connected to the Lighthouse server");
8+
9+
// Stream input events
10+
let mut stream = lh.stream_input().await?;
11+
while let Some(msg) = stream.next().await {
12+
let event = msg?.payload;
13+
if let Some(direction) = event.direction() {
14+
info!("Input direction: {:?}", direction);
15+
}
16+
}
17+
18+
Ok(())
19+
}
20+
21+
#[derive(Parser)]
22+
struct Args {
23+
/// The username.
24+
#[arg(short, long, env = "LIGHTHOUSE_USER")]
25+
username: String,
26+
/// The API token.
27+
#[arg(short, long, env = "LIGHTHOUSE_TOKEN")]
28+
token: String,
29+
/// The server URL.
30+
#[arg(long, env = "LIGHTHOUSE_URL", default_value = LIGHTHOUSE_URL)]
31+
url: String,
32+
}
33+
34+
#[tokio::main(flavor = "current_thread")]
35+
async fn main() -> Result<()> {
36+
tracing_subscriber::fmt().init();
37+
_ = dotenvy::dotenv();
38+
39+
let args = Args::parse();
40+
let auth = Authentication::new(&args.username, &args.token);
41+
let lh = Lighthouse::connect_with_tokio_to(&args.url, auth).await?;
42+
43+
run(lh).await
44+
}

lighthouse-protocol/src/input/input_event.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ impl InputEvent {
3434

3535
/// Parses the input event as an arbitrary direction.
3636
pub fn direction(&self) -> Option<Direction> {
37-
self.left_direction().or_else(|| self.right_direction())
37+
match self {
38+
InputEvent::Orientation(orientation) => orientation.direction(),
39+
_ => self.left_direction().or_else(|| self.right_direction()),
40+
}
3841
}
3942

4043
/// The direction if the input event represents a WASD key, D-pad or left stick.

lighthouse-protocol/src/input/orientation_event.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use serde::{Deserialize, Serialize};
22

3+
use crate::{Direction, Vec2};
4+
35
use super::EventSource;
46

57
/// A device orientation event.
@@ -17,3 +19,18 @@ pub struct OrientationEvent {
1719
/// The motion of the device around the y-axis (left to right motion), in degrees from -90 (inclusive) to 90 (exclusive).
1820
pub gamma: Option<f64>,
1921
}
22+
23+
impl OrientationEvent {
24+
/// The approximate direction (outside of a small deadzone) for a phone tilted against a flat surface.
25+
pub fn direction(&self) -> Option<Direction> {
26+
let Some(beta) = self.beta else { return None };
27+
let Some(gamma) = self.gamma else { return None };
28+
29+
let deadzone_radius: f64 = 10.0;
30+
if beta.abs().max(gamma.abs()) < deadzone_radius {
31+
return None;
32+
}
33+
34+
Direction::approximate_from(Vec2::new(gamma, beta))
35+
}
36+
}

0 commit comments

Comments
 (0)