Add Gauge widget

This commit is contained in:
Rafael Caricio 2020-06-04 23:09:54 +02:00
parent 66d98430d8
commit 5015711141
10 changed files with 171 additions and 16 deletions

View file

@ -22,3 +22,7 @@ path = "bar.rs"
[[example]] [[example]]
name = "button_click" name = "button_click"
path = "button_click.rs" path = "button_click.rs"
[[example]]
name = "gauge"
path = "gauge.rs"

View file

@ -5,7 +5,7 @@ use embedded_graphics_simulator::{
}; };
use lvgl::style::Style; use lvgl::style::Style;
use lvgl::widgets::{Bar, BarPart, Label, LabelAlign}; use lvgl::widgets::{Bar, BarPart, Label, LabelAlign};
use lvgl::{self, Align, Animation, Color, DisplayDriver, Event, ObjPart, Object, State, UI}; use lvgl::{self, Align, Animation, Color, DisplayDriver, Event, Object, Part, State, UI};
use lvgl_sys; use lvgl_sys;
use std::sync::{mpsc, Arc, Mutex}; use std::sync::{mpsc, Arc, Mutex};
use std::thread::sleep; use std::thread::sleep;
@ -32,7 +32,7 @@ fn main() -> Result<(), String> {
let mut screen_style = Style::default(); let mut screen_style = Style::default();
screen_style.set_bg_color(State::DEFAULT, Color::from_rgb((255, 255, 255))); screen_style.set_bg_color(State::DEFAULT, Color::from_rgb((255, 255, 255)));
screen_style.set_radius(State::DEFAULT, 0); screen_style.set_radius(State::DEFAULT, 0);
screen.add_style(ObjPart::Main, screen_style); screen.add_style(Part::Main, screen_style);
// Create the bar object // Create the bar object
let mut bar = Bar::new(&mut screen); let mut bar = Bar::new(&mut screen);
@ -52,7 +52,7 @@ fn main() -> Result<(), String> {
let mut loading_style = Style::default(); let mut loading_style = Style::default();
loading_style.set_text_color(State::DEFAULT, Color::from_rgb((0, 0, 0))); loading_style.set_text_color(State::DEFAULT, Color::from_rgb((0, 0, 0)));
loading_lbl.add_style(ObjPart::Main, loading_style); loading_lbl.add_style(Part::Main, loading_style);
let threaded_ui = Arc::new(Mutex::new(ui)); let threaded_ui = Arc::new(Mutex::new(ui));

View file

@ -5,7 +5,7 @@ use embedded_graphics_simulator::{
}; };
use lvgl::style::Style; use lvgl::style::Style;
use lvgl::widgets::{Button, Label}; use lvgl::widgets::{Button, Label};
use lvgl::{self, Align, Color, DisplayDriver, Event, ObjPart, Object, State, UI}; use lvgl::{self, Align, Color, DisplayDriver, Event, Object, Part, State, UI};
use lvgl_sys; use lvgl_sys;
use std::sync::{mpsc, Arc, Mutex}; use std::sync::{mpsc, Arc, Mutex};
use std::thread::sleep; use std::thread::sleep;
@ -31,7 +31,7 @@ fn main() -> Result<(), String> {
let mut screen_style = Style::default(); let mut screen_style = Style::default();
screen_style.set_bg_color(State::DEFAULT, Color::from_rgb((0, 0, 0))); screen_style.set_bg_color(State::DEFAULT, Color::from_rgb((0, 0, 0)));
screen.add_style(ObjPart::Main, screen_style); screen.add_style(Part::Main, screen_style);
// Create the button // Create the button
let mut button = Button::new(&mut screen); let mut button = Button::new(&mut screen);

View file

@ -6,7 +6,7 @@ use embedded_graphics_simulator::{
use lvgl; use lvgl;
use lvgl::style::Style; use lvgl::style::Style;
use lvgl::widgets::{Label, LabelAlign}; use lvgl::widgets::{Label, LabelAlign};
use lvgl::{Align, Color, ObjPart, Object, State, UI}; use lvgl::{Align, Color, Object, Part, State, UI};
use lvgl_sys; use lvgl_sys;
use std::sync::{mpsc, Arc, Mutex}; use std::sync::{mpsc, Arc, Mutex};
use std::thread::sleep; use std::thread::sleep;
@ -36,13 +36,13 @@ fn main() -> Result<(), String> {
let mut screen_style = Style::default(); let mut screen_style = Style::default();
screen_style.set_bg_color(State::DEFAULT, Color::from_rgb((0, 0, 0))); screen_style.set_bg_color(State::DEFAULT, Color::from_rgb((0, 0, 0)));
screen_style.set_radius(State::DEFAULT, 0); screen_style.set_radius(State::DEFAULT, 0);
screen.add_style(ObjPart::Main, screen_style); screen.add_style(Part::Main, screen_style);
let mut time = Label::new(&mut screen); let mut time = Label::new(&mut screen);
let mut style_time = Style::default(); let mut style_time = Style::default();
//style_time.set_text_font(font_noto_sans_numeric_28); //style_time.set_text_font(font_noto_sans_numeric_28);
style_time.set_text_color(State::DEFAULT, Color::from_rgb((255, 255, 255))); style_time.set_text_color(State::DEFAULT, Color::from_rgb((255, 255, 255)));
time.add_style(ObjPart::Main, style_time); time.add_style(Part::Main, style_time);
time.set_align(&mut screen, Align::Center, 0, 0); time.set_align(&mut screen, Align::Center, 0, 0);
time.set_text("20:46"); time.set_text("20:46");
time.set_width(240); time.set_width(240);

108
examples/gauge.rs Normal file
View file

@ -0,0 +1,108 @@
use embedded_graphics::pixelcolor::Rgb565;
use embedded_graphics::prelude::*;
use embedded_graphics_simulator::{
OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
};
use lvgl::style::{Opacity, Style};
use lvgl::widgets::{Gauge, GaugePart};
use lvgl::{self, Align, Color, DisplayDriver, Object, Part, State, UI};
use lvgl_sys;
use std::sync::{mpsc, Arc, Mutex};
use std::thread::sleep;
use std::time::Duration;
fn main() -> Result<(), String> {
let mut display: SimulatorDisplay<Rgb565> = SimulatorDisplay::new(Size::new(
lvgl_sys::LV_HOR_RES_MAX,
lvgl_sys::LV_VER_RES_MAX,
));
let output_settings = OutputSettingsBuilder::new().scale(2).build();
let mut window = Window::new("Gauge Example", &output_settings);
let mut ui = UI::init().unwrap();
// Implement and register your display:
let display_driver = DisplayDriver::new(&mut display);
ui.disp_drv_register(display_driver);
// Create screen and widgets
let mut screen = ui.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);
// Create the gauge
let mut gauge_style = Style::default();
// Set a background color and a radius
gauge_style.set_radius(State::DEFAULT, 5);
gauge_style.set_bg_opa(State::DEFAULT, Opacity::OPA_COVER);
gauge_style.set_bg_color(State::DEFAULT, Color::from_rgb((192, 192, 192)));
// Set some paddings
gauge_style.set_pad_inner(State::DEFAULT, 20);
gauge_style.set_pad_top(State::DEFAULT, 20);
gauge_style.set_pad_left(State::DEFAULT, 5);
gauge_style.set_pad_right(State::DEFAULT, 5);
gauge_style.set_scale_end_color(State::DEFAULT, Color::from_rgb((255, 0, 0)));
gauge_style.set_line_color(State::DEFAULT, Color::from_rgb((255, 255, 255)));
gauge_style.set_scale_grad_color(State::DEFAULT, Color::from_rgb((0, 0, 255)));
gauge_style.set_line_width(State::DEFAULT, 2);
gauge_style.set_scale_end_line_width(State::DEFAULT, 4);
gauge_style.set_scale_end_border_width(State::DEFAULT, 4);
let mut gauge = Gauge::new(&mut screen);
gauge.add_style(GaugePart::Main, gauge_style);
gauge.set_align(&mut screen, Align::Center, 0, 0);
gauge.set_value(0, 50);
let threaded_ui = Arc::new(Mutex::new(ui));
let (stop_ch, read_ch) = mpsc::channel();
let closure_ui = threaded_ui.clone();
let tick_thr = std::thread::spawn(move || loop {
let period = Duration::from_millis(5);
// Needs to be called periodically for LittlevGL internal timing calculations.
closure_ui.lock().unwrap().tick_inc(period);
sleep(period);
if read_ch.try_recv().is_ok() {
break;
}
});
let mut i = 0;
'running: loop {
threaded_ui.lock().unwrap().task_handler();
window.update(&display);
for event in window.events() {
match event {
SimulatorEvent::MouseButtonUp {
mouse_btn: _,
point,
} => {
println!("Clicked on: {:?}", point);
}
SimulatorEvent::Quit => break 'running,
_ => {}
}
}
sleep(Duration::from_millis(25));
gauge.set_value(0, i);
if i > 99 {
i = 0;
} else {
i = i + 1;
}
}
stop_ch.send(true).unwrap();
tick_thr.join().unwrap();
Ok(())
}

View file

@ -3,6 +3,7 @@ name = "lvgl-codegen"
version = "0.1.0" version = "0.1.0"
authors = ["Rafael Caricio <rafael@caricio.com>"] authors = ["Rafael Caricio <rafael@caricio.com>"]
edition = "2018" edition = "2018"
publish = false
# 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

View file

@ -94,7 +94,7 @@ pub trait Object: NativeObject {
impl Object for GenericObject { impl Object for GenericObject {
type SpecialEvent = (); type SpecialEvent = ();
type Part = ObjPart; type Part = Part;
unsafe fn from_raw(raw: ptr::NonNull<lvgl_sys::lv_obj_t>) -> Self { unsafe fn from_raw(raw: ptr::NonNull<lvgl_sys::lv_obj_t>) -> Self {
Self { raw: raw.as_ptr() } Self { raw: raw.as_ptr() }
@ -111,10 +111,10 @@ impl Default for GenericObject {
macro_rules! define_object { macro_rules! define_object {
($item:ident) => { ($item:ident) => {
define_object!($item, event = (), part = $crate::ObjPart); define_object!($item, event = (), part = $crate::Part);
}; };
($item:ident, event = $event_type:ty) => { ($item:ident, event = $event_type:ty) => {
define_object!($item, event = $event_type, part = $crate::ObjPart); define_object!($item, event = $event_type, part = $crate::Part);
}; };
($item:ident, part = $part_type:ty) => { ($item:ident, part = $part_type:ty) => {
define_object!($item, event = (), part = $part_type); define_object!($item, event = (), part = $part_type);
@ -189,16 +189,16 @@ impl State {
} }
} }
pub enum ObjPart { pub enum Part {
Main, Main,
All, All,
} }
impl Into<u8> for ObjPart { impl Into<u8> for Part {
fn into(self) -> u8 { fn into(self) -> u8 {
match self { match self {
ObjPart::Main => lvgl_sys::LV_OBJ_PART_MAIN as u8, Part::Main => lvgl_sys::LV_OBJ_PART_MAIN as u8,
ObjPart::All => lvgl_sys::LV_OBJ_PART_ALL as u8, Part::All => lvgl_sys::LV_OBJ_PART_ALL as u8,
} }
} }
} }

View file

@ -29,7 +29,6 @@ impl Style {
let native_state: u32 = state.get_bits(); let native_state: u32 = state.get_bits();
let string = CString::new(value).unwrap(); let string = CString::new(value).unwrap();
unsafe { unsafe {
lvgl_sys::LV_OPA_COVER;
lvgl_sys::_lv_style_set_ptr( lvgl_sys::_lv_style_set_ptr(
self.raw.as_mut(), self.raw.as_mut(),
(lvgl_sys::LV_STYLE_VALUE_STR (lvgl_sys::LV_STYLE_VALUE_STR

41
lvgl/src/widgets/gauge.rs Normal file
View file

@ -0,0 +1,41 @@
use crate::{GenericObject, NativeObject, Object};
use core::ptr;
define_object!(Gauge, part = GaugePart);
impl Gauge {
pub fn new<C>(parent: &mut C) -> Self
where
C: NativeObject,
{
unsafe {
let ptr = lvgl_sys::lv_gauge_create(parent.raw().as_mut(), ptr::null_mut());
let raw = ptr::NonNull::new_unchecked(ptr);
let core = GenericObject::from_raw(raw);
Self { core }
}
}
/// Set a new value on the gauge
pub fn set_value(&mut self, needle_id: u8, value: i32) {
unsafe {
lvgl_sys::lv_gauge_set_value(self.core.raw().as_mut(), needle_id, value);
}
}
}
pub enum GaugePart {
Main,
Major,
Needle,
}
impl From<GaugePart> for u8 {
fn from(part: GaugePart) -> Self {
match part {
GaugePart::Main => lvgl_sys::LV_GAUGE_PART_MAIN as u8,
GaugePart::Major => lvgl_sys::LV_GAUGE_PART_MAJOR as u8,
GaugePart::Needle => lvgl_sys::LV_GAUGE_PART_NEEDLE as u8,
}
}
}

View file

@ -1,7 +1,9 @@
mod bar; mod bar;
mod button; mod button;
mod gauge;
mod label; mod label;
pub use self::bar::*; pub use self::bar::*;
pub use self::button::*; pub use self::button::*;
pub use self::gauge::*;
pub use self::label::*; pub use self::label::*;