From 37d834d80b6951b77ab6e676aae0118b413c1c64 Mon Sep 17 00:00:00 2001 From: Avii Date: Thu, 12 Feb 2026 22:59:27 +0100 Subject: [PATCH] mouse stuff --- Cargo.lock | 11 +- scripts/main.luau => apps/main/main.lua | 30 +-- g13-os/Cargo.toml | 12 +- g13-os/src/main.rs | 21 ++- g13-os/src/mouse.rs | 92 ++++++++++ g13-os/src/os.rs | 231 ++++++++++++++++++++++++ 6 files changed, 357 insertions(+), 40 deletions(-) rename scripts/main.luau => apps/main/main.lua (58%) create mode 100644 g13-os/src/mouse.rs create mode 100644 g13-os/src/os.rs diff --git a/Cargo.lock b/Cargo.lock index 7be8306..d0f1234 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2787,6 +2787,7 @@ dependencies = [ "embedded-graphics", "g13-driver", "mlua", + "time", ] [[package]] @@ -3448,15 +3449,6 @@ dependencies = [ "imgref", ] -[[package]] -name = "luau0-src" -version = "0.18.2+luau708" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eed3214ab526e7e7b76c3f324a965d363db94a99ae67f65e67ec6fc499eb5e6d" -dependencies = [ - "cc", -] - [[package]] name = "mach2" version = "0.4.3" @@ -3587,7 +3579,6 @@ dependencies = [ "cc", "cfg-if", "libc", - "luau0-src", "pkg-config", ] diff --git a/scripts/main.luau b/apps/main/main.lua similarity index 58% rename from scripts/main.luau rename to apps/main/main.lua index cec1b65..3a35332 100644 --- a/scripts/main.luau +++ b/apps/main/main.lua @@ -1,5 +1,5 @@ name = "DVD" -authors = {"AviiNL"} +authors = { "AviiNL" } description = "Bouncing 'dvd' Logo" local width = g13.display_width @@ -19,7 +19,7 @@ function setup() joy.deadzone = 40 end -function update(delta: number) +function update(delta) x = x + speed * delta * joy.x * 1.2 y = y + speed * delta * joy.y @@ -29,26 +29,26 @@ function update(delta: number) g13.clear() - g13.set_color(color_x/2, color_y/2, color_z/2) + g13.set_color(color_x / 2, color_y / 2, color_z / 2) g13.text("Hello World!", 1, 1, true, 0, 0, 1) - for sy = -size,size do - for sx = -size,size do + for sy = -size, size do + for sx = -size, size do g13.set_pixel(x + sx, y + sy, true) end end end function dump(o) - if type(o) == 'table' then - local s = '{ ' - for k,v in pairs(o) do - if type(k) ~= 'number' then k = '"'..k..'"' end - s = s .. '['..k..'] = ' .. dump(v) .. ',' - end - return s .. '} ' - else - return tostring(o) - end + if type(o) == 'table' then + local s = '{ ' + for k, v in pairs(o) do + if type(k) ~= 'number' then k = '"' .. k .. '"' end + s = s .. '[' .. k .. '] = ' .. dump(v) .. ',' + end + return s .. '} ' + else + return tostring(o) + end end diff --git a/g13-os/Cargo.toml b/g13-os/Cargo.toml index f120b1a..f8de538 100644 --- a/g13-os/Cargo.toml +++ b/g13-os/Cargo.toml @@ -4,12 +4,8 @@ version.workspace = true edition.workspace = true [dependencies] -g13-driver.workspace = true -embedded-graphics = "0.8" -mlua = { version = "0.11.6", features = [ - "luau-jit", - "async", - "macros", - "serde", -] } ctrlc = "3.5" +embedded-graphics = "0.8" +g13-driver.workspace = true +mlua = { version = "0.11.6", features = ["lua54", "async", "macros", "serde"] } +time = { version = "0.3.47", features = ["formatting", "macros"] } diff --git a/g13-os/src/main.rs b/g13-os/src/main.rs index fc18955..a686aa9 100644 --- a/g13-os/src/main.rs +++ b/g13-os/src/main.rs @@ -1,3 +1,6 @@ +pub mod mouse; +mod os; + use g13_driver::{G13, G13_LCD_COLUMNS, G13_LCD_ROWS}; use mlua::{Error, ExternalResult, Lua}; use std::{ @@ -10,14 +13,18 @@ use std::{ time::{Duration, Instant}, }; +use crate::os::G13Os; + fn main() -> Result<(), Box> { let running = Arc::new(AtomicBool::new(true)); let _running = running.clone(); - ctrlc::set_handler(move || _running.store(false, Ordering::SeqCst)) - .expect("Error setting Ctrl-C handler"); - let g13 = G13::new()?; + let os = G13Os::new(); + + os.run(); + + /* let lua = Lua::new(); let globals = lua.globals(); let g13_table = lua.create_table()?; @@ -172,11 +179,11 @@ fn main() -> Result<(), Box> { joy.set("deadzone", 40)?; globals.set("joy", joy)?; - // load all files from dir `./scripts` for now, user configurable later or ~/.config/g13-os/*. + // load all files from dir `./scripts` for now, user configurable later or ~/.config/g13-os/\*. // for now, just main.luau - let main = fs::read_to_string("./scripts/main.luau")?; + let main = fs::read_to_string("./scripts/main.lua")?; - lua.load(main).set_name("main.luau").exec()?; + lua.load(main).set_name("main.lua").exec()?; if lua.load("setup ~= nil").eval()? { lua.load("setup()").exec()?; @@ -239,6 +246,6 @@ fn main() -> Result<(), Box> { } running.store(false, Ordering::SeqCst); - + */ Ok(()) } diff --git a/g13-os/src/mouse.rs b/g13-os/src/mouse.rs new file mode 100644 index 0000000..eb37ecf --- /dev/null +++ b/g13-os/src/mouse.rs @@ -0,0 +1,92 @@ +use std::cell::RefCell; + +use embedded_graphics::{Drawable, Pixel, pixelcolor::BinaryColor, prelude::Point}; +use g13_driver::{G13, G13_LCD_COLUMNS, G13_LCD_ROWS, State}; + +#[rustfmt::skip] +const MOUSE_CURSOR: &[u8] = &[ + 2, 0, 0, 0, 0, + 2, 2, 0, 0, 0, + 2, 1, 2, 0, 0, + 2, 1, 1, 2, 0, + 2, 1, 2, 2, 2, + 2, 2, 0, 0, 0, +]; + +pub struct Mouse { + deadzone: i32, + speed: f32, + x: RefCell, + y: RefCell, +} + +impl Default for Mouse { + fn default() -> Self { + Self::new() + } +} + +impl Mouse { + pub fn new() -> Self { + Self { + deadzone: 40, + speed: 20.0, + x: RefCell::new(G13_LCD_COLUMNS as f32 / 2.0 - 2.0), + y: RefCell::new(G13_LCD_ROWS as f32 / 2.0 - 3.0), + } + } + + pub fn update(&self, state: State, delta: f32) { + let offset: i32 = 512 - self.deadzone; + let mut x = state.x; + let mut y = state.y; + + x = if x.abs() < self.deadzone { + 0 + } else { + x - self.deadzone * x.signum() + }; + + y = if y.abs() < self.deadzone { + 0 + } else { + y - self.deadzone * y.signum() + }; + + let x = x as f32 / offset as f32; + let y = y as f32 / offset as f32; + + let mut sx = self.x.borrow_mut(); + let mut sy = self.y.borrow_mut(); + + *sx = (*sx + x * delta * self.speed * 1.2).clamp(0., (G13_LCD_COLUMNS - 1) as f32); + *sy = (*sy + y * delta * self.speed).clamp(0., (G13_LCD_ROWS - 1) as f32); + } + + pub fn get_x(&self) -> i32 { + *self.x.borrow() as i32 + } + + pub fn get_y(&self) -> i32 { + *self.y.borrow() as i32 + } + + pub fn render(&self, g13: &mut G13) { + for (i, value) in MOUSE_CURSOR.iter().enumerate() { + let x = i as i32 % 5; + let y = i as i32 / 5; + + let color = if *value == 1 { + BinaryColor::Off + } else if *value == 2 { + BinaryColor::On + } else { + continue; + }; + + Pixel(Point::new(self.get_x() + x, self.get_y() + y), color) + .draw(g13) + .expect("G13 to be Connected"); + } + } +} diff --git a/g13-os/src/os.rs b/g13-os/src/os.rs new file mode 100644 index 0000000..089c069 --- /dev/null +++ b/g13-os/src/os.rs @@ -0,0 +1,231 @@ +use std::{ + fs::read_dir, + sync::{ + Arc, + atomic::{AtomicBool, Ordering}, + }, + time::Instant, +}; + +use embedded_graphics::{ + Drawable, + mono_font::ascii::*, + pixelcolor::BinaryColor, + prelude::{DrawTarget, Point, Primitive, Size}, + primitives::{Line, PrimitiveStyle, PrimitiveStyleBuilder, Rectangle}, + text::{Alignment, Baseline, Text, TextStyleBuilder}, +}; +use g13_driver::{G13, G13_LCD_COLUMNS, G13_LCD_ROWS}; +use mlua::Lua; +use time::{OffsetDateTime, macros::offset}; + +use crate::mouse::Mouse; + +pub struct App { + name: String, +} + +pub struct Button { + label: String, + action: Option, +} + +pub struct G13Os { + g13: G13, + lua: Lua, + running: Arc, + apps: Vec, + running_app: Option, + buttons: [Option