lvgl-rs/examples/button_click.rs

103 lines
3.1 KiB
Rust
Raw Normal View History

use embedded_graphics::pixelcolor::Rgb565;
use embedded_graphics::prelude::*;
use embedded_graphics_simulator::{
OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
};
2020-06-04 18:14:43 +00:00
use lvgl::style::Style;
2020-06-12 16:54:28 +00:00
use lvgl::widgets::{Btn, Label};
2020-06-07 18:47:49 +00:00
use lvgl::{self, Align, Color, DisplayDriver, Event, LvError, Part, State, Widget, UI};
use lvgl_sys;
use std::sync::{mpsc, Arc, Mutex};
use std::thread::sleep;
use std::time::Duration;
2020-06-07 18:47:49 +00:00
fn main() -> Result<(), LvError> {
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("Bar Example", &output_settings);
2020-06-07 18:47:49 +00:00
let mut ui = UI::init()?;
// Implement and register your display:
let display_driver = DisplayDriver::new(&mut display);
ui.disp_drv_register(display_driver);
// Create screen and widgets
2020-06-07 18:47:49 +00:00
let mut screen = ui.scr_act()?;
2020-06-02 17:59:41 +00:00
let mut screen_style = Style::default();
2020-06-04 18:14:43 +00:00
screen_style.set_bg_color(State::DEFAULT, Color::from_rgb((0, 0, 0)));
2020-06-07 18:47:49 +00:00
screen.add_style(Part::Main, screen_style)?;
// Create the button
2020-06-07 18:47:49 +00:00
let mut button = Btn::new(&mut screen)?;
button.set_align(&mut screen, Align::InLeftMid, 30, 0)?;
button.set_size(180, 80)?;
let mut btn_lbl = Label::new(&mut button)?;
btn_lbl.set_text("Click me!")?;
2020-06-12 16:54:28 +00:00
let mut btn_state = false;
button.on_event(|mut btn, event| {
if let lvgl::Event::Clicked = event {
2020-06-12 16:54:28 +00:00
if btn_state {
btn_lbl.set_text("Click me!").unwrap();
} else {
btn_lbl.set_text("Clicked!").unwrap();
}
btn_state = !btn_state;
println!("Clicked!");
2020-06-12 16:54:28 +00:00
btn.toggle().unwrap();
}
2020-06-07 18:47:49 +00:00
})?;
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;
}
});
'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);
// Send a event to the button directly
threaded_ui
.lock()
.unwrap()
2020-06-07 18:47:49 +00:00
.event_send(&mut button, Event::Clicked)?;
}
SimulatorEvent::Quit => break 'running,
_ => {}
}
}
2020-06-13 14:27:02 +00:00
sleep(Duration::from_millis(5));
}
stop_ch.send(true).unwrap();
tick_thr.join().unwrap();
Ok(())
}