From cf094dd746e42ea2afa9a100cecb2bf6014c06aa Mon Sep 17 00:00:00 2001 From: Nickiel12 <35903114+Nickiel12@users.noreply.github.com> Date: Sat, 6 Apr 2024 11:03:21 -0700 Subject: [PATCH] got joystick working --- settings.toml | 2 +- src/coordinator.rs | 5 +++++ src/joystick_loop.rs | 16 +++++++++++++--- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/settings.toml b/settings.toml index 237caf5..5cebaaa 100644 --- a/settings.toml +++ b/settings.toml @@ -1,2 +1,2 @@ -ip = "10.0.0.30" +ip = "localhost" port = 8765 diff --git a/src/coordinator.rs b/src/coordinator.rs index cc5d8e0..56c82b0 100644 --- a/src/coordinator.rs +++ b/src/coordinator.rs @@ -89,6 +89,11 @@ impl <'a> CoordState<'a> { if let Err(e) = self.to_gui.send(GuiUpdate::SocketState(false)).await { error!("Cannot send message to gui thread: {e}"); } + } else { + if let Err(e) = self.to_gui.send(GuiUpdate::SocketState(true)).await { + error!("Cannot send message to gui thread: {e}"); + self.close().await; + } } } diff --git a/src/joystick_loop.rs b/src/joystick_loop.rs index 842ffb8..f18a4f1 100644 --- a/src/joystick_loop.rs +++ b/src/joystick_loop.rs @@ -2,11 +2,14 @@ use crate::coordinator::{ApplicationEvent, MoveEvent}; use async_channel::Sender; use gilrs::{ev::filter::FilterFn, Axis, Button, Event, EventType, Filter, Gilrs, GilrsBuilder}; -use log::{warn, info}; +use log::{debug, info, warn}; use std::{ - panic::{self, AssertUnwindSafe}, sync::{atomic::AtomicBool, Arc}, time::Duration + panic::{self, AssertUnwindSafe}, + sync::{atomic::AtomicBool, Arc}, + time::Duration }; +static MAX_SENT_ZEROS: u32 = 10; struct UnknownSlayer; impl FilterFn for UnknownSlayer { @@ -44,6 +47,7 @@ pub async fn joystick_loop( let mut curr_y: i32 = 0; let mut past_x: i32 = 0; let mut past_y: i32 = 0; + let mut count_zeros: u32 = 0; loop { // catch unwind because some buttons on the joystick will panic the gilrs object @@ -74,10 +78,16 @@ pub async fn joystick_loop( } } - if curr_x != past_x || curr_y != past_y { + if (curr_x != past_x || curr_y != past_y) || (count_zeros < MAX_SENT_ZEROS) { past_x = curr_x; past_y = curr_y; + if curr_x == 0 && curr_y == 0 { + count_zeros += 1; + } else { + count_zeros = 0; + } + match tx.try_send(ApplicationEvent::MoveEvent(MoveEvent { x: curr_x, y: curr_y,