Possible to set values in the bar widget with animations

This commit is contained in:
Rafael Caricio 2020-05-31 12:07:24 +02:00
parent e0e3924789
commit cb8a78137d
3 changed files with 21 additions and 6 deletions

View file

@ -3,7 +3,7 @@ use embedded_graphics::prelude::*;
use embedded_graphics_simulator::{ use embedded_graphics_simulator::{
OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window, OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
}; };
use lvgl::{self, widgets::Bar, Align, Color, DisplayDriver, Label, Object, Style, UI}; use lvgl::{self, widgets::Bar, Align, Animation, Color, DisplayDriver, Label, Object, Style, 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;
@ -38,7 +38,7 @@ fn main() -> Result<(), String> {
bar.set_size(175, 50); bar.set_size(175, 50);
bar.set_align(&mut screen, Align::Center, 0, 0); bar.set_align(&mut screen, Align::Center, 0, 0);
bar.set_range(0, 100); bar.set_range(0, 100);
bar.set_value(0); bar.set_value(0, Animation::OFF);
let mut loading_lbl = Label::new(&mut screen); let mut loading_lbl = Label::new(&mut screen);
loading_lbl.set_text("Loading..."); loading_lbl.set_text("Loading...");
@ -67,7 +67,7 @@ fn main() -> Result<(), String> {
if i > 100 { if i > 100 {
i = 0; i = 0;
} }
bar.set_value(i); bar.set_value(i, Animation::OFF);
i += 1; i += 1;
sleep(Duration::from_millis(25)); sleep(Duration::from_millis(25));

View file

@ -325,3 +325,17 @@ pub enum Align {
OutRightMid, OutRightMid,
OutRightBottom, OutRightBottom,
} }
pub enum Animation {
ON,
OFF,
}
impl From<Animation> for lvgl_sys::lv_anim_enable_t {
fn from(anim: Animation) -> Self {
match anim {
Animation::ON => lvgl_sys::LV_ANIM_ON as u8,
Animation::OFF => lvgl_sys::LV_ANIM_OFF as u8,
}
}
}

View file

@ -1,3 +1,4 @@
use crate::support::Animation;
use crate::support::{NativeObject, ObjectX}; use crate::support::{NativeObject, ObjectX};
use core::ptr; use core::ptr;
use lvgl_sys; use lvgl_sys;
@ -24,10 +25,10 @@ impl Bar {
} }
} }
/// Set the value of the bar /// Set a new value on the bar
pub fn set_value(&mut self, value: i16) { pub fn set_value(&mut self, value: i16, anim: Animation) {
unsafe { unsafe {
lvgl_sys::lv_bar_set_value(self.core.raw().as_mut(), value, 0); lvgl_sys::lv_bar_set_value(self.core.raw().as_mut(), value, anim.into());
} }
} }
} }