Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust bindings API review #51

Merged
merged 3 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 0 additions & 38 deletions examples/Cargo.toml

This file was deleted.

35 changes: 35 additions & 0 deletions examples/app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use embedded_graphics::pixelcolor::Rgb565;
use embedded_graphics::prelude::*;
use embedded_graphics_simulator::{OutputSettingsBuilder, SimulatorDisplay, Window};
use lvgl;
use lvgl::widgets::Label;
use lvgl::{Display, DrawBuffer, HOR_RES_MAX, VER_RES_MAX};
use std::cell::RefCell;

type ColorSpace = Rgb565;

#[allow(unused_mut)]
#[allow(unused_variables)]
fn main() {
let embedded_graphics_display: SimulatorDisplay<ColorSpace> =
SimulatorDisplay::new(Size::new(HOR_RES_MAX as u32, VER_RES_MAX as u32));

let output_settings = OutputSettingsBuilder::new().scale(2).build();
let mut window = Window::new("App Example", &output_settings);

let shared_native_display = RefCell::new(embedded_graphics_display);

// LVGL usage
lvgl::init();

let buffer = DrawBuffer::<{ (HOR_RES_MAX * VER_RES_MAX) as usize }>::new();

let display = Display::register(&buffer, |refresh| {
shared_native_display
.borrow_mut()
.draw_iter(refresh.as_pixels()).unwrap();
})
.unwrap();

let label: Label = "Nice!".into();
}
60 changes: 39 additions & 21 deletions examples/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ use embedded_graphics::prelude::*;
use embedded_graphics_simulator::{
OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
};
use lvgl;
use lvgl::style::Style;
use lvgl::widgets::{Arc, Label, LabelAlign};
use lvgl::{self, Align, Color, Part, State, UI};
use lvgl::{LvError, Widget};
use std::time::Instant;
use lvgl::{
Align, Color, Display, DrawBuffer, LvError, Part, State, Widget, HOR_RES_MAX,
VER_RES_MAX,
};
use lvgl_sys;
use std::cell::RefCell;
use std::thread;
use std::time::Duration;

