I can haz Lua?

This commit is contained in:
2026-02-12 14:34:21 +01:00
parent dcb205394d
commit 4a56e7d1dd
7 changed files with 288 additions and 13 deletions

9
g13-os/Cargo.toml Normal file
View File

@@ -0,0 +1,9 @@
[package]
name = "g13-os"
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"] }

87
g13-os/src/main.rs Normal file
View File

@@ -0,0 +1,87 @@
use std::{
fs,
thread::sleep,
time::{Duration, Instant},
};
use g13_driver::{G13, G13_LCD_COLUMNS, G13_LCD_ROWS};
use mlua::{ExternalResult, Lua};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let g13 = G13::new()?;
let lua = Lua::new();
let globals = lua.globals();
let g13_table = lua.create_table()?;
let _g13 = g13.clone();
g13_table.set(
"set_color",
lua.create_function(move |_, (r, g, b)| _g13.set_lcd_color(r, g, b).into_lua_err())?,
)?;
let _g13 = g13.clone();
let fn_clear = lua.create_function(move |_, on| {
let mut _g13 = _g13.clone();
use embedded_graphics::pixelcolor::BinaryColor;
use embedded_graphics::prelude::*;
let color = if on {
BinaryColor::On
} else {
BinaryColor::Off
};
_g13.clear(color).into_lua_err()
});
g13_table.set("clear", fn_clear?)?;
let mut _g13 = g13.clone();
let fn_set_pixel = lua.create_function_mut(move |_, (x, y, on)| {
use embedded_graphics::pixelcolor::BinaryColor;
use embedded_graphics::prelude::*;
let color = if on {
BinaryColor::On
} else {
BinaryColor::Off
};
Pixel(Point::new(x, y), color)
.draw(&mut _g13)
.into_lua_err()
});
g13_table.set("set_pixel", fn_set_pixel?)?;
g13_table.set("display_width", G13_LCD_COLUMNS)?;
g13_table.set("display_height", G13_LCD_ROWS)?;
globals.set("g13", g13_table)?;
// load all files from dir `./scripts` for now, user configurable later or ~/.config/g13-os/*.<lua[u]>
// for now, just main.luau
let main = fs::read_to_string("./scripts/main.luau")?;
lua.load(main).set_name("main.luau").exec()?;
if lua.load("setup ~= nil").eval()? {
lua.load("setup()").exec()?;
}
if lua.load("update ~= nil").eval()? {
let mut _g13 = g13.clone();
let mut delta: f32 = 0.0;
loop {
let start = Instant::now();
lua.load(format!("update({})", delta)).exec()?;
_g13.render()?;
let duration = Instant::now() - start;
// 30 fps lock
if duration < Duration::from_millis(33) {
sleep(Duration::from_millis(33) - duration);
}
delta = (Instant::now() - start).as_secs_f32();
}
}
Ok(())
}