Joystick fix

This commit is contained in:
2026-02-15 00:12:58 +01:00
parent f6ec4492f1
commit e9949a7526
6 changed files with 144 additions and 188 deletions

55
Cargo.lock generated
View File

@@ -2796,7 +2796,6 @@ dependencies = [
"g13-driver",
"input-linux",
"time",
"tokio",
]
[[package]]
@@ -3567,17 +3566,6 @@ dependencies = [
"simd-adler32",
]
[[package]]
name = "mio"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a69bcab0ad47271a0234d9422b131806bf3968021e5dc9328caf2d4cd58557fc"
dependencies = [
"libc",
"wasi",
"windows-sys 0.61.2",
]
[[package]]
name = "mlua"
version = "0.11.6"
@@ -5022,16 +5010,6 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "signal-hook-registry"
version = "1.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b"
dependencies = [
"errno",
"libc",
]
[[package]]
name = "simd-adler32"
version = "0.3.8"
@@ -5122,16 +5100,6 @@ dependencies = [
"serde",
]
[[package]]
name = "socket2"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86f4aa3ad99f2088c990dfa82d367e19cb29268ed67c574d10d0a4bfe71f07e0"
dependencies = [
"libc",
"windows-sys 0.60.2",
]
[[package]]
name = "spin"
version = "0.10.0"
@@ -5389,24 +5357,7 @@ version = "1.49.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72a2903cd7736441aac9df9d7688bd0ce48edccaadf181c3b90be801e81d3d86"
dependencies = [
"libc",
"mio",
"pin-project-lite",
"signal-hook-registry",
"socket2",
"tokio-macros",
"windows-sys 0.61.2",
]
[[package]]
name = "tokio-macros"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af407857209536a95c8e56f8231ef2c2e2aff839b22e07a1ffcbc617e9db9fa5"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
@@ -5660,12 +5611,6 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "wasi"
version = "0.11.1+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
[[package]]
name = "wasip2"
version = "1.0.2+wasi-0.2.9"

View File

@@ -43,6 +43,11 @@ pub enum Button {
Stick1,
Stick2,
Stick3,
StickUp,
StickDown,
StickLeft,
StickRight,
}
impl Button {
@@ -51,7 +56,7 @@ impl Button {
[
Action, Screen1, Screen2, Screen3, Screen4, Light, M1, M2, M3, MR, G1, G2, G3, G4, G5,
G6, G7, G8, G9, G10, G11, G12, G13, G14, G15, G16, G17, G18, G19, G20, G21, G22,
Stick1, Stick2, Stick3,
Stick1, Stick2, Stick3, StickUp, StickDown, StickLeft, StickRight,
]
.iter()
}

View File

@@ -8,12 +8,3 @@ g13-driver.workspace = true
input-linux = "0.7.1"
embedded-graphics = "0.8.1"
time = { version = "0.3.47", features = ["formatting", "macros"] }
tokio = { version = "1.49.0", features = [
"rt",
"rt-multi-thread",
"sync",
"macros",
"net",
"signal",
"time",
] }

112
joystick/src/app.rs Normal file
View File

@@ -0,0 +1,112 @@
use embedded_graphics::Drawable;
use embedded_graphics::mono_font::MonoTextStyle;
use embedded_graphics::mono_font::ascii::*;
use embedded_graphics::pixelcolor::BinaryColor;
use embedded_graphics::prelude::Point;
use embedded_graphics::text::*;
use g13_driver::G13;
use g13_driver::G13_LCD_COLUMNS;
use g13_driver::G13_LCD_ROWS;
use g13_driver::G13Event;
use g13_driver::joystick::Axis;
use g13_driver::joystick::Button;
use time::OffsetDateTime;
use time::macros::offset;
use crate::joystick::Joystick;
const CHARACTER_STYLE_TIME: MonoTextStyle<'static, BinaryColor> =
MonoTextStyle::new(&FONT_10X20, BinaryColor::On);
const CHARACTER_STYLE_DATE: MonoTextStyle<'static, BinaryColor> =
MonoTextStyle::new(&FONT_6X10, BinaryColor::On);
const TEXTSTYLE: TextStyle = TextStyleBuilder::new()
.alignment(Alignment::Center)
.baseline(embedded_graphics::text::Baseline::Middle)
.build();
pub struct App {
joystick: Joystick,
}
impl App {
pub fn new() -> Self {
let joystick = Joystick::new("g13-joystick").expect("Joystick to be available");
Self { joystick }
}
pub fn update(&mut self, events: G13Event) {
match events {
G13Event::Axis(x, y) => {
let x = (x * 512.0) as i32;
let y = (y * 512.0) as i32;
self.joystick.move_axis(Axis::X, x).ok();
self.joystick.move_axis(Axis::Y, y).ok();
if x <= -500 {
self.joystick.button_press(Button::StickLeft, true).ok();
} else {
self.joystick.button_press(Button::StickLeft, false).ok();
}
if x >= 500 {
self.joystick.button_press(Button::StickRight, true).ok();
} else {
self.joystick.button_press(Button::StickRight, false).ok();
}
if y <= -500 {
self.joystick.button_press(Button::StickUp, true).ok();
} else {
self.joystick.button_press(Button::StickUp, false).ok();
}
if y >= 500 {
self.joystick.button_press(Button::StickDown, true).ok();
} else {
self.joystick.button_press(Button::StickDown, false).ok();
}
}
G13Event::Button(button, (_, value)) => {
self.joystick.button_press(button, value).ok();
}
}
self.joystick.synchronise().ok();
}
pub fn draw(&mut self, display: &mut G13) {
let now = OffsetDateTime::now_utc().to_offset(offset!(+1)); // GMT+1
let time = format!(
"{:0>2}:{:0>2}:{:0>2}",
now.hour(),
now.minute(),
now.second()
);
let date = format!("{}, {} {}", now.weekday(), now.day(), now.month());
Text::with_text_style(
&time,
Point::new(
(G13_LCD_COLUMNS as f64 / 2.0) as i32,
(G13_LCD_ROWS as f64 / 2.0) as i32 - 8,
),
CHARACTER_STYLE_TIME,
TEXTSTYLE,
)
.draw(display)
.ok();
Text::with_text_style(
&date,
Point::new(
(G13_LCD_COLUMNS as f64 / 2.0) as i32,
(G13_LCD_ROWS as f64 / 2.0) as i32 + 8,
),
CHARACTER_STYLE_DATE,
TEXTSTYLE,
)
.draw(display)
.ok();
}
}

View File

@@ -153,5 +153,9 @@ fn to_evdev_button(button: Button) -> input_linux::Key {
Button::Stick1 => input_linux::Key::Unknown155,
Button::Stick2 => input_linux::Key::Unknown156,
Button::Stick3 => input_linux::Key::Unknown157,
Button::StickUp => input_linux::Key::Unknown158,
Button::StickDown => input_linux::Key::Unknown159,
Button::StickLeft => input_linux::Key::Unknown15A,
Button::StickRight => input_linux::Key::Unknown15B,
}
}

View File