fn mem_info() -> lvgl_sys::lv_mem_monitor_t {
let mut info = lvgl_sys::lv_mem_monitor_t {
Expand All @@ -35,68 +41,80 @@ fn main() -> Result<(), LvError> {
}

fn run_arc_demo() -> Result<(), LvError> {
let display: SimulatorDisplay<Rgb565> =
SimulatorDisplay::new(Size::new(lvgl::HOR_RES_MAX, lvgl::VER_RES_MAX));
lvgl::init();
let sim_display: SimulatorDisplay<Rgb565> =
SimulatorDisplay::new(Size::new(HOR_RES_MAX, VER_RES_MAX));

let output_settings = OutputSettingsBuilder::new().scale(2).build();
let mut window = Window::new("Arc Example", &output_settings);

let mut ui = UI::init()?;
let shared_native_display = RefCell::new(sim_display);

let buffer = DrawBuffer::<{ (HOR_RES_MAX * VER_RES_MAX) as usize }>::new();

// Implement and register your display:
ui.disp_drv_register(display)?;
let display = Display::register(&buffer, |refresh| {
shared_native_display
.borrow_mut()
.draw_iter(refresh.as_pixels())
.unwrap();
})?;

// Create screen and widgets
let mut screen = ui.scr_act()?;
let mut screen = display.get_scr_act()?;

let mut screen_style = Style::default();
screen_style.set_bg_color(State::DEFAULT, Color::from_rgb((255, 255, 255)));
screen_style.set_radius(State::DEFAULT, 0);
screen.add_style(Part::Main, screen_style)?;
screen.add_style(Part::Main, &mut screen_style)?;

// Create the arc object
let mut arc = Arc::new(&mut screen)?;
let mut arc = Arc::create(&mut screen, None)?;
arc.set_size(150, 150)?;
arc.set_align(&mut screen, Align::Center, 0, 10)?;
arc.set_start_angle(135)?;
arc.set_end_angle(135)?;

let mut loading_lbl = Label::new(&mut screen)?;
let mut loading_lbl = Label::create(&mut screen, None)?;
loading_lbl.set_text(CString::new("Loading...").unwrap().as_c_str())?;
loading_lbl.set_align(&mut arc, Align::OutTopMid, 0, -10)?;
loading_lbl.set_label_align(LabelAlign::Center)?;

let mut loading_style = Style::default();
loading_style.set_text_color(State::DEFAULT, Color::from_rgb((0, 0, 0)));
loading_lbl.add_style(Part::Main, loading_style)?;
loading_lbl.add_style(Part::Main, &mut loading_style)?;

let mut angle = 0;
let mut forward = true;
let mut i = 0;

let mut loop_started = Instant::now();
// LVGL timer thread
thread::spawn(|| {
let interval = Duration::from_millis(5);
loop {
thread::sleep(interval);
lvgl::tick_inc(interval);
}
});

'running: loop {
if i > 270 {
forward = if forward { false } else { true };
i = 1;
println!("meminfo running: {:?}", mem_info());
println!("mem info running: {:?}", mem_info());
}
angle = if forward { angle + 1 } else { angle - 1 };
arc.set_end_angle(angle + 135)?;
i += 1;

ui.task_handler();
window.update(ui.get_display_ref().unwrap());
lvgl::task_handler();
window.update(&shared_native_display.borrow());

for event in window.events() {
match event {
SimulatorEvent::Quit => break 'running,
_ => {}
}
}

ui.tick_inc(loop_started.elapsed());
loop_started = Instant::now();
lvgl::tick_inc(Duration::from_millis(15));
}

Ok(())
Expand Down
49 changes: 29 additions & 20 deletions examples/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,44 @@ use embedded_graphics::prelude::*;
use embedded_graphics_simulator::{
OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
};
use lvgl;
use lvgl::style::Style;
use lvgl::widgets::{Bar, Label, LabelAlign};
use lvgl::{self, Align, Animation, Color, Event, LvError, Part, State, Widget, UI};
use std::time::Instant;
use lvgl::{
Align, Animation, Color, Display, DrawBuffer, Event, LvError, Part, State, Widget, HOR_RES_MAX,
VER_RES_MAX,
};
use std::cell::RefCell;
use std::time::Duration;

fn main() -> Result<(), LvError> {
let display: SimulatorDisplay<Rgb565> =
SimulatorDisplay::new(Size::new(lvgl::HOR_RES_MAX, lvgl::VER_RES_MAX));
lvgl::init();
let sim_display: SimulatorDisplay<Rgb565> =
SimulatorDisplay::new(Size::new(HOR_RES_MAX, VER_RES_MAX));

let output_settings = OutputSettingsBuilder::new().scale(2).build();
let mut window = Window::new("Bar Example", &output_settings);

let mut ui = UI::init()?;
let shared_native_display = RefCell::new(sim_display);

let buffer = DrawBuffer::<{ (HOR_RES_MAX * VER_RES_MAX) as usize }>::new();

// Implement and register your display:
ui.disp_drv_register(display).unwrap();
let display = Display::register(&buffer, |refresh| {
shared_native_display
.borrow_mut()
.draw_iter(refresh.as_pixels())
.unwrap();
})?;

// Create screen and widgets
let mut screen = ui.scr_act()?;
let mut screen = display.get_scr_act()?;

let mut screen_style = Style::default();
screen_style.set_bg_color(State::DEFAULT, Color::from_rgb((255, 255, 255)));
screen_style.set_radius(State::DEFAULT, 0);
screen.add_style(Part::Main, screen_style)?;
screen.add_style(Part::Main, &mut screen_style)?;

// Create the bar object
let mut bar = Bar::new(&mut screen)?;
let mut bar = Bar::create(&mut screen, None)?;
bar.set_size(175, 20)?;
bar.set_align(&mut screen, Align::Center, 0, 10)?;
bar.set_range(0, 100)?;
Expand All @@ -41,29 +52,28 @@ fn main() -> Result<(), LvError> {
// // Set the indicator style for the bar object
let mut ind_style = Style::default();
ind_style.set_bg_color(State::DEFAULT, Color::from_rgb((100, 245, 100)));
bar.add_style(Part::All, ind_style)?;
bar.add_style(Part::All, &mut ind_style)?;

let mut loading_lbl = Label::new(&mut screen)?;
let mut loading_lbl = Label::create(&mut screen, None)?;
loading_lbl.set_text(CString::new("Loading...").unwrap().as_c_str())?;
loading_lbl.set_align(&mut bar, Align::OutTopMid, 0, -10)?;
loading_lbl.set_label_align(LabelAlign::Center)?;

let mut loading_style = Style::default();
loading_style.set_text_color(State::DEFAULT, Color::from_rgb((0, 0, 0)));
loading_lbl.add_style(Part::Main, loading_style)?;
loading_lbl.add_style(Part::Main, &mut loading_style)?;

let mut i = 0;
let mut loop_started = Instant::now();
'running: loop {
if i > 100 {
i = 0;
ui.event_send(&mut bar, Event::Clicked)?;
lvgl::event_send(&mut bar, Event::Clicked)?;
}
bar.set_value(i, Animation::ON)?;
i += 1;

ui.task_handler();
window.update(ui.get_display_ref().unwrap());
lvgl::task_handler();
window.update(&shared_native_display.borrow());

for event in window.events() {
match event {
Expand All @@ -72,8 +82,7 @@ fn main() -> Result<(), LvError> {
}
}

ui.tick_inc(loop_started.elapsed());
loop_started = Instant::now();
lvgl::tick_inc(Duration::from_millis(10));
}

Ok(())
Expand Down
53 changes: 29 additions & 24 deletions examples/button_click.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,57 @@ use embedded_graphics_simulator::{
OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
};

use lvgl;
use lvgl::input_device::{
generic::DisplayDriver,
generic::InputDriver,
pointer::{Pointer, PointerInputData},
};
use lvgl::style::Style;
use lvgl::widgets::{Btn, Label};
use lvgl::{self, Align, Color, LvError, Part, State, Widget, UI};

use std::thread::sleep;
use std::time::{Duration, Instant};
use lvgl::{
Align, Color, Display, DrawBuffer, LvError, Part, State, Widget, HOR_RES_MAX, VER_RES_MAX,
};
use std::cell::RefCell;
use std::time::Duration;

#[allow(unused_assignments)]
fn main() -> Result<(), LvError> {
let display: SimulatorDisplay<Rgb565> =
SimulatorDisplay::new(Size::new(lvgl::HOR_RES_MAX, lvgl::VER_RES_MAX));
lvgl::init();
let sim_display: SimulatorDisplay<Rgb565> =
SimulatorDisplay::new(Size::new(HOR_RES_MAX, VER_RES_MAX));

let output_settings = OutputSettingsBuilder::new().scale(2).build();
let mut window = Window::new("Bar Example", &output_settings);
let mut window = Window::new("Button Example", &output_settings);

let shared_native_display = RefCell::new(sim_display);

let mut ui = UI::init()?;
let buffer = DrawBuffer::<{ (HOR_RES_MAX * VER_RES_MAX) as usize }>::new();

// Register your display:
ui.disp_drv_register(display)?;
let display = Display::register(&buffer, |refresh| {
shared_native_display
.borrow_mut()
.draw_iter(refresh.as_pixels())
.unwrap();
})?;

// Define the initial state of your input
let mut latest_touch_status = PointerInputData::Touch(Point::new(0, 0)).released().once();

// Register a new input device that's capable of reading the current state of the input
let mut touch_screen = Pointer::new(|| latest_touch_status);
ui.indev_drv_register_pointer(&mut touch_screen)?;
lvgl::indev_drv_register(&mut touch_screen)?;

// Create screen and widgets
let mut screen = ui.scr_act()?;
let mut screen = display.get_scr_act()?;

let mut screen_style = Style::default();
screen_style.set_bg_color(State::DEFAULT, Color::from_rgb((0, 0, 0)));
screen.add_style(Part::Main, screen_style)?;

screen.add_style(Part::Main, &mut screen_style)?;
// Create the button
let mut button = Btn::new(&mut screen)?;
let mut button = Btn::create(&mut screen, None)?;
button.set_align(&mut screen, Align::InLeftMid, 30, 0)?;
button.set_size(180, 80)?;
let mut btn_lbl = Label::new(&mut button)?;
let mut btn_lbl = Label::create(&mut button, None)?;
btn_lbl.set_text(CString::new("Click me!").unwrap().as_c_str())?;

let mut btn_state = false;
Expand All @@ -65,11 +74,10 @@ fn main() -> Result<(), LvError> {
}
})?;

let mut loop_started = Instant::now();
let mut latest_touch_point = Point::new(0, 0);
'running: loop {
ui.task_handler();
window.update(ui.get_display_ref().unwrap());
lvgl::task_handler();
window.update(&shared_native_display.borrow());

let mut events = window.events().peekable();

Expand All @@ -95,10 +103,7 @@ fn main() -> Result<(), LvError> {
}
}

sleep(Duration::from_millis(15));

ui.tick_inc(loop_started.elapsed());
loop_started = Instant::now();
lvgl::tick_inc(Duration::from_millis(15));
}

Ok(())
Expand Down
Loading