Joystick fix
This commit is contained in:
@@ -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
112
joystick/src/app.rs
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user