@@ -1,135 +1,34 @@
mod error;
mod joystick;
pub(crate) mod app;
pub(crate) mod error;
pub(crate) mod joystick;
use std::time::Duration;
use std::time::{Duration, Instant};
use embedded_graphics::{
mono_font::{MonoTextStyle, ascii::*},
pixelcolor::BinaryColor,
prelude::*,
text::{Alignment, Text, TextStyleBuilder},
};
use time::{OffsetDateTime, macros::offset};
use tokio::time::Instant;
use embedded_graphics::pixelcolor::BinaryColor;
use embedded_graphics::prelude::*;
use g13_driver::G13;
use g13_driver::{
G13, G13_LCD_COLUMNS, G13_LCD_ROWS,
joystick::{Axis, Button},
};
use crate::app::App;
use crate::joystick::Joystick;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let g13 = G13::new()?;
let joystick = Joystick::new("g13-joystick")?;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut g13 = G13::new()?;
let mut app = App::new();
g13.set_lcd_color(4, 8, 96)?;
let mut _g13 = g13.clone();
tokio::spawn(async move {
let character_style_time = MonoTextStyle::new(&FONT_10X20, BinaryColor::On);
let character_style_date = MonoTextStyle::new(&FONT_6X10, BinaryColor::On);
let textstyle = TextStyleBuilder::new()
.alignment(Alignment::Center)
.baseline(embedded_graphics::text::Baseline::Middle)
.build();
loop {
let start = Instant::now();
let now = OffsetDateTime::now_utc().to_offset(offset!(+1)); // GMT+1
// Render
let time = format!(
"{:0>2}:{:0>2}:{:0>2}",
now.hour(),
now.minute(),
now.second()
);
let date = format!("{}, {} {}", now.weekday(), now.day(), now.month());
_g13.clear(BinaryColor::Off).unwrap();
Text::with_text_style(
&time,
Point::new(
(G13_LCD_COLUMNS as f64 / 2.0) as i32,
(G13_LCD_ROWS as f64 / 2.0) as i32 - 8,
),
character_style_time,
textstyle,
)
.draw(&mut _g13)
.unwrap();
Text::with_text_style(
&date,
Point::new(
(G13_LCD_COLUMNS as f64 / 2.0) as i32,
(G13_LCD_ROWS as f64 / 2.0) as i32 + 8,
),
character_style_date,
textstyle,
)
.draw(&mut _g13)
.unwrap();
_g13.render().unwrap();
// Calculate delta time
let delta = Instant::now() - start;
tokio::time::sleep(Duration::from_millis(33) - delta).await;
}
});
let events = g13.events();
let mut start = Instant::now();
loop {
let state = g13.read()?;
if let Ok(event) = events.try_recv() {
app.update(event);
}
joystick.move_axis(Axis::X, state.x)?;
joystick.move_axis(Axis::Y, state.y)?;
joystick.button_press(Button::Action, state.buttons.action)?;
joystick.button_press(Button::Screen1, state.buttons.screen1)?;
joystick.button_press(Button::Screen2, state.buttons.screen2)?;
joystick.button_press(Button::Screen3, state.buttons.screen3)?;
joystick.button_press(Button::Screen4, state.buttons.screen4)?;
joystick.button_press(Button::Light, state.buttons.light)?;
joystick.button_press(Button::M1, state.buttons.m1)?;
joystick.button_press(Button::M2, state.buttons.m2)?;
joystick.button_press(Button::M3, state.buttons.m3)?;
joystick.button_press(Button::MR, state.buttons.mr)?;
joystick.button_press(Button::G1, state.buttons.g1)?;
joystick.button_press(Button::G2, state.buttons.g2)?;
joystick.button_press(Button::G3, state.buttons.g3)?;
joystick.button_press(Button::G4, state.buttons.g4)?;
joystick.button_press(Button::G5, state.buttons.g5)?;
joystick.button_press(Button::G6, state.buttons.g6)?;
joystick.button_press(Button::G7, state.buttons.g7)?;
joystick.button_press(Button::G8, state.buttons.g8)?;
joystick.button_press(Button::G9, state.buttons.g9)?;
joystick.button_press(Button::G10, state.buttons.g10)?;
joystick.button_press(Button::G11, state.buttons.g11)?;
joystick.button_press(Button::G12, state.buttons.g12)?;
joystick.button_press(Button::G13, state.buttons.g13)?;
joystick.button_press(Button::G14, state.buttons.g14)?;
joystick.button_press(Button::G15, state.buttons.g15)?;
joystick.button_press(Button::G16, state.buttons.g16)?;
joystick.button_press(Button::G17, state.buttons.g17)?;
joystick.button_press(Button::G18, state.buttons.g18)?;
joystick.button_press(Button::G19, state.buttons.g19)?;
joystick.button_press(Button::G20, state.buttons.g20)?;
joystick.button_press(Button::G21, state.buttons.g21)?;
joystick.button_press(Button::G22, state.buttons.g22)?;
joystick.button_press(Button::Stick1, state.buttons.stick1)?;
joystick.button_press(Button::Stick2, state.buttons.stick2)?;
joystick.button_press(Button::Stick3, state.buttons.stick3)?;
joystick.synchronise()?;
if (Instant::now() - start) >= Duration::from_millis(33) {
start = Instant::now();
g13.clear(BinaryColor::Off)?;
app.draw(&mut g13);
g13.render()?;
}
}
}