Skip to content

Commit

Permalink
Start to write Graphics bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
AlixANNERAUD committed Jul 1, 2024
1 parent 1a16f59 commit ee39574
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 2 deletions.
1 change: 1 addition & 0 deletions Modules/Bindings/Build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ fn main() -> Result<(), ()> {
println!("cargo:rerun-if-changed=Tests/WASM_test/src/main.rs");
println!("cargo:rerun-if-changed=Tests/WASM_test/src/File_system.rs");
println!("cargo:rerun-if-changed=Tests/WASM_test/src/Task.rs");
println!("cargo:rerun-if-changed=Tests/WASM_test/src/Graphics.rs");
println!("cargo:rerun-if-changed=Tests/WASM_test/Cargo.toml");

// TODO : Add a check for test mode
Expand Down
9 changes: 8 additions & 1 deletion Modules/Bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ Binding_tool = { path = "../../../Binding_tool", features = ["Native"] }
Shared = { path = "../Shared" }
Users = { path = "../Users" }
Task = { path = "../Task" }
Graphics = { path = "../Graphics" }
Screen = { path = "../Screen" }

[[test]]
name = "File_system_bindings_tests"
path = "Tests/File_system.rs"

[[test]]
name = "Task_tests"
name = "Task_bindings_tests"
path = "Tests/Task.rs"

[[test]]
name = "Graphics_bindings_tests"
path = "Tests/Graphics.rs"

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

use std::{
thread::{self, sleep},
time::{Duration, Instant},
};

use Bindings::{File_system_bindings, Graphics_bindings, Task_bindings};
use File_system::{
Drivers::Native::File_system_type,
Prelude::{Path_type, Virtual_file_system_type},
};
use Graphics::{lvgl, Display_type, Draw_buffer_type, Input_type};
use Screen::{Drivers::SDL2::New_touchscreen, Prelude::Point_type};
use Virtual_machine::{Data_type, Instantiate_test_environment, WasmValue};

#[test]
fn Integration_test() {
let Binary_buffer = include_bytes!(
"../../../target/wasm32-unknown-unknown/release/File_system_bindings_WASM_test.wasm"
);

const Horizontal_resolution: u32 = 800;
const Vertical_resolution: u32 = 480;
const Buffer_size: usize = (Horizontal_resolution * Vertical_resolution / 2) as usize;

let Touchscreen = New_touchscreen(Point_type::New(
Horizontal_resolution as i16,
Vertical_resolution as i16,
));
assert!(Touchscreen.is_ok());
let (mut Screen, mut Pointer) = Touchscreen.unwrap();

let Buffer = Draw_buffer_type::<Buffer_size>::default();

let Display = Display_type::New(&mut Screen, Buffer);
assert!(Display.is_ok());
let Display = Display.unwrap();

let _Input = Input_type::New(&Pointer, &Display);
assert!(_Input.is_ok());
let mut _Input = _Input.unwrap();

let Display_object = Display.Get_object();
assert!(Display_object.is_ok());
let mut Display_object = Display_object.unwrap();

thread::spawn(|| {
let (_Runtime, _Module, Instance) = Instantiate_test_environment(
Binary_buffer,
Graphics_bindings::New(),
&Data_type::New(),
);

assert_eq!(
Instance
.Call_export_function("Test_graphics", &vec![])
.unwrap(),
WasmValue::I32(42)
)
});

loop {
let Start = Instant::now();
lvgl::task_handler();
sleep(Duration::from_millis(5));
lvgl::tick_inc(Instant::now().duration_since(Start));
Pointer.Update();
}
}
2 changes: 1 addition & 1 deletion Modules/Bindings/Tests/WASM_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Binding_tool = { path = "../../../../../Binding_tool", features = ["WASM"] }
Binding_tool = { path = "../../../../../Binding_tool", features = ["WASM"] }
31 changes: 31 additions & 0 deletions Modules/Bindings/Tests/WASM_test/src/Graphics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::thread::sleep;

use Binding_tool::Bind_function_WASM;

#[repr(transparent)]
pub struct Object_type([u8; 64]);

impl Default for Object_type {
fn default() -> Self {
Self([0; 64])
}
}

#[Bind_function_WASM]
fn Create_calendar(Result: &mut Object_type) {}

#[Bind_function_WASM]
fn Delete_object(Object: &mut Object_type) {}

#[no_mangle]
fn Test_graphics() -> u32 {
let mut Object = Object_type::default();

Create_calendar(&mut Object);

// sleep(std::time::Duration::from_secs(1));

// Delete_object(&mut Object);

42
}
2 changes: 2 additions & 0 deletions Modules/Bindings/Tests/WASM_test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
mod File_system;

mod Task;

mod Graphics;
53 changes: 53 additions & 0 deletions Modules/Bindings/src/Graphics/Object.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::{
mem::size_of,
ptr::{copy_nonoverlapping, null_mut},
};

use Binding_tool::Bind_function_native;
use Graphics::lvgl::sys::{
lv_calendar_create, lv_disp_get_default, lv_disp_get_hor_res, lv_disp_get_scr_act,
lv_disp_get_ver_res, lv_mem_alloc, lv_mem_free, lv_mem_realloc, lv_memset_00,
lv_obj_allocate_spec_attr, lv_obj_class_t, lv_obj_create, lv_obj_del,
lv_obj_enable_style_refresh, lv_obj_get_index, lv_obj_mark_layout_as_dirty, lv_obj_set_parent,
lv_obj_t,
};
use Virtual_machine::{Function_descriptor_type, Function_descriptors, Registrable_trait};

pub struct Graphics_bindings {}

impl Registrable_trait for Graphics_bindings {
fn Get_functions(&self) -> &[Function_descriptor_type] {
&Graphics_bindings_functions
}
}

impl Graphics_bindings {
pub fn New() -> Self {
println!("Size of lv_obj_t: {}", size_of::<lv_obj_t>());
Self {}
}
}

const Graphics_bindings_functions: [Function_descriptor_type; 2] =
Function_descriptors!(Create_object_binding, Create_calendar_binding);

#[Bind_function_native(Prefix = "Graphics")]
fn Create_object(Result: &mut lv_obj_t) {
unsafe {
let obj = lv_obj_create(null_mut());
}
}

#[Bind_function_native(Prefix = "Graphics")]
fn Create_calendar(Result: &mut lv_obj_t) {
unsafe {
let current_screen = lv_disp_get_scr_act(lv_disp_get_default());
}
}

#[Bind_function_native(Prefix = "Graphics")]
fn Delete_object(Object: &mut lv_obj_t) {
unsafe {
lv_obj_del(Object);
}
}
2 changes: 2 additions & 0 deletions Modules/Bindings/src/Graphics/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod Object;
pub use Object::*;
3 changes: 3 additions & 0 deletions Modules/Bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ pub use File_system::*;

mod Task;
pub use Task::*;

mod Graphics;
pub use Graphics::*;

0 comments on commit ee39574

Please sign in to comment.