basic i/o

This commit is contained in:
2026-08-06 09:15:53 +02:00
commit 032bcffe68
6 changed files with 1072 additions and 0 deletions

78
src/main.rs Normal file
View File

@@ -0,0 +1,78 @@
mod rgba;
mod screen;
use embedded_graphics::{prelude::*, primitives::{PrimitiveStyle, Rectangle}};
use evdev::{AbsoluteAxisCode, Device, EventSummary, KeyCode};
use screen::Screen;
use rgba::Rgba;
fn main() {
let mut screen = Screen::new();
let red = (255, 0, 0, 255);
let green = (0, 255, 0, 255);
let blue = (0, 0, 255, 255);
// /dev/input/event4: silead_ts
Rectangle::new(Point::new(0, 0), Size::new(screen.width(), 1))
.into_styled(PrimitiveStyle::with_fill(Rgba::from(red)))
.draw(&mut screen)
.ok();
Rectangle::new(Point::new(0, 1), Size::new(screen.width(), 1))
.into_styled(PrimitiveStyle::with_fill(Rgba::from(green)))
.draw(&mut screen)
.ok();
Rectangle::new(Point::new(0, 2), Size::new(screen.width(), 1))
.into_styled(PrimitiveStyle::with_fill(Rgba::from(blue)))
.draw(&mut screen)
.ok();
let Ok(mut device) = Device::open("/dev/input/event4") else {
panic!("No TS");
};
// device.set_nonblocking(true).unwrap();
let mut x = -1;
let mut y = -1;
loop {
// println!("dont block");
let Ok(events) = device.fetch_events() else {
continue;
};
for event in events {
match event.destructure(){
EventSummary::AbsoluteAxis(_, AbsoluteAxisCode::ABS_X, value) => {
x = (((value - 13) as f32 / 882.) * (screen.width() as f32)) as i32;
},
EventSummary::AbsoluteAxis(_, AbsoluteAxisCode::ABS_Y, value) => {
y = (((value - 13) as f32 / 624.) * (screen.height() as f32)) as i32;
},
EventSummary::Key(_, KeyCode::BTN_TOUCH, value) => {
// if value is 1, touch is started
// if value is 0, touch ended
println!("touch: {}", value)
},
_ => {}
}
}
println!("{}x{}", x, y);
screen.clear(Rgba::from((0,0,0,255))).ok();
Rectangle::new(Point::new(x - 50, y - 50), Size::new(100, 100))
.into_styled(PrimitiveStyle::with_fill(Rgba::from(blue)))
.draw(&mut screen)
.ok();
}
}

107
src/rgba.rs Normal file
View File

@@ -0,0 +1,107 @@
use embedded_graphics_core::pixelcolor::*;
use linfb::shape::Color;
/// Simple RGBA color wrapper.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Rgba<C: RgbColor>(C, u8);
#[allow(unused)]
#[inline(always)]
fn mul_blend_u8(delta: u32, a: u32) -> u32 {
// Exact (delta * a) / 255 using the div255 trick (no slow integer division).
// Valid for 0..=65535 inputs; see Hacker's Delight 10-16.
let t = delta * a + 128;
(t + (t >> 8)) >> 8
}
#[allow(unused)]
impl<C: RgbColor> Rgba<C> {
/// Create a new RGBA color.
pub const fn new(color: C, alpha: u8) -> Self {
Self(color, alpha)
}
/// Get the color component.
pub const fn rgb(&self) -> C {
self.0
}
pub fn r(&self) -> u8 {
self.0.r()
}
pub fn g(&self) -> u8 {
self.0.g()
}
pub fn b(&self) -> u8 {
self.0.b()
}
/// Get the alpha component (0..=255).
pub const fn a(&self) -> u8 {
self.1
}
}
impl<C: RgbColor> PixelColor for Rgba<C> {
type Raw = C::Raw;
}
#[allow(unused)]
pub trait Blend<T> {
fn blend(&self, bg: T) -> T;
}
impl Blend<Rgb888> for Rgba<Rgb888> {
#[inline(always)]
fn blend(&self, bg: Rgb888) -> Rgb888 {
let a = self.a() as u32;
if a == 0 {
return bg;
}
if a == 255 {
return self.rgb();
}
let fr = self.rgb().r() as u32;
let fg = self.rgb().g() as u32;
let fb = self.rgb().b() as u32;
let br = bg.r() as u32;
let bgc = bg.g() as u32;
let bb = bg.b() as u32;
let r = (br + mul_blend_u8(fr.wrapping_sub(br), a)) as u8;
let g = (bgc + mul_blend_u8(fg.wrapping_sub(bgc), a)) as u8;
let b = (bb + mul_blend_u8(fb.wrapping_sub(bb), a)) as u8;
Rgb888::new(r, g, b)
}
}
impl From<(u8,u8,u8,u8)> for Rgba<Rgb888> {
fn from(value: (u8,u8,u8,u8)) -> Self {
Self(
Rgb888::new(
value.0,
value.1,
value.2,
),
value.3,
)
}
}
impl From<Color> for Rgba<Rgb888> {
fn from(value: Color) -> Self {
Self(
Rgb888::new(
value.red,
value.green,
value.blue,
),
value.alpha,
)
}
}

78
src/screen.rs Normal file
View File

@@ -0,0 +1,78 @@
use embedded_graphics::{pixelcolor::Rgb888, prelude::*, primitives::Rectangle};
use linfb::{Framebuffer, shape::Color};
use crate::rgba::Rgba;
pub struct Screen {
framebuffer: Framebuffer,
width: u32,
height: u32,
}
impl Screen {
pub fn new() -> Self {
let framebuffer = Framebuffer::open()
.expect("Failed to open framebuffer");
let width = framebuffer.screen_info.xres;
let height = framebuffer.screen_info.yres;
Self {
framebuffer,
width,
height
}
}
pub fn width(&self) -> u32 {
self.width
}
pub fn height(&self) -> u32 {
self.height
}
}
impl Dimensions for Screen {
fn bounding_box(&self) -> Rectangle {
Rectangle::new(
Point::new(0, 0),
Size::new(self.width, self.height),
)
}
}
impl DrawTarget for Screen {
type Color = Rgba<Rgb888>;
type Error = Box<dyn std::error::Error>;
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where
I: IntoIterator<Item = embedded_graphics::Pixel<Self::Color>>,
{
let width = self.bounding_box().size.width;
let height = self.bounding_box().size.height;
for p in pixels {
let x = p.0.x as u32;
let y = p.0.y as u32;
let color = Color {
red: p.1.r(),
green: p.1.g(),
blue: p.1.b(),
alpha: p.1.a(),
};
if !(0..width).contains(&x) || !(0..height).contains(&y) {
continue;
}
self.framebuffer.set_pixel(x, y, color);
}
self.framebuffer.flush();
Ok(())
}
}