got joystick working
This commit is contained in:
parent
4506d7784b
commit
cf094dd746
3 changed files with 19 additions and 4 deletions
|
@ -1,2 +1,2 @@
|
|||
ip = "10.0.0.30"
|
||||
ip = "localhost"
|
||||
port = 8765
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue