Initial Rust-y API

This commit is contained in:
Rafael Caricio 2020-04-12 12:19:28 +02:00
parent b7bf19d059
commit 8455e28a82
7 changed files with 126 additions and 12 deletions

View file

@ -1,4 +1,5 @@
use lvgl_sys; use lvgl_sys;
use lvgl;
use sdl2::event::Event; use sdl2::event::Event;
use sdl2::keyboard::Keycode; use sdl2::keyboard::Keycode;
use sdl2::pixels::Color; use sdl2::pixels::Color;
@ -62,17 +63,25 @@ fn main() -> Result<(), String> {
} }
// Create screen and widgets // Create screen and widgets
let screen = unsafe { lvgl_sys::lv_disp_get_scr_act(std::ptr::null_mut()) }; // let screen = unsafe { lvgl_sys::lv_disp_get_scr_act(std::ptr::null_mut()) };
let btn = unsafe { lvgl_sys::lv_btn_create(screen, std::ptr::null_mut()) }; // let btn = unsafe { lvgl_sys::lv_btn_create(screen, std::ptr::null_mut()) };
unsafe { // unsafe {
lvgl_sys::lv_obj_set_pos(btn, 10, 10); // lvgl_sys::lv_obj_set_pos(btn, 10, 10);
lvgl_sys::lv_obj_set_size(btn, 200, 50) // lvgl_sys::lv_obj_set_size(btn, 200, 50)
} // }
let label = unsafe { lvgl_sys::lv_label_create(btn, std::ptr::null_mut()) }; // let label = unsafe { lvgl_sys::lv_label_create(btn, std::ptr::null_mut()) };
let text = CString::new("Click me!").unwrap(); // let text = CString::new("Click me!").unwrap();
unsafe { // unsafe {
lvgl_sys::lv_label_set_text(label, text.as_ptr()); // lvgl_sys::lv_label_set_text(label, text.as_ptr());
} // }
let mut screen = lvgl::display::get_active_screen();
let mut button = lvgl::Button::new(&mut screen);
button.set_pos(100, 10);
let mut label = lvgl::Label::new(&mut button);
label.set_text("Hello Beauty!");
let mut event_pump = sdl_context.event_pump()?; let mut event_pump = sdl_context.event_pump()?;
'running: loop { 'running: loop {

View file

@ -1,10 +1,14 @@
[package] [package]
name = "lvgl-sys" name = "lvgl-sys"
description = "Raw bindings to the LittlevGL C library."
version = "0.1.0" version = "0.1.0"
authors = ["Rafael Caricio <crates.lvgl-sys@caric.io>"] authors = ["Rafael Caricio <crates.lvgl-sys@caric.io>"]
edition = "2018" edition = "2018"
license = "MIT" license = "MIT"
repository = "https://github.com/rafaelcaricio/lvgl-rs" repository = "https://github.com/rafaelcaricio/lvgl-rs"
readme = "README.md"
categories = ["external-ffi-bindings", "embedded", "gui", "no-std"]
keywords = ["littlevgl", "lvgl"]
build = "build.rs" build = "build.rs"
links = "lvgl" links = "lvgl"

2
lvgl-sys/README.md Normal file
View file

@ -0,0 +1,2 @@
# lvgl-sys
Rust raw bindings for LittlevGL library.

View file

@ -5,8 +5,12 @@ authors = ["Rafael Caricio <crates.lvgl@caric.io>"]
edition = "2018" edition = "2018"
repository = "https://github.com/rafaelcaricio/lvgl-rs" repository = "https://github.com/rafaelcaricio/lvgl-rs"
license = "MIT" license = "MIT"
readme = "../README.md"
categories = ["api-bindings", "embedded", "gui", "no-std"]
keywords = ["littlevgl", "lvgl", "graphical_interfaces"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
lvgl-sys = { path = "../lvgl-sys" } lvgl-sys = { path = "../lvgl-sys", version = "0.1.0" }
cty = "0.2.1"

10
lvgl/src/display.rs Normal file
View file

@ -0,0 +1,10 @@
use crate::objx::Object;
use core::ptr;
pub fn get_active_screen() -> Object {
let raw = unsafe {
ptr::NonNull::new_unchecked(lvgl_sys::lv_disp_get_scr_act(ptr::null_mut()))
};
Object::new(raw)
}

View file

@ -1,5 +1,10 @@
#![no_std] #![no_std]
mod objx;
pub mod display;
pub use objx::*;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]

80
lvgl/src/objx.rs Normal file
View file

@ -0,0 +1,80 @@
use lvgl_sys;
use core::ffi;
use core::ptr;
use cty;
pub trait Container {
fn raw(&self) -> ptr::NonNull<lvgl_sys::lv_obj_t>;
}
pub struct Object {
raw: ptr::NonNull<lvgl_sys::lv_obj_t>,
}
impl Object {
pub(crate) fn new(raw: ptr::NonNull<lvgl_sys::lv_obj_t>) -> Self {
Self { raw }
}
}
impl Container for Object {
fn raw(&self) -> ptr::NonNull<lvgl_sys::lv_obj_t> {
unsafe {
ptr::NonNull::new_unchecked(self.raw.as_ptr())
}
}
}
pub struct Button {
core: Object,
}
impl Button {
pub fn new(parent: &mut dyn Container) -> Self {
let raw = unsafe {
let ptr = lvgl_sys::lv_btn_create(parent.raw().as_mut(), ptr::null_mut());
ptr::NonNull::new_unchecked(ptr)
};
let core = Object::new(raw);
Self { core }
}
pub fn set_pos(&mut self, x: u16, y: u16) {
unsafe {
lvgl_sys::lv_obj_set_pos(self.raw().as_mut(), x as lvgl_sys::lv_coord_t, y as lvgl_sys::lv_coord_t);
}
}
}
impl Container for Button {
fn raw(&self) -> ptr::NonNull<lvgl_sys::lv_obj_t> {
self.core.raw()
}
}
pub struct Label {
core: Object,
}
impl Label {
pub fn new(parent: &mut dyn Container) -> Self {
let raw = unsafe {
let ptr = lvgl_sys::lv_label_create(parent.raw().as_mut(), ptr::null_mut());
ptr::NonNull::new_unchecked(ptr)
};
let core = Object::new(raw);
Self { core }
}
pub fn set_text(&mut self, text: &str) {
unsafe {
lvgl_sys::lv_label_set_text(self.core.raw().as_mut(), text.as_ptr() as *const cty::c_char);
}
}
}
impl Container for Label {
fn raw(&self) -> ptr::NonNull<lvgl_sys::lv_obj_t> {
self.core.raw()
}
}