This fundamentally doesn't work..?

This commit is contained in:
2026-02-14 16:17:30 +01:00
parent 112820bf1f
commit 87490152bd
4 changed files with 61 additions and 23 deletions

View File

@@ -200,7 +200,6 @@ impl Iterator for G13Input {
for &(button, index, mask) in BUTTON_MAP {
if let Some(event) = self.handle_button(button, buf[index] & mask != 0) {
// self.calibrating = false;
return Some(Some(event));
}
}

View File

@@ -3,32 +3,65 @@ pub(super) mod widgets;
use std::collections::VecDeque;
use embedded_graphics::{pixelcolor::BinaryColor, prelude::DrawTarget};
use g13_driver::G13Event;
use embedded_graphics::{
Drawable,
draw_target::DrawTarget,
mono_font::{MonoTextStyle, ascii::FONT_6X10},
pixelcolor::BinaryColor,
prelude::Point,
text::{Alignment, Baseline, LineHeight, Text, TextStyle, TextStyleBuilder},
};
use g13_driver::{G13Event, Vec2};
use super::app::widgets::Page;
type Color = BinaryColor;
const CHARACTER_STYLE: MonoTextStyle<'static, BinaryColor> =
MonoTextStyle::new(&FONT_6X10, BinaryColor::On);
const TEXT_STYLE: TextStyle = TextStyleBuilder::new()
.baseline(Baseline::Top)
.alignment(Alignment::Left)
.build();
pub struct App {
pages: VecDeque<Box<dyn Page>>,
joy: Vec2<f32>,
}
impl App {
pub fn new() -> Self {
Self {
pages: VecDeque::new(),
joy: Vec2::new(0.0, 0.0),
}
}
pub fn update(&mut self, event: G13Event) {
println!("{:?}", event);
match event {
G13Event::Axis(x, y) => {
self.joy.x = x;
self.joy.y = y;
}
G13Event::Button(_button, (_prev, _value)) => { /* noop */ }
}
}
pub fn draw<D>(&mut self, display: &mut D) -> Result<(), Box<dyn std::error::Error>>
pub fn draw<D>(&mut self, display: &mut D)
where
D: DrawTarget<Color = Color>,
{
Ok(())
if let Some(last_page) = self.pages.iter().last() {
last_page.draw(display);
}
// let string = format!(
// "{}x{}",
// (self.joy.x * 256.0) as i32,
// (self.joy.y * 256.0) as i32
// );
// let text = Text::with_text_style(&string, Point::new(1, 1), CHARACTER_STYLE, TEXT_STYLE);
// let _ = text.draw(display);
}
}

View File

@@ -1,2 +1,16 @@
use embedded_graphics::draw_target::DrawTarget;
use g13_driver::G13Event;
pub trait Page: Widget {}
pub trait Widget {}
pub trait Widget {
fn update(&mut self, event: G13Event);
fn children() -> Vec<Box<dyn Widget>>; // the trait `app::widgets::Widget` is not dyn compatible
// for a trait to be dyn compatible it needs to allow building a vtable
// for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
// Generics aren't allowed, but I need that drawtarget here >_<
fn draw<D>(&mut self, display: &mut D)
where
D: DrawTarget;
}

View File

@@ -1,9 +1,6 @@
mod app;
use std::{
thread::sleep,
time::{Duration, Instant},
};
use std::time::{Duration, Instant};
use embedded_graphics::pixelcolor::BinaryColor;
use embedded_graphics::prelude::*;
@@ -19,22 +16,17 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let events = g13.events();
let mut start = Instant::now();
loop {
let start = Instant::now();
if let Ok(event) = events.try_recv() {
app.update(event);
}
g13.clear(BinaryColor::Off)?;
if let Err(e) = app.draw(&mut g13) {
eprintln!("{:?}", e);
app.update(event); // update can happen A LOT more often
}
g13.render()?;
let elapsed = Instant::now() - start;
if elapsed.as_millis() < 33 {
sleep(Duration::from_millis(33) - (elapsed));
if (Instant::now() - start) >= Duration::from_millis(33) {
start = Instant::now();
g13.clear(BinaryColor::Off)?;
app.draw(&mut g13);
g13.render()?; // 33 fps rendering
}
}
}