diff --git a/examples/bar.rs b/examples/bar.rs index 78636e2..9672043 100644 --- a/examples/bar.rs +++ b/examples/bar.rs @@ -4,7 +4,7 @@ use embedded_graphics_simulator::{ OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window, }; use lvgl::widgets::{Bar, Label}; -use lvgl::{self, Align, Animation, Color, DisplayDriver, Object, Style, UI}; +use lvgl::{self, Align, Animation, Color, DisplayDriver, Event, Object, Style, UI}; use lvgl_sys; use std::sync::{mpsc, Arc, Mutex}; use std::thread::sleep; @@ -45,9 +45,9 @@ fn main() -> Result<(), String> { loading_lbl.set_text("Loading..."); loading_lbl.set_align(&mut bar, Align::OutTopMid, 0, -10); - loading_lbl.on_event(|mut this, event| { + loading_lbl.on_event(|_, event| { if let lvgl::Event::Clicked = event { - this.set_text("Clicked!"); + println!("Loaded!"); } }); @@ -73,6 +73,10 @@ fn main() -> Result<(), String> { 'running: loop { if i > 100 { i = 0; + threaded_ui + .lock() + .unwrap() + .event_send(&mut loading_lbl, Event::Clicked) } bar.set_value(i, Animation::OFF); i += 1; diff --git a/lvgl/src/global.rs b/lvgl/src/global.rs index 2081913..2d4846c 100644 --- a/lvgl/src/global.rs +++ b/lvgl/src/global.rs @@ -1,4 +1,4 @@ -use crate::{DisplayDriver, Object, ObjectX}; +use crate::{DisplayDriver, Event, Object, ObjectX}; use alloc::boxed::Box; use core::marker::PhantomData; use core::ptr; @@ -54,6 +54,15 @@ impl UI { } } + pub fn event_send(&mut self, obj: &mut T, event: Event) + where + T: Object, + { + unsafe { + lvgl_sys::lv_event_send(obj.raw().as_mut(), event.into(), ptr::null_mut()); + } + } + pub fn tick_inc(&mut self, tick_period: Duration) { unsafe { lvgl_sys::lv_tick_inc(tick_period.as_millis() as u32); diff --git a/lvgl/src/support.rs b/lvgl/src/support.rs index d5179e0..d8daa03 100644 --- a/lvgl/src/support.rs +++ b/lvgl/src/support.rs @@ -374,6 +374,24 @@ impl TryFrom for Event { } } +impl From> for lvgl_sys::lv_event_t { + fn from(event: Event) -> Self { + let native_event = match event { + Event::Pressed => lvgl_sys::LV_EVENT_PRESSED, + Event::Pressing => lvgl_sys::LV_EVENT_PRESSING, + Event::PressLost => lvgl_sys::LV_EVENT_PRESS_LOST, + Event::ShortClicked => lvgl_sys::LV_EVENT_SHORT_CLICKED, + Event::Clicked => lvgl_sys::LV_EVENT_CLICKED, + Event::LongPressed => lvgl_sys::LV_EVENT_LONG_PRESSED, + Event::LongPressedRepeat => lvgl_sys::LV_EVENT_LONG_PRESSED_REPEAT, + Event::Released => lvgl_sys::LV_EVENT_RELEASED, + // TODO: handle all types... + _ => lvgl_sys::LV_EVENT_CLICKED, + }; + native_event as lvgl_sys::lv_event_t + } +} + /// These events are sent only by pointer-like input devices (E.g. mouse or touchpad) pub enum PointerEvent { DragBegin,