Skip to content

Commit

Permalink
Merge pull request #41 from Xila-Project/Feature/Graphics
Browse files Browse the repository at this point in the history
Feature/Graphics
  • Loading branch information
AlixANNERAUD authored Oct 11, 2024
2 parents 19cf0c5 + f3b4198 commit ec3d26b
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 123 deletions.
81 changes: 0 additions & 81 deletions Modules/Drivers/Tests/Native/Graphics.rs

This file was deleted.

8 changes: 3 additions & 5 deletions Modules/Drivers/src/Native/Devices/SDL2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use sdl2::{
video::Window,
EventPump, Sdl, VideoSubsystem,
};
use File_system::{Device_trait, Result_type, Size_type};
use File_system::{Create_device, Device_trait, Device_type, Result_type, Size_type};

use std::{marker::PhantomData, mem::size_of, process::exit, sync::RwLock};

Expand Down Expand Up @@ -249,9 +249,7 @@ impl Device_trait for Pointer_device_type {
}
}

pub fn New_touchscreen(
Size: Point_type,
) -> Result<(Screen_device_type, Pointer_device_type), String> {
pub fn New_touchscreen(Size: Point_type) -> Result<(Device_type, Device_type), String> {
let Context = sdl2::init().map_err(|Error| format!("Error initializing SDL2: {:?}", Error))?;

let Video_subsystem = Context
Expand All @@ -272,5 +270,5 @@ pub fn New_touchscreen(

let Screen = Screen_device_type::New(Context, Video_subsystem, Window)?;

Ok((Screen, Pointer))
Ok((Create_device!(Screen), Create_device!(Pointer)))
}
17 changes: 2 additions & 15 deletions Modules/Drivers/src/Native/Devices/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
mod Drive_file;
mod SDL2;

use std::sync::Arc;

pub use Drive_file::*;
use File_system::Device_type;
use Graphics::Point_type;
use Task::Task_identifier_type;
use Virtual_file_system::Virtual_file_system_type;
Expand All @@ -20,21 +17,11 @@ pub fn Mount_devices(
New_touchscreen(Resolution).expect("Error creating touchscreen");

Virtual_file_systems
.Mount_device(
Task,
"/Devices/Pointer",
Device_type::New(Arc::new(Pointer_device)),
false,
)
.Mount_device(Task, "/Devices/Pointer", Pointer_device, false)
.expect("Error adding pointer device");

Virtual_file_systems
.Mount_device(
Task,
"/Devices/Screen",
Device_type::New(Arc::new(Screen_device)),
false,
)
.Mount_device(Task, "/Devices/Screen", Screen_device, false)
.expect("Error adding screen device");

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion Modules/Graphics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
[dependencies]
cstr_core = "0.2.6"
Task = { path = "../Task" }
Virtual_file_system = { path = "../Virtual_file_system" }
File_system = { path = "../File_system" }
lvgl_rust_sys = { git = "https://github.com/Xila-Project/lvgl_rust_sys.git", default-features = false }

[target.'cfg( target_os = "espidf" )'.dependencies]
Expand All @@ -18,6 +18,7 @@ Shared = { path = "../Shared" }
Time = { path = "../Time" }

[dev-dependencies]
Drivers = { path = "../Drivers" }
Users = { path = "../Users" }
Task = { path = "../Task" }

Expand Down
48 changes: 48 additions & 0 deletions Modules/Graphics/Tests/Graphics.rs
Original file line number Diff line number Diff line change
@@ -1 +1,49 @@
#![allow(non_snake_case)]
#![allow(non_camel_case_types)]
#![allow(non_upper_case_globals)]

#[cfg(target_os = "linux")]
#[test]
#[ignore]
fn main() {
use Drivers::Native::New_touchscreen;
use File_system::Create_device;
use Graphics::{lvgl, Get_recommended_buffer_size, Point_type};
use Time::Duration_type;

Users::Initialize().expect("Error initializing users manager");

let Task_instance = Task::Initialize().expect("Error initializing task manager");

Time::Initialize(Create_device!(Drivers::Native::Time_driver_type::New()))
.expect("Error initializing time manager");

const Resolution: Point_type = Point_type::New(800, 480);

const Buffer_size: usize = Get_recommended_buffer_size(&Resolution);

let (Screen_device, Pointer_device) =
New_touchscreen(Resolution).expect("Error creating touchscreen");

let Task = Task_instance
.Get_current_task_identifier()
.expect("Failed to get current task identifier");

Graphics::Initialize().expect("Error initializing manager");

let Graphics_manager = Graphics::Get_instance().expect("Error getting manager");

let Display = Graphics_manager
.Create_display::<Buffer_size>(Screen_device, Pointer_device, false)
.expect("Error adding screen");

let Screen_object = Display.Get_object();

let _Calendar = unsafe { lvgl::lv_calendar_create(Screen_object) };

loop {
Graphics::Manager_type::Loop();

Task::Manager_type::Sleep(Duration_type::from_millis(1000));
}
}
15 changes: 8 additions & 7 deletions Modules/Graphics/src/Display.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::slice;
use std::{ffi::c_void, ptr::null_mut};

use Virtual_file_system::File_type;
use File_system::Device_type;

use crate::{
Area_type, Color_type, Draw_buffer::Buffer_type, Point_type, Result_type,
Expand All @@ -10,8 +10,8 @@ use crate::{

use super::lvgl;

struct User_data<'a> {
File: File_type<'a>,
struct User_data {
Device: Device_type,
}

pub struct Display_type<const Buffer_size: usize> {
Expand Down Expand Up @@ -39,9 +39,10 @@ unsafe extern "C" fn Binding_callback_function(

let User_data = unsafe { &*(lvgl::lv_display_get_user_data(Display) as *mut User_data) };

let File = &User_data.File;
let Device = &User_data.Device;

File.Write(Screen_write_data.as_ref())
Device
.Write(Screen_write_data.as_ref())
.expect("Error writing to display");

lvgl::lv_display_flush_ready(Display);
Expand All @@ -57,7 +58,7 @@ impl<const Buffer_size: usize> Drop for Display_type<Buffer_size> {

impl<const Buffer_size: usize> Display_type<Buffer_size> {
pub fn New(
File: File_type,
File: Device_type,
Resolution: Point_type,
Double_buffered: bool,
) -> Result_type<Self> {
Expand Down Expand Up @@ -88,7 +89,7 @@ impl<const Buffer_size: usize> Display_type<Buffer_size> {
}

// Set the user data.
let User_data = Box::new(User_data { File });
let User_data = Box::new(User_data { Device: File });

unsafe {
lvgl::lv_display_set_user_data(LVGL_display, Box::into_raw(User_data) as *mut c_void)
Expand Down
16 changes: 8 additions & 8 deletions Modules/Graphics/src/Input.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::ffi::c_void;

//use lvgl::input_device::{pointer, InputDriver};
use Virtual_file_system::File_type;
use File_system::Device_type;

use crate::{Display::Display_type, Pointer_data_type, Result_type};

use super::lvgl;

struct User_data_type<'a> {
pub File: File_type<'a>,
struct User_data_type {
pub Device: Device_type,
}

pub struct Input_type {
Expand Down Expand Up @@ -45,11 +44,12 @@ unsafe extern "C" fn Binding_callback_function(
) {
let User_data = unsafe { lvgl::lv_indev_get_user_data(Input_device) as *mut User_data_type };

let File = &(*User_data).File;
let Device = &(*User_data).Device;

let mut Pointer_data = Pointer_data_type::default();

File.Read(Pointer_data.as_mut())
Device
.Read(Pointer_data.as_mut())
.expect("Error reading from input device");

unsafe {
Expand All @@ -59,11 +59,11 @@ unsafe extern "C" fn Binding_callback_function(

impl Input_type {
pub fn New<const Buffer_size: usize>(
File: File_type,
File: Device_type,
_: &Display_type<Buffer_size>,
) -> Result_type<Self> {
// User_data is a pinned box, so it's ownership can be transferred to LVGL and will not move or dropper until the Input_device is dropped.
let User_data = Box::new(User_data_type { File });
let User_data = Box::new(User_data_type { Device: File });

let Input_device = unsafe {
let Input_device = lvgl::lv_indev_create();
Expand Down
13 changes: 7 additions & 6 deletions Modules/Graphics/src/Manager.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::sync::Mutex;
use std::time::Duration;

use File_system::Device_type;

use super::lvgl;

use super::Point_type;
use Virtual_file_system::File_type;

use crate::Display_type;
use crate::{Error_type, Input_type, Result_type, Screen_read_data_type};
Expand Down Expand Up @@ -76,21 +77,21 @@ impl Manager_type {

pub fn Create_display<const Buffer_size: usize>(
&self,
Screen_file: File_type,
Pointer_file: File_type,
Screen_device: Device_type,
Pointer_device: Device_type,
Double_buffered: bool,
) -> Result_type<Display_type<Buffer_size>> {
let mut Screen_read_data = Screen_read_data_type::default();

Screen_file
Screen_device
.Read(Screen_read_data.as_mut())
.map_err(|_| Error_type::Failed_to_get_resolution)?;

let Resolution: Point_type = Screen_read_data.Get_resolution();

let Display = Display_type::New(Screen_file, Resolution, Double_buffered)?;
let Display = Display_type::New(Screen_device, Resolution, Double_buffered)?;

let Input = Input_type::New(Pointer_file, &Display)?;
let Input = Input_type::New(Pointer_device, &Display)?;

self.0.lock()?.0.replace(Input);

Expand Down

0 comments on commit ec3d26b

Please sign in to comment.